import java.awt.*; import java.awt.event.*; import java.applet.*; public class WorklistDemo extends Applet implements ItemListener, ActionListener { /** * Our Worklist, initially a Stack. */ private Worklist theWorklist = new Stack(); /** * The last String removed from our Worklist. This is null whenever * it is meaningless -- whenever no String has been removed from the * current Worklist since it was created. */ private String lastRemoved = null; /** * This Panel contains our window's content, whether we are running * as an applet or as an application. */ private Panel content; /** * One of the radio buttons that control which implementation of the Worklist * interface we use. They are grouped so that setting one unsets the * others. They are kept in sync with the actual class of theWorklist * by the update method. * @see update */ private Checkbox stackButton; /** * One of the radio buttons that control which implementation of the Worklist * interface we use. They are grouped so that setting one unsets the * others. They are kept in sync with the actual class of theWorklist * by the update method. * @see update */ private Checkbox queueButton; /** * One of the radio buttons that control which implementation of the Worklist * interface we use. They are grouped so that setting one unsets the * others. They are kept in sync with the actual class of theWorklist * by the update method. * @see update */ private Checkbox priorityQueueButton; /** * The Remove button. It will cause one element to be removed from * the worklist. It is enabled iff the worklist is not empty; see * the update method. * @see update */ private Button removeButton; /** * A Label that reports the current state of theWorklist.hasMore(). * This is kept up-to-date by the update method. * @see update */ private Label hasMoreLabel; /** * A Label that reports the last string removed from the Worklist. * This is kept in sync with lastRemoved by the update method. * @see update */ private Label lastRemovedLabel; /** * The TextField in which new elements for the Worklist are typed. */ private TextField addField; /** * WorklistDemo no-arg constructor. We initialize the content Panel with * all the elements it contains: buttons, labels, and so on. Note that * we may or may not be running as an applet: if we are an application, all * the Applet stuff we inherited will be unused, but our own instance * variables must still be initialized. */ public WorklistDemo() { // First, create the group of three Checkbox objects (radio buttons). // They are all in the same CheckboxGroup. Also, create the Remove // button. CheckboxGroup group = new CheckboxGroup(); stackButton = new Checkbox("Stack",group,true); stackButton.addItemListener(this); queueButton = new Checkbox("Queue",group,false); queueButton.addItemListener(this); priorityQueueButton = new Checkbox("Priority Queue",group,false); priorityQueueButton.addItemListener(this); removeButton = new Button("Remove"); removeButton.addActionListener(this); // Next, create the Labels, and the TextField where the user will // enter strings to be added to the Worklist. Label addLabel = new Label("To add, type the item here and hit Enter:"); addField = new TextField(20); addField.addActionListener(this); hasMoreLabel = new Label(); lastRemovedLabel = new Label(); // Now we will create an inner Panel to hold the radio buttons // stacked one on top of the other. Panel buttons = new Panel(new GridLayout(3,0)); buttons.add(stackButton); buttons.add(queueButton); buttons.add(priorityQueueButton); // Then, another inner Panel to hold the Labels and TextField, // also stacked up. Panel text = new Panel(new GridLayout(4,0)); text.add(addLabel); text.add(addField); text.add(hasMoreLabel); text.add(lastRemovedLabel); // Now an inner panel to flow the previous two pieces together: // buttons to the left, text to the right (if the window is wide enough). Panel top = new Panel(); top.add(buttons); top.add(text); // Then another inner panel to hold the Remove button in its preferred size. Panel bottom = new Panel(); bottom.add(removeButton); // Finally, our content Panel: we will use a BorderLayout with the buttons // and text in the north border, and the remove button in the south. content = new Panel(new BorderLayout()); content.add(top,BorderLayout.NORTH); content.add(bottom,BorderLayout.SOUTH); // Get a consistent initial state for the buttons and labels. update(); } /** * Handle an ActionEvent. This is either a completed text * entry in our TextField (that is, return was pressed), or a click * on the Remove button. We adjust our state accordingly, then update * the display. * @param e the relevant ActionEvent */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); // A completed text entry -- add the text to our Worklist. if (source==addField) { theWorklist.add(addField.getText()); addField.setText(""); } // A click on the Remove button -- remove an item and remember // what it was. The Remove button is only enabled if hasMore // says true, but we check again here just in case. else if (source==removeButton) { if (theWorklist.hasMore()) lastRemoved = theWorklist.remove(); } // Update the display. update(); } /** * The Applet init method, called when we are running as an Applet and are * loaded into the browser or applet viewer. Add our content Panel to our * inherited Applet panel. */ public void init() { add(content); } /** * Handle an ItemEvent. This is an action on one of our radio buttons. * If the type of the Worklist has been changed, we discard the old one * and switch to a new one of the correct type, then update the display. * @param e the relevant ItemEvent */ public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source == stackButton) { if (!(theWorklist instanceof Stack)) { theWorklist = new Stack(); lastRemoved = null; } } else if (source == queueButton) { if (!(theWorklist instanceof Queue)) { theWorklist = new Queue(); lastRemoved = null; } } else if (source==priorityQueueButton) { if (!(theWorklist instanceof PriorityQueue)) { theWorklist = new PriorityQueue(); lastRemoved = null; } } update(); } /** * The main method. Run as an application, we create * a Frame of our own (a window with the title "Worklist Demo"), * and place the demo content panel inside it. * @param s a String[] containing command line args, which we ignore */ public static void main(String[] s) { // Construct a WorklistDemo object. We will not use the Applet // part, but we will use the other parts (demo.content and all that). WorklistDemo demo = new WorklistDemo(); // Construct the window Frame, and add a listener so we will // exit when the window is closed. Frame frame = new Frame("Worklist Demo"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); // Add our content Panel to the Frame, pack it, and make it visible. frame.add("Center", demo.content); frame.pack(); frame.setVisible(true); } /** * Update our display, by making the Remove button, the radio buttons, and * the Labels that report the state of hasMore and the last item removed, * all agree with out internal data. */ private void update() { removeButton.setEnabled(theWorklist.hasMore()); if (theWorklist instanceof Stack) stackButton.setState(true); else if (theWorklist instanceof PriorityQueue) priorityQueueButton.setState(true); else if (theWorklist instanceof Queue) queueButton.setState(true); hasMoreLabel.setText("Current value of hasMore(): " + theWorklist.hasMore()); lastRemovedLabel.setText(lastRemoved==null ? "" : "Last item removed: " + lastRemoved); } }