Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditor example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditor example

    Hello,
    Is ther example for property editor.Component like integer, number, date are often and we need default PropertyEditor for this

    regards

  • #2
    the userdocumentation contains an entry about registering a propertyeditor for a date property.

    http://opensource.atlassian.com/conf...ropertyEditors

    Peter

    Comment


    • #3
      Peter,
      I see documentation.I want example PropertyEditor for integer, number and date - not registering PropertyEditor

      Thanks

      Comment


      • #4
        Originally posted by snpe
        I want example PropertyEditor for integer, number and date
        I do agree with snipe. I like examples too.

        Comment


        • #5
          Originally posted by snpe
          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:
          Code:
          package com.mycompany.propertyeditors;
          
          import org.springframework.beans.propertyeditors.CustomNumberEditor;
          
          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:

          Code:
          package com.mycompany.propertyeditors;
          
          import org.springframework.beans.propertyeditors.CustomNumberEditor;
          
          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 &#40;text.trim&#40;&#41;.equals&#40;"-"&#41;&#41;
              &#123;
                text = "";
              &#125;
              
              super.setAsText&#40;text&#41;;
            &#125;
          &#125;
          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?

          Comment


          • #6
            sorry, shouldn't have posted that quick :oops:

            haven't tried with JFormattedTextFields yet, so I'm not able to help you there :cry:

            Comment


            • #7
              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?

              Comment


              • #8
                I tried fiddling with the code at http://java.sun.com/docs/books/tutor...textfield.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.
                Last edited by robyn; May 14th, 2006, 11:22 AM.

                Comment


                • #9
                  Originally posted by cyboc
                  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! Maybe I'll look at code when I get time.

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      Originally posted by kdonald
                      Thanks for all the notes on the reporting system bugs, btw.
                      No problem. I'll keep them coming if it helps.

                      Originally posted by kdonald
                      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!

                      Originally posted by kdonald
                      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?

                      Comment


                      • #12
                        See my message in rcp-dev (RevertAsYouTypeTextValueSetter), please

                        regards

                        Comment


                        • #13
                          Originally posted by snpe
                          See my message in rcp-dev (RevertAsYouTypeTextValueSetter), please
                          Thanks, but where is rcp-dev?

                          Comment


                          • #14
                            snpe meant to refer you to http://sourceforge.net/mailarchive/f...forum_id=39905

                            Comment


                            • #15
                              Jdigger,
                              Are you try ?

                              regards

                              Comment

                              Working...
                              X