Skip to content

Commit

Permalink
unifies objectProperty for layer selection
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Gasdorf <[email protected]>
  • Loading branch information
fgdrf committed Jul 15, 2020
1 parent 85d7690 commit 9f7f621
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Require-Bundle: org.eclipse.gef;bundle-version="3.7.0",
org.locationtech.udig.project.edit,
org.locationtech.udig.project.tests;visibility:=reexport,
org.locationtech.udig.catalog.ui.tests;visibility:=reexport,
org.locationtech.udig.tool.default
org.locationtech.udig.tool.default,
org.easymock
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* uDig - User Friendly Desktop Internet GIS client
* http://udig.refractions.net
* (C) 2020, Refractions Research Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
*
*/
package org.locationtech.udig.project.ui.operations;

import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.locationtech.udig.project.ILayer;
import org.opengis.filter.Filter;

@RunWith(Parameterized.class)
public class LayerSelectionPropertyParameterTest {
private Boolean expectedResult;

private Filter givenFilter;

private String givenValue;

@SuppressWarnings("rawtypes")
@Parameterized.Parameters
public static Collection primeNumbers() {
return Arrays.asList(new Object[][] {
// no selection on layer
{ false, Filter.EXCLUDE, "true" }, { false, Filter.EXCLUDE, "TRUE" },
{ false, Filter.EXCLUDE, null }, { false, Filter.EXCLUDE, "True" },
{ true, Filter.EXCLUDE, "false" }, { true, Filter.EXCLUDE, "FALSE" },
{ true, Filter.EXCLUDE, "False" },

// layer has selection
{ true, Filter.INCLUDE, "true" }, { true, Filter.INCLUDE, "TRUE" },
{ true, Filter.INCLUDE, "True" }, { true, Filter.INCLUDE, null },
{ false, Filter.INCLUDE, "false" }, { false, Filter.INCLUDE, "FALSE" },
{ false, Filter.INCLUDE, "False" },

// without value or anything else than boolean values it does not really make sense
{ true, Filter.EXCLUDE, "whatever" }, { false, Filter.INCLUDE, "whatever" }, });
}

public LayerSelectionPropertyParameterTest(boolean expected, Filter givenFilter,
String givenValue) {
this.expectedResult = expected;
this.givenFilter = givenFilter;
this.givenValue = givenValue;
}

ILayer layerMock = createNiceMock(ILayer.class);

@Test
public void testHasSelection() {
expect(layerMock.getFilter()).andReturn(givenFilter).anyTimes();
LayerSelectionProperty layerSelectionProperty = new LayerSelectionProperty();
replay(layerMock);
assertEquals(
MessageFormat.format("for {0} with expectedValue {1}", givenFilter, givenValue),
expectedResult, layerSelectionProperty.isTrue(layerMock, givenValue));

verify(layerMock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* uDig - User Friendly Desktop Internet GIS client
* http://udig.refractions.net
* (C) 2020, Refractions Research Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
*
*/
package org.locationtech.udig.project.ui.operations;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertFalse;

import org.junit.Test;
import org.locationtech.udig.project.ILayer;
import org.locationtech.udig.project.internal.Layer;
import org.locationtech.udig.ui.operations.IOpFilterListener;
import org.opengis.filter.Filter;

public class LayerSelectionPropertyTest {

ILayer layerMock = createNiceMock(Layer.class);

IOpFilterListener listenerMock = createNiceMock(IOpFilterListener.class);

@Test
public void testLayerGetsNotificationOnFilterChange() {
expect(layerMock.getFilter()).andReturn(Filter.EXCLUDE).anyTimes();
layerMock.addListener(anyObject());
expectLastCall().times(2);

LayerSelectionProperty layerSelectionProperty = new LayerSelectionProperty();
replay(layerMock, listenerMock);
// first call tries to add Listener
layerSelectionProperty.isTrue(layerMock, "Whatever");
// second call tries to add Listener again
layerSelectionProperty.isTrue(layerMock, "Whatever");

verify(layerMock, listenerMock);
}

@Test
public void testCachingIsDisabled() {
assertFalse(new LayerSelectionProperty().canCacheResult());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Export-Package: org.locationtech.udig.project.internal.ui.wizard,
org.locationtech.udig.project.ui.internal.tool.impl,
org.locationtech.udig.project.ui.internal.wizard,
org.locationtech.udig.project.ui.internal.wizard.url,
org.locationtech.udig.project.ui.operations,
org.locationtech.udig.project.ui.operations.example,
org.locationtech.udig.project.ui.preferences,
org.locationtech.udig.project.ui.render.displayAdapter,
Expand Down
4 changes: 2 additions & 2 deletions plugins/org.locationtech.udig.project.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@
point="org.locationtech.udig.ui.objectProperty">
<object targetClass="org.locationtech.udig.project.ILayer">
<property
id="GeometryType"
class="org.locationtech.udig.project.ui.internal.tool.display.GeometryProperty"/>
class="org.locationtech.udig.project.ui.internal.tool.display.GeometryProperty"
id="GeometryType"/>
<property
class="org.locationtech.udig.project.ui.operations.LayerSelectionProperty"
id="hasSelection"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@
package org.locationtech.udig.project.ui.operations;

import org.locationtech.udig.project.ILayer;
import org.locationtech.udig.project.ILayerListener;
import org.locationtech.udig.project.LayerEvent;
import org.locationtech.udig.ui.operations.AbstractPropertyValue;
import org.locationtech.udig.ui.operations.PropertyValue;
import org.opengis.filter.Filter;

/**
* Checks if a layer has a selection
* Checks if a layer has a selection. This property allows to enable operations if layer
* hasSelection. If given value is set to false, its possible to check, if the layer has no
* selection. This allows uses to provide functionality to be enabled if and only if a layer has no
* selection filter set.
*
* @author Jesse
* @author Frank Gasdorf
*/
public class LayerSelectionProperty extends AbstractPropertyValue<ILayer>
implements PropertyValue<ILayer> {

private final ILayerListener layerListener = new ILayerListener() {

public void refresh(LayerEvent event) {
if (event.getType() == LayerEvent.EventType.FILTER) {
notifyListeners(event.getSource());
}
}

};

public boolean canCacheResult() {
return false;
}
Expand All @@ -32,9 +48,13 @@ public boolean isBlocking() {
return false;
}

public boolean isTrue(ILayer object, String value) {
Boolean hasSelection = object.getFilter() != Filter.INCLUDE;
return hasSelection.toString().equalsIgnoreCase(value);
public boolean isTrue(ILayer layer, String expectedBooleanAsString) {
Boolean hasSelection = layer.getFilter() != Filter.EXCLUDE;

layer.addListener(layerListener);

return hasSelection.equals(expectedBooleanAsString == null ? Boolean.TRUE
: Boolean.valueOf(expectedBooleanAsString));
}

}
12 changes: 2 additions & 10 deletions plugins/org.locationtech.udig.tool.select/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
tooltip="%zoom.select.tooltip">
<enablement>
<property
expectedValue="unimportant"
propertyId="layerHasSelectionProperty"/>
expectedValue="true"
propertyId="hasSelection" />
</enablement>
</actionTool>
<toolCursor
Expand Down Expand Up @@ -137,14 +137,6 @@
<viewShortcut id="org.locationtech.udig.tool.select.TableView"/>
</perspectiveExtension>
</extension>
<extension
point="org.locationtech.udig.ui.objectProperty">
<object targetClass="org.locationtech.udig.project.ILayer">
<property
id="layerHasSelectionProperty"
class="org.locationtech.udig.tool.select.internal.LayerHasSelectionProperty"/>
</object>
</extension>
<extension
point="org.locationtech.udig.project.ui.featureEditor">
<editor
Expand Down

This file was deleted.

0 comments on commit 9f7f621

Please sign in to comment.