/* Title: SimpleDialog.java Author: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 21/10/98 Description: This class implements a simple dialogue - the user is presented with a button labelled "Show Dialogue". If they select it then a dialogue box is presented until they select the "Done" button. More importantly, this Applet illustrates the use both of Event debugging and of graphical structure debugging. VERY IMPORTANT - notice how I've left the Debug statements in place so that if any problems arise in the future then I can switch them on simply by changing the value of the DEBUG constant to true This Applet also illustrates the way in which event handlers must be provided for each class of windows, frames and dialogs. */ import java.applet.Applet; import java.awt.*; public class SimpleDialog extends Applet { /* EVENT_DEBUG if set to true, prints information about events to System.out as they */ /* pass through handle_Event() and its associated convenience methods, such as Action */ /* This is typically used with two statements that first convert the event information */ /* to a string and then print it: */ /* My_String = event.toString(); */ /* System.out.println(My_String); */ /* See any of the action() or handleEvent() methods for an example of this */ static boolean EVENT_DEBUG = false; /* GRAPHIC_DEBUG if set to true, prints information about the composition, properties */ /* position etc of the graphical components used in the class. */ /* This is typically used in conjuction with list() that provides detailed information about */ /* graphical components. */ static boolean GRAPHIC_DEBUG = false; static boolean GRAPHIC_DEBUG1 = true; static Button launchbutton = new Button ("Show Dialogue..."); private Dialog_Box my_dialog; Frame myframe; public void init() { add(launchbutton); } public void start(){ myframe = getFrame(this); my_dialog = new Dialog_Box(myframe, "Simple Dialog"); /* The following statement illustrates a GRAPHIC_DEBUG message, */ /* notice how we preceded the information by an indication both of */ /* the class and method that generated the output */ if (GRAPHIC_DEBUG){ System.out.println("SimpleDialog.Start:"); my_dialog.list(); } } public boolean handleEvent(Event event){ String My_String = ""; /* The following statement illustrates an EVENT_DEBUG message, */ /* notice how we preceded the information by an indication both of */ /* the class and method that generated the output */ if (EVENT_DEBUG) { System.out.println("SimpleDialog.HandleEvent:"); My_String = event.toString(); System.out.println(My_String); } switch (event.id) { case Event.WINDOW_DESTROY:{ my_dialog.dispose(); return true; } case Event.ACTION_EVENT: return action(event, event.arg); } return super.handleEvent(event); } public boolean action(Event event, Object what){ String My_String = ""; if (EVENT_DEBUG) { System.out.println("SimpleDialog.Action:"); My_String = event.toString(); System.out.println(My_String); } if (event.target == launchbutton){ /* The following code places the dialog box so that it slightly overlaps the */ /* frame with the launchbutton - this code could be ammended so that they */ /* do not overlap at all and then the user could read both at the same time */ Point scrnLoc= myframe.location(); if (scrnLoc.x - 20 > 0) /* offset the dialog if it will fit on screen */ my_dialog.move(scrnLoc.x - 20, scrnLoc.y); else /* the offset of 20 would put it off the screen */ my_dialog.move(scrnLoc.x, scrnLoc.y); showStatus(null); my_dialog.show(); showStatus("Dialog shown"); } return true; } static Frame getFrame(Component c) { /* Returns the highest parent frame of the component, for example the browser window */ Frame frame = null; while((c=c.getParent()) != null) { if(c instanceof Frame){ frame = (Frame)c; } } return frame; } } /* End of SimpleDialog */ /****************************************************************/ class Dialog_Box extends Dialog { static Button donebutton = new Button ("Done"); /* EVENT_DEBUG if set to true, prints information about events to System.out as they */ /* pass through handle_Event() and its associated convenience methods, such as Action */ static boolean EVENT_DEBUG = false; /* GRAPHIC_DEBUG if set to true, prints information about the composition, properties */ /* position etc of the graphical components used in the class. */ static boolean GRAPHIC_DEBUG = false; /* If MODALITY is true then the dialog is modal - in other words the user cannot proceed */ /* until they click on the DONE button and dismiss the window */ static boolean MODALITY = true; static int dialog_width=100; static int dialog_height=100; static int button_width=50; static int button_height=30; Dialog_Box(Frame dw, String Title){ super(dw, Title, MODALITY); /* Create an instance of Super Class (Dialog) */ resize(dialog_width,dialog_height); donebutton.resize(button_width,button_height); setLayout(new BorderLayout()); add("Center", donebutton); /*pack();*/ } public boolean handleEvent(Event event){ String My_String = ""; if (EVENT_DEBUG) { System.out.println("Dialog_Box.HandleEvent"); My_String = event.toString(); System.out.println(My_String); } /* Notice how a case statement is used even though it only has one entry - I could have */ /* used a simpe IF-ELSE IF statement but I decided to use a case so that I could easily */ /* extend the program in the future, by adding other cases to handle more events. */ switch (event.id) { case Event.ACTION_EVENT: return action(event, event.arg); } return super.handleEvent(event); /* pass unhandled events back to parent */ } /*handleEvent */ public boolean action(Event event, Object what){ String My_String = ""; if (EVENT_DEBUG) { System.out.println("Dialog_Box.Action"); My_String = event.toString(); System.out.println(My_String); } if (event.target == donebutton){ dispose(); /* close the Dialog_Box */ } return true; } /* End of Action */ }/* End of Dialog_Box */