Skip to main content

[patch] more binding fixes

4 replies [Last post]
Anonymous

Hi

More fixes to the binding stuff

- JavaBeanDataModel - complete the primitives stuff I started
- ComboBoxBinding - make it honour nullability so that the user can
select "nothing".

Regards,
Noel Grandin (jdnc_username: grandinj, JCA filed)

NOTICE: Please note that this email, and the contents thereof,
are subject to the standard Peralex email disclaimer, which may
be found at: http://www.peralex.com/disclaimer.html

If you cannot access the disclaimer through the URL attached
and you wish to receive a copy thereof please send
an email to email@peralex.com
? bin
Index: src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java
===================================================================
RCS file: /cvs/databinding/src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java,v
retrieving revision 1.3
diff -u -r1.3 JavaBeanDataModel.java
--- src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java 10 May 2005 10:47:10 -0000 1.3
+++ src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java 12 May 2005 07:10:10 -0000
@@ -41,6 +41,12 @@
private static final Map primitivesToBoxed = new HashMap();
static {
primitivesToBoxed.put(Integer.TYPE, Integer.class);
+ primitivesToBoxed.put(Long.TYPE, Long.class);
+ primitivesToBoxed.put(Short.TYPE, Short.class);
+ primitivesToBoxed.put(Byte.TYPE, Byte.class);
+ primitivesToBoxed.put(Float.TYPE, Float.class);
+ primitivesToBoxed.put(Double.TYPE, Double.class);
+ primitivesToBoxed.put(Boolean.TYPE, Boolean.class);
}

