Skip to content

Commit 1cf168d

Browse files
committed
unifies objectPropery for layer selection
Signed-off-by: Frank Gasdorf <[email protected]>
1 parent 85d7690 commit 1cf168d

File tree

8 files changed

+163
-67
lines changed

8 files changed

+163
-67
lines changed

plugins/org.locationtech.udig.project.ui.tests/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Require-Bundle: org.eclipse.gef;bundle-version="3.7.0",
5050
org.locationtech.udig.project.edit,
5151
org.locationtech.udig.project.tests;visibility:=reexport,
5252
org.locationtech.udig.catalog.ui.tests;visibility:=reexport,
53-
org.locationtech.udig.tool.default
53+
org.locationtech.udig.tool.default,
54+
org.easymock
5455
Bundle-ActivationPolicy: lazy
5556
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* uDig - User Friendly Desktop Internet GIS client
3+
* http://udig.refractions.net
4+
* (C) 2004, Refractions Research Inc.
5+
*
6+
* All rights reserved. This program and the accompanying materials
7+
* are made available under the terms of the Eclipse Public License v1.0
8+
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
9+
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
10+
*
11+
*/
12+
package org.locationtech.udig.project.ui.operations;
13+
14+
import static org.easymock.EasyMock.createNiceMock;
15+
import static org.easymock.EasyMock.expect;
16+
import static org.easymock.EasyMock.replay;
17+
import static org.easymock.EasyMock.verify;
18+
import static org.junit.Assert.assertEquals;
19+
20+
import java.text.MessageFormat;
21+
import java.util.Arrays;
22+
import java.util.Collection;
23+
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.Parameterized;
27+
import org.locationtech.udig.project.ILayer;
28+
import org.opengis.filter.Filter;
29+
30+
@RunWith(Parameterized.class)
31+
public class LayerSelectionPropertyParameterTest {
32+
private Boolean expectedResult;
33+
34+
private Filter givenFilter;
35+
36+
private String givenValue;
37+
38+
@SuppressWarnings("rawtypes")
39+
@Parameterized.Parameters
40+
public static Collection primeNumbers() {
41+
return Arrays.asList(new Object[][] {
42+
// no selection on layer
43+
{ false, Filter.EXCLUDE, "true" }, { false, Filter.EXCLUDE, "TRUE" },
44+
{ false, Filter.EXCLUDE, null }, { false, Filter.EXCLUDE, "True" },
45+
{ true, Filter.EXCLUDE, "false" }, { true, Filter.EXCLUDE, "FALSE" },
46+
{ true, Filter.EXCLUDE, "False" },
47+
48+
// layer has selection
49+
{ true, Filter.INCLUDE, "true" }, { true, Filter.INCLUDE, "TRUE" },
50+
{ true, Filter.INCLUDE, "True" }, { true, Filter.INCLUDE, null },
51+
{ false, Filter.INCLUDE, "false" }, { false, Filter.INCLUDE, "FALSE" },
52+
{ false, Filter.INCLUDE, "False" },
53+
54+
// without value or anything else than boolean values it does not really make sense
55+
{ true, Filter.EXCLUDE, "whatever" }, { false, Filter.INCLUDE, "whatever" }, });
56+
}
57+
58+
public LayerSelectionPropertyParameterTest(boolean expected, Filter givenFilter,
59+
String givenValue) {
60+
this.expectedResult = expected;
61+
this.givenFilter = givenFilter;
62+
this.givenValue = givenValue;
63+
}
64+
65+
ILayer layerMock = createNiceMock(ILayer.class);
66+
67+
@Test
68+
public void testHasSelection() {
69+
expect(layerMock.getFilter()).andReturn(givenFilter).anyTimes();
70+
LayerSelectionProperty layerSelectionProperty = new LayerSelectionProperty();
71+
replay(layerMock);
72+
assertEquals(
73+
MessageFormat.format("for {0} with expectedValue {1}", givenFilter, givenValue),
74+
expectedResult, layerSelectionProperty.isTrue(layerMock, givenValue));
75+
76+
verify(layerMock);
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* uDig - User Friendly Desktop Internet GIS client
3+
* http://udig.refractions.net
4+
* (C) 2004, Refractions Research Inc.
5+
*
6+
* All rights reserved. This program and the accompanying materials
7+
* are made available under the terms of the Eclipse Public License v1.0
8+
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
9+
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
10+
*
11+
*/
12+
package org.locationtech.udig.project.ui.operations;
13+
14+
import static org.easymock.EasyMock.createNiceMock;
15+
import static org.easymock.EasyMock.expect;
16+
import static org.easymock.EasyMock.expectLastCall;
17+
import static org.easymock.EasyMock.replay;
18+
import static org.easymock.EasyMock.verify;
19+
import static org.junit.Assert.assertFalse;
20+
21+
import org.easymock.EasyMock;
22+
import org.junit.Test;
23+
import org.locationtech.udig.project.ILayer;
24+
import org.locationtech.udig.project.internal.Layer;
25+
import org.locationtech.udig.ui.operations.IOpFilterListener;
26+
import org.opengis.filter.Filter;
27+
28+
public class LayerSelectionPropertyTest {
29+
30+
ILayer layerMock = createNiceMock(Layer.class);
31+
32+
IOpFilterListener listenerMock = createNiceMock(IOpFilterListener.class);
33+
34+
@Test
35+
public void testLayerGetsNotificationOnFilterChange() {
36+
expect(layerMock.getFilter()).andReturn(Filter.EXCLUDE).anyTimes();
37+
layerMock.addListener(EasyMock.anyObject());
38+
expectLastCall().times(2);
39+
40+
LayerSelectionProperty layerSelectionProperty = new LayerSelectionProperty();
41+
replay(layerMock, listenerMock);
42+
// first call tries to add Listener
43+
layerSelectionProperty.isTrue(layerMock, "Whatever");
44+
// second call tries to add Listener again
45+
layerSelectionProperty.isTrue(layerMock, "Whatever");
46+
47+
verify(layerMock, listenerMock);
48+
}
49+
50+
@Test
51+
public void testCachingIsDisabled() {
52+
assertFalse(new LayerSelectionProperty().canCacheResult());
53+
}
54+
}

plugins/org.locationtech.udig.project.ui/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Export-Package: org.locationtech.udig.project.internal.ui.wizard,
2727
org.locationtech.udig.project.ui.internal.tool.impl,
2828
org.locationtech.udig.project.ui.internal.wizard,
2929
org.locationtech.udig.project.ui.internal.wizard.url,
30+
org.locationtech.udig.project.ui.operations,
3031
org.locationtech.udig.project.ui.operations.example,
3132
org.locationtech.udig.project.ui.preferences,
3233
org.locationtech.udig.project.ui.render.displayAdapter,

plugins/org.locationtech.udig.project.ui/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,8 @@
765765
point="org.locationtech.udig.ui.objectProperty">
766766
<object targetClass="org.locationtech.udig.project.ILayer">
767767
<property
768-
id="GeometryType"
769-
class="org.locationtech.udig.project.ui.internal.tool.display.GeometryProperty"/>
768+
class="org.locationtech.udig.project.ui.internal.tool.display.GeometryProperty"
769+
id="GeometryType"/>
770770
<property
771771
class="org.locationtech.udig.project.ui.operations.LayerSelectionProperty"
772772
id="hasSelection"/>

plugins/org.locationtech.udig.project.ui/src/org/locationtech/udig/project/ui/operations/LayerSelectionProperty.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,34 @@
1212
package org.locationtech.udig.project.ui.operations;
1313

1414
import org.locationtech.udig.project.ILayer;
15+
import org.locationtech.udig.project.ILayerListener;
16+
import org.locationtech.udig.project.LayerEvent;
1517
import org.locationtech.udig.ui.operations.AbstractPropertyValue;
1618
import org.locationtech.udig.ui.operations.PropertyValue;
1719
import org.opengis.filter.Filter;
1820

1921
/**
20-
* Checks if a layer has a selection
22+
* Checks if a layer has a selection. This property allows to enable operations if layer
23+
* hasSelection. If given value is set to false, its possible to check, if the layer has no
24+
* selection. This allows uses to provide functionality to be enabled if and only if a layer has no
25+
* selection filter set.
2126
*
2227
* @author Jesse
28+
* @author Frank Gasdorf
2329
*/
2430
public class LayerSelectionProperty extends AbstractPropertyValue<ILayer>
2531
implements PropertyValue<ILayer> {
2632

33+
private final ILayerListener layerListener = new ILayerListener() {
34+
35+
public void refresh(LayerEvent event) {
36+
if (event.getType() == LayerEvent.EventType.FILTER) {
37+
notifyListeners(event.getSource());
38+
}
39+
}
40+
41+
};
42+
2743
public boolean canCacheResult() {
2844
return false;
2945
}
@@ -32,9 +48,13 @@ public boolean isBlocking() {
3248
return false;
3349
}
3450

35-
public boolean isTrue(ILayer object, String value) {
36-
Boolean hasSelection = object.getFilter() != Filter.INCLUDE;
37-
return hasSelection.toString().equalsIgnoreCase(value);
51+
public boolean isTrue(ILayer layer, String expectedBooleanAsString) {
52+
Boolean hasSelection = layer.getFilter() != Filter.EXCLUDE;
53+
54+
layer.addListener(layerListener);
55+
56+
return hasSelection.equals(expectedBooleanAsString == null ? Boolean.TRUE
57+
: Boolean.valueOf(expectedBooleanAsString));
3858
}
3959

4060
}

plugins/org.locationtech.udig.tool.select/plugin.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
tooltip="%zoom.select.tooltip">
8989
<enablement>
9090
<property
91-
expectedValue="unimportant"
92-
propertyId="layerHasSelectionProperty"/>
91+
expectedValue="true"
92+
propertyId="hasSelection" />
9393
</enablement>
9494
</actionTool>
9595
<toolCursor
@@ -137,14 +137,6 @@
137137
<viewShortcut id="org.locationtech.udig.tool.select.TableView"/>
138138
</perspectiveExtension>
139139
</extension>
140-
<extension
141-
point="org.locationtech.udig.ui.objectProperty">
142-
<object targetClass="org.locationtech.udig.project.ILayer">
143-
<property
144-
id="layerHasSelectionProperty"
145-
class="org.locationtech.udig.tool.select.internal.LayerHasSelectionProperty"/>
146-
</object>
147-
</extension>
148140
<extension
149141
point="org.locationtech.udig.project.ui.featureEditor">
150142
<editor

plugins/org.locationtech.udig.tool.select/src/org/locationtech/udig/tool/select/internal/LayerHasSelectionProperty.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)