From 3402e4c69e194b3915a203927f9d9367ef673795 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Thu, 14 May 2015 16:46:06 -0400 Subject: [PATCH] Bug 467350 - Public API to open consoles Change-Id: I3ff760a39cb953c7994c108085020d9b78a41e8d Signed-off-by: Rob Stryker --- .../console/TerminalConsoleUtility.java | 49 ++++++++ .../console/TerminalConsoleFactory.java | 107 ++++++++++++------ .../internal/console/TerminalConsolePage.java | 2 +- 3 files changed, 121 insertions(+), 37 deletions(-) create mode 100644 bundles/org.eclipse.remote.console/src/org/eclipse/remote/console/TerminalConsoleUtility.java diff --git a/bundles/org.eclipse.remote.console/src/org/eclipse/remote/console/TerminalConsoleUtility.java b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/console/TerminalConsoleUtility.java new file mode 100644 index 00000000000..740c188b518 --- /dev/null +++ b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/console/TerminalConsoleUtility.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat Inc. - Initial API and implementation + *******************************************************************************/ +package org.eclipse.remote.console; + +import java.util.List; + +import org.eclipse.remote.core.IRemoteConnection; +import org.eclipse.remote.internal.console.TerminalConsoleFactory; +import org.eclipse.ui.console.IConsole; + +/** + * A collection of public API utility methods to open + * consoles to IRemoteConnection objects + */ +public class TerminalConsoleUtility { + /** + * Opens a dialog to allow selection of an IRemoteConnection, + * encoding, etc. and then open a console to it. + */ + public void openConsole() { + new TerminalConsoleFactory().openConsole(); + } + + /** + * Open a specific IRemoteConnection and encoding combination. + * @param connection + * @param encoding + */ + public static void openConsole(final IRemoteConnection connection, final String encoding) { + new TerminalConsoleFactory().openConsole(connection, encoding); + } + + /** + * Find an existing console for the given IRemoteConnection + * @param connection + * @return + */ + public static List findConsole(IRemoteConnection connection) { + return new TerminalConsoleFactory().findConsole(connection); + } +} diff --git a/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsoleFactory.java b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsoleFactory.java index 411f1563dea..7bb235e9773 100644 --- a/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsoleFactory.java +++ b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsoleFactory.java @@ -10,6 +10,9 @@ *******************************************************************************/ package org.eclipse.remote.internal.console; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -36,48 +39,80 @@ public class TerminalConsoleFactory implements IConsoleFactory { } public static void openConsole(final IRemoteConnection connection, final String encoding) { - new Job(ConsoleMessages.OPENNING_TERMINAL) { + Job j = new Job(ConsoleMessages.OPENNING_TERMINAL) { @Override public IStatus run(IProgressMonitor monitor) { - IRemoteCommandShellService commandShellService = connection.getService(IRemoteCommandShellService.class); - if (commandShellService == null) { - return Status.CANCEL_STATUS; - } + return openConsoleImplementation(connection, encoding, monitor); + } + }; + j.schedule(); + } + + private static IStatus openConsoleImplementation( final IRemoteConnection connection, + final String encoding, IProgressMonitor monitor) { + IRemoteCommandShellService commandShellService = connection.getService(IRemoteCommandShellService.class); + if (commandShellService == null) { + return Status.CANCEL_STATUS; + } + try { + IConsole ret = createConsole(connection, encoding, commandShellService, monitor); + return Status.OK_STATUS; + } catch(RemoteConnectionException rce) { + return rce.getStatus(); + } - try { - if (!connection.isOpen()) { - connection.open(monitor); - } + } + + private static IConsole createConsole( final IRemoteConnection connection, + final String encoding, IRemoteCommandShellService service, + IProgressMonitor monitor) throws RemoteConnectionException { + if (!connection.isOpen()) { + connection.open(monitor); + } - // TODO, how to handle command shells that are singletons, like serial ports + // TODO, how to handle command shells that are singletons, like serial ports - // Find the index; - IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager(); - IConsole[] consoles = consoleManager.getConsoles(); - boolean[] indices = new boolean[consoles.length]; - for (IConsole console : consoles) { - if (console instanceof TerminalConsole) { - TerminalConsole terminalConsole = (TerminalConsole) console; - if (terminalConsole.getConnection().equals(connection)) { - indices[terminalConsole.getIndex()] = true; - } - } - } - int index = 0; - while (index < indices.length && indices[index]) { - index++; - } + IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager(); + // Find the next index; + int index = findNextIndex(consoleManager, connection); - TerminalConsole terminalConsole = new TerminalConsole(connection, index, encoding); - consoleManager.addConsoles(new IConsole[] { terminalConsole }); - consoleManager.showConsoleView(terminalConsole); - - return Status.OK_STATUS; - } catch (RemoteConnectionException e) { - return e.getStatus(); + TerminalConsole terminalConsole = new TerminalConsole(connection, index, encoding); + consoleManager.addConsoles(new IConsole[] { terminalConsole }); + consoleManager.showConsoleView(terminalConsole); + return terminalConsole; + } + + private static int findNextIndex(IConsoleManager consoleManager, IRemoteConnection connection) { + IConsole[] consoles = consoleManager.getConsoles(); + boolean[] indices = new boolean[consoles.length]; + for (IConsole console : consoles) { + if (console instanceof TerminalConsole) { + TerminalConsole terminalConsole = (TerminalConsole) console; + if (terminalConsole.getConnection().equals(connection)) { + indices[terminalConsole.getIndex()] = true; } } - }.schedule(); + } + int index = 0; + while (index < indices.length && indices[index]) { + index++; + } + return index; } - -} + + public static List findConsole(IRemoteConnection connection) { + ArrayList ret = new ArrayList(); + IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager(); + IConsole[] consoles = consoleManager.getConsoles(); + for (IConsole console : consoles) { + if (console instanceof TerminalConsole) { + TerminalConsole terminalConsole = (TerminalConsole) console; + if (terminalConsole.getConnection().equals(connection)) { + ret.add(terminalConsole); + } + } + } + return ret; + } + +} \ No newline at end of file diff --git a/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsolePage.java b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsolePage.java index e5a0d1d0720..c426ecf77c5 100644 --- a/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsolePage.java +++ b/bundles/org.eclipse.remote.console/src/org/eclipse/remote/internal/console/TerminalConsolePage.java @@ -148,7 +148,7 @@ public class TerminalConsolePage extends Page { public void disconnectTerminal() { if (tViewCtrl.getState() != TerminalState.CLOSED) { - tViewCtrl.getTerminalConnector().disconnect(); + tViewCtrl.disconnectTerminal(); } }