Skip to content

Commit

Permalink
Workaround to enable linking multiple resources to HistoryView
Browse files Browse the repository at this point in the history
When given a selection the GenericHistoryView only considers the first
element and adapts it to an IResource before passing it on as input. To
work around this behavior the GitHistoryPage now looks at the current
selection itself and uses it as input if preferable.

Bug: 392948
Signed-off-by: Stefan Dirix <sdirix@eclipsesource.com>
Signed-off-by: Axel Richard <axel.richard@obeo.fr>
Change-Id: Idd3f2434881085d90a547f825fb20ba8895b7e79
  • Loading branch information
sdirix authored and hudson committed Mar 29, 2016
1 parent 15c37a1 commit 68d2fd9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Copyright (C) 2012, François Rey <eclipse.org_@_francois_._rey_._name>
* Copyright (C) 2015, IBM Corporation (Dani Megert <daniel_megert@ch.ibm.com>)
* Copyright (C) 2015, Thomas Wolf <thomas.wolf@paranor.ch>
* Copyright (C) 2015, Stefan Dirix (Stefan Dirix <sdirix@eclipsesource.com>)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.eclipse.egit.ui.internal.repository.tree.RefNode;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
import org.eclipse.egit.ui.internal.repository.tree.TagNode;
import org.eclipse.egit.ui.internal.selection.SelectionUtils;
import org.eclipse.egit.ui.internal.trace.GitTraceLocation;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jface.action.Action;
Expand Down Expand Up @@ -1341,17 +1343,32 @@ public void run() {
@Override
public boolean setInput(Object object) {
try {
Object useAsInput = object;

// Workaround for the limitation of GenericHistoryView to only
// forward the first part of a selection and adapting it
// immediately to IResource.
ISelection selection = getSite().getPage().getSelection();
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
HistoryPageInput mostFittingInput = SelectionUtils
.getMostFittingInput(structuredSelection, object);
if (mostFittingInput != null) {
useAsInput = mostFittingInput;
}
}

// hide the warning text initially
setWarningText(null);
trace = GitTraceLocation.HISTORYVIEW.isActive();
if (trace)
GitTraceLocation.getTrace().traceEntry(
GitTraceLocation.HISTORYVIEW.getLocation(), object);
GitTraceLocation.HISTORYVIEW.getLocation(), useAsInput);

if (object == getInput())
if (useAsInput == getInput())
return true;
this.input = null;
return super.setInput(object);
return super.setInput(useAsInput);
} finally {
if (trace)
GitTraceLocation.getTrace().traceExit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -27,9 +28,11 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.egit.core.AdapterUtils;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.CommonUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.history.HistoryPageInput;
import org.eclipse.egit.ui.internal.revision.FileRevisionEditorInput;
import org.eclipse.egit.ui.internal.trace.GitTraceLocation;
import org.eclipse.jgit.annotations.NonNull;
Expand Down Expand Up @@ -165,12 +168,27 @@ public static IPath[] getSelectedLocations(
}

/**
* Returns the resources contained in the given selection.
*
* @param selection
* @return the resources in the selection
*/
@NonNull
public static IResource[] getSelectedResources(
@NonNull IStructuredSelection selection) {
Set<IResource> result = getSelectedResourcesSet(selection);
return result.toArray(new IResource[result.size()]);
}

/**
* Returns the resources contained in the given selection.
*
* @param selection
* @return the resources in the selection
*/
@NonNull
private static Set<IResource> getSelectedResourcesSet(
@NonNull IStructuredSelection selection) {
Set<IResource> result = new LinkedHashSet<IResource>();
for (Object o : selection.toList()) {
ResourceMapping mapping = AdapterUtils.adapt(o,
Expand All @@ -184,7 +202,7 @@ public static IResource[] getSelectedResources(
}

}
return result.toArray(new IResource[result.size()]);
return result;
}

private static List<IResource> extractResourcesFromMapping(
Expand All @@ -211,6 +229,51 @@ private static List<IResource> extractResourcesFromMapping(
return result;
}

/**
* Determines the most fitting {@link HistoryPageInput} for the given
* {@link IStructuredSelection}. The {@code mandatoryObject} must be
* contained in the selection and in a repository.
* <p>
* Most fitting means that the input will contain all selected resources
* which are contained in the same repository as the given
* {@code mandatoryObject}.
* </p>
*
* @param selection
* The selection for which the most fitting HistoryPageInput is
* to be determined.
* @param mandatoryObject
* The object to which the HistoryPageInput is tailored. Must be
* contained in the given selection and in a repository.
* @return The most fitting HistoryPageInput. Will return {@code null} when
* the {@code mandatoryObject} is not contained in the given
* selection or in a repository.
*/
@Nullable
public static HistoryPageInput getMostFittingInput(
@NonNull IStructuredSelection selection, Object mandatoryObject) {
Set<IResource> resources = getSelectedResourcesSet(selection);
if (!resources.contains(mandatoryObject)) {
return null;
}

Repository repository = getRepository((IResource) mandatoryObject);
if (repository == null) {
return null;
}

for (Iterator<IResource> it = resources.iterator(); it.hasNext();) {
IResource resource = it.next();
if (getRepository(resource) != repository) {
it.remove();
}
}

IResource[] resourceArray = resources.toArray(new IResource[resources
.size()]);
return new HistoryPageInput(repository, resourceArray);
}

/**
* Determines a set of either {@link IResource}s or {@link IPath}s from a
* selection. For selection contents that adapt to {@link IResource} or
Expand Down Expand Up @@ -363,4 +426,17 @@ private static IEvaluationContext getEvaluationContext() {
return ctx;
}

@Nullable
private static Repository getRepository(IResource resource) {
IPath location = resource.getLocation();
if (location != null) {
RepositoryMapping repositoryMapping = RepositoryMapping
.getMapping(location);
if (repositoryMapping != null) {
return repositoryMapping.getRepository();
}
}
return null;
}

}

0 comments on commit 68d2fd9

Please sign in to comment.