Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-10-14 06:56:31 -0400
committerThomas Wolf2019-10-17 05:00:02 -0400
commit7704906cfc0e237ebb4019d3da911486fa0ba9d8 (patch)
tree92edd271400c230adb55a8e8d32a673e0c13350f /org.eclipse.egit.ui/src/org
parent7110c6405d11cc3409017f881c8431991a28dcaf (diff)
downloadegit-master.tar.gz
egit-master.tar.xz
egit-master.zip
[repo view] Let "link with selection" be always enabledHEADmaster
It's actually a normal state toggling command for which a default handler is fine. It didn't have a default handler because it was badly implemented and assumed that the currently active part was the repositories view. Change the handling of all three toggle commands in the repositories view. Let them only toggle their state, don't make them refresh the view. Make the view listen to state changes and refresh if needed. The content provider doesn't need to listen to changes; it has no way to inform the viewer to refresh. Also remove duplicating the state into local boolean variables; use the state's current value instead. Bug: 537958 Change-Id: I2027a64bd6f4e88439e7975b9d3c3eb3d381efef Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.egit.ui/src/org')
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java114
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewContentProvider.java42
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryTreeNodeDecorator.java3
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AbstractToggleCommand.java18
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/LinkWithSelectionCommand.java36
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchCommitCommand.java7
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchHierarchyCommand.java7
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleLinkWithSelectionCommand.java26
8 files changed, 112 insertions, 141 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java
index 025d72b64..d4619b4f5 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesView.java
@@ -30,7 +30,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
-import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.IStateListener;
+import org.eclipse.core.commands.State;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Adapters;
@@ -69,6 +70,8 @@ import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNodeType;
import org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode;
import org.eclipse.egit.ui.internal.repository.tree.TagNode;
import org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode;
+import org.eclipse.egit.ui.internal.repository.tree.command.ToggleBranchHierarchyCommand;
+import org.eclipse.egit.ui.internal.repository.tree.command.ToggleLinkWithSelectionCommand;
import org.eclipse.egit.ui.internal.selection.SelectionUtils;
import org.eclipse.egit.ui.internal.staging.StagingView;
import org.eclipse.egit.ui.internal.trace.GitTraceLocation;
@@ -195,11 +198,63 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
private volatile long lastInputUpdate = -1L;
- private boolean reactOnSelection;
+ private State reactOnSelection;
- private final IPreferenceChangeListener configurationListener;
+ private final ISelectionListener selectionChangedListener = (part,
+ selection) -> {
+ if (!((Boolean) reactOnSelection.getValue()).booleanValue()
+ || part == RepositoriesView.this) {
+ return;
+ }
+
+ // this may happen if we switch between editors
+ if (part instanceof IEditorPart) {
+ IEditorInput input = ((IEditorPart) part).getEditorInput();
+ if (input instanceof IFileEditorInput) {
+ reactOnSelection(new StructuredSelection(
+ ((IFileEditorInput) input).getFile()));
+ } else if (input instanceof IURIEditorInput) {
+ reactOnSelection(new StructuredSelection(input));
+ }
+
+ } else {
+ reactOnSelection(selection);
+ }
+ };
+
+ private final IStateListener reactOnSelectionListener = (state,
+ oldValue) -> {
+ if (((Boolean) state.getValue()).booleanValue()) {
+ PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
+ ISelectionService service = CommonUtils
+ .getService(getViewSite(), ISelectionService.class);
+ if (service == null) {
+ return;
+ }
+ ISelection currentSelection = service.getSelection();
+ if (currentSelection == null || currentSelection.isEmpty()) {
+ return;
+ }
+ IWorkbenchWindow window = PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow();
+ if (window == null) {
+ return;
+ }
+ IWorkbenchPart part = window.getPartService().getActivePart();
+ if (part != null) {
+ selectionChangedListener.selectionChanged(part,
+ currentSelection);
+ }
+ });
+ }
+ };
+
+ private State branchHierarchy;
- private ISelectionListener selectionChangedListener;
+ private final IStateListener stateChangeListener = (state,
+ oldValue) -> refresh();
+
+ private final IPreferenceChangeListener configurationListener;
/**
* The default constructor
@@ -242,30 +297,6 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
scheduleRefresh(DEFAULT_REFRESH_DELAY, null);
}
};
-
- selectionChangedListener = new ISelectionListener() {
- @Override
- public void selectionChanged(IWorkbenchPart part,
- ISelection selection) {
- if (!reactOnSelection || part == RepositoriesView.this) {
- return;
- }
-
- // this may happen if we switch between editors
- if (part instanceof IEditorPart) {
- IEditorInput input = ((IEditorPart) part).getEditorInput();
- if (input instanceof IFileEditorInput) {
- reactOnSelection(new StructuredSelection(
- ((IFileEditorInput) input).getFile()));
- } else if (input instanceof IURIEditorInput) {
- reactOnSelection(new StructuredSelection(input));
- }
-
- } else {
- reactOnSelection(selection);
- }
- }
- };
}
/**
@@ -370,7 +401,6 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
.grab(true, false).applyTo(createLink);
}
- @SuppressWarnings("boxing")
@Override
public void createPartControl(Composite aParent) {
Composite displayArea = new Composite(aParent, SWT.NONE);
@@ -383,11 +413,12 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
IWorkbenchWindow w = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
ICommandService csrv = CommonUtils.getService(w, ICommandService.class);
- Command command = csrv
- .getCommand("org.eclipse.egit.ui.RepositoriesLinkWithSelection"); //$NON-NLS-1$
- reactOnSelection = (Boolean) command.getState(
- RegistryToggleState.STATE_ID).getValue();
-
+ reactOnSelection = csrv.getCommand(ToggleLinkWithSelectionCommand.ID)
+ .getState(RegistryToggleState.STATE_ID);
+ reactOnSelection.addListener(reactOnSelectionListener);
+ branchHierarchy = csrv.getCommand(ToggleBranchHierarchyCommand.ID)
+ .getState(RegistryToggleState.STATE_ID);
+ branchHierarchy.addListener(stateChangeListener);
IWorkbenchSiteProgressService service = CommonUtils.getService(getSite(), IWorkbenchSiteProgressService.class);
if (service != null) {
service.showBusyForFamily(JobFamilies.REPO_VIEW_REFRESH);
@@ -413,15 +444,6 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
return super.getAdapter(adapter);
}
- /**
- * Used by the "link with selection" action
- *
- * @param reactOnSelection
- */
- public void setReactOnSelection(boolean reactOnSelection) {
- this.reactOnSelection = reactOnSelection;
- }
-
@Override
protected CommonViewer createCommonViewer(Composite aParent) {
CommonViewer viewer = super.createCommonViewer(aParent);
@@ -580,6 +602,12 @@ public class RepositoriesView extends CommonNavigator implements IShowInSource,
@Override
public void dispose() {
+ if (reactOnSelection != null) {
+ reactOnSelection.removeListener(reactOnSelectionListener);
+ }
+ if (branchHierarchy != null) {
+ branchHierarchy.removeListener(stateChangeListener);
+ }
// make sure to cancel the refresh job
if (this.scheduledJob != null) {
this.scheduledJob.cancel();
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewContentProvider.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewContentProvider.java
index f50a9b74a..90761f16e 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewContentProvider.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoriesViewContentProvider.java
@@ -1,5 +1,6 @@
/*******************************************************************************
- * Copyright (c) 2010, 2013 SAP AG and others.
+ * Copyright (c) 2010, 2019 SAP AG and others.
+ *
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -36,7 +37,6 @@ import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
-import org.eclipse.core.commands.IStateListener;
import org.eclipse.core.commands.State;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.WorkspaceJob;
@@ -94,21 +94,19 @@ import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.handlers.RegistryToggleState;
/**
* Content Provider for the Git Repositories View
*/
-public class RepositoriesViewContentProvider implements ITreeContentProvider,
- IStateListener {
+public class RepositoriesViewContentProvider implements ITreeContentProvider {
private static final Object[] NO_CHILDREN = new Object[0];
private final RepositoryCache repositoryCache = org.eclipse.egit.core.Activator
.getDefault().getRepositoryCache();
- private final State commandState;
-
- private boolean branchHierarchyMode = false;
+ private final State branchHierarchy;
private boolean showUnbornHead = false;
@@ -134,16 +132,8 @@ public class RepositoriesViewContentProvider implements ITreeContentProvider,
super();
this.showUnbornHead = showUnbornHead;
ICommandService srv = CommonUtils.getService(PlatformUI.getWorkbench(), ICommandService.class);
- commandState = srv.getCommand(
- ToggleBranchHierarchyCommand.ID)
- .getState(ToggleBranchHierarchyCommand.TOGGLE_STATE);
- commandState.addListener(this);
- try {
- this.branchHierarchyMode = ((Boolean) commandState.getValue())
- .booleanValue();
- } catch (Exception e) {
- Activator.handleError(e.getMessage(), e, false);
- }
+ branchHierarchy = srv.getCommand(ToggleBranchHierarchyCommand.ID)
+ .getState(RegistryToggleState.STATE_ID);
}
@Override
@@ -187,9 +177,7 @@ public class RepositoriesViewContentProvider implements ITreeContentProvider,
@Override
public void dispose() {
- commandState.removeListener(this);
- for (ListenerHandle handle : refsChangedListeners.values())
- handle.remove();
+ refsChangedListeners.values().forEach(ListenerHandle::remove);
refsChangedListeners.clear();
}
@@ -391,7 +379,7 @@ public class RepositoriesViewContentProvider implements ITreeContentProvider,
private Object[] getBranchChildren(RepositoryTreeNode node, Repository repo,
String prefix) {
- if (branchHierarchyMode) {
+ if (isHierarchical()) {
return getBranchHierarchyChildren(node, repo, prefix);
} else {
try {
@@ -592,16 +580,6 @@ public class RepositoriesViewContentProvider implements ITreeContentProvider,
}
}
- @Override
- public void handleStateChange(State state, Object oldValue) {
- try {
- this.branchHierarchyMode = ((Boolean) state.getValue())
- .booleanValue();
- } catch (Exception e) {
- Activator.handleError(e.getMessage(), e, false);
- }
- }
-
private synchronized Map<String, Ref> getRefs(final Repository repo, final String prefix) throws IOException {
Map<String, Ref> allRefs = branchRefs.get(repo);
if (allRefs == null) {
@@ -674,6 +652,6 @@ public class RepositoriesViewContentProvider implements ITreeContentProvider,
* layout; {@code false} otherwise
*/
public boolean isHierarchical() {
- return branchHierarchyMode;
+ return ((Boolean) branchHierarchy.getValue()).booleanValue();
}
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryTreeNodeDecorator.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryTreeNodeDecorator.java
index 046038b97..d462fe03d 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryTreeNodeDecorator.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/RepositoryTreeNodeDecorator.java
@@ -42,6 +42,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.handlers.RegistryToggleState;
/**
* Lightweight decorator for {@link RepositoryTreeNode}s. Note that this
@@ -63,7 +64,7 @@ public class RepositoryTreeNodeDecorator extends GitDecorator
public RepositoryTreeNodeDecorator() {
ICommandService srv = CommonUtils.getService(PlatformUI.getWorkbench(), ICommandService.class);
verboseBranchModeState = srv.getCommand(ToggleBranchCommitCommand.ID)
- .getState(ToggleBranchCommitCommand.TOGGLE_STATE);
+ .getState(RegistryToggleState.STATE_ID);
verboseBranchModeState.addListener(this);
try {
this.verboseBranchMode = ((Boolean) verboseBranchModeState
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AbstractToggleCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AbstractToggleCommand.java
index 69cbda43b..7f1a692cb 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AbstractToggleCommand.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AbstractToggleCommand.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
+ * Copyright (c) 2017, 2019 Thomas Wolf <thomas.wolf@paranor.ch>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -12,12 +12,7 @@ package org.eclipse.egit.ui.internal.repository.tree.command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.egit.ui.internal.repository.RepositoriesView;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
/**
@@ -29,17 +24,6 @@ public abstract class AbstractToggleCommand
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
HandlerUtil.toggleCommandState(event.getCommand());
- IWorkbenchWindow window = PlatformUI.getWorkbench()
- .getActiveWorkbenchWindow();
- if (window != null) {
- IWorkbenchPage page = window.getActivePage();
- if (page != null) {
- IViewPart part = page.findView(RepositoriesView.VIEW_ID);
- if (part instanceof RepositoriesView) {
- (((RepositoriesView) part).getCommonViewer()).refresh();
- }
- }
- }
return null;
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/LinkWithSelectionCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/LinkWithSelectionCommand.java
deleted file mode 100644
index 2a8b41e44..000000000
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/LinkWithSelectionCommand.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010 SAP AG.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License 2.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * Mathias Kinzler (SAP AG) - initial implementation
- *******************************************************************************/
-package org.eclipse.egit.ui.internal.repository.tree.command;
-
-import org.eclipse.core.commands.Command;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.ui.handlers.RegistryToggleState;
-
-/**
- * Implements "Link With Selection" toggle
- */
-public class LinkWithSelectionCommand extends
- RepositoriesViewCommandHandler<RepositoryTreeNode> {
- @Override
- public Object execute(final ExecutionEvent event) throws ExecutionException {
- Command command = event.getCommand();
- HandlerUtil.toggleCommandState(command);
- @SuppressWarnings("boxing")
- boolean test = (Boolean) command.getState(RegistryToggleState.STATE_ID).getValue();
- getView(event).setReactOnSelection(test);
- return null;
- }
-}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchCommitCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchCommitCommand.java
index d942c26b9..0adebd4cb 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchCommitCommand.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchCommitCommand.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011, 2017 GitHub Inc. and others.
+ * Copyright (c) 2011, 2019 GitHub Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -23,9 +23,4 @@ public class ToggleBranchCommitCommand extends AbstractToggleCommand {
*/
public static final String ID = "org.eclipse.egit.ui.RepositoriesToggleBranchCommit"; //$NON-NLS-1$
- /**
- * The toggle state of this command
- */
- public static final String TOGGLE_STATE = "org.eclipse.ui.commands.toggleState"; //$NON-NLS-1$
-
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchHierarchyCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchHierarchyCommand.java
index 680c4775d..91346fc5d 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchHierarchyCommand.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleBranchHierarchyCommand.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2017 SAP AG and others.
+ * Copyright (c) 2010, 2019 SAP AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -23,9 +23,4 @@ public class ToggleBranchHierarchyCommand extends AbstractToggleCommand {
*/
public static final String ID = "org.eclipse.egit.ui.RepositoriesToggleBranchHierarchy"; //$NON-NLS-1$
- /**
- * The toggle state of this command
- */
- public static final String TOGGLE_STATE = "org.eclipse.ui.commands.toggleState"; //$NON-NLS-1$
-
}
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleLinkWithSelectionCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleLinkWithSelectionCommand.java
new file mode 100644
index 000000000..8441214bd
--- /dev/null
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/ToggleLinkWithSelectionCommand.java
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * Copyright (c) 2010, 2019 SAP AG and others
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Mathias Kinzler (SAP AG) - initial implementation
+ *******************************************************************************/
+package org.eclipse.egit.ui.internal.repository.tree.command;
+
+/**
+ * Implements "Link With Selection" toggle
+ */
+public class ToggleLinkWithSelectionCommand extends AbstractToggleCommand {
+
+ /**
+ * The command id for toggling "link with selection"
+ */
+ public static final String ID = "org.eclipse.egit.ui.RepositoriesLinkWithSelection"; //$NON-NLS-1$
+
+}

Back to the top