package GCB; import Application.Constants; import Application.ParameterPanel; import java.awt.*; import java.awt.event.*; import java.util.*; /** Build the main GUI * * Put up a window with three areas * Top area is a menu bar * Middle area is a scroll window to display text (use append() method) * Bottom area is an input area that controls the program operation * and supplies input that the program can use. * @author George C. Blankenship, Jr. */ public class MainGUI extends Frame { static final String CODE_FILE = "GUI"; static final int CODE_REVISION = 5; static boolean stopSystem; // global data MainGUI mainGUI; // so that sub-object can get to main window MenuBar mainMenu; // menu bar on above window TextArea displayArea; // display area of above window ParameterPanel parameterPanel; String lastInput, // last input entered on input line title, author; ListHead menus; // link list of menu items on above window InputField inputLine; // the command line at the bottom TextField clock; // clock at the bottom Dimension screenSize, windowSize; MessageBox box; // window with message and buttons boolean boxButton, // true->OK, otherwise false systemActive; // menu operations should be suspended (system processing) long milliSeconds; // internal "system" time // internal classes class MenuList extends ListEntry { // all of the menus on the main window Menu menu; String name; public MenuList(String n, Menu m) { super(); menu = m; name = n; } public String getName() {return name;} public Menu getMenu() {return menu;} } class MessageBox extends Dialog { // information window with an OK and cancel buttons public MessageBox(Frame p, Dimension screenSize, String t, String m, int buttons, WindowListener wl, ActionListener al, Color back, Color front) { super(p,t,true); // (model) force caller to wait for dispose() box = this; boxButton = false; Panel boxPanel = new Panel(); // area for text message boxPanel.add(new Label(m)); Panel buttonPanel = new Panel(); // area for button(s) Button OK = new Button("OK"); OK.setBackground(Color.lightGray); OK.setForeground(Color.darkGray); OK.addActionListener(al); Button cancel = new Button("Cancel"); cancel.setBackground(Color.lightGray); cancel.setForeground(Color.darkGray); cancel.addActionListener(al); if(buttons==2) { // create two button box buttonPanel.add("West",OK); buttonPanel.add("East",cancel); } else // single button box buttonPanel.add("Center",OK); add("Center",boxPanel); // message in the center add("South",buttonPanel); // bottons at bottom setBackground(back); // set color scheme setForeground(front); pack(); addWindowListener(wl); windowSize = getSize(); // center the window setLocation((screenSize.width-windowSize.width)/2, (screenSize.height-windowSize.height)/2); setVisible(true); } } class MessageBoxEvents extends WindowAdapter implements ActionListener { // events from the box public void windowClosing(WindowEvent e) { // someone wants to closed the main window boxButton = false; box.dispose(); // release the window } public void actionPerformed(ActionEvent e) { // an input field has generated an event if(e.getActionCommand()=="OK") boxButton = true; else boxButton = false; box.dispose(); } } class MainWindowEvents extends WindowAdapter implements ActionListener { // events from the main GUI // potential methods: // windowActivated() // windowClosed() // windowClosing() - X pressed // windowDactivated() // windowDeiconified() - restored // windowIconified() - minimized // windowOpened() public void windowClosing(WindowEvent e) { // someone wants to closed the main window System.out.println("All done!"); System.exit(0); } public void actionPerformed(ActionEvent e) { // a menu has generated an event String name = e.getActionCommand(); // get menu keyword if(name=="Exit") { // Exit command from file menu System.out.println("All done!"); System.exit(0); } if(name=="About") { // About command from help menu message("About "+title, title+" "+Constants.getRelease()+" written by "+author); } } } class InputField extends TextField { // input area class InputActions implements ActionListener { // input from an input field public void actionPerformed(ActionEvent e) { // an input field has generated an event lastInput = getText(); setText(""); // clear the input area } } public InputField(String title, int Characters) { // create the input area super(title,Characters); // create the field addActionListener(new InputField.InputActions()); // wait for input } } // the following class is used to debug the sizing and placement of a window class WindowComponentListener implements ComponentListener { public void componentResized(ComponentEvent e) { System.out.println("Resize component "+e.paramString()); e.getComponent().list(); } public void componentMoved(ComponentEvent e) { System.out.println("Move component "+e.paramString()); e.getComponent().list(); } public void componentHidden(ComponentEvent e) { System.out.println("Hide component "+e.paramString()); e.getComponent().list(); } public void componentShown(ComponentEvent e) { System.out.println("Show component "+e.paramString()); e.getComponent().list(); } } // private methods void setupMenu() { Menu menu = new Menu("File"); // define File menu MenuItem item = new MenuItem("Exit"); // define Exit item item.addActionListener(new MainGUI.MainWindowEvents()); menu.add(item); // place on File menu addMenu("File",menu); // add File to menu bar menu = new Menu("Help"); // define File menu item = new MenuItem("About"); // define About item item.addActionListener(new MainGUI.MainWindowEvents()); menu.add(item); // place on Help menu addMenu("Help",menu); // add Help to menu bar mainMenu.setHelpMenu(menu); // define the help menu setMenuBar(mainMenu); // display the bar } void setupPanels(String t, String a) { // build the rest of the window displayArea = new TextArea("",30,120); displayArea.setEditable(false); // read only display Font d = new Font("Courier",Font.PLAIN,12); displayArea.setFont(d); displayArea.setBackground(Color.gray); displayArea.setForeground(Color.white); Label command = new Label("Command"); inputLine = new InputField("",12); clock = new TextField(" : : ",12); clock.setEditable(false); Font i = new Font("Courier",Font.PLAIN,12); clock.setFont(i); clock.setBackground(Color.white); clock.setForeground(Color.black); Panel parameters = parameterPanel.setMainPanel(); Insets border = new Insets(0,0,0,0); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = 1; constraints.weightx = 1.0; constraints.weighty = 1.0; constraints.anchor = GridBagConstraints.NORTH; constraints.fill = GridBagConstraints.NONE; constraints.insets = border; constraints.ipadx = 0; constraints.ipady = 0; GridBagLayout layout = new GridBagLayout(); layout.setConstraints(displayArea,constraints); constraints.gridx = GridBagConstraints.RELATIVE; constraints.gridy = GridBagConstraints.RELATIVE; constraints.gridwidth = 1; constraints.weightx = 1.0; constraints.anchor = GridBagConstraints.EAST; layout.setConstraints(command,constraints); constraints.anchor = GridBagConstraints.WEST; constraints.gridwidth = GridBagConstraints.RELATIVE; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.weightx = 1.0; layout.setConstraints(inputLine,constraints); constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.anchor = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.NONE; constraints.weightx = 1.0; layout.setConstraints(clock,constraints); constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridheight = GridBagConstraints.REMAINDER; constraints.weighty = 1.0; constraints.gridx = 0; layout.setConstraints(parameters,constraints); setLayout(layout); // (GridBagLayout) add(displayArea); add(command); add(inputLine); add(clock); add(parameters); pack(); setSize(getPreferredSize()); setVisible(true); // make window visible displayArea.setText(a); // initial text on the display displayArea.append(" is the author of "+t); parameterPanel.updateMainPanel(); updated(); } // public methods /* Constructor for the main window */ public MainGUI(String t, String a, ParameterPanel p) { // initialize the main window super(t); // define the main window stopSystem = false; // system is to run screenSize = Toolkit.getDefaultToolkit().getScreenSize(); title = t; author = a; parameterPanel = p; menus = new ListHead(); // place to keep menus mainGUI = this; // so other objects can reference mainMenu = new MenuBar(); // so that it can be referenced setupMenu(); // set up the menu bar setupPanels(t,a); // set up the display panels inputLine.requestFocus(); addWindowListener(new MainGUI.MainWindowEvents()); // wait for something to happen // addComponentListener(new GUI.WindowComponentListener()); // needed to find real information lastInput = ""; // input area is blank windowSize = getSize(); // center the window setLocation((screenSize.width-windowSize.width)/2, (screenSize.height-windowSize.height)/2); systemActive = false; setClock(0); append(CODE_FILE+" (r."+CODE_REVISION+") ready"); System.out.println(CODE_FILE+" (r."+CODE_REVISION+") ready"); } public synchronized void setActive(boolean f) { systemActive = f; if(systemActive) { displayArea.setBackground(Color.red); displayArea.setForeground(Color.black); repaint(); } else { displayArea.setBackground(Color.gray); displayArea.setForeground(Color.white); repaint(); } } public synchronized boolean getActive(){return systemActive;} public void setTitle(String s){super.setTitle(title+" ["+s+"]");repaint();} public void resetTitle(){super.setTitle(title);repaint();} /* A subpanel was updated */ public synchronized void updated() { pack(); setSize(getPreferredSize()); repaint(); } /* Clear the display area */ public synchronized void clear(){displayArea.setText("");} /* Add some text to the display area */ public synchronized void append(String t) { String s = "\n"+timeStamp(); displayArea.append(s+" "+t); } /* Get/set the text in the input area */ public synchronized String input() { return inputLine.getText(); // last thing entered } public synchronized void setInput(String input) { inputLine.setText(input); // set the input area } /* Set a value in the clock */ public synchronized void setClock(long ms) { String s; int hour, minute, second; second = (int) ms/1000; // append("seconds = "+second); hour = second/3600; // append("hour = "+hour); second = second%3600; minute = second/60; // append("minute = "+minute); second = second%60; // append("second = "+second); if(hour<10) s = " "+hour; else if(hour<100) s = " "+hour; else if(hour<1000) s = " "+hour; else s = Integer.toString(hour); s += ":"; s += minute<10?" "+minute:Integer.toString(minute); s += ":"; s += second<10?" "+second:Integer.toString(second); clock.setText(s); milliSeconds = ms; } public synchronized long getClock(){return milliSeconds;} /** Add a menu to the menu list and to the menu bar * (the menu object must already be complete) * @param name Name to be displayed on the menu bar * @param menu Menu item * @return (no returned data) */ public void addMenu(String name, Menu menu) { Menu helpMenu = mainMenu.getHelpMenu(); if(helpMenu!=null) mainMenu.remove(helpMenu); menus.setTail(new MenuList(name,menu)); // add to menu list mainMenu.add(menu); // add to the menu bar if(helpMenu!=null) mainMenu.add(helpMenu); repaint(); } /* Remove a menu from the menu list and the menu bar */ public boolean removeMenu(String name, Menu menu) { MenuList l = (MenuList)menus.getFirst(); // find the menu (by name) while((l!=null) && (l.getName()!=name)) l = (MenuList)l.getNext(); if(l==null) return false; // could not find menu menus.remove(l); mainMenu.remove(menu); return true; } /* Add a menu item to an existing menu in the menu list (the menu item must be already complete) */ /** Add a menu item to an existing menu in the menu list * (the menu item must be already complete) * @param name name to be displayed on the pulldown list * @param item menu item for the menu * @return (boolean) - true->menu item added * false->menu item was not added */ public boolean addMenuItem(String name, MenuItem item) { MenuList l = (MenuList)menus.getFirst(); // find the menu (by name) while((l!=null) && (l.getName()!=name)) l = (MenuList)l.getNext(); if(l==null) return false; // could not find menu l.getMenu().add(item); // add to the menu return true; } public boolean addMenuItem(String name, MenuItem item, int i) { MenuList l = (MenuList)menus.getFirst(); // find the menu (by name) while((l!=null) && (l.getName()!=name)) l = (MenuList)l.getNext(); if(l==null) return false; // could not find menu l.getMenu().insert(item,i); // add to the menu return true; } /* Display a message with OK f: any GUI frame d: dimensions of the GUI frame t: window title m: text at bottom */ public void message(Frame f, Dimension d, String t, String m) { MessageBoxEvents be = new MessageBoxEvents(); MessageBox b = new MessageBox(f,d,t,m,1,be,be,Color.white,Color.black); } public void message(String t, String m) { MessageBoxEvents be = new MessageBoxEvents(); MessageBox b = new MessageBox(this,screenSize,t,m,1,be,be,Color.white,Color.black); } public boolean question(String t, String m) { MessageBoxEvents be = new MessageBoxEvents(); MessageBox b = new MessageBox(this,screenSize,t,m,2,be,be,Color.yellow,Color.black); return boxButton; } public void error(String t, String m) { MessageBoxEvents be = new MessageBoxEvents(); MessageBox b = new MessageBox(this,screenSize,t,m,1,be,be,Color.red,Color.yellow); } // static methods /* Build a time stamp */ public static String timeStamp() { Calendar d = Calendar.getInstance(); int month = d.get(Calendar.MONTH)+1, day = d.get(Calendar.DAY_OF_MONTH), hour = d.get(Calendar.HOUR_OF_DAY), minute = d.get(Calendar.MINUTE), second = d.get(Calendar.SECOND); String s; if(month<10) s = " "+month+"/"; else s = month+"/"; if(day<10) s += " "+day+"/"; else s += day+"/"; s += d.get(Calendar.YEAR)+" "; if(hour<10) s += " "+hour+":"; else s += hour+":"; if(minute<10) s += " "+minute+":"; else s += minute+":"; if(second<10) s += " "+second; else s += second; return s; } public static String timeFix() { Calendar d = Calendar.getInstance(); int month = d.get(Calendar.MONTH)+1, day = d.get(Calendar.DAY_OF_MONTH), hour = d.get(Calendar.HOUR_OF_DAY), minute = d.get(Calendar.MINUTE), second = d.get(Calendar.SECOND); String s; s = String.valueOf(d.get(Calendar.YEAR)); if(month<10) s += "0"+month; else s += month; if(day<10) s += "0"+day; else s += day; if(hour<10) s += "0"+hour; else s += hour; if(minute<10) s += "0"+minute; else s += minute; if(second<10) s += "0"+second; else s += second; return s; } /* Fetch the system to stop flag */ public static boolean stop() {return stopSystem;} }