diff options
author | Xavier Coulon | 2016-11-29 14:27:23 +0000 |
---|---|---|
committer | Jeff Johnston | 2016-11-29 20:33:52 +0000 |
commit | f5cd20c288a446f200fcb37e7dfc21826f9d7a06 (patch) | |
tree | a0c3a281cdf24623f5ec3b737d100387ecc421fd | |
parent | caef6906b5a4ac6bbfcbf541e7ac4fdbc332a2a6 (diff) | |
download | org.eclipse.linuxtools-f5cd20c288a446f200fcb37e7dfc21826f9d7a06.tar.gz org.eclipse.linuxtools-f5cd20c288a446f200fcb37e7dfc21826f9d7a06.tar.xz org.eclipse.linuxtools-f5cd20c288a446f200fcb37e7dfc21826f9d7a06.zip |
Bug 508027 - ClassCastException in BaseResourceAwareLaunchShortcut.launch
Retrieving the IResources using a class cast or using the
IAdaptable#gtAdapter(Class) method, and dealing with the case
where the result would be null by displaying an error message
to the user.
Change-Id: I64f26599356c02098cb659d281738c479c44fa7b
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
Reviewed-on: https://git.eclipse.org/r/85942
Tested-by: Hudson CI
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
2 files changed, 38 insertions, 4 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BaseResourceAwareLaunchShortcut.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BaseResourceAwareLaunchShortcut.java index 04a8790881..8fa2c27ea0 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BaseResourceAwareLaunchShortcut.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BaseResourceAwareLaunchShortcut.java @@ -8,6 +8,7 @@ import java.util.stream.Stream; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; @@ -17,10 +18,12 @@ import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.IDebugModelPresentation; import org.eclipse.debug.ui.ILaunchShortcut; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.linuxtools.docker.core.IDockerConnection; import org.eclipse.linuxtools.docker.ui.Activator; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.dialogs.ElementListSelectionDialog; @@ -34,14 +37,42 @@ import org.eclipse.ui.dialogs.ElementListSelectionDialog; public abstract class BaseResourceAwareLaunchShortcut implements ILaunchShortcut { @Override - public void launch(ISelection selection, String mode) { + public void launch(final ISelection selection, final String mode) { if (selection instanceof IStructuredSelection) { - IResource resource = (IResource) ((IStructuredSelection) selection) - .toArray()[0]; - launch(resource, mode); + final IResource resource = getResourceFromSelection(selection); + if (resource != null) { + launch(resource, mode); + } else { + MessageDialog.openError(Display.getDefault().getActiveShell(), + LaunchMessages.getString("LaunchShortcut.error.msg"), //$NON-NLS-1$ + LaunchMessages.getString("LaunchShortcut.error.msg")); //$NON-NLS-1$ + } } } + /** + * Retrieves the {@link IResource} associated with the first element in the + * given {@code selection}. + * + * @param selection + * the {@link ISelection} to process + * @return the corresponding {@link IResource} or <code>null</code> if none + * was found + */ + private static IResource getResourceFromSelection( + final ISelection selection) { + final Object selectedElement = ((IStructuredSelection) selection) + .toArray()[0]; + if (selectedElement instanceof IResource) { + return (IResource) selectedElement; + } else if (selectedElement instanceof IAdaptable) { + // may return null, which will be dealt with in the caller method + return ((IAdaptable) selection).getAdapter(IResource.class); + } + // if the selected element is neither a resource nor an 'adaptable' + return null; + } + @Override public void launch(IEditorPart editor, String mode) { launch(editor.getEditorInput().getAdapter(IResource.class), mode); diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/LaunchMessages.properties b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/LaunchMessages.properties index 4656df033a..27de568c90 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/LaunchMessages.properties +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/LaunchMessages.properties @@ -77,3 +77,6 @@ DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.browseworkspace.dial DockerComposeUpLaunchConfigurationMainTab.dockerComposePath.browsefilesystem.button.label=Browse File System... DockerComposeUpShortcutConfigSelection.title=Docker Compose Configuration DockerComposeUpShortcutChooseLaunch.msg=Select the launch configuration to run 'docker-compose' + +LaunchShortcut.error=Error +LaunchShortcut.error.msg=The selection is not a valid resource |