Revision: 1275
Author:   mathiasbr
Date:     2006-08-07 01:27:08 -0700 (Mon, 07 Aug 2006)
ViewCVS:  http://svn.sourceforge.net/spring-rich-c/?rev=1275&view=rev

Log Message:
-----------
added filter support for combobox binder

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinder.java
    
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinding.java

Added Paths:
-----------
    
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java

Removed Paths:
-------------
    
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingAbstractTests.java
Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinder.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinder.java
 2006-08-07 08:26:11 UTC (rev 1274)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinder.java
 2006-08-07 08:27:08 UTC (rev 1275)
@@ -25,6 +25,7 @@
 
 import org.springframework.binding.form.FormModel;
 import org.springframework.binding.value.ValueModel;
+import org.springframework.core.closure.Constraint;
 import org.springframework.richclient.form.binding.Binding;
 import org.springframework.richclient.form.binding.support.AbstractBinder;
 import org.springframework.richclient.list.BeanPropertyValueComboBoxEditor;
@@ -64,6 +65,9 @@
         if (context.containsKey(SELECTABLE_ITEMS_HOLDER_KEY)) {
             
binding.setSelectableItemsHolder((ValueModel)context.get(SELECTABLE_ITEMS_HOLDER_KEY));
         }
+        if (context.containsKey(FILTER_KEY)) {
+            binding.setFilter((Constraint)context.get(FILTER_KEY));
+        }
         if (context.containsKey(RENDERER_KEY)) {
             binding.setRenderer((ListCellRenderer)context.get(RENDERER_KEY));
         }

Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinding.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinding.java
        2006-08-07 08:26:11 UTC (rev 1274)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinding.java
        2006-08-07 08:27:08 UTC (rev 1275)
@@ -17,10 +17,12 @@
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Iterator;
 
 import javax.swing.ComboBoxEditor;
 import javax.swing.ComboBoxModel;
@@ -28,10 +30,12 @@
 import javax.swing.JComponent;
 import javax.swing.ListCellRenderer;
 
+import org.apache.commons.lang.builder.EqualsBuilder;
 import org.springframework.binding.form.FormModel;
 import org.springframework.binding.value.ValueModel;
 import org.springframework.binding.value.support.ListListModel;
 import org.springframework.binding.value.support.ValueHolder;
