From b8ad961b3425f2a48b00b294a3a0be07e06cc153 Mon Sep 17 00:00:00 2001 From: Frank Gasdorf Date: Tue, 18 Jul 2017 12:44:38 +0200 Subject: [PATCH 1/2] fixes enablement of layer move up/down actions * see https://github.com/locationtech/udig-platform/issues/247 Signed-off-by: Frank Gasdorf --- .../udig/project/ui/internal/LayersView.java | 99 +++++++++++++++++-- 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/plugins/org.locationtech.udig.project.ui/src/org/locationtech/udig/project/ui/internal/LayersView.java b/plugins/org.locationtech.udig.project.ui/src/org/locationtech/udig/project/ui/internal/LayersView.java index 14df0d6a7a..1fe8e8d7a7 100644 --- a/plugins/org.locationtech.udig.project.ui/src/org/locationtech/udig/project/ui/internal/LayersView.java +++ b/plugins/org.locationtech.udig.project.ui/src/org/locationtech/udig/project/ui/internal/LayersView.java @@ -31,6 +31,8 @@ import org.locationtech.udig.project.internal.Map; import org.locationtech.udig.project.internal.ProjectPackage; import org.locationtech.udig.project.internal.ProjectPlugin; +import org.locationtech.udig.project.internal.impl.IEListVisitor; +import org.locationtech.udig.project.internal.impl.SynchronizedEObjectWithInverseResolvingEList; import org.locationtech.udig.project.render.IViewportModel; import org.locationtech.udig.project.render.IViewportModelListener; import org.locationtech.udig.project.render.ViewportModelEvent; @@ -400,6 +402,15 @@ public void selectionChanged( SelectionChangedEvent event ) { private abstract class LayerAction extends Action implements ISelectionListener { + /** + * marker if layer is selected + */ + protected static final char SELECTED = 'X'; + /** + * marker if layer isn't selected + */ + protected static final char UNSELECTED = 'O'; + protected final String SEL_INDEX_SEARCH_PATTERN = new StringBuilder().append(UNSELECTED).append(SELECTED).toString(); protected IStructuredSelection selection; /** @@ -417,21 +428,77 @@ public void selectionChanged( IWorkbenchPart part, ISelection selection ) { if (!(selection instanceof IStructuredSelection)) return; this.selection = (IStructuredSelection) selection; - if (part instanceof LayersView && selection != null && !selection.isEmpty()) - setEnabled(true); + if (getCurrentMap() != null && part instanceof LayersView) { + setEnabled(canEnable(this.selection)); + return; + } + setEnabled(false); } + abstract boolean canEnable(IStructuredSelection selection); + /** + * Returns a String of selection status for each map layer. if a layer is selected {@link #SELECTED} is at the specific + * index and if a layer isn't selected, {@link #UNSELECTED} is at the index. + * e.g '0XX0' means, that layer 1 and 4 are not selected whereas 2 and 3 are. It can be used to find out, if an action + * should be enabled/disabled. + * + * @param currentMap current map + * @param selection current selection + * @return empty string (anything else than layers are in the selection) or String of {@link #SELECTED} and + * {@link #UNSELECTED} for each layer at the specific index + */ + @SuppressWarnings("rawtypes") + protected String getSelectionIndexForLayers(Map currentMap, IStructuredSelection selection) { + final List selectedLayers = new ArrayList(); + if (currentMap == null || selection == null) { + return selectedLayers.toString(); + } + + if (selection != null) { + for (Iterator iter = selection.iterator(); iter.hasNext();) { + Object obj = iter.next(); + if (obj instanceof Layer) { + selectedLayers.add((Layer) obj); + } + } + } + + final StringBuilder selectionIndex = new StringBuilder(); + List mapLayers = currentMap.getLayersInternal(); + if (mapLayers instanceof SynchronizedEObjectWithInverseResolvingEList) { + ((SynchronizedEObjectWithInverseResolvingEList) mapLayers).syncedIteration(new IEListVisitor() { + @Override + public void visit(Layer layer) { + if (selectedLayers.contains(layer)) { + selectionIndex.append(SELECTED); + } else { + selectionIndex.append(UNSELECTED); + } + } + }); + + } else { + for (Layer layer : mapLayers) { + if (selectedLayers.contains(layer)) { + selectionIndex.append(SELECTED); + } else { + selectionIndex.append(UNSELECTED); + } + } + } + return selectionIndex.toString(); + } /** * @see org.eclipse.jface.action.Action#setEnabled(boolean) */ - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") public void setEnabled( boolean enabled ) { super.setEnabled(false); if (!enabled || selection == null || selection.isEmpty()) return; - for( Iterator iter = selection.iterator(); iter.hasNext(); ) { + for(Iterator iter = selection.iterator(); iter.hasNext(); ) { Object obj = iter.next(); if (!(obj instanceof Layer)) return; @@ -634,7 +701,7 @@ public void notifyChanged( Notification notification ) { setCurrentMap((Map)obj); } } - + viewer.setSorter(new ViewerLayerSorter()); // sets the layer visibility to match the check box setting. @@ -735,9 +802,16 @@ private LayerAction downAction() { public void run() { if( selection.isEmpty() ) return; IMap map = getCurrentMap(); - // map.sendCommandSync( new LayerMoveDownCommand( selection )); - map.sendCommandASync( new LayerMoveDownCommand( selection )); + map.sendCommandSync( new LayerMoveDownCommand( selection )); + viewer.setSelection(viewer.getSelection()); } + + @Override + boolean canEnable(IStructuredSelection selection) { + String selectionIndex = new StringBuilder(getSelectionIndexForLayers(getCurrentMap(), selection)).toString(); + return selectionIndex.indexOf(SEL_INDEX_SEARCH_PATTERN) >= 0; + } + }; downAction.setEnabled(false); downAction.setToolTipText(Messages.LayersView_down_tooltip); @@ -756,9 +830,16 @@ private LayerAction upAction() { public void run() { if( selection.isEmpty() ) return; IMap map = getCurrentMap(); - //map.sendCommandSync( new LayerMoveUpCommand( selection )); - map.sendCommandASync( new LayerMoveUpCommand( selection )); + map.sendCommandSync( new LayerMoveUpCommand( selection )); + viewer.setSelection(viewer.getSelection()); } + + @Override + boolean canEnable(IStructuredSelection selection) { + String selectionIndex = new StringBuilder(getSelectionIndexForLayers(getCurrentMap(), selection)).reverse().toString(); + return selectionIndex.indexOf(SEL_INDEX_SEARCH_PATTERN) >= 0; + } + }; upAction.setEnabled(false); upAction.setToolTipText(Messages.LayersView_up_tooltip); From 242157770d5b199a8c7527ef6b27170945a2716e Mon Sep 17 00:00:00 2001 From: Nikolaos Pringouris Date: Tue, 13 Feb 2018 12:19:13 +0200 Subject: [PATCH 2/2] fix issue with multiple non consecutive layer movement Signed-off-by: Nikolaos Pringouris --- .../udig/project/command/map/LayerMoveDownCommand.java | 7 +++++++ .../udig/project/command/map/LayerMoveUpCommand.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveDownCommand.java b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveDownCommand.java index 129acb6a11..0aac6c62f8 100644 --- a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveDownCommand.java +++ b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveDownCommand.java @@ -67,10 +67,17 @@ public String getName() { public void run( IProgressMonitor monitor ) throws Exception { // need to reverse otherwise nothing happens on multiselect Collections.reverse(selection); + int lastAllowedIndex = 0; for( ILayer layer : selection ) { + int layerIndex = layer.getZorder(); + if (layerIndex == lastAllowedIndex) { + lastAllowedIndex++; + continue; + } getMap().lowerLayer((Layer) layer); } } + public void rollback( IProgressMonitor monitor ) throws Exception { for( ILayer layer : selection ) { getMap().raiseLayer((Layer) layer); diff --git a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveUpCommand.java b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveUpCommand.java index d75e7586d6..7e1683fbb0 100644 --- a/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveUpCommand.java +++ b/plugins/org.locationtech.udig.project/src/org/locationtech/udig/project/command/map/LayerMoveUpCommand.java @@ -65,10 +65,17 @@ public String getName() { } public void run( IProgressMonitor monitor ) throws Exception { + int maxAllowedIndex = getMap().getLayersInternal().size()-1; for( ILayer layer : selection ) { + int layerIndex = layer.getZorder(); + if (layerIndex == maxAllowedIndex) { + maxAllowedIndex--; + continue; + } getMap().raiseLayer((Layer) layer); } } + public void rollback( IProgressMonitor monitor ) throws Exception { // need to reverse otherwise nothing happens on multiselect Collections.reverse(selection);