001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) 2005 Integrated Systems Analysts, Inc.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: *
026: * Integrated Systems Analysts, Inc.
027: * 630C Anchors St., Suite 101
028: * Fort Walton Beach, Florida
029: * USA
030: *
031: * (850)862-7321
032: */
033: package org.openjump.core.ui.plugin.customize;
034:
035: import bsh.EvalError;
036: import bsh.Interpreter;
037:
038: import com.vividsolutions.jump.I18N;
039: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
040: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
041: import com.vividsolutions.jump.workbench.ui.toolbox.ToolboxDialog;
042: import com.vividsolutions.jump.workbench.ui.MenuNames;
043: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
044: import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
045: import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager;
046:
047: import java.io.File;
048: import java.io.IOException;
049:
050: import javax.swing.event.MenuEvent;
051: import javax.swing.event.MenuListener;
052: import javax.swing.JComponent;
053: import javax.swing.JMenu;
054: import javax.swing.JMenuItem;
055: import java.awt.event.ActionListener;
056: import java.awt.event.ActionEvent;
057:
058: import org.apache.log4j.Logger;
059:
060: /**
061: * This OpenJUMP PlugIn adds the capability to launch a scripted file from the menu.
062: * The original design is from ISA (Larry Becker - 2005)
063: * Modified by Michaël Michaud in order to make hirarchical menus possible.
064: */
065: public class BeanToolsPlugIn extends AbstractPlugIn {
066:
067: private static final Logger LOG = Logger
068: .getLogger(BeanToolsPlugIn.class);
069:
070: private String lastcmd = "";
071: private String beanShellDirName;
072: private TaskMonitorManager taskMonitorManager;
073: private FeatureInstaller featureInstaller;
074:
075: public void initialize(PlugInContext context) throws Exception {
076: File plugInDirectory = context.getWorkbenchContext()
077: .getWorkbench().getPlugInManager().getPlugInDirectory();
078: if (null == plugInDirectory || !plugInDirectory.exists()) {
079: LOG
080: .debug("BeanTools plugin has not been initialized : the plugin directory is missing");
081: return;
082: }
083: beanShellDirName = plugInDirectory.getPath()
084: + File.separator
085: + I18N
086: .get("ui.plugin.customize.BeanToolsPlugIn.BeanTools");
087: File beanShellDir = new File(beanShellDirName);
088: featureInstaller = context.getFeatureInstaller();
089: taskMonitorManager = new TaskMonitorManager();
090: if (beanShellDir.exists()) {
091: scanBeanShellDir(beanShellDir, context);
092: }
093: }
094:
095: /**
096: * Extracts the filepath as a String from dir to file
097: */
098: private String ancestors(File dir, File file) throws IOException {
099: String path = file.getCanonicalPath();
100: return path.substring(path.lastIndexOf(dir.getName()), path
101: .lastIndexOf(file.getName()));
102: }
103:
104: /**
105: * Scan beanShellDir iteratively and makes a script menu-item from each
106: * .bsh file.
107: */
108: private void scanBeanShellDir(final File file,
109: final PlugInContext context) throws IOException {
110: // iterates over subdirectories
111: if (file.isDirectory()) {
112: for (File f : file.listFiles()) {
113: scanBeanShellDir(f, context);
114: }
115: }
116: // add a menu item for the beanshell script
117: else if (file.getName().endsWith(".bsh")) {
118: File beanShellDir = new File(beanShellDirName);
119: String ancestors = ancestors(beanShellDir, file);
120: String shellName = file.getName().substring(0,
121: file.getName().length() - 4);
122: JMenu menu = featureInstaller
123: .menuBarMenu(MenuNames.CUSTOMIZE);
124: if (menu == null) {
125: menu = (JMenu) featureInstaller.installMnemonic(
126: new JMenu(I18N.get(MenuNames.CUSTOMIZE)),
127: featureInstaller.menuBar());
128: featureInstaller.menuBar().add(menu);
129: }
130: JMenu parent = featureInstaller.createMenusIfNecessary(
131: menu, ancestors.split(File.separator.replace("\\",
132: "\\\\")));
133: final JMenuItem menuItem = featureInstaller
134: .installMnemonic(new JMenuItem(shellName), parent);
135: final ActionListener listener = AbstractPlugIn
136: .toActionListener(this , context
137: .getWorkbenchContext(), taskMonitorManager);
138: menuItem.addActionListener(new ActionListener() {
139: // if one press this menu item
140: public void actionPerformed(ActionEvent e) {
141: // change the lastcmd path
142: if (e != null)
143: lastcmd = file.getPath();
144: // execute this plugin execute method
145: listener.actionPerformed(e);
146: }
147: });
148: parent.add(menuItem);
149: }
150: // file is not a directory and it does not end with .bsh
151: else
152: ;
153: }
154:
155: public String getName() {
156: return "Bean Tools";
157: }
158:
159: public boolean execute(final PlugInContext context)
160: throws Exception {
161: ToolboxDialog toolbox = new ToolboxDialog(context
162: .getWorkbenchContext());
163: try {
164: Interpreter interpreter = new Interpreter();
165: interpreter
166: .setClassLoader(toolbox.getContext().getWorkbench()
167: .getPlugInManager().getClassLoader());
168: interpreter.set("wc", toolbox.getContext());
169: interpreter.eval("setAccessibility(true)");
170: interpreter.eval("import com.vividsolutions.jts.geom.*");
171: interpreter
172: .eval("import com.vividsolutions.jump.feature.*");
173: //interpreter.source(beanShellDirName + File.separator + lastcmd + ".bsh");
174: interpreter.source(lastcmd);
175: } catch (EvalError e) {
176: toolbox.getContext().getErrorHandler().handleThrowable(e);
177: }
178: return true;
179: }
180:
181: }
|