Permalink
Browse files

Improvements of send to console

added IErlangConsole and IErlangConsolePage
  • Loading branch information...
1 parent bfb0695 commit 46aa898a22f45d6693432909062a73f4a46c1d54 @jakobc jakobc committed Mar 4, 2013
@@ -351,6 +351,10 @@ public IErlModule findModule(final IFile file) {
@Override
public IErlProject findProject(final IProject project) {
+ try {
+ open(null);
+ } catch (final ErlModelException e) {
+ }
final IErlElement e = findElement(project);
if (e == null) {
return null;
@@ -50,7 +50,6 @@
import org.erlide.ui.console.actions.ConsoleTerminateAction;
import org.erlide.ui.console.actions.ShowStandardOutAction;
import org.erlide.ui.console.actions.ShowWhenContentChangesAction;
-import org.erlide.ui.internal.ErlideUIPlugin;
import org.erlide.ui.util.DisplayUtils;
/**
@@ -123,10 +122,6 @@ public void init(final IPageBookViewPage page, final IConsole console) {
// create handler and submissions for EOF
fEOFHandler = new EOFHandler();
-
- // set global ref, used by the SendToConsole action
- // FIXME global is bad, use project's page (keyed by backend?)
- ErlideUIPlugin.getDefault().setConsolePage((ErlangConsolePage) fPage);
}
@Override
@@ -152,9 +147,6 @@ public void dispose() {
fStdOut = null;
}
fConsole = null;
- if (ErlideUIPlugin.getDefault().getConsolePage() == fPage) {
- ErlideUIPlugin.getDefault().setConsolePage(null);
- }
}
/**
@@ -246,7 +238,6 @@ public void activated() {
fActivatedContext = contextService.activateContext(fContextId);
fActivatedHandler = handlerService.activateHandler(
"org.eclipse.debug.ui.commands.eof", fEOFHandler); //$NON-NLS-1$
- ErlideUIPlugin.getDefault().setConsolePage((ErlangConsolePage) fPage);
}
@Override
@@ -1,6 +1,5 @@
package org.erlide.ui.console;
-import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IProject;
@@ -14,12 +13,16 @@
import org.erlide.util.ErlLogger;
import org.erlide.util.IDisposable;
+import com.google.common.collect.Maps;
+
public class ErlConsoleManager implements IDisposable, IBackendListener {
- private final Map<IBackend, IConsole> consoles;
+ private final Map<IBackend, IErlangConsole> consoles;
+ private final Map<IErlangConsole, IErlangConsolePage> pages;
private final IConsoleManager conMan;
public ErlConsoleManager() {
- consoles = new HashMap<IBackend, IConsole>();
+ consoles = Maps.newHashMap();
+ pages = Maps.newHashMap();
final ConsolePlugin consolePlugin = ConsolePlugin.getDefault();
conMan = consolePlugin.getConsoleManager();
@@ -52,6 +55,23 @@ public void runtimeRemoved(final IBackend b) {
conMan.removeConsoles(new IConsole[] { console });
}
+ public void addPage(final IErlangConsole console,
+ final IErlangConsolePage page) {
+ pages.put(console, page);
+ }
+
+ public void removePage(final IErlangConsole console) {
+ pages.remove(console);
+ }
+
+ public IErlangConsolePage getPage(final IErlangConsole console) {
+ return pages.get(console);
+ }
+
+ public IErlangConsole getConsole(final IBackend backend) {
+ return consoles.get(backend);
+ }
+
@Override
public void dispose() {
BackendCore.getBackendManager().removeBackendListener(this);
@@ -13,18 +13,15 @@
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleDocumentPartitioner;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.part.IPageBookViewPage;
import org.erlide.backend.IBackend;
import org.erlide.runtime.shell.IBackendShell;
+import org.erlide.ui.internal.ErlideUIPlugin;
-public class ErlangConsole extends TextConsole {
+public class ErlangConsole extends TextConsole implements IErlangConsole {
private final IBackendShell shell;
protected ListenerList consoleListeners;
protected ErlangConsolePartitioner partitioner;
@@ -45,13 +42,19 @@ public ErlangConsole(final IBackend backend) {
@Override
public IPageBookViewPage createPage(final IConsoleView view) {
- return new ErlangConsolePage(view, this, backend.getRpcSite());
+ final ErlangConsolePage erlangConsolePage = new ErlangConsolePage(view,
+ this, backend.getRpcSite());
+ ErlideUIPlugin.getDefault().getErlConsoleManager()
+ .addPage(this, erlangConsolePage);
+ return erlangConsolePage;
}
+ @Override
public IBackend getBackend() {
return backend;
}
+ @Override
public IBackendShell getShell() {
return shell;
}
@@ -80,18 +83,18 @@ public void removePropertyChangeListener(
final IPropertyChangeListener listener) {
}
- public void show() {
- final IWorkbenchPage page = PlatformUI.getWorkbench()
- .getActiveWorkbenchWindow().getActivePage();
- final String id = IConsoleConstants.ID_CONSOLE_VIEW;
- IConsoleView view;
- try {
- view = (IConsoleView) page.showView(id);
- view.display(this);
- } catch (final PartInitException e) {
- e.printStackTrace();
- }
- }
+ // public void show() {
+ // final IWorkbenchPage page = PlatformUI.getWorkbench()
+ // .getActiveWorkbenchWindow().getActivePage();
+ // final String id = IConsoleConstants.ID_CONSOLE_VIEW;
+ // IConsoleView view;
+ // try {
+ // view = (IConsoleView) page.showView(id);
+ // view.display(this);
+ // } catch (final PartInitException e) {
+ // e.printStackTrace();
+ // }
+ // }
@Override
protected IConsoleDocumentPartitioner getPartitioner() {
@@ -75,13 +75,14 @@
import org.erlide.runtime.ParserException;
import org.erlide.runtime.RuntimeHelper;
import org.erlide.runtime.shell.IBackendShell;
+import org.erlide.ui.internal.ErlideUIPlugin;
import com.ericsson.otp.erlang.OtpErlangList;
import com.ericsson.otp.erlang.OtpErlangObject;
@SuppressWarnings("restriction")
public class ErlangConsolePage extends Page implements IAdaptable,
- IPropertyChangeListener {
+ IPropertyChangeListener, IErlangConsolePage {
public static final String ID = "org.erlide.ui.views.console";
Color bgColor_Ok;
@@ -141,6 +142,7 @@ public void dispose() {
bgColor_Err.dispose();
bgColor_Ok.dispose();
}
+ ErlideUIPlugin.getDefault().getErlConsoleManager().removePage(fConsole);
super.dispose();
}
@@ -167,6 +169,7 @@ protected void sendInput() {
consoleInput.setText("");
}
+ @Override
public void input(final String data) {
final String data2 = data.trim() + "\n";
shell.input(data2);
@@ -0,0 +1,13 @@
+package org.erlide.ui.console;
+
+import org.eclipse.ui.console.IConsole;
+import org.erlide.backend.IBackend;
+import org.erlide.runtime.shell.IBackendShell;
+
+public interface IErlangConsole extends IConsole {
+
+ public abstract IBackendShell getShell();
+
+ public abstract IBackend getBackend();
+
+}
@@ -0,0 +1,7 @@
+package org.erlide.ui.console;
+
+public interface IErlangConsolePage {
+
+ public abstract void input(final String data);
+
+}
@@ -182,7 +182,7 @@ protected void createCommonActions() {
sendToConsole = new SendToConsoleAction(getSite(),
ErlangEditorMessages.getBundleForConstructedKeys(),
- "SendToConsole.", this, false);
+ "SendToConsole.", this, false, getProject());
sendToConsole
.setActionDefinitionId(IErlangEditorActionDefinitionIds.SEND_TO_CONSOLE);
setAction("SendToConsole", sendToConsole);
@@ -191,7 +191,7 @@ protected void createCommonActions() {
sendToConsoleWithResult = new SendToConsoleAction(getSite(),
ErlangEditorMessages.getBundleForConstructedKeys(),
- "SendToConsoleWithResult.", this, true);
+ "SendToConsoleWithResult.", this, true, getProject());
sendToConsoleWithResult
.setActionDefinitionId(IErlangEditorActionDefinitionIds.SEND_TO_CONSOLE_WITH_RESULT);
setAction("SendToConsoleWithResult", sendToConsoleWithResult);
@@ -2,7 +2,9 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
+import java.util.Set;
+import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension4;
@@ -11,10 +13,17 @@
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.texteditor.ITextEditor;
+import org.erlide.backend.BackendCore;
+import org.erlide.backend.IBackend;
+import org.erlide.backend.IBackendManager;
+import org.erlide.model.root.IErlProject;
+import org.erlide.model.util.PluginUtils;
import org.erlide.runtime.shell.BackendShellListener;
import org.erlide.runtime.shell.IBackendShell;
import org.erlide.ui.actions.SelectionDispatchAction;
-import org.erlide.ui.console.ErlangConsolePage;
+import org.erlide.ui.console.ErlConsoleManager;
+import org.erlide.ui.console.IErlangConsole;
+import org.erlide.ui.console.IErlangConsolePage;
import org.erlide.ui.internal.ErlideUIPlugin;
import org.erlide.util.ErlLogger;
@@ -53,46 +62,61 @@ public void changed(final IBackendShell shell) {
}
}
- private final ITextEditor fEditor;
+ private final ITextEditor editor;
private final boolean getOutput;
private final ConsoleBackendShellListener consoleBackendShellListener;
- private ErlangConsolePage consolePage;
+ IErlProject project;
@Override
public void run(ITextSelection selection) {
- final ErlangConsolePage consolePage = ErlideUIPlugin.getDefault()
- .getConsolePage();
- // make sure we have a console page to send it to
- if (consolePage != null) {
- if (this.consolePage != consolePage && this.consolePage != null) {
- this.consolePage.getShell().removeListener(
- consoleBackendShellListener);
- }
- // if selection is empty, grab the whole line
- selection = getLineSelection(selection, false);
- // try to make the text a full erlang expression, ending with dot
- String text = selection.getText().trim();
- if (text.endsWith(",") || text.endsWith(";")) { //$NON-NLS-1$ //$NON-NLS-2$
- text = text.substring(0, text.length() - 1);
- }
- if (!text.endsWith(".")) { //$NON-NLS-1$
- text += "."; //$NON-NLS-1$
+ final IBackendManager backendManager = BackendCore.getBackendManager();
+ final Set<IBackend> executionBackends = backendManager
+ .getExecutionBackends(project.getWorkspaceProject());
+ IErlangConsole console = null;
+ final ErlConsoleManager erlConsoleManager = ErlideUIPlugin.getDefault()
+ .getErlConsoleManager();
+ for (final IBackend backend : executionBackends) {
+ console = erlConsoleManager.getConsole(backend);
+ if (console != null) {
+ break;
}
- text += "\n"; //$NON-NLS-1$
- // send it off to the console
- if (getOutput) {
- consoleBackendShellListener.setup(getLineSelection(selection,
- true).getOffset());
- consolePage.getShell().addListener(consoleBackendShellListener);
- }
- consolePage.input(text);
}
+ if (console == null) {
+ final String message = "There is no runtime launched for this backend. Please start a runtime to send commands to.";
+ ErrorDialog
+ .openError(getShell(), "No runtime", message, PluginUtils
+ .makeStatus(new Exception("No runtime started")));
+
+ return;
+ }
+ // make sure we have a console page to send it to
+ final IErlangConsolePage consolePage = ErlideUIPlugin.getDefault()
+ .getErlConsoleManager().getPage(console);
+ console.getShell().removeListener(consoleBackendShellListener);
+ // if selection is empty, grab the whole line
+ selection = getLineSelection(selection, false);
+ // try to make the text a full erlang expression, ending with dot
+ String text = selection.getText().trim();
+ if (text.endsWith(",") || text.endsWith(";")) { //$NON-NLS-1$ //$NON-NLS-2$
+ text = text.substring(0, text.length() - 1);
+ }
+ if (!text.endsWith(".")) { //$NON-NLS-1$
+ text += "."; //$NON-NLS-1$
+ }
+ text += "\n"; //$NON-NLS-1$
+ // send it off to the console
+ if (getOutput) {
+ consoleBackendShellListener.setup(getLineSelection(selection, true)
+ .getOffset());
+ console.getShell().addListener(consoleBackendShellListener);
+ }
+ consolePage.input(text);
super.run(selection);
}
public void addMessage(final int offset, final String message) {
- final IDocument document = fEditor.getDocumentProvider().getDocument(
- fEditor.getEditorInput());
+ final IDocument document = editor.getDocumentProvider().getDocument(
+ editor.getEditorInput());
try {
final String delimiter = document.getLineDelimiter(document
.getLineOfOffset(offset - 1));
@@ -126,8 +150,8 @@ public void run() {
protected ITextSelection getLineSelection(ITextSelection selection,
final boolean beginningOfNextLine) {
- final IDocument document = fEditor.getDocumentProvider().getDocument(
- fEditor.getEditorInput());
+ final IDocument document = editor.getDocumentProvider().getDocument(
+ editor.getEditorInput());
if (selection.getLength() == 0) { // don't use isEmpty()!
selection = ErlangTextEditorAction.extendSelectionToWholeLines(
document, selection);
@@ -148,13 +172,15 @@ protected ITextSelection getLineSelection(ITextSelection selection,
public SendToConsoleAction(final IWorkbenchSite site,
final ResourceBundle bundle, final String prefix,
- final ITextEditor editor, final boolean getOutput) {
+ final ITextEditor editor, final boolean getOutput,
+ final IErlProject project) {
super(site);
this.getOutput = getOutput;
+ this.project = project;
setText(getString(bundle, prefix + "label")); //$NON-NLS-1$
setToolTipText(getString(bundle, prefix + "tooltip")); //$NON-NLS-1$
setDescription(getString(bundle, prefix + "description")); //$NON-NLS-1$
- fEditor = editor;
+ this.editor = editor;
consoleBackendShellListener = new ConsoleBackendShellListener();
}
Oops, something went wrong.

0 comments on commit 46aa898

Please sign in to comment.