public JavaBeanDataModel(Class beanClass) throws IntrospectionException {
@@ -78,7 +84,8 @@
|| prop.getPropertyType()==Short.TYPE
|| prop.getPropertyType()==Byte.TYPE
|| prop.getPropertyType()==Float.TYPE
- || prop.getPropertyType()==Double.TYPE)
+ || prop.getPropertyType()==Double.TYPE
+ || prop.getPropertyType()==Boolean.TYPE)
{
// convert the primitive types to their boxed types
// this makes the binding code happier in
Index: src/java/org/jdesktop/binding/impl/swing/ComboBoxBinding.java
===================================================================
RCS file: /cvs/databinding/src/java/org/jdesktop/binding/impl/swing/ComboBoxBinding.java,v
retrieving revision 1.2
diff -u -r1.2 ComboBoxBinding.java
--- src/java/org/jdesktop/binding/impl/swing/ComboBoxBinding.java 6 May 2005 13:06:07 -0000 1.2
+++ src/java/org/jdesktop/binding/impl/swing/ComboBoxBinding.java 6 May 2005 15:13:50 -0000
@@ -11,6 +11,8 @@
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
+import java.util.Arrays;
+import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
@@ -30,7 +32,15 @@
* @version 1.0
*/
public class ComboBoxBinding extends AbstractBinding {
+
private JComboBox comboBox;
+
+ /**
+ * this is used as a "marker object" for selecting nothing
+ * It is public so that custom combobox renderers can also check for it.
+ */
+ public static final String EMPTY_SELECTION = new String(" ");
+
/* Note: we cannot support binding to any component with a ComboBoxModel
* because ComboBoxModel fires no event when the value changes!
* JW: ??
@@ -74,7 +84,14 @@
}

protected void updateComboBoxModel(Object[] items) {
- DefaultComboBoxModel model = new DefaultComboBoxModel(items);
+ final DefaultComboBoxModel model;
+ if (metaData.getMinValueCount()==0) { // nullable
+ Vector v = new Vector(Arrays.asList(items));
+ v.add(0, EMPTY_SELECTION);
+ model = new DefaultComboBoxModel(v);
+ } else {
+ model = new DefaultComboBoxModel(items);
+ }
model.setSelectedItem(getComponentValue());
// JW: brute force
comboBox.setModel(model);
@@ -97,11 +114,20 @@
}

protected Object getComponentValue(){
- return comboBox.getSelectedItem();
+ final Object obj = comboBox.getSelectedItem();
+ if (metaData.getMinValueCount()==0 && obj==EMPTY_SELECTION) {
+ return null;
+ } else {
+ return obj;
+ }
}

protected void setComponentValue(Object value) {
- comboBox.getModel().setSelectedItem(value);
+ if (value==null && metaData.getMinValueCount()==0) {
+ comboBox.getModel().setSelectedItem(EMPTY_SELECTION);
+ } else {
+ comboBox.getModel().setSelectedItem(value);
+ }
}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kleopatra

Hi Noel,

late but...

>
> More fixes to the binding stuff
>
> - JavaBeanDataModel - complete the primitives stuff I started

okay - I added the patch (and fixed it - it falsely handled boolean
primitives like numbers)

At the same time I took the opportunity to refactor a bit (the tests
don't cover much of the behaviour but seem to indicate that nothing's
broken by doing so).

A deeper change is to make the model listen to change notification from
the underlying bean and report them as value changes (needed it for the
decorator demo - I'm ever so lazy to do any binding manually ). This
is not tested extensively - if there are any problems please let me know.

> - ComboBoxBinding - make it honour nullability so that the user can
> select "nothing".
>

Hmmm ... I agree that it's a very good requirement to add, only I don't
really like the idea of some spaces as marker. Will try to think about
alternatives (why not use a null instead? Just asking - I faintly
remember problems when doing so, but forgot what they were)

Thanks
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

Noel Grandin

Hi

Just got back from a 6 week vacation, so this might be a little late....

Thanks for adding the patches.

I tried using null, but that causes problems with the standard Swing
JComboBox - I can't remember exactly what where it was, but one of the
APIs in the Swing stuff treated null as "don't do anything" rather than
"this is a new value to be displayed".

So I resorted to a "marker" object.
I can't see any other way of doing this - the marker object could be
hidden by defining a static "isSelectedNull()" method, which might
improve the usability a little.

Cheers,
Noel

Kleopatra wrote:

> Hi Noel,
>
> late but...
>
>>
>> More fixes to the binding stuff
>>
>> - JavaBeanDataModel - complete the primitives stuff I started
>
>
> okay - I added the patch (and fixed it - it falsely handled boolean
> primitives like numbers)
>
> At the same time I took the opportunity to refactor a bit (the tests
> don't cover much of the behaviour but seem to indicate that nothing's
> broken by doing so).
>
> A deeper change is to make the model listen to change notification
> from the underlying bean and report them as value changes (needed it
> for the decorator demo - I'm ever so lazy to do any binding manually
> ). This is not tested extensively - if there are any problems
> please let me know.
>
>> - ComboBoxBinding - make it honour nullability so that the user can
>> select "nothing".
>>
>
> Hmmm ... I agree that it's a very good requirement to add, only I
> don't really like the idea of some spaces as marker. Will try to think
> about alternatives (why not use a null instead? Just asking - I
> faintly remember problems when doing so, but forgot what they were)
>
> Thanks
> Jeanette
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
>

NOTICE: Please note that this email, and the contents thereof,
are subject to the standard Peralex email disclaimer, which may
be found at: http://www.peralex.com/disclaimer.html

If you cannot access the disclaimer through the URL attached
and you wish to receive a copy thereof please send
an email to email@peralex.com

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

Kleopatra

Noel Grandin wrote:
>
> Just got back from a 6 week vacation, so this might be a little late....
>

lucky you :-) I have no excuse for not responding more timely - somehow
simply overlooked this, sorry.

>
> I tried using null, but that causes problems with the standard Swing
> JComboBox - I can't remember exactly what where it was, but one of the
> APIs in the Swing stuff treated null as "don't do anything" rather than
> "this is a new value to be displayed".
>

hmm .. can't really reproduce it. See f.i. the table demo in the
swingxdemo: the combo for choosing the highlighters is bound to a list
of the available highlighters, the first value is null. Choosing the
null correctly sets the null highlighter. But some LFs seem to have a
problem to render the null - it's only a very small, barely recognizable
line on the top of the drop-down (which crazyly sometimes shows
correctly). Without having dug into it I would guess it to be a view
problem, not a functional.. Will search the bug parade for it, maybe
something turns up.

Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

Noel Grandin

Hi

Kleopatra wrote:

> Noel Grandin wrote:
>
>> I tried using null, but that causes problems with the standard Swing
>> JComboBox - I can't remember exactly what where it was, but one of
>> the APIs in the Swing stuff treated null as "don't do anything"
>> rather than "this is a new value to be displayed".
>>
>
Found it - ComboBoxModel assumes that calling setSelectedItem(null)
means "set to nothing selected".

> hmm .. can't really reproduce it. See f.i. the table demo in the
> swingxdemo: the combo for choosing the highlighters is bound to a list
> of the available highlighters, the first value is null. Choosing the
> null correctly sets the null highlighter. But some LFs seem to have a
> problem to render the null - it's only a very small, barely
> recognizable line on the top of the drop-down (which crazyly sometimes
> shows correctly). Without having dug into it I would guess it to be a
> view problem, not a functional.. Will search the bug parade for it,
> maybe something turns up.
>
Ah yes, I see what you mean. The problem is that
javax.swing.DefaultListCellRenderer#getListCellRendererComponent
attempts to render an empty string.

It could be fixed by making it render a string with one space, or by
overriding the default renderer in the combobox.

However, because of the assumption built into ComboBoxModel, I think
this is going to be inherently fragile, and we're going to need to use
some kind of marker object.
Choice of 2 evils I guess :-(

Regards,
Noel Grandin

NOTICE: Please note that this email, and the contents thereof,
are subject to the standard Peralex email disclaimer, which may
be found at: http://www.peralex.com/disclaimer.html

If you cannot access the disclaimer through the URL attached
and you wish to receive a copy thereof please send
an email to email@peralex.com

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net