Skip to main content

[patch] binding

7 replies [Last post]
Anonymous

Username: grandinj, JCA submitted
Against current cvs.dev.java.net/cvs/databinding HEAD.

Add check in AbstractBinding to make finding mistakes easier.

Add methods in DefaultDataModel for sorting of fields.

Improve handling of number fields in JavaBeanDataModel.

Fix SpinnerBinding to respect readonly metadata.

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/AbstractBinding.java
===================================================================
RCS file: /cvs/databinding/src/java/org/jdesktop/binding/impl/AbstractBinding.java,v
retrieving revision 1.1
diff -u -r1.1 AbstractBinding.java
--- src/java/org/jdesktop/binding/impl/AbstractBinding.java 14 Mar 2005 21:45:01 -0000 1.1
+++ src/java/org/jdesktop/binding/impl/AbstractBinding.java 14 Apr 2005 09:36:12 -0000
@@ -381,6 +381,7 @@
this.dataModel = dataModel;
this.fieldName = fieldName;
metaData = dataModel.getMetaData(fieldName);
+ checkNull(metaData, "Field " + fieldName + " does not exist in metadata");
installDataModelListener();
installMetaDataListener();
}
Index: src/java/org/jdesktop/binding/impl/DefaultDataModel.java
===================================================================
RCS file: /cvs/databinding/src/java/org/jdesktop/binding/impl/DefaultDataModel.java,v
retrieving revision 1.1
diff -u -r1.1 DefaultDataModel.java
--- src/java/org/jdesktop/binding/impl/DefaultDataModel.java 14 Mar 2005 21:45:01 -0000 1.1
+++ src/java/org/jdesktop/binding/impl/DefaultDataModel.java 14 Apr 2005 09:39:30 -0000
@@ -8,9 +8,10 @@
package org.jdesktop.binding.impl;

import java.util.ArrayList;
-import java.util.List;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
-import org.jdesktop.binding.*;
+
import org.jdesktop.binding.metadata.MetaData;

@@ -80,6 +81,24 @@
values.put(fieldName, value);
}

+
+ /**
+ * Sort the fields. This is useful because the UI display is sometimes dependent on the order of the fields.
+ */
+ public void sortFields(Comparator comparator) {
+ Collections.sort(fieldNames, comparator);
+ }
+
+ /**
+ * Sort the fields. This is useful because the UI display is sometimes dependent on the order of the fields.
+ * Note: this is a lexicographical sort
+ * @see String#compareTo
+ */
+ public void sortFields() {
+ Collections.sort(fieldNames);
+ }
+
+
public int getRecordCount() {
return 1;
}
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.2
diff -u -r1.2 JavaBeanDataModel.java
--- src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java 22 Mar 2005 08:52:19 -0000 1.2
+++ src/java/org/jdesktop/binding/impl/JavaBeanDataModel.java 14 Apr 2005 09:41:09 -0000
@@ -8,13 +8,15 @@
package org.jdesktop.binding.impl;

import java.beans.BeanInfo;
-import java.beans.Introspector;
import java.beans.IntrospectionException;
+import java.beans.Introspector;
import java.beans.PropertyDescriptor;
-
import java.lang.reflect.Method;
-import org.jdesktop.binding.impl.DefaultDataModel;
+import java.util.HashMap;
+import java.util.Map;
+
import org.jdesktop.binding.metadata.MetaData;
+import org.jdesktop.binding.metadata.NumberMetaData;

