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
AxelRICHARD authored and hudson committed Feb 12, 2016
1 parent 01ffb3d commit b064edf
Show file tree
Hide file tree
Showing 2 changed files with 93 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 @@ -1335,17 +1337,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 @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -26,6 +27,7 @@
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 @@ -162,12 +164,26 @@ 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
*/
private static Set<IResource> getSelectedResourcesSet(
IStructuredSelection selection) {
Set<IResource> result = new LinkedHashSet<IResource>();
for (Object o : selection.toList()) {
ResourceMapping mapping = AdapterUtils.adapt(o,
Expand All @@ -181,7 +197,7 @@ public static IResource[] getSelectedResources(
}

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

private static List<IResource> extractResourcesFromMapping(
Expand All @@ -208,6 +224,50 @@ 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.
*/
public static HistoryPageInput getMostFittingInput(
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);
}

/**
* Figure out which repository to use. All selected resources must map to
* the same Git repository.
Expand Down Expand Up @@ -327,4 +387,16 @@ private static IEvaluationContext getEvaluationContext() {
return ctx;
}

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 b064edf

Please sign in to comment.