Revision: 1332
Author: mathiasbr
Date: 2006-08-22 06:26:50 -0700 (Tue, 22 Aug 2006)
ViewCVS: http://svn.sourceforge.net/spring-rich-c/?rev=1332&view=rev
Log Message:
-----------
support for empty selection value for combobox bindings
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
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.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-22 09:44:35 UTC (rev 1331)
+++
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinder.java
2006-08-22 13:26:50 UTC (rev 1332)
@@ -33,12 +33,20 @@
public static final String EDITOR_KEY = "editor";
+ /**
+ * context key for a value which is used to mark an empty Selection.
+ * If this value is selected null will be assigned to the fields value
+ */
+ public static final String EMPTY_SELECTION_VALUE = "emptySelectionValue";
+
private ListCellRenderer renderer;
private ComboBoxEditor editor;
+
+ private Object emptySelectionValue;
public ComboBoxBinder() {
- this(null, new String[] { SELECTABLE_ITEMS_KEY, COMPARATOR_KEY,
RENDERER_KEY, EDITOR_KEY, FILTER_KEY });
+ this(null, new String[] { SELECTABLE_ITEMS_KEY, COMPARATOR_KEY,
RENDERER_KEY, EDITOR_KEY, FILTER_KEY, EMPTY_SELECTION_VALUE });
}
public ComboBoxBinder(String[] supportedContextKeys) {
@@ -68,6 +76,11 @@
} else if (editor != null) {
comboBoxBinding.setEditor((ComboBoxEditor) decorate(editor,
comboBoxBinding.getEditor()));
}
+ if (context.containsKey(EMPTY_SELECTION_VALUE)) {
+
comboBoxBinding.setEmptySelectionValue(context.get(EMPTY_SELECTION_VALUE));
+ } else if (emptySelectionValue != null) {
+ comboBoxBinding.setEmptySelectionValue(emptySelectionValue);
+ }
}
protected JComponent createControl(Map context) {
@@ -90,4 +103,11 @@
this.editor = editor;
}
+ public Object getEmptySelectionValue() {
+ return emptySelectionValue;
+ }
+
+ public void setEmptySelectionValue(Object emptySelectionValue) {
+ this.emptySelectionValue = emptySelectionValue;
+ }
}
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-22 09:44:35 UTC (rev 1331)
+++
trunk/spring-richclient/support/src/main/java/org/springframework/richclient/form/binding/swing/ComboBoxBinding.java
2006-08-22 13:26:50 UTC (rev 1332)
@@ -26,7 +26,6 @@
import javax.swing.event.ListDataEvent;
import org.springframework.binding.form.FormModel;
-import org.springframework.binding.value.ValueModel;
import org.springframework.richclient.list.AbstractFilteredListModel;
/**
@@ -36,6 +35,10 @@
*/
public class ComboBoxBinding extends AbstractListBinding {
+ private Object emptySelectionValue;
+
+ private BoundComboBoxModel boundModel;
+
public ComboBoxBinding(FormModel formModel, String formPropertyPath) {
this(new JComboBox(), formModel, formPropertyPath, null);
}
@@ -49,7 +52,8 @@
}
protected void doBindControl(ListModel bindingModel) {
- getComboBox().setModel(new BoundComboBoxModel(bindingModel));
+ boundModel = new BoundComboBoxModel(bindingModel);
+ getComboBox().setModel(boundModel);
}
protected ListModel getDefaultModel() {
@@ -83,6 +87,22 @@
getValueModel().addValueChangeListener(this);
}
+ public int getSize() {
+ if (emptySelectionValue != null) {
+ return super.getSize() + 1;
+ }
+ return super.getSize();
+ }
+
+ public Object getElementAt(int index) {
+ if (emptySelectionValue != null) {
+ if (index == 0)
+ return emptySelectionValue;
+ return super.getElementAt(index - 1);
+ }
+ return super.getElementAt(index);
+ }
+
private boolean updateSelectedItem() {
Object selectedItem = getSelectedItem();
if (selectedItem != null) {
@@ -115,24 +135,43 @@
}
public void setSelectedItem(Object selectedItem) {
+ if (selectedItem == emptySelectionValue) {
+ selectedItem = null;
+ }
getValueModel().setValueSilently(selectedItem, this);
fireContentsChanged(this, -1, -1);
}
public Object getSelectedItem() {
- return getValue();
+ Object value = getValue();
+ if(emptySelectionValue != null && value == null) {
+ return emptySelectionValue;
+ }
+ return value;
}
public void propertyChange(PropertyChangeEvent evt) {
fireContentsChanged(this, -1, -1);
}
+
+ public void emptySelectionValueChanged() {
+ fireContentsChanged(this, -1, -1);
+ }
}
/**
- * @param valueHolder
- * @deprecated use [EMAIL PROTECTED] #setSelectableItems(Object)} instead
+ * @param value
*/
- public void setSelectableItemsHolder(ValueModel valueModel) {
- setSelectableItems(valueModel);
+ public void setEmptySelectionValue(Object value) {
+ if (value != emptySelectionValue && boundModel != null) {
+ emptySelectionValue = value;
+ boundModel.emptySelectionValueChanged();
+ } else {
+ emptySelectionValue = value;
+ }
}
+
+ public Object getEmptySelectionValue() {
+ return emptySelectionValue;
+ }
}
\ No newline at end of file
Modified:
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
===================================================================
---
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
2006-08-22 09:44:35 UTC (rev 1331)
+++
trunk/spring-richclient/support/src/test/java/org/springframework/richclient/form/binding/swing/ComboBoxBindingTests.java
2006-08-22 13:26:50 UTC (rev 1332)
@@ -28,6 +28,7 @@
import javax.swing.JComboBox;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
import org.springframework.binding.value.ValueModel;
import org.springframework.binding.value.support.ValueHolder;
@@ -45,11 +46,15 @@
private JComboBox cb;
+ private TestDataListener testListener;
+
protected String setUpBinding() {
cbb = new ComboBoxBinding(fm, "simpleProperty");
cb = (JComboBox) cbb.getControl();
sih = new ValueHolder(SELECTABLEITEMS);
cbb.setSelectableItems(sih);
+ testListener = new TestDataListener();
+ cb.getModel().addListDataListener(testListener);
return "simpleProperty";
}
@@ -201,7 +206,7 @@
testConstraint.testCalled = 0;
testConstraint.setFilterValues(new Object[] { "2" });
- //assertEquals(SELECTABLEITEMS.length, testConstraint.testCalled);
+ // assertEquals(SELECTABLEITEMS.length, testConstraint.testCalled);
assertEquals(testConstraint.filterValues.length, model.getSize());
assertEquals("2", model.getElementAt(0));
}
@@ -238,6 +243,42 @@
assertEquals("4", bindingModel.getElementAt(4));
}
+ public void testEmptySelectionValue() throws Exception {
+ ComboBoxModel model = cb.getModel();
+ int modelSize = model.getSize();
+ testListener.contentsChanged = null;
+ testListener.intervalAdded = null;
+ testListener.intervalRemoved = null;
+ String emptyValue = "select a Value";
+ cbb.setEmptySelectionValue(emptyValue);
+ assertEquals(modelSize + 1, model.getSize());
+ assertEquals(emptyValue, model.getElementAt(0));
+ assertNotNull(testListener.contentsChanged);
+ assertNull(testListener.intervalAdded);
+ assertNull(testListener.intervalRemoved);
+ cb.setSelectedItem(SELECTABLEITEMS[0]);
+ assertEquals(SELECTABLEITEMS[0], model.getSelectedItem());
+ assertEquals(SELECTABLEITEMS[0], vm.getValue());
+ cb.setSelectedItem(emptyValue);
+ assertEquals(emptyValue, model.getSelectedItem());
+ assertEquals(null, vm.getValue());
+ cb.setSelectedItem(null);
+ assertEquals(emptyValue, model.getSelectedItem());
+ assertEquals(null, vm.getValue());
+
+ cb.setSelectedItem(emptyValue);
+ testListener.contentsChanged = null;
+ testListener.intervalAdded = null;
+ testListener.intervalRemoved = null;
+ cbb.setEmptySelectionValue(null);
+ assertNotNull(testListener.contentsChanged);
+ assertNull(testListener.intervalAdded);
+ assertNull(testListener.intervalRemoved);
+ assertEquals(modelSize, model.getSize());
+ assertEquals(SELECTABLEITEMS[0], model.getElementAt(0));
+ assertNull(vm.getValue());
+ }
+
private static class TestConstraint extends Observable implements
Constraint {
Object[] filterValues = new Object[] { "1", "4" };
@@ -258,4 +299,26 @@
notifyObservers();
}
}
+
+ private class TestDataListener implements ListDataListener {
+
+ private ListDataEvent contentsChanged;
+
+ private ListDataEvent intervalAdded;
+
+ private ListDataEvent intervalRemoved;
+
+ public void contentsChanged(ListDataEvent e) {
+ contentsChanged = e;
+ }
+
+ public void intervalAdded(ListDataEvent e) {
+ intervalAdded = e;
+ }
+
+ public void intervalRemoved(ListDataEvent e) {
+ intervalRemoved = e;
+ }
+
+ }
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
spring-rich-c-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs