From 8d4250bac65eea74e4896b0c3c87a1391b42806d Mon Sep 17 00:00:00 2001 From: Uwe Stieber Date: Tue, 11 Aug 2015 11:01:52 +0200 Subject: [PATCH] Bug 460277: Show In > Terminals fails in the "Git Repositories" Perspective --- .../META-INF/MANIFEST.MF | 4 +- .../terminal/view/ui/view/TerminalsView.java | 75 +++++++++++++++- .../view/showin/GitShowInContextHandler.java | 85 +++++++++++++++++++ 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/showin/GitShowInContextHandler.java diff --git a/plugins/org.eclipse.tm.terminal.view.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.tm.terminal.view.ui/META-INF/MANIFEST.MF index a916af47dca..cee61b0212c 100644 --- a/plugins/org.eclipse.tm.terminal.view.ui/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.tm.terminal.view.ui/META-INF/MANIFEST.MF @@ -8,6 +8,7 @@ Bundle-Vendor: %providerName Require-Bundle: org.eclipse.core.expressions;bundle-version="3.4.400", org.eclipse.core.runtime;bundle-version="3.8.0", org.eclipse.core.resources;bundle-version="3.8.1";resolution:=optional, + org.eclipse.egit.ui;bundle-version="4.0.0";resolution:=optional, org.eclipse.tm.terminal.view.core;bundle-version="4.0.0", org.eclipse.tm.terminal.control;bundle-version="4.0.0", org.eclipse.ui;bundle-version="3.8.0" @@ -31,4 +32,5 @@ Export-Package: org.eclipse.tm.terminal.view.ui.actions, org.eclipse.tm.terminal.view.ui.services, org.eclipse.tm.terminal.view.ui.streams, org.eclipse.tm.terminal.view.ui.tabs, - org.eclipse.tm.terminal.view.ui.view + org.eclipse.tm.terminal.view.ui.view, + org.eclipse.tm.terminal.view.ui.view.showin diff --git a/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsView.java b/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsView.java index a41e7a38608..9f12b2696f5 100644 --- a/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsView.java +++ b/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/TerminalsView.java @@ -10,7 +10,10 @@ *******************************************************************************/ package org.eclipse.tm.terminal.view.ui.view; +import java.io.File; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.UUID; import org.eclipse.core.commands.Command; @@ -19,12 +22,14 @@ import org.eclipse.core.expressions.EvaluationContext; import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabFolder; import org.eclipse.swt.custom.CTabItem; @@ -55,6 +60,7 @@ import org.eclipse.tm.terminal.view.ui.nls.Messages; import org.eclipse.tm.terminal.view.ui.tabs.TabFolderManager; import org.eclipse.tm.terminal.view.ui.tabs.TabFolderMenuHandler; import org.eclipse.tm.terminal.view.ui.tabs.TabFolderToolbarHandler; +import org.eclipse.tm.terminal.view.ui.view.showin.GitShowInContextHandler; import org.eclipse.ui.IMemento; import org.eclipse.ui.ISources; import org.eclipse.ui.IViewSite; @@ -656,29 +662,92 @@ public class TerminalsView extends ViewPart implements ITerminalsView, IShowInTa if (context != null) { // Get the selection from the context ISelection selection = context.getSelection(); - // The selection must contain elements that can be adapted to IResource + + // If the selection is not set or empty, look at the input element of + // the show in context. + if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) { + Object input = context.getInput(); + // If coming from the EGit repository viewer, the input element is + // org.eclipse.egit.ui.internal.history.HistoryPageInput + if ("org.eclipse.egit.ui.internal.history.HistoryPageInput".equals(input.getClass().getName())) { //$NON-NLS-1$ + Bundle bundle = Platform.getBundle("org.eclipse.egit.ui"); //$NON-NLS-1$ + if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) { + selection = GitShowInContextHandler.getSelection(input); + } + } + } + + // The selection must contain elements that can be adapted to IResource, File or IPath if (selection instanceof IStructuredSelection && !selection.isEmpty()) { boolean isValid = true; + // Build a new structured selection with the adapted elements + List elements = new ArrayList(); + Iterator iterator = ((IStructuredSelection)selection).iterator(); while (iterator.hasNext() && isValid) { Object element = iterator.next(); Object adapted = null; + if (element instanceof File) { + if (!elements.contains(element)) elements.add(element); + continue; + } + adapted = element instanceof IAdaptable ? ((IAdaptable)element).getAdapter(File.class) : null; + if (adapted == null) adapted = Platform.getAdapterManager().getAdapter(element, File.class); + if (adapted == null) adapted = Platform.getAdapterManager().loadAdapter(element, File.class.getName()); + if (adapted != null) { + if (!elements.contains(adapted)) elements.add(adapted); + continue; + } + + if (element instanceof IPath) { + if (!elements.contains(element)) elements.add(element); + continue; + } + adapted = element instanceof IAdaptable ? ((IAdaptable)element).getAdapter(IPath.class) : null; + if (adapted == null) adapted = Platform.getAdapterManager().getAdapter(element, IPath.class); + if (adapted == null) adapted = Platform.getAdapterManager().loadAdapter(element, IPath.class.getName()); + if (adapted != null) { + if (!elements.contains(adapted)) elements.add(adapted); + continue; + } + Bundle bundle = Platform.getBundle("org.eclipse.core.resources"); //$NON-NLS-1$ if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) { - if (element instanceof org.eclipse.core.resources.IResource) continue; + if (element instanceof org.eclipse.core.resources.IResource) { + if (!elements.contains(element)) elements.add(element); + continue; + } adapted = element instanceof IAdaptable ? ((IAdaptable)element).getAdapter(org.eclipse.core.resources.IResource.class) : null; if (adapted == null) adapted = Platform.getAdapterManager().getAdapter(element, org.eclipse.core.resources.IResource.class); if (adapted == null) adapted = Platform.getAdapterManager().loadAdapter(element, org.eclipse.core.resources.IResource.class.getName()); } + if (adapted != null) { + if (!elements.contains(adapted)) elements.add(adapted); + continue; + } - isValid = adapted != null; + // The EGit repository view can also set a RepositoryTreeNode (and subclasses) + // "org.eclipse.egit.ui.internal.repository.tree...." + if (element.getClass().getName().startsWith("org.eclipse.egit.ui.internal.repository.tree")) { //$NON-NLS-1$ + bundle = Platform.getBundle("org.eclipse.egit.ui"); //$NON-NLS-1$ + if (bundle != null && bundle.getState() != Bundle.UNINSTALLED && bundle.getState() != Bundle.STOPPING) { + adapted = GitShowInContextHandler.getPath(element); + } + } + if (adapted != null) { + if (!elements.contains(adapted)) elements.add(adapted); + continue; + } + + isValid = false; } // If the selection is valid, fire the command to open the local terminal if (isValid) { + selection = new StructuredSelection(elements); ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); Command command = service != null ? service.getCommand("org.eclipse.tm.terminal.connector.local.command.launch") : null; //$NON-NLS-1$ if (command != null && command.isDefined() && command.isEnabled()) { diff --git a/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/showin/GitShowInContextHandler.java b/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/showin/GitShowInContextHandler.java new file mode 100644 index 00000000000..cdd43810948 --- /dev/null +++ b/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/view/showin/GitShowInContextHandler.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2015 Wind River Systems, 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: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.terminal.view.ui.view.showin; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.StructuredSelection; + +/** + * Git show in context handler implementation. + */ +@SuppressWarnings("restriction") +public class GitShowInContextHandler { + + /** + * Converts the data from the input object into a selection. + * + * @param input The input element. Must not be null. + * @return The selection or null. + */ + public static ISelection getSelection(Object input) { + Assert.isNotNull(input); + + List elements = new ArrayList(); + + if (input instanceof org.eclipse.egit.ui.internal.history.HistoryPageInput) { + org.eclipse.egit.ui.internal.history.HistoryPageInput inp = (org.eclipse.egit.ui.internal.history.HistoryPageInput) input; + + if (inp.isSingleFile()) { + elements.add(inp.getSingleFile()); + } else { + File[] fl = inp.getFileList(); + if (fl != null && fl.length > 0) { + for (File f : fl) { + if (f.canRead() && !elements.contains(f)) { + elements.add(f); + } + } + } + + IResource[] rl = inp.getItems(); + if (rl != null && rl.length > 0) { + for (IResource r : rl) { + if (r.isAccessible() && !elements.contains(r)) { + elements.add(r); + } + } + } + } + } + + return elements.isEmpty() ? null : new StructuredSelection(elements); + } + + /** + * Returns the path of the given element. + * + * @param element The element. Must not be null. + * @return The path or null. + */ + public static IPath getPath(Object element) { + Assert.isNotNull(element); + + IPath path = null; + + if (element instanceof org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) { + path = ((org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode)element).getPath(); + } + + return path; + } +}