Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands')
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.java38
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.properties7
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandUtils.java106
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInSystemExplorerCommandHandler.java149
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInWebBrowserCommandHandler.java105
5 files changed, 377 insertions, 28 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.java
new file mode 100644
index 0000000000..9a30b1d174
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Red Hat.
+ * 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 - Initial Contribution
+ *******************************************************************************/
+package org.eclipse.linuxtools.internal.docker.ui.commands;
+
+import java.text.MessageFormat;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+public class CommandMessages {
+
+ private static final String BUNDLE_NAME = CommandMessages.class.getName();
+
+ public static String getString(String key) {
+ try {
+ return ResourceBundle.getBundle(BUNDLE_NAME).getString(key);
+ } catch (MissingResourceException e) {
+ return '!' + key + '!';
+ } catch (NullPointerException e) {
+ return '#' + key + '#';
+ }
+ }
+
+ public static String getFormattedString(String key, String arg) {
+ return MessageFormat.format(getString(key), new Object[] { arg });
+ }
+
+ public static String getFormattedString(String key, String... args) {
+ return MessageFormat.format(getString(key), (Object[]) args);
+ }
+}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.properties b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.properties
new file mode 100644
index 0000000000..b942924940
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandMessages.properties
@@ -0,0 +1,7 @@
+command.showIn.webBrowser=Opening in Web Browser...
+command.showIn.webBrowser.failure=Failed to open in Web Browser
+
+command.showIn.systemExplorer=Opening in System Explorer...
+command.showIn.systemExplorer.failure=Failed to open in Web Browser
+command.showIn.systemExplorer.failure.command_unavailable=Failed to open in Web Browser: command unavailable
+command.showIn.systemExplorer.failure.execute=Failed to execute {0}. Return code was: {1} \ No newline at end of file
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandUtils.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandUtils.java
index 7077d8e729..c6549ab204 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandUtils.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/CommandUtils.java
@@ -27,10 +27,16 @@ import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerContainer;
import org.eclipse.linuxtools.docker.core.IDockerImage;
+import org.eclipse.linuxtools.docker.core.IDockerPortMapping;
import org.eclipse.linuxtools.docker.ui.Activator;
import org.eclipse.linuxtools.internal.docker.ui.RunConsole;
import org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerContainersView;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerLink;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerLinksCategory;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerPortMappingsCategory;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerVolume;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerVolumesCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainersCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerImagesCategory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerView;
@@ -87,6 +93,29 @@ public class CommandUtils {
} else if (firstElement instanceof DockerContainersCategory) {
return ((DockerContainersCategory) firstElement)
.getConnection();
+ } else if (firstElement instanceof DockerContainersCategory) {
+ return ((DockerContainersCategory) firstElement)
+ .getConnection();
+ } else if (firstElement instanceof DockerContainerLinksCategory) {
+ return ((DockerContainerLinksCategory) firstElement)
+ .getContainer().getConnection();
+ } else if (firstElement instanceof DockerContainerLink) {
+ return ((DockerContainerLink) firstElement).getContainer()
+ .getConnection();
+ } else
+ if (firstElement instanceof DockerContainerPortMappingsCategory) {
+ return ((DockerContainerPortMappingsCategory) firstElement)
+ .getContainer().getConnection();
+ } else if (firstElement instanceof IDockerPortMapping) {
+ return ((IDockerPortMapping) firstElement)
+ .getContainer()
+ .getConnection();
+ } else if (firstElement instanceof DockerContainerVolumesCategory) {
+ return ((DockerContainerVolumesCategory) firstElement)
+ .getContainer().getConnection();
+ } else if (firstElement instanceof DockerContainerVolume) {
+ return ((DockerContainerVolume) firstElement).getContainer()
+ .getConnection();
} else if (firstElement instanceof DockerImagesCategory) {
return ((DockerImagesCategory) firstElement).getConnection();
}
@@ -104,37 +133,57 @@ public class CommandUtils {
public static List<IDockerContainer> getSelectedContainers(final IWorkbenchPart activePart) {
if (activePart instanceof DockerContainersView) {
final ISelection selection = ((DockerContainersView) activePart).getSelection();
- return getSelectedContainers(selection);
+ return convertSelectionTo(selection, IDockerContainer.class);
} else if (activePart instanceof DockerExplorerView) {
final ISelection selection = ((DockerExplorerView) activePart)
.getCommonViewer().getSelection();
- return getSelectedContainers(selection);
+ return convertSelectionTo(selection, IDockerContainer.class);
}
return Collections.emptyList();
}
/**
- *
- * @param selection
- * the current selection
- * @return the {@link List} of {@link IDockerContainer} associated with the
- * given {@link ISelection}, or {@link Collections#emptyList()} if
- * none was selected.
+ * @param activePart
+ * the active {@link IWorkbenchPart}
+ * @return the {@link List} of selected {@link DockerContainerPortMapping}
+ * in the given active part of {@link Collections#emptyList()} if
+ * none was selected
*/
- public static List<IDockerContainer> getSelectedContainers(final ISelection selection) {
- if (selection instanceof IStructuredSelection) {
- final List<IDockerContainer> selectedContainers = new ArrayList<>();
- final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
- for (Iterator<?> iterator = structuredSelection.iterator(); iterator.hasNext();) {
- final Object selectedElement = iterator.next();
- if (selectedElement instanceof IDockerContainer) {
- selectedContainers.add((IDockerContainer) selectedElement);
- }
- }
- return Collections.unmodifiableList(selectedContainers);
+ public static List<IDockerPortMapping> getSelectedPortMappings(
+ final IWorkbenchPart activePart) {
+ if (activePart instanceof DockerContainersView) {
+ final ISelection selection = ((DockerContainersView) activePart)
+ .getSelection();
+ return convertSelectionTo(selection, IDockerPortMapping.class);
+ } else if (activePart instanceof DockerExplorerView) {
+ final ISelection selection = ((DockerExplorerView) activePart)
+ .getCommonViewer().getSelection();
+ return convertSelectionTo(selection, IDockerPortMapping.class);
}
return Collections.emptyList();
}
+
+ /**
+ * @param activePart
+ * the active {@link IWorkbenchPart}
+ * @return the {@link List} of selected {@link DockerContainerVolume} in the
+ * given active part of {@link Collections#emptyList()} if none was
+ * selected
+ */
+ public static List<DockerContainerVolume> getSelectedVolumes(
+ final IWorkbenchPart activePart) {
+ if (activePart instanceof DockerContainersView) {
+ final ISelection selection = ((DockerContainersView) activePart)
+ .getSelection();
+ return convertSelectionTo(selection, DockerContainerVolume.class);
+ } else if (activePart instanceof DockerExplorerView) {
+ final ISelection selection = ((DockerExplorerView) activePart)
+ .getCommonViewer().getSelection();
+ return convertSelectionTo(selection, DockerContainerVolume.class);
+ }
+ return Collections.emptyList();
+ }
+
/**
* @param activePart
* the active {@link IWorkbenchPart}
@@ -147,11 +196,11 @@ public class CommandUtils {
if (activePart instanceof DockerImagesView) {
final ISelection selection = ((DockerImagesView) activePart)
.getSelection();
- return getSelectedImages(selection);
+ return convertSelectionTo(selection, IDockerImage.class);
} else if (activePart instanceof DockerExplorerView) {
final ISelection selection = ((DockerExplorerView) activePart)
.getCommonViewer().getSelection();
- return getSelectedImages(selection);
+ return convertSelectionTo(selection, IDockerImage.class);
}
return Collections.emptyList();
}
@@ -160,23 +209,24 @@ public class CommandUtils {
*
* @param selection
* the current selection
- * @return the {@link List} of {@link IDockerImage} associated with the
+ * @return the {@link List} of {@link IDockerContainer} associated with the
* given {@link ISelection}, or {@link Collections#emptyList()} if
* none was selected.
*/
- public static List<IDockerImage> getSelectedImages(
- final ISelection selection) {
+ @SuppressWarnings("unchecked")
+ private static <T> List<T> convertSelectionTo(final ISelection selection,
+ final Class<T> targetClass) {
if (selection instanceof IStructuredSelection) {
- final List<IDockerImage> selectedImages = new ArrayList<>();
+ final List<T> selectedContainers = new ArrayList<>();
final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
for (Iterator<?> iterator = structuredSelection.iterator(); iterator
.hasNext();) {
final Object selectedElement = iterator.next();
- if (selectedElement instanceof IDockerImage) {
- selectedImages.add((IDockerImage) selectedElement);
+ if (targetClass.isAssignableFrom(selectedElement.getClass())) {
+ selectedContainers.add((T) selectedElement);
}
}
- return Collections.unmodifiableList(selectedImages);
+ return Collections.unmodifiableList(selectedContainers);
}
return Collections.emptyList();
}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInSystemExplorerCommandHandler.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInSystemExplorerCommandHandler.java
new file mode 100644
index 0000000000..a8a7e89a28
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInSystemExplorerCommandHandler.java
@@ -0,0 +1,149 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Red Hat.
+ * 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 - Initial Contribution
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.internal.docker.ui.commands;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.util.Util;
+import org.eclipse.linuxtools.docker.ui.Activator;
+import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainerVolume;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.internal.ide.handlers.ShowInSystemExplorerHandler;
+
+/**
+ * Command handler to open the selection in the Web Browser.
+ *
+ * @see ShowInSystemExplorerHandler
+ */
+@SuppressWarnings("restriction")
+public class ShowInSystemExplorerCommandHandler extends AbstractHandler {
+
+ private static final String VARIABLE_RESOURCE = "${selected_resource_loc}"; //$NON-NLS-1$
+ private static final String VARIABLE_RESOURCE_URI = "${selected_resource_uri}"; //$NON-NLS-1$
+ private static final String VARIABLE_FOLDER = "${selected_resource_parent_loc}"; //$NON-NLS-1$
+
+ @Override
+ public Object execute(final ExecutionEvent event) {
+ final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
+ final List<DockerContainerVolume> volumes = CommandUtils
+ .getSelectedVolumes(activePart);
+ if (volumes == null || volumes.isEmpty()) {
+ return null;
+ }
+ final DockerContainerVolume selectedVolume = volumes.get(0);
+ final File hostFile = new File(selectedVolume.getHostPath());
+ final String launchCmd = getShowInSystemExplorerCommand(hostFile);
+
+ if (launchCmd == null) {
+ return null;
+ }
+ final Job job = new Job(
+ CommandMessages.getString("command.showIn.systemExplorer")) {
+ // $NON-NLS-1$
+ @Override
+ protected IStatus run(final IProgressMonitor monitor) {
+
+ try {
+ final Process p = getLaunchProcess(launchCmd, hostFile);
+ final int retCode = p.waitFor();
+ if (retCode != 0 && !Util.isWindows()) {
+ Activator.logErrorMessage(
+ CommandMessages.getFormattedString(
+ "command.showIn.systemExplorer.failure.command.execute", //$NON-NLS-1$
+ launchCmd, Integer.toString(retCode)));
+ }
+ } catch (IOException | InterruptedException e) {
+ Activator
+ .logErrorMessage(CommandMessages.getFormattedString(
+ "command.showIn.systemExplorer.failure", //$NON-NLS-1$
+ launchCmd), e);
+ } finally {
+ monitor.done();
+ }
+ return Status.OK_STATUS;
+ }
+ };
+ job.setUser(true);
+ job.schedule();
+ return null;
+ }
+
+ /**
+ * @param launchCmd
+ * the launch command
+ * @return the platform-dependent {@link Process} to run to open the file in
+ * the System Explorer
+ * @throws IOException
+ */
+ private Process getLaunchProcess(final String launchCmd, final File dir)
+ throws IOException {
+ if (Util.isLinux() || Util.isMac()) {
+ return Runtime.getRuntime().exec(
+ new String[] { "/bin/sh", "-c", launchCmd }, null, dir); //$NON-NLS-1$ //$NON-NLS-2$
+ } else {
+ return Runtime.getRuntime().exec(launchCmd, null, dir);
+ }
+ }
+
+ /**
+ * Prepare command for launching system explorer to show a path
+ *
+ * @param path
+ * the path to show
+ * @return the command that shows the path
+ * @see ShowInSystemExplorerHandler#getDefaultCommand()
+ */
+ private String getShowInSystemExplorerCommand(final File path) {
+ String command = ShowInSystemExplorerHandler.getDefaultCommand();
+ if ("".equals(command)) {
+ Activator.logErrorMessage(CommandMessages.getString(
+ "command.showIn.systemExplorer.failure.command_unavailable"));
+ return null;
+ }
+ try {
+ command = Util.replaceAll(command, VARIABLE_RESOURCE,
+ quotePath(path.getCanonicalPath()));
+ command = Util.replaceAll(command, VARIABLE_RESOURCE_URI,
+ path.getCanonicalFile().toURI().toString());
+ File parent = path.getParentFile();
+ if (parent != null) {
+ command = Util.replaceAll(command, VARIABLE_FOLDER,
+ quotePath(parent.getCanonicalPath()));
+ }
+ return command;
+ } catch (IOException e) {
+ Activator.logErrorMessage(CommandMessages
+ .getString("command.showIn.systemExplorer.failure"), e);
+ return null;
+ }
+ }
+
+ private String quotePath(String path) {
+ if (Util.isLinux() || Util.isMac()) {
+ // Quote for usage inside "", man sh, topic QUOTING:
+ path = path.replaceAll("[\"$`]", "\\\\$0"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ // Windows: Can't quote, since explorer.exe has a very special command
+ // line parsing strategy.
+ return path;
+ }
+
+}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInWebBrowserCommandHandler.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInWebBrowserCommandHandler.java
new file mode 100644
index 0000000000..21a47df797
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/commands/ShowInWebBrowserCommandHandler.java
@@ -0,0 +1,105 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Red Hat.
+ * 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 - Initial Contribution
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.internal.docker.ui.commands;
+
+import static org.eclipse.linuxtools.internal.docker.ui.commands.CommandUtils.getCurrentConnection;
+import static org.eclipse.linuxtools.internal.docker.ui.commands.CommandUtils.getSelectedPortMappings;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.linuxtools.docker.core.Activator;
+import org.eclipse.linuxtools.docker.core.IDockerConnection;
+import org.eclipse.linuxtools.docker.core.IDockerPortMapping;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/**
+ * Command handler to open the selection in the Web Browser.
+ */
+public class ShowInWebBrowserCommandHandler extends AbstractHandler {
+
+ @Override
+ public Object execute(final ExecutionEvent event) {
+ final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
+ final List<IDockerPortMapping> portMappings = getSelectedPortMappings(
+ activePart);
+ if (portMappings == null || portMappings.isEmpty()) {
+ return null;
+ }
+ final Job job = new Job(
+ CommandMessages.getString("command.showIn.webBrowser")) { //$NON-NLS-1$
+ @Override
+ protected IStatus run(final IProgressMonitor monitor) {
+ try {
+ final IDockerConnection currentConnection = getCurrentConnection(
+ activePart);
+ final IDockerPortMapping selectedPort = portMappings
+ .get(0);
+ final URI connectionURI = new URI(
+ currentConnection.getUri());
+ if ("tcp".equalsIgnoreCase(connectionURI.getScheme()) //$NON-NLS-1$
+ || "unix" //$NON-NLS-1$
+ .equalsIgnoreCase(connectionURI.getScheme())
+ || "http" //$NON-NLS-1$
+ .equalsIgnoreCase(connectionURI.getScheme())
+ || "https".equalsIgnoreCase( //$NON-NLS-1$
+ connectionURI.getScheme())) {
+ final String host = connectionURI.getHost();
+ final URL location = new URL("http", host, //$NON-NLS-1$
+ selectedPort.getPublicPort(), "/");
+ openLocationInWebBrowser(location);
+ }
+ } catch (URISyntaxException | MalformedURLException e) {
+ Activator.logErrorMessage(
+ CommandMessages.getString(
+ "command.showIn.webBrowser.failure"), //$NON-NLS-1$
+ e);
+ }
+ monitor.done();
+ return Status.OK_STATUS;
+ }
+ };
+ job.setUser(true);
+ job.schedule();
+ return null;
+ }
+
+ private void openLocationInWebBrowser(final URL location) {
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ PlatformUI.getWorkbench().getBrowserSupport()
+ .getExternalBrowser().openURL(location);
+ } catch (Exception e) {
+ Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+ CommandMessages.getString(
+ "command.showIn.webBrowser.failure"), //$NON-NLS-1$
+ e));
+ }
+ }
+ });
+ }
+
+}

Back to the top