// HabTextDemo.java // JDK 1.1/Habanero 2.0 version by Ian Chai // // Note: this is the Habanerization of Ian Chai's JDK 1.1 version of // TextDemo.java // This version uses read/writeUTF to serialize the hablet. package TextDemo; // added for Habanero's convenience import java.awt.*; import java.awt.event.*; import java.applet.*; // imports added due to Habanerization // import ncsa.habanero.Habanero; import ncsa.habanero.*; import ncsa.habanero.streams.*; import java.io.IOException; public class HabTextDemo extends Hablet // change Applet to Hablet { TextField inArea; TextArea outArea; public void init() { inArea = new TextField(20); outArea = new TextArea(5, 20); outArea.setEditable(false); inArea.addActionListener(new FieldListener()); //Add Components to the Applet. GridBagLayout gridBag = new GridBagLayout(); setLayout(gridBag); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(inArea, c); add(inArea); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; gridBag.setConstraints(outArea, c); add(outArea); validate(); } // Each instance of this class listens to one of the TextFields. class FieldListener implements ActionListener { public void actionPerformed(ActionEvent e) { TextField f; try {f = (TextField)Habanero.getSource();} // change getSource from the ActionEvent to Habanero // because marshalling loses the event's source. catch (ClassCastException evt) { outArea.append("Error: source isn't a TextField!\n"); return; } outArea.append(e.getActionCommand()+"\n"); // change because if we did the original f.getText() we // would get the text from the *local* copy of the field // and not the one in the original Hablet. f.selectAll(); } } // added all the methods below this line protected void writeHablet(MarshallOutputStream out) throws IOException { out.writeUTF(outArea.getText()); } protected void readHablet(MarshallInputStream in) throws IOException { outArea.setText(in.readUTF()); } public void startInFrame(MirrorFrame f) { f.setTitle("Habanerized TextDemo"); f.add("Center", this); // Center hablet in window f.setSize(500,200); // Window's size f.setVisible(true); // Window initially visible f.show(); // Show the window init(); // Initialize hablet code start(); // Start hablet running try { Habanero.addSharedEventType( Class.forName("java.awt.event.ActionEvent"), this); } catch (ClassNotFoundException e) { Habanero.println("could not find ActionEvent class object."); } } }