package genegui; import java.awt.*; import java.awt.event.*; import javax.swing.*; import main.Model; import main.Globals; import main.NetworkFile; import // TODO Slated for destruction main.Network; public class GeneNetInterface { // protected JFrame mainWindow = new JFrame(); public InspectorList inspectors = new InspectorList(); // Sends out action events to other parts of interface and to core of program private ActionListener actionListener = null; // All messages from the items in the main menu bar pass through this listener. // This listener handles a few of them (loading and saving, primarily) and passes // the rest on by making a new action event and sending it out. Anything that listens // to this class will therefore receive menu item events. private ActionListener topListener = null; // These are constants that are used to define different types of action events public final static int LOAD_MODEL = 100, LOAD_EXPERIMENT = 101, LOAD_CAM_FILE = 102, SAVE_MODEL = 103, INTERFACE_ACTION = InspectorList.INTERFACE_ACTION; // The following are the names for various menu items in the primary menu bar. // Defined here so they only need to be changed in one place. public final static String LOAD_TEXT = "Load...", LOAD_EXPT_TEXT = "Load Experiment...", SAVE_MODEL_TEXT = "Save Model...", REMOVE_EXPERIMENT_TEXT = "Remove Experiment", OPTIONS_TEXT = "Options...", QUIT_TEXT = "Quit", RUN_MODEL_TEXT = "Run Model", STEP_MODEL_TEXT = "Step Model", RESET_MODEL_TEXT = "Reset Model", RUN_FIXED_POINT_TEXT = "Run Fixed Point", RUN_ITERATOR_TEXT = "Run Iterator", STOP_TEXT = "Stop", PHASE_PORTRAIT = "Phase Portrait", UPDATE_MODEL = InspectorList.UPDATE_MODEL; Network network = null; public GeneNetInterface() { // mainWindow.setBounds(10, 10, 750, 550); // mainWindow.setContentPane(new JDesktopPane()); topListener = new SymAction(); addActionListener(inspectors); inspectors.addActionListener(topListener); // Note - windows should be made AFTER the listeners are all hooked up. // each window gets its menu by passing a message back to this class saying that // its just been made, and this class then adds a menu. But that will only happen // if listeners are hooked up. // inspectors.makeWindows(mainWindow.getContentPane()); inspectors.makeWindows(); // mainWindow.setJMenuBar(makeMenuBar()); // mainWindow.setVisible(true); } // This function makes the main menu bar. It's called from the event handling inner-class below private JMenuBar makeMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menu1 = new JMenu("File"); localAddToMenu(menu1, LOAD_TEXT, topListener, 'O'); //java.awt.event.KeyEvent.VK_O); localAddToMenu(menu1, SAVE_MODEL_TEXT, topListener, 'S'); menu1.addSeparator(); // localAddToMenu(menu1, LOAD_EXPT_TEXT, topListener, ' '); //0); // localAddToMenu(menu1, REMOVE_EXPERIMENT_TEXT, topListener, ' '); //0); // menu1.addSeparator(); localAddToMenu(menu1, OPTIONS_TEXT, topListener, ' '); localAddToMenu(menu1, QUIT_TEXT, topListener, 'Q'); menuBar.add(menu1); JMenu menu2 = new JMenu("Run"); localAddToMenu(menu2, RUN_MODEL_TEXT, topListener, 'R'); localAddToMenu(menu2, STEP_MODEL_TEXT, topListener, 'G'); localAddToMenu(menu2, RESET_MODEL_TEXT, topListener, 'H'); localAddToMenu(menu2, RUN_FIXED_POINT_TEXT, topListener, ' '); localAddToMenu(menu2, RUN_ITERATOR_TEXT, topListener, 'I'); localAddToMenu(menu2, STOP_TEXT, topListener, 'T'); menuBar.add(menu2); JMenu menu3 = new JMenu("Other"); localAddToMenu(menu3, "Model State Editor", topListener, ' '); localAddToMenu(menu3, "Inspect Network", topListener, ' '); localAddToMenu(menu3, "Inspect Cells", topListener, ' '); localAddToMenu(menu3, "Inspect Parameters...", topListener, ' '); localAddToMenu(menu3, "Inspect Model File", topListener, ' '); localAddToMenu(menu3, PHASE_PORTRAIT, topListener, ' '); menuBar.add(menu3); return menuBar; } // A function used by makeMenuBar to cut down on typing private void localAddToMenu(JMenu menu, String name, ActionListener listener, char shortcut) { JMenuItem item = new JMenuItem(name); if(shortcut != ' ') { KeyStroke ks = KeyStroke.getKeyStroke(shortcut, java.awt.Event.CTRL_MASK); //item.setShortcut(new MenuShortcut(shortcut)); item.setAccelerator(ks); } if(listener != null) item.addActionListener(listener); menu.add(item); } // Many of the menu commands are handled elsewhere. This one picks up the remainder, // primarily loading and saving class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { if(event.getID() == ActionEvent.ACTION_PERFORMED) { String command = event.getActionCommand(); if(command == LOAD_TEXT) { /* JFileChooser fd = new JFileChooser(); // fd.addChooseableFileType("Network Files (*.net)", "*.net", null); // fd.addChooseableFileType("Iterator Files (*.iter)", "*.iter", null); // fd.addChooseableFileType("Cam Files (*.cam)", "*.cam", null); // fd.addChooseableFileType("Stopper Files (*.stop)", "*.stop", null); // fd.addChooseableFileType("All Files (*.*)", "*.*", null); fd.addChoosableFileFilter(fd.getAcceptAllFileFilter()); if(fd.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) return; // Cancel String filestring = fd.getSelectedFile().toString(); */ FileDialog fd = new FileDialog(new Frame(), "Load Setup File"); fd.setVisible(true); if(fd.getFile() == null) return; // Cancel // Pass the load message on to the non-gui part of the program through an action event. // Currently handled in the main file if(actionListener != null) { // actionListener.actionPerformed(new ActionEvent(this, LOAD_MODEL, filestring)); actionListener.actionPerformed(new ActionEvent(this, LOAD_MODEL, fd.getDirectory() + fd.getFile())); } } else if(command == LOAD_EXPT_TEXT) { FileDialog fd = new FileDialog(new Frame(), "Experiment"); fd.setVisible(true); if(fd.getFile() == null) return; // Cancel // Pass the load message on to the non-gui part of the program through an action event. // Currently handled in the main file if(actionListener != null) { actionListener.actionPerformed(new ActionEvent(this, LOAD_EXPERIMENT, fd.getDirectory() + fd.getFile())); } } else if(command == SAVE_MODEL_TEXT) { FileDialog fd = new FileDialog(new Frame(), "Save into file:", FileDialog.SAVE); if(network != null) fd.setFile(network.getName() + ".net"); fd.show(); if(fd.getFile() == null) return; if(actionListener != null) { actionListener.actionPerformed(new ActionEvent(this, SAVE_MODEL, fd.getDirectory() + fd.getFile())); } } else if(command == OPTIONS_TEXT) { new OptionsDlog(); } else actionListener.actionPerformed(event); // Pass it on } else if(event.getID() == InspectorList.ADD_WINDOW) { // Used to add main menu to new windows // This is a bit of a clumsy way to get every window that needs one a main menu bar that // is hooked up to the right action listeners. As new windows are created by // the InspectorList class, it sends out ADD_WINDOW messages for each one that // needs a main menu bar. That message gets received here, and a new main menu gets // created and added. // ((JFrame)event.getSource()).setJMenuBar(makeMenuBar()); // Blocked by WJS } else if(event.getID() == INTERFACE_ACTION && event.getActionCommand().equals(UPDATE_MODEL)) { if(actionListener != null) actionListener.actionPerformed(event); } } } /** Call this when the currently loaded model changes */ public void setModel(Model model) { inspectors.setModel(model); } /** Call this when the currently loaded model (network) changes */ public void setNetwork(Network network) { inspectors.setNetwork(network); this.network = network; // Use this to get name when saving file } // /** Called when the class holding the text for the network is changed. Currently // only called at startup. */ // public void setNetworkFile(NetworkFile file) { // inspectors.setNetworkFile(file); // } // Stock code for sending out action events public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener, l); } public void removeActionListener(ActionListener l) { actionListener = AWTEventMulticaster.remove(actionListener, l); } }