//
// CounterFrame.java: sample client of counter class
//

import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class CounterClient extends JFrame implements ActionListener
{ 
   private JButton incrButton  = new JButton("Increment");
   private JButton exitButton  = new JButton("Exit");

   // updatable field
   private IntField xcount = new IntField(5);

   private CounterService remoteCounter;      // remote counter

   public CounterClient()
   {
      super("Counter Frame");
      setSize(650, 400); // larger than needed; resizing done below

      int counterValue = 0;
      // set up the remote counter and read it
      try
      {
         System.out.println("Connecting...");
         String serverObjectName = "rmi://"
            + CounterServiceImpl.ServiceHost + ":"
            + CounterServiceImpl.ServicePort + "/"
            + CounterServiceImpl.ServiceName;
         remoteCounter = (CounterService)Naming.lookup(serverObjectName);
         counterValue = remoteCounter.value();
      }
      catch ( Exception e )
      {
         System.out.println("Exception: " + e.getMessage());
         e.printStackTrace();
         return;
      }

      // a bit of magic to halt the application if the user closes the window
      addWindowListener(
         new WindowAdapter() {
         public void windowClosing(WindowEvent event) { System.exit(0); }
         });

      // main box; everything placed in this
      JPanel box = new JPanel();
      box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
      getContentPane().add(box);

      // set up tool bar; notice buttons placed left to right against right 
      //    edge
      JPanel toolbar = new JPanel();
      toolbar.setLayout(new FlowLayout(FlowLayout.RIGHT));
      incrButton.addActionListener(this);
      toolbar.add(incrButton);
      exitButton.addActionListener(this);
      toolbar.add(exitButton);
      box.add(toolbar, BorderLayout.NORTH);

      // create labels and fields
      xcount.setText("0");
      xcount.setEditable(false);

      JLabel xcountName = new JLabel("Counter", JLabel.RIGHT);

      JPanel dataPanel = new JPanel();
      dataPanel.setLayout(new GridLayout(2, 2, 5, 5));

      dataPanel.add(xcountName);
      dataPanel.add(xcount);
      box.add(dataPanel);

      JPanel responsePanel = new JPanel();
      responsePanel.setLayout(new GridLayout(1, 2, 5, 5));
      box.add(responsePanel);

      // resize frame
      pack();

      xcount.setText(Integer.toString(counterValue));
   }

   public void actionPerformed(ActionEvent event)
   {
      // handle event
      if ( event.getSource() == incrButton )
         incrButtonClick();
      else if ( event.getSource() == exitButton )
         System.exit(0);
   }

   private void incrButtonClick()
   {
      incrementField(xcount);
   }

   private void incrementField(IntField fld)
   {
      try
      {
         remoteCounter.increment();
         int val = remoteCounter.value();
         fld.setText(Integer.toString(val));
      }
      catch ( Exception e )
      {
         System.out.println("Exception: " + e.getMessage());
         e.printStackTrace();
         return;
      }
   }

   public static void main(String[] args)
   {
      CounterClient sample = new CounterClient();
      sample.setVisible(true);
   }
}
