Index: NavigationSupport.java =================================================================== RCS file: /cvs/scripting/src/org/netbeans/modules/scripting/navigation/NavigationSupport.java,v retrieving revision 1.2 diff -u -r1.2 NavigationSupport.java --- NavigationSupport.java 2001/01/22 13:24:44 1.2 +++ NavigationSupport.java 2001/03/02 22:11:38 @@ -13,6 +13,9 @@ package org.netbeans.modules.scripting.navigation; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dialog; import java.awt.event.ActionEvent; import java.beans.PropertyVetoException; import java.lang.reflect.*; @@ -28,6 +31,8 @@ import java.util.Properties; import java.util.Stack; +import javax.swing.*; + import org.openide.TopManager; import org.openide.actions.ActionManager; import org.openide.actions.ToolsAction; @@ -58,7 +63,6 @@ public Properties forteProps = new Properties(System.getProperties()); public final String moduleName = "fjscript"; // NOI18N - public boolean topManagerLoaded = true; /** * This ExplorerManager is the context for selected nodes in Fjscript. * This is used by setCurObject to activate the currently selected object. @@ -104,6 +108,12 @@ */ public PrintStream outStrm = System.out; + /** + * used to create objects + */ + private Dialog templateDialog = null; + private NonGuiTemplateWizard templateWizard = null; + public NavigationSupport(InputStream in,PrintStream out) { inStrm = in; outStrm = out; @@ -118,13 +128,8 @@ boolean wasExited = exited; exited = true; - // this should test TopManager.isInitialized(), - // but that doesn't exist, so use local topManagerLoaded, - // which is set in FjscriptMain. - // -/// if (!wasExited && TopManager.isInitialized()) - if (!wasExited && topManagerLoaded) - TopManager.getDefault().exit(); + if (!wasExited && TopManager.isInitialized()) + TopManager.getDefault().exit(); System.exit(status); } @@ -159,8 +164,7 @@ // the system class loader won't be able to find them. // ClassLoader curLoader; -// if (TopManager.isInitialized()) { - if (topManagerLoaded) { + if (TopManager.isInitialized()) { curLoader = TopManager.getDefault().currentClassLoader(); if (curLoader != null) { cls = curLoader.loadClass(className); @@ -172,7 +176,7 @@ } catch (ClassNotFoundException e) { if (showException) - printException("Fjscript.loadClass", e); // NOI18N + printException("NavigationSupport.loadClass", e); // NOI18N } return(cls); } @@ -192,10 +196,10 @@ } } catch (IllegalAccessException iae) { - printException("Fjscript.createObject", iae); // NOI18N + printException("NavigationSupport.createObject", iae); // NOI18N } catch (InstantiationException ie) { - printException("Fjscript.createObject", ie); // NOI18N + printException("NavigationSupport.createObject", ie); // NOI18N } return obj; @@ -216,7 +220,7 @@ public int listMethods(Class c, boolean details) { Method ms[]; if (c == null) { - print("Fjscript.listMethods: "); // NOI18N + print("NavigationSupport.listMethods: "); // NOI18N println(getString("MSG_NullClass")); return CMD_FAIL; } @@ -236,7 +240,7 @@ } catch(Exception e) { - printException("Fjscript.listMethods", e); // NOI18N + printException("NavigationSupport.listMethods", e); // NOI18N return CMD_FAIL; } return CMD_OK; @@ -273,7 +277,7 @@ } catch(Exception e) { - printException("Fjscript.listConstructors", e); // NOI18N + printException("NavigationSupport.listConstructors", e); // NOI18N return CMD_FAIL; } return CMD_OK; @@ -330,7 +334,7 @@ public final static int DIRECTORY = 1; public final static int JAR = 2; - public int mountFileSystem (String fileName, int type) { + public FileSystem mountFileSystem (String fileName, int type) { FileSystem fs; fileName = expandVars(fileName); @@ -350,32 +354,32 @@ new Integer(type), new Integer(DIRECTORY), new Integer(JAR))); - return CMD_FAIL; + return null; } } catch (IOException ioe) { - printException("Fjscript.mountDirectory", ioe); // NOI18N - return CMD_FAIL; + printException("NavigationSupport.mountDirectory", ioe); // NOI18N + return null; } catch (java.beans.PropertyVetoException pe){ - printException("Fjscript.mountFileSystem", pe); // NOI18N - return CMD_FAIL; + printException("NavigationSupport.mountFileSystem", pe); // NOI18N + return null; } TopManager.getDefault().getRepository().addFileSystem(fs); - return CMD_OK; + return fs; } - public int mountDir(String fileName) { + public FileSystem mountDir(String fileName) { return mountDirectory(fileName); } - public int mountDirectory(String fileName) { + public FileSystem mountDirectory(String fileName) { return mountFileSystem(fileName, DIRECTORY); } - public int mountJar(String fileName) { + public FileSystem mountJar(String fileName) { return mountFileSystem(fileName, JAR); } @@ -405,11 +409,25 @@ } } else if (o instanceof JarFileSystem) { - JarFileSystem jfs=(JarFileSystem) o; - if (jfs.getJarFile().equals(theFile)) { - fs=jfs; - break; - } + JarFileSystem jfs=(JarFileSystem) o; + File jfsFile = jfs.getJarFile(); + boolean equal = jfsFile.equals(theFile); + // + // check for canonical file, too, to handle case + // where mounted jar is a symbolic link (b4363096) + // + if (!equal) { + try { + equal = jfsFile.equals(theFile.getCanonicalFile()); + } + catch (IOException io) { + //ignore + } + } + if (equal) { + fs=jfs; + break; + } } //else need to handle other file systems (CVS, Teamware, etc) //--why isn't there a generic equals(fileName) for FileSystem? @@ -427,6 +445,17 @@ return CMD_OK; } + public int unmountAll() { + Repository repo = TopManager.getDefault().getRepository(); + Enumeration all=repo.getFileSystems(); + while (all.hasMoreElements()) { + FileSystem fs = (FileSystem)all.nextElement(); + if (!fs.isHidden() && !fs.isDefault()) + repo.removeFileSystem(fs); + } + return CMD_OK; + } + public Object loadCommandClass (Object dataObj) { if (dataObj == null) @@ -610,10 +639,10 @@ explorerMgr.setSelectedNodes(arr); } catch (PropertyVetoException e) { - printException("Fjscript.setSelectedNodes", e); // NOI18N + printException("NavigationSupport.setSelectedNodes", e); // NOI18N } catch (IllegalArgumentException e) { - printException("Fjscript.setSelectedNodes", e); // NOI18N + printException("NavigationSupport.setSelectedNodes", e); // NOI18N } } @@ -695,7 +724,7 @@ return dataObj; } catch(DataObjectNotFoundException donf) { - printException("Fjscript.findObject", donf); // NOI18N + printException("NavigationSupport.findObject", donf); // NOI18N } return null; } @@ -802,15 +831,56 @@ } } } + + /** + * Run the specified action on the current data object (curDataObj). + * Loads the class and calls its actionPerformed method + * on the current data object. + * + * @param actionName full class name of the action to run + */ + public int doAction (String actionName) { + return doAction(actionName, 0); + } + + /** + * A variant of doAction for passing modifiers such as ActionEvent.CTRL_MASK. + * Run the specified action on the current data object (curDataObj). + * Loads the class and calls its actionPerformed method + * on the current data object. + * + * @param actionName full class name of the action to run + * @param modifiers the modifier keys held down during this action + */ + public int doAction (String actionName, int modifiers) { + return doAction(actionName, modifiers, 0, ""); + } + + /** + * Run the specified action on the current data object (curDataObj). + * Loads the class and calls its actionPerformed method + * on the current data object. + * A variant of doAction with the full set of options for creating the ActionEvent passed to the action. + * + * @param actionName full class name of the action to run + * @param modifiers the modifier keys held down during this action + * @param id an integer that identifies the event + * @param command a string that may specify a command (possibly one of several) associated with the event + * + * @see java.awt.event.ActionEvent + */ + public int doAction (String actionName, int modifiers, int id, String command) { + String origName = actionName; - public int doAction (String actionName) { checkCurDataObj(); Class cls = loadClass(actionName, false); - if (cls == null) - cls = loadClass("org.openide.actions." + actionName, false); + if (cls == null) { + actionName = "org.openide.actions." + actionName; + cls = loadClass(actionName, false); + } if (cls == null) { - println(getString("MSG_NoClass", actionName)); + println(getString("MSG_NoClass", origName)); return CMD_FAIL; } @@ -825,7 +895,7 @@ } Node node = curDataObj.getNodeDelegate(); - ((SystemAction) obj).actionPerformed(new ActionEvent(node, 0, "")); // NOI18N + ((SystemAction) obj).actionPerformed(new ActionEvent(node, id, command, modifiers)); // NOI18N return CMD_OK; } @@ -932,11 +1002,13 @@ if (curObj != null) { String clsName = curObj.getClass().getName(); String name = clsName.substring(clsName.lastIndexOf('.')+1); + String helpStr; - if (curObj instanceof CommandHelper) { + if (curObj instanceof CommandHelper + && (helpStr = ((CommandHelper)curObj).help()) != "") { println(getString("MSG_CmdClass", name)); println(getString("MSG_UndClass")); - println(((CommandHelper)curObj).help()); + println(helpStr); } else { println(getString("MSG_MethodsOn", name)); @@ -1006,7 +1078,7 @@ */ public int loadPropertyFile (String fileName) { - final String methodName = "Fjscript.loadPropertyFile"; // NOI18N + final String methodName = "NavigationSupport.loadPropertyFile"; // NOI18N Exception exc = null; fileName = expandVars(fileName); @@ -1174,7 +1246,7 @@ ModuleInstaller.deployTestModule (new File (fileName).getAbsoluteFile ()); } catch (IOException ioe) { - printException("Fjscript.loadModule", ioe); + printException("NavigationSupport.loadModule", ioe); return CMD_FAIL; } @@ -1343,6 +1415,221 @@ } + /** + * Run the new from template with the given template. + * You can use the templateSetXXX commands to set the values. When + * done do templateInstantiate. + */ + public void templateStart(String folder, String name) { + DataObject child = getTemplate(folder, name); + + if (child == null) { + outStrm.println( "Template " + + folder + + "/" + + name + + "not found."); + return; + } + templateWizard = new NonGuiTemplateWizard(); + templateWizard.setTemplate(child); + templateDialog = TopManager.getDefault().createDialog(templateWizard); + } + + public void templateFinish() throws Throwable { + if (templateWizard == null) { + outStrm.println( + "You must do a templateStart before you can run this command."); + return; + } + templateWizard.finish(); + templateWizard = null; + templateDialog = null; + } + + public Dialog templateGetDialog() { + return templateDialog; + } + + public TemplateWizard templateGetWizard() { + return templateWizard; + } + + public void templatePrintDialog() { + if (templateDialog == null) { + outStrm.println("You must run templateStart before this command."); + return; + } + outStrm.println(templateDialog.getClass().getName() + ":"); + printDialog(templateDialog, ""); + } + + public void printDialog(Container cont, String indent) { + Component[] comps; + + indent = indent + " "; + comps = cont.getComponents(); + for (int i = 0; i < comps.length; i++) { + String txt = ""; + + try { + Method getText; + + getText = comps[i].getClass().getMethod("getText", null); + txt = (String) getText.invoke(comps[i], null); + } catch (Throwable t) { + txt = ""; + } + outStrm.println( indent + + comps[i].getClass().getName() + + ":" + + comps[i].getName() + + ":" + + txt); + if (comps[i] instanceof Container) { + printDialog((Container) comps[i], indent); + } + } + } + + public void templatePrintTextField(String name) { + JTextField tFld = getTextField(name); + + if (tFld != null) { + outStrm.println(tFld.getText()); + } + } + + public void templateSetTextField(String name, String value) { + JTextField tFld = getTextField(name); + + if (tFld != null) { + tFld.setText(value); + } + } + + public void templateClickButton(String name) { + AbstractButton b = getButton(name); + + if (b != null) { + b.doClick(); + } + } + + + protected JTextField getTextField(String name) { + if (templateDialog == null) { + outStrm.println( + "You must run templateStart before running this command."); + return null; + } + JTextField tFld = getTextField(templateDialog, name); + if (tFld == null) { + outStrm.println("Text field " + name + " not found."); + return null; + } + return tFld; + } + + protected JTextField getTextField(Container cont, String name) { + Component[] comps; + + comps = cont.getComponents(); + for (int i = 0; i < comps.length; i++) { + if ( comps[i] instanceof JLabel + && i + 1 < comps.length + && comps[i+1] instanceof JTextField) { + JLabel lab = (JLabel) comps[i]; + + if (lab.getText().equals(name)) { + return (JTextField) comps[i+1]; + } + } + if (comps[i] instanceof Container) { + JTextField rval; + + if ((rval = getTextField((Container) comps[i], name)) != null) { + return rval; + } + } + } + return null; + } + + protected AbstractButton getButton(String name) { + if (templateDialog == null) { + outStrm.println( + "You must run templateStart before running this command."); + return null; + } + AbstractButton button = (AbstractButton) getComponent(templateDialog, + name, + AbstractButton.class); + if (button == null) { + outStrm.println("Button " + name + " not found."); + return null; + } + return button; + } + + protected Component getComponent(Container cont, + String name, + Class theClass) { + Component[] comps; + + comps = cont.getComponents(); + for (int i = 0; i < comps.length; i++) { + if (theClass.isAssignableFrom(comps[i].getClass())) { + String txt = null; + + if ( comps[i].getName() != null + && comps[i].getName().equals(name)) { + return comps[i]; + } + try { + Method getText = null; + + getText = comps[i].getClass().getMethod("getText", null); + txt = (String) getText.invoke(comps[i], null); + } catch (Throwable t) { + txt = null; + } + if (txt != null && txt.equals(name)) { + return comps[i]; + } + } + if (comps[i] instanceof Container) { + Component rval; + + rval = getComponent((Container) comps[i], name, theClass); + if (rval != null) { + return rval; + } + } + } + return null; + } + + public class NonGuiTemplateWizard extends TemplateWizard { + public void finish() throws Throwable { + handleInstantiate(); + } + } + + // The equivalent to the unix cat command for InputStream + public int printInputStream(InputStream is) { + byte[] buf = new byte[1024]; + int len; + try { + while ((len = is.read(buf)) > 0) { + outStrm.write(buf, 0, len); + } + } catch (IOException ioe) { + printException("NonGui.printInputStream", ioe); + return CMD_FAIL; + } + return CMD_OK; + } } //