This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new f875db2  Fixed enable evaluation for cloned PropertyMonitors. Defined 
default behaviour for collections and numbers (#701)
f875db2 is described below

commit f875db2fa731fac09d516f68f4ecf5e818d48125
Author: Svatopluk Dedic <[email protected]>
AuthorDate: Fri Aug 17 11:31:55 2018 +0200

    Fixed enable evaluation for cloned PropertyMonitors. Defined default 
behaviour for collections and numbers (#701)
---
 openide.awt/src/org/openide/awt/ActionState.java   |  3 ++
 .../src/org/openide/awt/PropertyMonitor.java       | 10 ++++++
 .../openide/awt/StatefulActionProcessorTest.java   | 42 ++++++++++++++++++++++
 3 files changed, 55 insertions(+)

diff --git a/openide.awt/src/org/openide/awt/ActionState.java 
b/openide.awt/src/org/openide/awt/ActionState.java
index fda05ba..814ebf0 100644
--- a/openide.awt/src/org/openide/awt/ActionState.java
+++ b/openide.awt/src/org/openide/awt/ActionState.java
@@ -59,6 +59,9 @@ import org.openide.util.actions.Presenter;
  * <li>if {@link #checkValue} is {@link #NON_NULL_VALUE}, the action is 
checked if and only if
  * the value is not {@code null}. 
  * <li>if the value type is an enum, its {@link Enum#name} is compared to 
{@link #checkValue}
+ * <li>if the value is a {@link Collection} or {@link Map}, state evaluates to 
the {@link Collection#isEmpty} or
+ * {@link Map#isEmpty}, respectively.
+ * <li>if the value is a {@link Number}, state will be true if value evaluates 
to <b>a positive integer</b>.
  * <li>the state will be {@code false} (unchecked) otherwise.
  * <p/>
  * If {@link #type} is set to {@link Action}.class, the annotated element 
<b>must
diff --git a/openide.awt/src/org/openide/awt/PropertyMonitor.java 
b/openide.awt/src/org/openide/awt/PropertyMonitor.java
index 7b36d4d..9ed2cae 100644
--- a/openide.awt/src/org/openide/awt/PropertyMonitor.java
+++ b/openide.awt/src/org/openide/awt/PropertyMonitor.java
@@ -26,6 +26,7 @@ import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.EventListener;
 import java.util.List;
@@ -520,6 +521,14 @@ class PropertyMonitor<T> implements 
ContextAction.StatefulMonitor<T>, PropertyCh
             }
         }
         if (checkedValue == null) {
+            // v is not null;
+            if (v instanceof Collection) {
+                return !((Collection) v).isEmpty();
+            } else if (v instanceof Map) {
+                return !((Map) v).isEmpty();
+            } else if (Number.class.isInstance(v)) {
+                return ((Number)v).intValue() > 0;
+            }
             return false;
         }
         if (checkedValue == ActionState.NON_NULL_VALUE) {
@@ -551,6 +560,7 @@ class PropertyMonitor<T> implements 
ContextAction.StatefulMonitor<T>, PropertyCh
         this.listenerType = other.listenerType;
         this.refGetter = other.refGetter;
         this.valueFactory = other.valueFactory;
+        this.valType = other.valType;
         this.refAddListener = other.refAddListener;
         this.refRemoveListener = other.refRemoveListener;
         this.listenerInterface = other.listenerInterface;
diff --git 
a/openide.awt/test/unit/src/org/netbeans/modules/openide/awt/StatefulActionProcessorTest.java
 
b/openide.awt/test/unit/src/org/netbeans/modules/openide/awt/StatefulActionProcessorTest.java
index 2c59454..5de7162 100644
--- 
a/openide.awt/test/unit/src/org/netbeans/modules/openide/awt/StatefulActionProcessorTest.java
+++ 
b/openide.awt/test/unit/src/org/netbeans/modules/openide/awt/StatefulActionProcessorTest.java
@@ -31,6 +31,8 @@ import java.util.Collection;
 import java.util.List;
 import javax.swing.AbstractAction;
 import javax.swing.Action;
+import javax.swing.DefaultListModel;
+import javax.swing.DefaultListSelectionModel;
 import javax.swing.ListSelectionModel;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
@@ -1377,4 +1379,44 @@ public class StatefulActionProcessorTest extends 
NbTestCase implements ContextGl
             received = e;
         }
     }
+    
+    public void testCustomContextAwareInstance() {
+        Action a = Actions.forID("Foo", "test.ListAction");
+        DefaultListSelectionModel model = new DefaultListSelectionModel();
+        
+        InstanceContent localContent1 = new InstanceContent();
+        AbstractLookup localLookup1 = new AbstractLookup(localContent1);
+        
+        Action la = 
((ContextAwareAction)a).createContextAwareInstance(localLookup1);
+        
+        assertFalse(a.isEnabled());
+        assertFalse(la.isEnabled());
+        
+        localContent1.add(model);
+        
+        assertFalse(a.isEnabled());
+        assertTrue(la.isEnabled());
+        assertFalse((Boolean)la.getValue(Action.SELECTED_KEY));
+        
+        // checks that the context-bound instance changes its selected state
+        // if the model changes (relevant property change event is fired)
+        model.setSelectionInterval(1, 2);
+        assertTrue((Boolean)la.getValue(Action.SELECTED_KEY));
+    }
+    
+    public void testCustomListenerAction() {
+        Action a = Actions.forID("Foo", "test.ListAction");
+        DefaultListSelectionModel model = new DefaultListSelectionModel();
+        
+        assertFalse(a.isEnabled());
+        assertFalse((Boolean)a.getValue(Action.SELECTED_KEY));
+        
+        lookupContent.add(model);
+        assertTrue(a.isEnabled());
+        assertFalse((Boolean)a.getValue(Action.SELECTED_KEY));
+        
+        model.addSelectionInterval(1, 1);
+        assertTrue(a.isEnabled());
+        assertTrue((Boolean)a.getValue(Action.SELECTED_KEY));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to