import heartMonitor.*; 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.*; import javax.swing.event.*; // This class implements a simulated heart patient in the hospital // // Written by: Stuart Hansen // Date: February 2, 2009 public class HeartPatient extends JFrame { private HeartRateMonitor station; // the remote nurses station // The GUI Components private JSlider rateSlider; private String name; // the patient's name private boolean alarmSounding; // a flag stating whether the alarm is set // The constructor sets up the ORB and the GUI public HeartPatient(String [] args) { this.name = args[0]; this.alarmSounding = false; setUpORB(args); station.register(name); setUpGUI(name); } // When a patient is removed from their monitor, the nurses' // station is notified protected void finalize () throws Throwable { System.out.println("Finalizing " + name); super.finalize(); station.unregister(name); } // This method sets up all the GUI components public void setUpGUI (String name) { // Get the content pane of the application Container c = getContentPane(); // Set up the patient's slider rateSlider = new JSlider (0, 255, 80); rateSlider.setLabelTable(rateSlider.createStandardLabels(50)); rateSlider.setPaintLabels(true); rateSlider.setPaintTicks(true); rateSlider.addChangeListener(new RateHandler()); c.add(rateSlider); // Set a couple of properties and open the window setTitle("Heart Patient - " + name); setSize (300, 75); setLocation(50, 50); // Note that we want to use custom code here to make certain // that the Nursing station is notified of the window closing. //setDefaultCloseOperation(EXIT_ON_CLOSE); addWindowListener(new WindowClosingHandler()); setVisible(true); } // An inner class to handle heart rate changes public class RateHandler implements ChangeListener { public void stateChanged (ChangeEvent e) { int rate = rateSlider.getValue(); // Sound the alarm if the patient's value go out of range if (!alarmSounding && (rate <=40 || rate >= 140)) { alarmSounding = true; station.soundAlarm(name, rate); } // Turn the local alarm off if patient recovers. else if (alarmSounding && rate > 40 && rate < 140) alarmSounding = false; station.newRate(name, rate); } } // Special Class to make certain the nursing station is notified if // the patient window is closed public class WindowClosingHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { station.unregister(name); } } // This method sets up the data communication with the nursing station public 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("NursesStation"); // Narrow (cast) the object reference to a calculator reference station = HeartRateMonitorHelper.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 HeartPatient (args); } }