+import org.springframework.core.closure.Constraint;
 import org.springframework.richclient.form.binding.support.CustomBinding;
 
 /**
@@ -49,6 +53,8 @@
 
     private ValueModel selectableItemsHolder;
 
+    private Constraint filter;
+
     public ComboBoxBinding(FormModel formModel, String formPropertyPath) {
         super(formModel, formPropertyPath, null);
         this.comboBox = createComboBox();
@@ -68,25 +74,34 @@
     }
 
     /**
-     * @return Collections.EMPTY_LIST if SelectableItemHolder's value was null,
-     *         a created List if value was an instance of Object[] or a
-     *         Collection if the value was already a collection.
+     * @return Collections.EMPTY_LIST if SelectableItemHolder's value was 
null, a created List if value was an instance
+     *         of Object[] or a Collection if the value was already a 
collection.
      * @exception UnsupportedOperationException
      *                if none the SelectableItemholder's value wasn't any of 
the above
      */
     protected Collection getSelectableItems() {
         final Object selectableItems = getSelectableItemsHolder().getValue();
+        Collection items;
         if (selectableItems == null) {
-            return Collections.EMPTY_LIST;
+            items = Collections.EMPTY_LIST;
         } else if (selectableItems instanceof Object[]) {
-            return Arrays.asList((Object[])selectableItems);
+            items = Arrays.asList((Object[]) selectableItems);
+        } else if (selectableItems instanceof Collection) {
+            items = (Collection) selectableItems;
+        } else {
+            throw new UnsupportedOperationException("selectableItemsHolder 
must contain an array or a Collection");
         }
-        else if (selectableItems instanceof Collection) {
-            return (Collection)selectableItems;
+        if (filter != null) {
+            Collection filteredItems = new ArrayList(items.size());
+            for (Iterator iter = items.iterator(); iter.hasNext();) {
+                Object item = iter.next();
+                if (filter.test(item)) {
+                    filteredItems.add(item);
+                }
+            }
+            items = filteredItems;
         }
-        else {
-            throw new UnsupportedOperationException("selectableItemsHolder 
must contain an array or a Collection");
-        }
+        return items;
     }
 
     protected void updateSelectableItems() {
@@ -172,4 +187,15 @@
             updateSelectableItems();
         }
     }
+
+    public void setFilter(Constraint filter) {
+        if (!new EqualsBuilder().append(this.filter, filter).isEquals()) {
+            this.filter = filter;
+            updateSelectableItems();
+        }
+    }
+
+    public Constraint getFilter() {
+        return filter;
+    }
 }
\ No newline at end of file

Deleted: 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingAbstractTests.java
===================================================================
--- 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingAbstractTests.java
   2006-08-07 08:26:11 UTC (rev 1274)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingAbstractTests.java
   2006-08-07 08:27:08 UTC (rev 1275)
@@ -1,127 +0,0 @@
-/*
- * Copyright 2002-2004 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.springframework.richclient.form.binding.swing;
-
-import java.util.Collections;
-
-import javax.swing.DefaultComboBoxModel;
-import javax.swing.JComboBox;
-import javax.swing.event.ListDataEvent;
-
-import org.springframework.binding.value.ValueModel;
-import org.springframework.binding.value.support.ValueHolder;
-import org.springframework.richclient.form.binding.Binding;
-
-public class ComboBoxBindingAbstractTests extends BindingAbstractTests {
-
-    private ValueModel sih;
-
-    private ComboBoxBinding cbb;
-
-    private JComboBox cb;
-
-    protected String setUpBinding() {
-        cbb = new ComboBoxBinding(fm, "simpleProperty");
-        cb = (JComboBox)cbb.getControl();
-        sih = new ValueHolder(new Object[] {"0", "1", "2", "3", "4"});
-        cbb.setSelectableItemsHolder(sih);
-        return "simpleProperty";
-    }
-
-    public void testValueModelUpdatesComponent() {        
-        TestListDataListener tldl = new TestListDataListener();
-        cb.getModel().addListDataListener(tldl);
-        
-        assertEquals(null, cb.getSelectedItem());
-        assertEquals(-1, cb.getSelectedIndex());
-        tldl.assertCalls(0);        
-
-        vm.setValue("1");
-        assertEquals("1", cb.getSelectedItem());
-        assertEquals(1, cb.getSelectedIndex());
-        tldl.assertEvent(1, ListDataEvent.CONTENTS_CHANGED, -1, -1);
-
-        vm.setValue("2");
-        assertEquals("2", cb.getSelectedItem());
-        assertEquals(2, cb.getSelectedIndex());
-        tldl.assertEvent(2, ListDataEvent.CONTENTS_CHANGED, -1, -1);
-
-        vm.setValue(null);
-        assertEquals(null, cb.getSelectedItem());
-        assertEquals(-1, cb.getSelectedIndex());
-        tldl.assertEvent(3, ListDataEvent.CONTENTS_CHANGED, -1, -1);
-        
-        vm.setValue(null);
-        tldl.assertCalls(3);
-    }
-
-    public void testComponentUpdatesValueModel() {
-        cb.setSelectedIndex(1);
-        assertEquals("1", vm.getValue());
-
-        cb.setSelectedItem("2");
-        assertEquals("2", vm.getValue());
-
-        cb.setSelectedIndex(-1);
-        assertEquals(null, vm.getValue());
-    }
-
-    public void testSelectableValueChangeUpdatesComboBoxModel() {
-        assertEquals("0", cb.getModel().getElementAt(0));
-
-        sih.setValue(new Object[] {"1"});
-        assertEquals("1", cb.getModel().getElementAt(0));
-    }
-
-    public void testComponentTracksEnabledChanges() {
-        assertTrue(cb.isEnabled());
-
-        fm.getFieldMetadata("simpleProperty").setEnabled(false);
-        assertFalse(cb.isEnabled());
-
-        fm.getFieldMetadata("simpleProperty").setEnabled(true);
-        assertTrue(cb.isEnabled());
-    }
-
-    public void testComponentTracksReadOnlyChanges() {
-        assertTrue(cb.isEnabled());
-
-        fm.getFieldMetadata("simpleProperty").setReadOnly(true);
-        assertFalse(cb.isEnabled());
-
-        fm.getFieldMetadata("simpleProperty").setReadOnly(false);
-        assertTrue(cb.isEnabled());
-    }
-    
-    public void testSelectableItemHolderNullValue()
-    {
-        ComboBoxBinding binding = new ComboBoxBinding(fm, "simpleProperty");
-        binding.getControl();
-        ValueHolder valueHolder = new ValueHolder();
-        binding.setSelectableItemsHolder(valueHolder);
-        assertEquals(binding.getSelectableItems(), Collections.EMPTY_LIST);
-    }
-
-    public void testExistingModel() 
-    {
-        JComboBox cb = new JComboBox(new DefaultComboBoxModel(new Object[] {
-                "1", "2", "3" }));
-        ComboBoxBinder binder = new ComboBoxBinder();
-        Binding binding = binder.bind(cb, fm, "simpleProperty",
-                Collections.EMPTY_MAP);
-        assertEquals(3, ((JComboBox) 
binding.getControl()).getModel().getSize());
-    }
-}
\ No newline at end of file

Copied: 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
 (from rev 1271, 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingAbstractTests.java)
===================================================================
--- 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
                           (rev 0)
+++ 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
   2006-08-07 08:27:08 UTC (rev 1275)
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2002-2004 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.richclient.form.binding.swing;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.event.ListDataEvent;
+
+import org.springframework.binding.value.ValueModel;
+import org.springframework.binding.value.support.ValueHolder;
+import org.springframework.core.closure.Constraint;
+import org.springframework.richclient.form.binding.Binding;
+
+public class ComboBoxBindingTests extends BindingAbstractTests {
+
+    private static final Object[] SELECTABLEITEMS = new Object[] { "0", "1", 
"2", "3", "4" };
+
+    private ValueModel sih;
+
+    private ComboBoxBinding cbb;
+
+    private JComboBox cb;
+
+    protected String setUpBinding() {
+        cbb = new ComboBoxBinding(fm, "simpleProperty");
+        cb = (JComboBox) cbb.getControl();
+        sih = new ValueHolder(SELECTABLEITEMS);
+        cbb.setSelectableItemsHolder(sih);
+        return "simpleProperty";
+    }
+
+    public void testValueModelUpdatesComponent() {
+        TestListDataListener tldl = new TestListDataListener();
+        cb.getModel().addListDataListener(tldl);
+
+        assertEquals(null, cb.getSelectedItem());
+        assertEquals(-1, cb.getSelectedIndex());
+        tldl.assertCalls(0);
+
+        vm.setValue("1");
+        assertEquals("1", cb.getSelectedItem());
+        assertEquals(1, cb.getSelectedIndex());
+        tldl.assertEvent(1, ListDataEvent.CONTENTS_CHANGED, -1, -1);
+
+        vm.setValue("2");
+        assertEquals("2", cb.getSelectedItem());
+        assertEquals(2, cb.getSelectedIndex());
+        tldl.assertEvent(2, ListDataEvent.CONTENTS_CHANGED, -1, -1);
+
+        vm.setValue(null);
+        assertEquals(null, cb.getSelectedItem());
+        assertEquals(-1, cb.getSelectedIndex());
+        tldl.assertEvent(3, ListDataEvent.CONTENTS_CHANGED, -1, -1);
+
+        vm.setValue(null);
+        tldl.assertCalls(3);
+    }
+
+    public void testComponentUpdatesValueModel() {
+        cb.setSelectedIndex(1);
+        assertEquals("1", vm.getValue());
+
+        cb.setSelectedItem("2");
+        assertEquals("2", vm.getValue());
+
+        cb.setSelectedIndex(-1);
+        assertEquals(null, vm.getValue());
+    }
+
+    public void testSelectableValueChangeUpdatesComboBoxModel() {
+        assertEquals("0", cb.getModel().getElementAt(0));
+
+        sih.setValue(new Object[] { "1" });
+        assertEquals("1", cb.getModel().getElementAt(0));
+    }
+
+    public void testComponentTracksEnabledChanges() {
+        assertTrue(cb.isEnabled());
+
+        fm.getFieldMetadata("simpleProperty").setEnabled(false);
+        assertFalse(cb.isEnabled());
+
+        fm.getFieldMetadata("simpleProperty").setEnabled(true);
+        assertTrue(cb.isEnabled());
+    }
+
+    public void testComponentTracksReadOnlyChanges() {
+        assertTrue(cb.isEnabled());
+
+        fm.getFieldMetadata("simpleProperty").setReadOnly(true);
+        assertFalse(cb.isEnabled());
+
+        fm.getFieldMetadata("simpleProperty").setReadOnly(false);
+        assertTrue(cb.isEnabled());
+    }
+
+    public void testSelectableItemHolderNullValue() {
+        ComboBoxBinding binding = new ComboBoxBinding(fm, "simpleProperty");
+        binding.getControl();
+        ValueHolder valueHolder = new ValueHolder();
+        binding.setSelectableItemsHolder(valueHolder);
+        assertEquals(binding.getSelectableItems(), Collections.EMPTY_LIST);
+    }
+
+    public void testExistingModel() {
+        JComboBox cb = new JComboBox(new DefaultComboBoxModel(new Object[] { 
"1", "2", "3" }));
+        ComboBoxBinder binder = new ComboBoxBinder();
+        Binding binding = binder.bind(cb, fm, "simpleProperty", 
Collections.EMPTY_MAP);
+        assertEquals(3, ((JComboBox) 
binding.getControl()).getModel().getSize());
+    }
+
+    public void testFilter() {
+        setUpBinding();
+        Collection selectableItems = cbb.getSelectableItems();
+        assertEquals(Arrays.asList(SELECTABLEITEMS), selectableItems);
+        cbb.setFilter(new Constraint() {
+            public boolean test(Object argument) {
+                return "1".equals(argument) || "4".equals(argument);
+            }
+        });
+        selectableItems = cbb.getSelectableItems();
+        assertEquals(Arrays.asList(new Object[] { "1", "4" }), 
selectableItems);
+    }
+
+    public void testFilterWithContext() {
+        ComboBoxBinder binder = new ComboBoxBinder();
+        Map context = new HashMap();
+        Constraint filter = new Constraint() {
+            public boolean test(Object argument) {
+                return true;
+            }
+        };
+        context.put(ComboBoxBinder.FILTER_KEY, filter);
+        ComboBoxBinding binding = (ComboBoxBinding) binder.bind(fm, 
"simpleProperty", context);
+        assertEquals(filter, binding.getFilter());
+    }
+}
\ No newline at end of file


Property changes on: 
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to