View Full Version : PropertyEditor example
snpe
Nov 8th, 2004, 08:17 AM
Hello,
Is ther example for property editor.Component like integer, number, date are often and we need default PropertyEditor for this
regards
pdbruycker
Nov 8th, 2004, 09:13 AM
the userdocumentation contains an entry about registering a propertyeditor for a date property.
http://opensource.atlassian.com/confluence/spring/display/RCP/Forms+Support#FormsSupport-PropertyEditors
Peter
snpe
Nov 8th, 2004, 09:34 AM
Peter,
I see documentation.I want example PropertyEditor for integer, number and date - not registering PropertyEditor
Thanks
steve_smith
Nov 8th, 2004, 10:45 AM
I want example PropertyEditor for integer, number and date
I do agree with snipe. I like examples too.
cyboc
Nov 8th, 2004, 12:57 PM
Peter,
I see documentation.I want example PropertyEditor for integer, number and date - not registering PropertyEditor
Thanks
Here is a very simplistic (I'm admitting this is simplistic so no flames please) PropertyEditor for Integer:
package com.mycompany.propertyeditors;
import org.springframework.beans.propertyeditors.CustomNu mberEditor;
public class CustomIntegerEditor extends CustomNumberEditor
{
public CustomIntegerEditor()
{
super(Integer.class, true);
}
}
Unfortunately, if you are using an AsYouTypeTextValueSetter (the default on forms, I think) you get a weird validation message if you first type "-" when you are trying to enter a negative Integer. My cheesy, inelegant fix for that is to override setAsText in the above class like this:
package com.mycompany.propertyeditors;
import org.springframework.beans.propertyeditors.CustomNu mberEditor;
public class CustomIntegerEditor extends CustomNumberEditor
{
public CustomIntegerEditor()
{
super(Integer.class, true);
}
public void setAsText(String text) throws IllegalArgumentException
{
// Temporary hack to find unfriendly error message when you enter
// "-" while trying to enter a negative number on a Form. Unfriendly
// message looks like "<my property name> org.springframework.
// binding.form.support.
// ValidatingFormModel$ValueSetterConstraint@ed65e0."
if (text.trim().equals("-"))
{
text = "";
}
super.setAsText(text);
}
}
That fixes the "-" problem but you still get an unfriendly message if you type in something non-numeric like a letter (e.g. "A", "B", "C"...in case you don't know what a letter is...:lol:). So, there must be a better solution than my cheesy example. For example, I think you could override getCustomEditor() to return a JFormattedTextField(). I haven't tried it yet. Does anybody have an example of this for Integers?
pdbruycker
Nov 8th, 2004, 01:13 PM
sorry, shouldn't have posted that quick :oops:
haven't tried with JFormattedTextFields yet, so I'm not able to help you there :cry:
cyboc
Nov 8th, 2004, 01:22 PM
Hmn. I don't know if JFormattedTextField will work if you are using an AsYouTypeTextValueSetter. Here's a quote from the Javadocs for JFormattedTextField: "JFormattedTextField allows configuring what action should be taken when focus is lost." In other words, I think JFormattedTextField's services are only activated once focus is lost, which doesn't help for AsYouTypeTextValueSetter.
I don't really want to give up using AsYouTypeTextValueSetter because I think that is a cool feature. Perhaps there is a better solution than JFormattedTextField?
cyboc
Nov 8th, 2004, 02:38 PM
I tried fiddling with the code at http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html
to find out what JFormattedTextField can do. As I suspected, it only really does something once a focus lost event is fired. The exception is if you attach a javax.swing.text.MaskFormatter to the JFormattedTextField. MaskFormatter will do some "as you type" checking (i.e. if you have a mask like "###" it will only let you type in numbers). However, MaskFormatter is very primitive. For example, it doesn't really deal with optional characters (for example, first character could be "-" if you are entering a negative number ) or variable length data (for example, you could enter say 1, 10, or 100 for a "percentage" field). IMHO, MaskFormatter and JFormattedTextField just won't work with "as you type" checking.
A better solution might be to extend regex constraints (org.springframework.rules.constraint.RegexpConstr aint) to support Integer properties instead of just String properties, as suggested by steve_smith in this topic:
http://forum.springframework.org/showthread.php?t=11220
Note that in that topic someone suggested using JFormattedTextField. However, as pointed out above, that probably won't work for "as you type" checking.
Comments and/or help would be appreciated.
cyboc
Nov 8th, 2004, 02:52 PM
A better solution might be to extend regex constraints (org.springframework.rules.constraint.RegexpConstr aint) to support Integer properties instead of just String properties.
Hmn, upon looking at some of the Spring RCP code, this might involve quite a bit of refactoring. Currently it looks like the "argument" parameter to RegexpConstraint.test(Object argument) is converted to the type of the bound form property BEFORE test() is called. For example, for an Integer property, I think "argument" is an Integer. However, I think for this idea to work, the conversion of the form field's String data to an Integer would need to occur AFTER test() is called. I don't know for sure, but this could require lots of refactoring. But I'm sure Super Keith is up to it! :D Maybe I'll look at code when I get time.
Keith Donald
Nov 8th, 2004, 03:38 PM
Hmmm, well, right now I stand by the position that validation happens after type conversion. So you're right, that belief is built into the framework, and a depature from that would require refactoring.
So, in the case of a regexp match, you should be dealing with a property that is a intrinsically a string, not an integer. I don't think it makes sense to apply a regexp constraint to an integer: use range or between instead.
Thanks for all the notes on the reporting system bugs, btw. I will be taking care of these. We'll be on our way to a first release when reporting and docking/multiple views per page (and likely preferences, too) are ready to go.
As far as JFormattedTextField, I found it a pain in the arse to work with. It's ridiculously difficult to extend. What I wanted to be able to do was combine it's cool "visual policies" for things like restricting invalid character input, with the validation system for driving the actual validation (with configurable 'when-to-validate' policies). This was really kludgy to do. To get AsYouType validation, I just subscribe as a document listener to the underlying text field. You can also listen for "value" property change events on the JFormattedTextField when the formatter is configured to commit on every edit, but I found this difficult.
Keith
cyboc
Nov 8th, 2004, 03:58 PM
Thanks for all the notes on the reporting system bugs, btw.
No problem. I'll keep them coming if it helps.
We'll be on our way to a first release when reporting and docking/multiple views per page (and likely preferences, too) are ready to go.
Cool! :D
To get AsYouType validation, I just subscribe as a document listener to the underlying text field.
If possible, could you please post an example of this for an Integer property that can be positive or negative?
snpe
Nov 20th, 2004, 09:04 AM
See my message in rcp-dev (RevertAsYouTypeTextValueSetter), please
regards
cyboc
Nov 22nd, 2004, 09:51 AM
See my message in rcp-dev (RevertAsYouTypeTextValueSetter), please
Thanks, but where is rcp-dev?
jdigger
Nov 22nd, 2004, 10:40 AM
snpe meant to refer you to http://sourceforge.net/mailarchive/forum.php?thread_id=5999930&forum_id=39905
snpe
Nov 22nd, 2004, 11:50 AM
Jdigger,
Are you try ?
regards
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.