/**
* A class that creates a collection of MetaData based BeanInfo
@@ -36,10 +38,19 @@
private Class beanClass;
private Object bean; // current bean instance

+ private static final Map primitivesToBoxed = new HashMap();
+ static {
+ primitivesToBoxed.put(Integer.TYPE, Integer.class);
+ }
+
public JavaBeanDataModel(Class beanClass) throws IntrospectionException {
this(beanClass, null);
}

+ public JavaBeanDataModel(Object bean) throws IntrospectionException {
+ this(bean.getClass(), bean);
+ }
+
/**
* Constructs a JavaBeanDataModel by introspecting on the class and using the data from
* the object as the current bean
@@ -55,9 +66,34 @@
//
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; i++) {
- addField(new MetaData(props[i].getName(),
- props[i].getPropertyType(),
- props[i].getDisplayName()));
+ PropertyDescriptor prop = props[i];
+ if (Number.class.isAssignableFrom(prop.getPropertyType())) {
+ NumberMetaData metadata = new NumberMetaData(
+ prop.getName(),
+ prop.getPropertyType(),
+ prop.getDisplayName());
+ addField(metadata);
+ } else if (prop.getPropertyType()==Integer.TYPE
+ || prop.getPropertyType()==Long.TYPE
+ || prop.getPropertyType()==Short.TYPE
+ || prop.getPropertyType()==Byte.TYPE
+ || prop.getPropertyType()==Float.TYPE
+ || prop.getPropertyType()==Double.TYPE)
+ {
+ // convert the primitive types to their boxed types
+ // this makes the binding code happier in
+ // AbstractBinding#convertToModelType
+ NumberMetaData metadata = new NumberMetaData(
+ prop.getName(),
+ primitivesToBoxed.get(prop.getPropertyType()),
+ prop.getDisplayName());
+ metadata.setRequired(true);
+ addField(metadata);
+ } else {
+ addField(new MetaData(prop.getName(), prop
+ .getPropertyType(), prop
+ .getDisplayName()));
+ }
}
}

@@ -118,7 +154,7 @@
writeMethod.invoke(getJavaBean(), new Object[] {value});
}
catch (Exception ex) {
- // XXX excecption for illegal access, etc..
+ // XXX exception for illegal access, etc..
ex.printStackTrace();
}
}
Index: src/java/org/jdesktop/binding/impl/swing/SpinnerBinding.java
===================================================================
RCS file: /cvs/databinding/src/java/org/jdesktop/binding/impl/swing/SpinnerBinding.java,v
retrieving revision 1.1
diff -u -r1.1 SpinnerBinding.java
--- src/java/org/jdesktop/binding/impl/swing/SpinnerBinding.java 14 Mar 2005 21:45:06 -0000 1.1
+++ src/java/org/jdesktop/binding/impl/swing/SpinnerBinding.java 14 Apr 2005 09:38:35 -0000
@@ -7,14 +7,16 @@

package org.jdesktop.binding.impl.swing;

-import org.jdesktop.binding.DataModel;
-import org.jdesktop.binding.metadata.MetaData;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
+
+import org.jdesktop.binding.DataModel;
import org.jdesktop.binding.impl.AbstractBinding;

/**
@@ -57,8 +59,26 @@

protected void setComponent(JComponent component) {
this.component = component;
+ configureEditability();
}

+ protected void configureEditability() {
+ if (!(component instanceof JSpinner)) return;
+ JSpinner spinnerComponent = (JSpinner) component;
+ spinnerComponent.setEnabled(!metaData.isReadOnly());
+ }
+
+ protected void installMetaDataListener() {
+ PropertyChangeListener l = new PropertyChangeListener() {
+ public void propertyChange(PropertyChangeEvent evt) {
+ if ("readOnly".equals(evt.getPropertyName())) {
+ configureEditability();
+ }
+ }
+ };
+ metaData.addPropertyChangeListener(l);
+ }
+
protected Object getComponentValue(){
return spinnerModel.getValue();
}

---------------------------------------------------------------------
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.
Noel Grandin

Kleopatra wrote:

> DefaultDataModel - is there really a common need to sort the fields?
> Just asking, because personally I never ran across it. If it's needed,
> then it probably should be delegated to the (Default)MetaDataProvider
> probably which has to guarantee the - not-yet-existing - change
> notification.
>
Yes, because the order in the data model is the order that JForm will
use to render the fields. When you have objects with large numbers of
properties, the visual order is important.

> AbstractBinding - I agree that the check for non-null metaData is
> principally a correct thing to do. The reason we don't do it right now
> is because DirectTableBinding is slightly bending the DataModel
> contract (binds to the DataModel instead of to one of its fields). Hmm
> ...
>
This was intended as a "find problem fast" thing because without it, a
small typo takes a while to track down.

Thanks,
Noel

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:

>>DefaultDataModel - is there really a common need to sort the fields?
>>Just asking, because personally I never ran across it. If it's needed,
>>then it probably should be delegated to the (Default)MetaDataProvider
>>probably which has to guarantee the - not-yet-existing - change
>>notification.
>>
>
> Yes, because the order in the data model is the order that JForm will
> use to render the fields. When you have objects with large numbers of
> properties, the visual order is important.
>

Hmm, what I don't understand is: how comes that the visual order is
related to the lexical order of the fieldnames?

>
> This was intended as a "find problem fast" thing because without it, a
> small typo takes a while to track down.

yeah, that's a good point. Shouldn't it be solved nearer to the root?
Like make DataModel throw an Exception if a client tries to access a
field that's not contained in the array of fieldnames?

Jeanette

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

Noel Grandin

Kleopatra wrote:

> Noel Grandin wrote:
>
>>> DefaultDataModel - is there really a common need to sort the fields?
>>> Just asking, because personally I never ran across it. If it's needed,
>>
>> Yes, because the order in the data model is the order that JForm will
>> use to render the fields. When you have objects with large numbers of
>> properties, the visual order is important.
>>
>
> Hmm, what I don't understand is: how comes that the visual order is
> related to the lexical order of the fieldnames?
>
It doesn't have to be - that's why I created a sort method that takes a
Comparator, so that the client can decide how to sort the fields.
Perhaps this is better solved by setting a Comparator on JForm, and
making JForm sort the fields before display, since JForm is really the
only piece that needs to know about ordering of fields.

>>
>> This was intended as a "find problem fast" thing because without it,
>> a small typo takes a while to track down.
>
>
> yeah, that's a good point. Shouldn't it be solved nearer to the root?
> Like make DataModel throw an Exception if a client tries to access a
> field that's not contained in the array of fieldnames?
>
Makes sense to me.

Let me know if you'd like a new patch with these changes.

Regards,
Noel

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

rbair
Offline
Joined: 2003-07-08

Hey noel,

I didn't want this to get lost -- I was waiting for Jeanette to get back from vacation to take a look at these. We'll get to them soon. (and the other message's as well).

Thanks
Richard

Noel Grandin

Hi

Good to hear, I was beginning to think this was a patch-unfriendly project!

Thanks,
Noel

jdnc-interest@javadesktop.org wrote:

>Hey noel,
>
>I didn't want this to get lost -- I was waiting for Jeanette to get back from vacation to take a look at these. We'll get to them soon. (and the other message's as well).
>
>Thanks
>Richard
>---
>[Message sent by forum member 'rbair' (Richard Bair)]
>
>http://www.javadesktop.org/forums/thread.jspa?messageID=73832
>
>---------------------------------------------------------------------
>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:

>
> Good to hear, I was beginning to think this was a patch-unfriendly project!
>

we try not to be ;-)

Concerning your patches:

DefaultDataModel - is there really a common need to sort the fields?
Just asking, because personally I never ran across it. If it's needed,
then it probably should be delegated to the (Default)MetaDataProvider
probably which has to guarantee the - not-yet-existing - change
notification.

AbstractBinding - I agree that the check for non-null metaData is
principally a correct thing to do. The reason we don't do it right now
is because DirectTableBinding is slightly bending the DataModel contract
(binds to the DataModel instead of to one of its fields). Hmm ...

Will commit the other two changes soon, thanks.

Jeanette

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

Kleopatra

Kleopatra wrote:
> is because DirectTableBinding is slightly bending the DataModel contract

oops, should read "Binding contract"

Jeanette

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