/* SCCS @(#)NslExecutive.java 1.24---06/02/99--17:59:16 */ // Copyright: Copyright (c) 1997 University of Southern California Brain Project. // Copyright: This software may be freely copied provided the toplevel // Copyright: COPYRIGHT file is included with each such copy. // Copyright: Email nsl@java.usc.edu. package nslj.src.display; import java.io.*; import java.lang.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.lang.reflect.*; import nslj.src.lang.*; import nslj.src.system.*; import nslj.src.cmd.*; import nslj.src.nsls.struct.*; /** NSL Executive window @author Nikunj Mehta */ public class NslExecutive extends Frame implements ActionListener, ClipboardOwner { Frame myFrame; Vector menuOptions; static final public int trainIndex=5; static final public int runIndex=8; Menu protocol; Vector protocols; Clipboard clipboard; public NslExecutive (NslSystem s) { this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); system = s; s.setExecutive(this); setTitle("NSL Executive"); setBounds(0,0,500,250); MenuBar mbar = new MenuBar(); Menu m,md,sm; myFrame=this; NslThreadCommands.setExecutive(this); menuOptions = new Vector(10); protocols = new Vector(); clipboard = getToolkit().getSystemClipboard(); NslSystemActionAdapter systemListener = new NslSystemActionAdapter(this); NslEditActionAdapter editListener = new NslEditActionAdapter(); // System m = createMenu("System", new String[] {"Nsls file..."}, false); m.addActionListener(systemListener); mbar.add(m); sm = createMenu("Set", new String[] {"TrainEndTime","TrainStepSize", "TrainDelta","NumTrainEpochs","RunEndTime","RunStepSize","RunDelta", "NumRunEpochs", "DiffAprox", "DiffDelta", "Logging", "DisplayDelta"}, false); sm.addActionListener(systemListener); m.add(sm); m.add(new MenuItem("Exit")); // Edit m = createMenu("Edit", new String[] {"Select","Copy","Paste"}, true); m.addActionListener(editListener); mbar.add(m); // Protocol protocol = createMenu("Protocol", new String[] {"Manual","Separator"}, true); protocol.addActionListener(new NslProtocolActionListener()); mbar.add(protocol); // Simulation m = createMenu("Simulation", new String[] {"initSys","initModule","endModule","endSys"}, true); m.addActionListener(this); mbar.add(m); // Simulation menu commands run in the following sequence // "Initialize", "Run", "Break","Continue", "Step" // the sequence of commands affects a lot of the code!!! // To change the simulation menu, change code for enabling menuitems m = createMenu("Train", new String[] {"InitTrain", "SimTrain", "EndTrain", "Separator", "Train", "TrainAll", "Separator", "Break", "Continue"}, true); m.addActionListener(this); sm = createMenu("Step", new String[] { "1", "2", "3", "4", "5", "10", "20", "30", "40", "50" }, true); sm.addActionListener(this); Vector menuItems = new Vector(); menuItems.addElement(sm); menuOptions.addElement(menuItems); m.add(sm); mbar.add(m); m = createMenu("Run", new String[] { "InitRun", "SimRun", "EndRun", "Separator", "Run", "RunAll", "Separator", "Break", "Continue"}, true); m.addActionListener(this); sm = createMenu("Step", new String[] { "1", "2", "3", "4", "5", "10", "20", "30", "40", "50" }, true); sm.addActionListener(this); menuItems = new Vector(); menuItems.addElement(sm); menuOptions.addElement(menuItems); m.add(sm); mbar.add(m); // Display - may add input displays later md = createMenu("Display", new String[] { "New Output Frame", "New Input Frame"}, false); md.addActionListener(new NslActionAdapter()); mbar.add(md); /*m = createMenu("Options", new String[] {"Run Time and Run Step Size"}, false); m.addActionListener(new OptionActionAdapter(this)); mbar.add(m);*/ // Add link to bring up HTML Help page m =createMenu("Help", new String[] {"How To","Command Help","Setup"}, false); mbar.add(m); mbar.setHelpMenu(m); setMenuBar(mbar); disable(trainIndex,1); // simTrain disable(trainIndex,2); // endTrain disable(trainIndex,5); // break disable(trainIndex,6); // continue disable(trainIndex+2,0); // step disable(runIndex,1); // simRun disable(runIndex,2); // endRun disable(runIndex,5); // break disable(runIndex,6); // continue disable(runIndex+2,0); // step disable(2,1); // Copy setLayout(new GridLayout(1,1)); add(shell = new NslShell(system.getInterpreter().executive)); //shell = new NslShellThread(this, system); //shell.start(); } public void enable(int m, int s){ ((MenuItem)((Vector)(menuOptions.elementAt(m))).elementAt(s)).setEnabled(true); } public void disable(int m, int s){ ((MenuItem)((Vector)(menuOptions.elementAt(m))).elementAt(s)).setEnabled(false); } private class Protocol { public String name; public NslModule module; public boolean methodFound = true; Protocol(String n, NslModule m) { name = n; module = m; } } public boolean protocolInList(String name) { Enumeration e = protocols.elements(); Protocol p; String protocolName; while(e.hasMoreElements()) { p = (Protocol) e.nextElement(); protocolName = (String)p.name; if (name.equals(protocolName)) { return true; } } return false; } public void addProtocol(String name, NslModule module) { if (!protocolInList(name)) { MenuItem m = new MenuItem(name); protocol.add(m); protocols.addElement(new Protocol(name,module)); // We have to add it to the enable vector } } public void addProtocol(String name, String label, NslModule module) { if (!protocolInList(name)) { MenuItem m = new MenuItem(label); m.setActionCommand(name); protocol.add(m); protocols.addElement(new Protocol(name,module)); // We have to add it to the enable vector } } private Menu createMenu(String name, String[] items, boolean tearOff) { Menu m = new Menu(name,tearOff); Vector menuItems = new Vector(); for (int i = 0; i < items.length; i++) if (items[i].equals("Separator")) { m.addSeparator(); } else { MenuItem mi = new MenuItem(items[i]); m.add(mi); menuItems.addElement(mi); } menuOptions.addElement(menuItems); return m; } private class NslDotNslsFilter implements FilenameFilter { public boolean accept(File dir, String name) { return name.endsWith(".nsls"); } } public void execProtocol(String command) { Enumeration e = protocols.elements(); Protocol p; NslModule module=null; while(e.hasMoreElements()) { p = (Protocol)e.nextElement(); if (p.name.equals(command)) { try { String n = command+"Protocol"; Method m = p.module.getClass().getMethod(n,null); m.invoke(p.module,null); break; } catch (Exception ex) { if (p.methodFound) { System.out.println("Warning: Couldn't find method "+command+ "Protocol() in module "+p.module.getRealName()); } p.methodFound = false; } } } } private class NslProtocolActionListener implements ActionListener { public void actionPerformed(ActionEvent evt) { MenuItem menu = ((MenuItem)evt.getSource()); String command = evt.getActionCommand(); if (command.equals("Manual")) { // System.out.println("Manual mode"); command = "manual"; } system.setCurrentProtocol(command); } } private class NslEditActionAdapter implements ActionListener { public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (command.equals("Paste")) { //System.out.println("Paste"); Transferable contents = clipboard.getContents(this); if (contents != null)/* && DataFlavor.stringFlavor != null && contents.isDataFlavorSupported( DataFlavor.stringFlavor))*/ { try { String string; string = (String) contents.getTransferData( DataFlavor.stringFlavor); shell.paste(string); } catch (Exception e) { // e.printStackTrace(); } } } else if (command.equals("Copy")) { String string = shell.getSelectedText(); // System.out.println("Copying "+ string); shell.setSelectFlag(false); StringSelection contents = new StringSelection(string); clipboard.setContents(contents, NslExecutive.this); disable(2,1); enable(2,0); enable(2,2); } else { //System.out.println("Select"); shell.setSelectFlag(true); disable(2,0); disable(2,2); enable(2,1); } } } public void lostOwnership(Clipboard clip, Transferable transferable) { } private class NslSystemActionAdapter implements ActionListener { public NslSystemActionAdapter(Frame frame) { this.frame=frame; } Frame frame; public void actionPerformed(ActionEvent evt) { MenuItem menu = ((MenuItem)evt.getSource()); if (evt.getActionCommand().equals("Exit")) { dispose(); System.exit(0); } else if (evt.getActionCommand().equals("Nsls file...")) { FileDialog dialog = new FileDialog(myFrame, "Source nsls file"); dialog.setFilenameFilter(new NslDotNslsFilter()); dialog.setVisible(true); String fileName; if ((fileName=dialog.getFile()) != null) { //System.out.println("Sourcing file "+fileName); //Executive.sourceFile(fileName); NslThreadCommands cmdExec = new NslThreadCommands( "Source", fileName, system, system.getInterpreter()); cmdExec.start(); //} else { // System.out.println("Source file cancelled"); } } else { NslSetVariable window = new NslSetVariable(frame, evt.getActionCommand(),system); } } } private class NslActionAdapter implements ActionListener { public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("New Output Frame")) { // NslDisplaySystem ds = new NslDisplaySystem(null, system); NslOutModule m = new NslOutDummyModule(); m.show(); } } } public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); NslThreadCommands cmdExec = new NslThreadCommands( command, system, system.getInterpreter()); cmdExec.start(); } /* private class OptionActionAdapter implements ActionListener { Frame frame; OptionActionAdapter(Frame f) { frame = f;} public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("Run Time and Run Step Size")) { NslSetVariable ap ; do { ap = new NslSetVariable(frame, system.getEndTime(), system.getRunStepSize(), system.getEndTime() ); system.setEndTime( ap.value ); system.setRunStepSize( ap.secvalue ); NslTemporalCanvas.graphsize = ap.thirdvalue; if ((system.endTime() < 0) || (system.getRunStepSize() < 0) || (ap.thirdvalue < 0)) { NslErrorWrong errorwindow = new NslErrorWrong(frame); } else { System.out.println("nsl set system.endTime "+ap.value); System.out.println("nsl set system.stepSize "+ap.secvalue); } } while ((system.endTime() < 0) && (system.getRunStepSize() < 0)); } } }*/ static NslSystem system; NslShell shell; }