diff --git a/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JConsoleOutputConsole.java b/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JConsoleOutputConsole.java deleted file mode 100644 index 3c8042f1bf..0000000000 --- a/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JConsoleOutputConsole.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * JGrass - Free Open Source Java GIS http://www.jgrass.org - * (C) HydroloGIS - www.hydrologis.com - * - * This library is free software; you can redistribute it and/or modify it under - * the terms of the GNU Library General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) any - * later version. - * - * This library is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more - * details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; if not, write to the Free Foundation, Inc., 59 - * Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package eu.udig.jconsole; - -import java.io.PrintStream; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.console.MessageConsole; -import org.eclipse.ui.console.MessageConsoleStream; - -/** - * The {@link MessageConsole} of the script editor. - * - * @author Andrea Antonello (www.hydrologis.com) - */ -public class JConsoleOutputConsole extends MessageConsole { - - private final static String title = "JConsole Output"; - public final PrintStream internal; - public final PrintStream err; - public final PrintStream out; - - private Color COLOR_GRAY; - private Color COLOR_RED; - private Color COLOR_BLACK; - - public JConsoleOutputConsole( String newTitle ) { - super(newTitle == null ? title : newTitle, null); - - Display.getDefault().syncExec(new Runnable(){ - public void run() { - COLOR_GRAY = Display.getDefault().getSystemColor(SWT.COLOR_GRAY); - COLOR_RED = Display.getDefault().getSystemColor(SWT.COLOR_RED); - COLOR_BLACK = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); - } - }); - - this.setTabWidth(4); - MessageConsoleStream internalStream = newMessageStream(); - internal = new PrintStream(internalStream, true); - internalStream.setColor(COLOR_BLACK); - MessageConsoleStream errorStream = newMessageStream(); - err = new PrintStream(errorStream, true); - errorStream.setColor(COLOR_RED); - MessageConsoleStream outputStream = newMessageStream(); - out = new PrintStream(outputStream, true); - outputStream.setColor(COLOR_BLACK); - } - - protected void dispose() { - super.dispose(); - } - - public void setName( String arg0 ) { - super.setName(arg0); - } -} diff --git a/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JavaEditor.java b/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JavaEditor.java index 3b439b73bf..15e017b8ab 100644 --- a/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JavaEditor.java +++ b/plugins/eu.udig.jconsole/src/eu/udig/jconsole/JavaEditor.java @@ -26,6 +26,8 @@ import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; +import eu.udig.omsbox.core.JConsoleOutputConsole; + /** * Java specific text editor. */ diff --git a/plugins/eu.udig.jconsole/src/eu/udig/jconsole/actions/StartStopAction.java b/plugins/eu.udig.jconsole/src/eu/udig/jconsole/actions/StartStopAction.java new file mode 100644 index 0000000000..1c2c9220a0 --- /dev/null +++ b/plugins/eu.udig.jconsole/src/eu/udig/jconsole/actions/StartStopAction.java @@ -0,0 +1,169 @@ +/* + * JGrass - Free Open Source Java GIS http://www.jgrass.org + * (C) HydroloGIS - www.hydrologis.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package eu.udig.jconsole.actions; + +import java.io.File; +import java.io.PrintStream; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.console.IConsole; +import org.eclipse.ui.console.IConsoleManager; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.ui.texteditor.TextEditorAction; +import org.joda.time.DateTime; + +import eu.udig.jconsole.JConsolePlugin; +import eu.udig.jconsole.JavaEditor; +import eu.udig.jconsole.JavaEditorMessages; +import eu.udig.omsbox.OmsBoxPlugin; +import eu.udig.omsbox.core.IProcessListener; +import eu.udig.omsbox.core.JConsoleOutputConsole; +import eu.udig.omsbox.core.OmsScriptExecutor; +import eu.udig.omsbox.utils.OmsBoxConstants; + +/** + * A toolbar action which toggles start and stop of the module. + * + * @author Andrea Antonello (www.hydrologis.com) + */ +public class StartStopAction extends TextEditorAction implements IProcessListener { + + public static final String START = "icons/start.gif"; //$NON-NLS-1$ + public static final String STOP = "icons/stop.gif"; //$NON-NLS-1$ + private String scriptID; + + /** + * Constructs and updates the action. + */ + public StartStopAction() { + super(JavaEditorMessages.getResourceBundle(), "StartStop.", null); //$NON-NLS-1$ + setButtonOnStart(); + update(); + } + + public void run() { + JavaEditor editor = (JavaEditor) getTextEditor(); + + Process process = editor.getProcess(); + if (process != null) { + process.destroy(); + process = null; + editor.setProcess(null); + // editor.setExecutor(null); + setButtonOnStart(); + } else { + IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput()); + JConsoleOutputConsole outputConsole = editor.getOutputConsole(); + outputConsole.clearConsole(); + + String text = null; + ISelection selection = editor.getSelectionProvider().getSelection(); + if (selection instanceof ITextSelection) { + ITextSelection textSelection = (ITextSelection) selection; + if (!textSelection.isEmpty()) { + text = textSelection.getText(); + } + } + if (text == null || 0 >= text.length()) { + text = doc.get(); + } + + execute(text); + } + } + + private void execute( String text ) { + String trimmed = text.trim(); + int limit = 15; + if (trimmed.length() <= limit) { + limit = trimmed.length() - 1; + } + String title = trimmed.substring(0, limit); + JConsoleOutputConsole outputConsole = new JConsoleOutputConsole("Script: " + title); + outputConsole.clearConsole(); + + PrintStream internalStream = outputConsole.internal; + // PrintStream outputStream = outputConsole.out; + PrintStream errorStream = outputConsole.err; + // open console + IConsoleManager manager = org.eclipse.ui.console.ConsolePlugin.getDefault().getConsoleManager(); + manager.addConsoles(new IConsole[]{outputConsole}); + manager.showConsoleView(outputConsole); + + try { + OmsScriptExecutor executor = new OmsScriptExecutor(); + executor.addProcessListener(this); + String loggerLevelGui = OmsBoxPlugin.getDefault().retrieveSavedLogLevel(); + String ramLevel = String.valueOf(OmsBoxPlugin.getDefault().retrieveSavedHeap()); + Process process = executor.exec(text, internalStream, errorStream, loggerLevelGui, ramLevel); + + scriptID = "geoscript_" + new DateTime().toString(OmsBoxConstants.dateTimeFormatterYYYYMMDDHHMMSS); + OmsBoxPlugin.getDefault().addProcess(process, scriptID); + + // cleanup when leaving uDig + // scriptFile.deleteOnExit(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void update() { + ITextEditor textEditor = getTextEditor(); + if (textEditor instanceof JavaEditor) { + JavaEditor editor = (JavaEditor) textEditor; + + if (OmsBoxPlugin.getDefault().getRunningProcessesMap().get(scriptID) != null) { + setButtonOnStop(); + } else { + setButtonOnStart(); + } + JConsoleOutputConsole outputConsole = editor.getOutputConsole(); + if (outputConsole != null) { + IConsoleManager manager = org.eclipse.ui.console.ConsolePlugin.getDefault().getConsoleManager(); + manager.showConsoleView(outputConsole); + } + } + } + + private void setButtonOnStart() { + ImageDescriptor id = AbstractUIPlugin.imageDescriptorFromPlugin(JConsolePlugin.PLUGIN_ID, START); + setImageDescriptor(id); + setChecked(false); + setEnabled(true); + } + + private void setButtonOnStop() { + ImageDescriptor id = AbstractUIPlugin.imageDescriptorFromPlugin(JConsolePlugin.PLUGIN_ID, STOP); + setImageDescriptor(id); + setChecked(true); + setEnabled(true); + } + + @Override + public void onProcessStopped() { + setButtonOnStart(); + JavaEditor editor = (JavaEditor) getTextEditor(); + editor.setProcess(null); + // editor.setExecutor(null); + } + +} diff --git a/plugins/eu.udig.omsbox/src/eu/udig/omsbox/view/actions/OmsScriptExecutionAction.java b/plugins/eu.udig.omsbox/src/eu/udig/omsbox/view/actions/OmsScriptExecutionAction.java index 0629d63c45..27b89f8827 100644 --- a/plugins/eu.udig.omsbox/src/eu/udig/omsbox/view/actions/OmsScriptExecutionAction.java +++ b/plugins/eu.udig.omsbox/src/eu/udig/omsbox/view/actions/OmsScriptExecutionAction.java @@ -92,6 +92,7 @@ private void execute( String path ) { e.printStackTrace(); } } + public void selectionChanged( IAction action, ISelection selection ) { }