import calculator.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; // This class implements a simple calculator client program. // It relies on a CORBA server to do the actual calculations // // Written by: Stuart Hansen // Date: February 2, 2009 public class CalculatorClient extends JFrame { private Calculator calc; // the remote calculator // The GUI Components private JLabel firstLabel, secondLabel, resultLabel; private JTextField first, second, result; private JButton addButton, subButton, multButton; // The constructor sets up the ORB and the GUI public CalculatorClient(String [] args) { setUpORB(args); setUpGUI(); } // This method sets up all the GUI components private void setUpGUI () { // Get the content pane of the application Container c = getContentPane(); c.setLayout(null); // Add the label and textbox for the first number firstLabel = new JLabel ("First Number:"); firstLabel.setBounds(20, 20, 100, 20); first = new JTextField(""); first.setBounds(140, 20, 50, 20); c.add (firstLabel); c.add (first); // Add the label and the textbox for the second number secondLabel = new JLabel ("Second Number:"); secondLabel.setBounds(20, 50, 100, 20); second = new JTextField(""); second.setBounds (140, 50, 50, 20); c.add (secondLabel); c.add (second); // Add the label and the textbox for the result resultLabel = new JLabel ("Result"); resultLabel.setBounds(20, 80, 100, 20); result = new JTextField(""); result.setBounds (140, 80, 50, 20); c.add (resultLabel); c.add (result); // Add the Add button addButton = new JButton("Add"); addButton.setBounds (20, 110, 70, 20); addButton.addActionListener(new AddHandler()); c.add(addButton); // Add the Subtract Button subButton = new JButton("Sub"); subButton.setBounds (100, 110, 70, 20); subButton.addActionListener(new SubHandler()); c.add(subButton); // Add the Multiply Button multButton = new JButton("Mult"); multButton.setBounds (180, 110, 70, 20); multButton.addActionListener(new MultHandler()); c.add(multButton); // Set a couple of properties and open the window setTitle("CORBA Calculator"); setSize (300, 170); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } // An inner class to handle clicks on the add button private class AddHandler implements ActionListener { public void actionPerformed (ActionEvent e) { int firstNum = Integer.parseInt(first.getText()); int secondNum = Integer.parseInt(second.getText()); int resultNum = calc.add(firstNum, secondNum); result.setText(Integer.toString(resultNum)); } } // An inner class to handle clicks on the subtract button private class SubHandler implements ActionListener { public void actionPerformed (ActionEvent e) { int firstNum = Integer.parseInt(first.getText()); int secondNum = Integer.parseInt(second.getText()); int resultNum = calc.sub(firstNum, secondNum); result.setText(Integer.toString(resultNum)); } } // An inner class to handle clicks on the multiply button private class MultHandler implements ActionListener { public void actionPerformed (ActionEvent e) { int firstNum = Integer.parseInt(first.getText()); int secondNum = Integer.parseInt(second.getText()); int resultNum = calc.mult(firstNum, secondNum); result.setText(Integer.toString(resultNum)); } } // Set up the data communication with the server private void setUpORB(String [] args) { try { // Create and initialize the ORB ORB orb = ORB.init(args, null); // Get a reference to the name service org.omg.CORBA.Object nameServiceObj = orb.resolve_initial_references("NameService"); // Narrow (cast) the object reference to a name service reference NamingContextExt nameService = NamingContextExtHelper.narrow(nameServiceObj); // Get a reference to the calculator from the name service org.omg.CORBA.Object calcObj = nameService.resolve_str("CalculatorServer"); // Narrow (cast) the object reference to a calculator reference calc = CalculatorHelper.narrow(calcObj); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } // The main simply makes a new calculator client public static void main(String args[]) { new CalculatorClient (args); } }