You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by anton dos santos <ad...@free.fr> on 2011/03/06 10:11:54 UTC

TextInput and Validator issues

Hi
I am trying to use a TexInput with an IntValidator in a TableViewRowEditor but I can't get it to do 
what I want. I'd like to have an input field where user can enter only digits and the input field 
can be empty (in that case background can become red if input is mandatory or stay white).

When I set StrictValidation="true", then it is impossible to have an empty InputField; user can't do 
following : erase current value in input and enter a new value. User has to enter the new value and 
then remove the old, which is not intuitive (at least to me).

Then I tried StrictValidation="false"; it seems to work, but user can also enter characters (which 
is not what I want) and when he hits return key to end editing, an exception is thrown:
java.lang.NumberFormatException: For input string: "145jhk"
         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
         at java.lang.Long.parseLong(Long.java:419)
         at java.lang.Long.parseLong(Long.java:468)
         at org.apache.pivot.beans.BeanAdapter.coerce(BeanAdapter.java:897)
         at org.apache.pivot.beans.BeanAdapter.put(BeanAdapter.java:286)
         at org.apache.pivot.json.JSON.put(JSON.java:152)
         at org.apache.pivot.wtk.TextInput.store(TextInput.java:841)
         at org.apache.pivot.wtk.Container.store(Container.java:613)
         at org.apache.pivot.wtk.content.TableViewRowEditor.endEdit(TableViewRowEditor.java:254)
Here, maybe TextInput#store() shoudn't be invoked when TextInput#isTextValid() == false ?

I also tried StrictValidation="true" and an intValidator that accepts empty input by overriding 
isValid():
     public boolean isValid(String text) {
         final ParsePosition pos = new ParsePosition(0);
         Object obj = format.parseObject(text, pos);
         if( obj == null || text.length() == 0) {
           return true;
         }

         // the text is only valid is we successfully parsed ALL of it. Don't want trailing bits of
         // not-valid text.
         return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == text.length();
     }
this works fine until user erases everything in the TextInput, from then on use can enter any 
character, he is no longer limited to digits.

What else could I try ?

Regards
Anton

Re: TextInput and Validator issues

Posted by Chris Bartlett <cb...@gmail.com>.
Anton,

I was just searching to find a similar mailing list post that came up a
while ago, and realised that it was also by you, but I didn't end up sending
my draft reply on Feb 1st.  Sorry about that.

Anyway, this is something that I will also need in the near future, so I
promise to get back to you as soon as I have looked at it.

Chris

On 6 March 2011 16:11, anton dos santos <ad...@free.fr> wrote:

> Hi
> I am trying to use a TexInput with an IntValidator in a TableViewRowEditor
> but I can't get it to do what I want. I'd like to have an input field where
> user can enter only digits and the input field can be empty (in that case
> background can become red if input is mandatory or stay white).
>
> When I set StrictValidation="true", then it is impossible to have an empty
> InputField; user can't do following : erase current value in input and enter
> a new value. User has to enter the new value and then remove the old, which
> is not intuitive (at least to me).
>
> Then I tried StrictValidation="false"; it seems to work, but user can also
> enter characters (which is not what I want) and when he hits return key to
> end editing, an exception is thrown:
> java.lang.NumberFormatException: For input string: "145jhk"
>        at
> java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
>        at java.lang.Long.parseLong(Long.java:419)
>        at java.lang.Long.parseLong(Long.java:468)
>        at org.apache.pivot.beans.BeanAdapter.coerce(BeanAdapter.java:897)
>        at org.apache.pivot.beans.BeanAdapter.put(BeanAdapter.java:286)
>        at org.apache.pivot.json.JSON.put(JSON.java:152)
>        at org.apache.pivot.wtk.TextInput.store(TextInput.java:841)
>        at org.apache.pivot.wtk.Container.store(Container.java:613)
>        at
> org.apache.pivot.wtk.content.TableViewRowEditor.endEdit(TableViewRowEditor.java:254)
> Here, maybe TextInput#store() shoudn't be invoked when
> TextInput#isTextValid() == false ?
>
> I also tried StrictValidation="true" and an intValidator that accepts empty
> input by overriding isValid():
>    public boolean isValid(String text) {
>        final ParsePosition pos = new ParsePosition(0);
>        Object obj = format.parseObject(text, pos);
>        if( obj == null || text.length() == 0) {
>          return true;
>        }
>
>        // the text is only valid is we successfully parsed ALL of it. Don't
> want trailing bits of
>        // not-valid text.
>        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() ==
> text.length();
>    }
> this works fine until user erases everything in the TextInput, from then on
> use can enter any character, he is no longer limited to digits.
>
> What else could I try ?
>
> Regards
> Anton
>

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
I was going to suggest something like this, though it seems like it might be better to make this configurable in the stock validator.

On Mar 6, 2011, at 2:26 PM, Chris Bartlett wrote:

> On 6 March 2011 16:11, anton dos santos <ad...@free.fr> wrote:
> I also tried StrictValidation="true" and an intValidator that accepts empty input by overriding isValid():
>    public boolean isValid(String text) {
>        final ParsePosition pos = new ParsePosition(0);
>        Object obj = format.parseObject(text, pos);
>        if( obj == null || text.length() == 0) {
>          return true;
>        }
> 
>        // the text is only valid is we successfully parsed ALL of it. Don't want trailing bits of
>        // not-valid text.
>        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == text.length();
>    }
> this works fine until user erases everything in the TextInput, from then on use can enter any character, he is no longer limited to digits.
> 
> What else could I try ?
> 
> 
> This seems to work for me.
> 
> 
> public class XXXIntRangeValidator extends IntRangeValidator {
>   // Constructors...
> 
>   @Override
>   public boolean isValid(String text) {
>  	  return (text.length() == 0) || super.isValid(text); 
>   }
> }


Re: TextInput and Validator issues

Posted by Sandro Martini <sa...@gmail.com>.
Hi all,
just applied the fix, so now it's in trunk.

Good tests.

Bye,
Sandro

Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Greg Brown-2 wrote:
> 
> If you build from source you can fix it locally.
> 

Done. Now everything works as it should.
Thanks a lot.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648333.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
If you build from source you can fix it locally.

On Mar 7, 2011, at 5:20 PM, lello wrote:

> 
> Greg Brown-2 wrote:
>> 
>>> So the error is there only when there is no edit effect.
>> 
>> That's what I thought. This is due to a bug in TableViewRowEditor. This
>> line in the displayMouseHandler instance is causing the problem:
>> consumed = (getEditEffect() != null);
>> It should probably be changed to:
>> consumed = true;
>> G
>> 
> 
> Ok. I can leave the edit effect on for the moment, but there is another
> table where I cannot have an edit effect because this conflicts with the way
> I handle the keyPressed() method...
> Hope to see this fixed soon.
> 
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648254.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Greg Brown-2 wrote:
> 
>> So the error is there only when there is no edit effect.
> 
> That's what I thought. This is due to a bug in TableViewRowEditor. This
> line in the displayMouseHandler instance is causing the problem:
> consumed = (getEditEffect() != null);
> It should probably be changed to:
> consumed = true;
> G
> 

Ok. I can leave the edit effect on for the moment, but there is another
table where I cannot have an edit effect because this conflicts with the way
I handle the keyPressed() method...
Hope to see this fixed soon.


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648254.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
> So the error is there only when there is no edit effect.

That's what I thought. This is due to a bug in TableViewRowEditor. This line in the displayMouseHandler instance is causing the problem:
consumed = (getEditEffect() != null);
It should probably be changed to:
consumed = true;
G



Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Greg Brown-2 wrote:
> 
> Does it work if you single-click outside the editor, or if you
> double-click with an edit effect turned on?
> 

Just to summarize:
Text valid: The roweditor works fine.
Text not valid - no edit effect: if I single click outside the editor I
don't get any new RowEditor, which is what I want. When I double click I get
the error reported in the previous message.
Text valid/Text not valid + edit effect (crossfade): everything works fine.

So the error is there only when there is no edit effect.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648186.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Does it work if you single-click outside the editor, or if you double-click with an edit effect turned on?

On Mar 7, 2011, at 4:47 PM, lello wrote:

> 
> Greg Brown-2 wrote:
>> 
>> Are you using an edit effect?
>> 
> 
> No, this is the error I get when I double click:
> 
> java.lang.IllegalArgumentException: Component already has a parent.
>        at org.apache.pivot.wtk.TablePane$Row.insert(TablePane.java:199)
>        at org.apache.pivot.wtk.TablePane$Row.add(TablePane.java:187)
>        at
> org.apache.pivot.wtk.content.TableViewRowEditor.beginEdit(TableViewRowEditor.java:211)
>        at
> org.apache.pivot.wtk.skin.terra.TerraTableViewSkin.mouseClick(TerraTableViewSkin.java:1276)
>        at
> org.apache.pivot.wtk.Component$ComponentMouseButtonListenerList.mouseClick(Component.java:483)
>        at org.apache.pivot.wtk.Component.mouseClick(Component.java:2777)
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648100.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Greg Brown-2 wrote:
> 
> Are you using an edit effect?
> 

No, this is the error I get when I double click:

java.lang.IllegalArgumentException: Component already has a parent.
        at org.apache.pivot.wtk.TablePane$Row.insert(TablePane.java:199)
        at org.apache.pivot.wtk.TablePane$Row.add(TablePane.java:187)
        at
org.apache.pivot.wtk.content.TableViewRowEditor.beginEdit(TableViewRowEditor.java:211)
        at
org.apache.pivot.wtk.skin.terra.TerraTableViewSkin.mouseClick(TerraTableViewSkin.java:1276)
        at
org.apache.pivot.wtk.Component$ComponentMouseButtonListenerList.mouseClick(Component.java:483)
        at org.apache.pivot.wtk.Component.mouseClick(Component.java:2777)

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648100.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Are you using an edit effect?

On Mar 7, 2011, at 4:36 PM, lello wrote:

> 
> Greg Brown-2 wrote:
>> 
>> See my other recent post - try overriding endEdit() instead.
>> 
> 
> Thanks, it is almost working. Clicking on the table doesn't trigger anymore
> the store method, however double clicking trigger the beginEdit() on another
> row...how can I avoid that?
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648059.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Greg Brown-2 wrote:
> 
> See my other recent post - try overriding endEdit() instead.
> 

Thanks, it is almost working. Clicking on the table doesn't trigger anymore
the store method, however double clicking trigger the beginEdit() on another
row...how can I avoid that?

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648059.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
See my other recent post - try overriding endEdit() instead.

On Mar 7, 2011, at 4:15 PM, lello wrote:

> Sorry, I always forget to reference the post.
> I added the close method, and now I check for a valid text however if the
> text is not valid, and I click on the table, super.close() is not called and
> I have the following error:
> 
> Exception thrown during paint(): java.lang.NullPointerException
> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
>        at
> org.apache.pivot.wtk.content.TableViewRowEditor$RowImage.paint(TableViewRowEditor.java:67)
> 
> 
> 
> Greg Brown-2 wrote:
>> 
>> Right - your custom row editor needs to override close() and make sure
>> that the data is valid. If you call super.close(), it will call store().
>> 
>> On Mar 7, 2011, at 3:44 PM, lello wrote:
>> 
>>> Ok, the editor itself does not override mouseDown(), but, as I said I can
>>> add
>>> a ContainerMouseButtonListener,
>>> to it, however the problem is not the editor but the table. If I am
>>> editing
>>> a row and click outside the editor with the mouse, store() is called
>>> whether
>>> or not the row object is valid or not.
>>> 
>>> --
>>> View this message in context:
>>> http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647837.html
>>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>> 
> 
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647979.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Sorry, I always forget to reference the post.
I added the close method, and now I check for a valid text however if the
text is not valid, and I click on the table, super.close() is not called and
I have the following error:

Exception thrown during paint(): java.lang.NullPointerException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at
org.apache.pivot.wtk.content.TableViewRowEditor$RowImage.paint(TableViewRowEditor.java:67)



Greg Brown-2 wrote:
> 
> Right - your custom row editor needs to override close() and make sure
> that the data is valid. If you call super.close(), it will call store().
> 
> On Mar 7, 2011, at 3:44 PM, lello wrote:
> 
>> Ok, the editor itself does not override mouseDown(), but, as I said I can
>> add
>> a ContainerMouseButtonListener,
>> to it, however the problem is not the editor but the table. If I am
>> editing
>> a row and click outside the editor with the mouse, store() is called
>> whether
>> or not the row object is valid or not.
>> 
>> --
>> View this message in context:
>> http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647837.html
>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
> 


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647979.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Right - your custom row editor needs to override close() and make sure that the data is valid. If you call super.close(), it will call store().

On Mar 7, 2011, at 3:44 PM, lello wrote:

> Ok, the editor itself does not override mouseDown(), but, as I said I can add
> a ContainerMouseButtonListener,
> to it, however the problem is not the editor but the table. If I am editing
> a row and click outside the editor with the mouse, store() is called whether
> or not the row object is valid or not.
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647837.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Ok, the editor itself does not override mouseDown(), but, as I said I can add
a ContainerMouseButtonListener,
to it, however the problem is not the editor but the table. If I am editing
a row and click outside the editor with the mouse, store() is called whether
or not the row object is valid or not.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647837.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
No, I'm talking about the editor. It is a subclass of Window.

On Mar 7, 2011, at 3:33 PM, lello wrote:

> Sorry, of course you are referring to the table, not to the row editor. Still
> I can't find a way to avoid callig valueOf in my custom BindMapping when I
> click the table with the mouse. 
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647788.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Sorry, of course you are referring to the table, not to the row editor. Still
I can't find a way to avoid callig valueOf in my custom BindMapping when I
click the table with the mouse. 

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647788.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
I'm not sure which message you are responding to. It would help if you could paste the relevant part of the thread into your replies.

On Mar 7, 2011, at 3:56 PM, lello wrote:

> I see, but I can't figure out where is my mistake. My validator returns false
> but then I select another row, and the BindMapping immediately calls
> valueOf() and then store...I catch an exception if valueOf() but store() is
> called in any case.
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647897.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
I see, but I can't figure out where is my mistake. My validator returns false
but then I select another row, and the BindMapping immediately calls
valueOf() and then store...I catch an exception if valueOf() but store() is
called in any case.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647897.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Right, but that should only happen when store() is called. It's up to your app to perform the necessary validation and prevent store() from being called.

On Mar 7, 2011, at 3:37 PM, lello wrote:

> I can add the listener by calling getComponentMouseButtonListener().add(....)
> and so on, the problem however is not the row edito bu the table itself. It
> triggers a valueOf() even if the input is not valid.
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647805.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
I can add the listener by calling getComponentMouseButtonListener().add(....)
and so on, the problem however is not the row edito bu the table itself. It
triggers a valueOf() even if the input is not valid.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647805.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
That class extends Window which extends Component, which defines this method:

protected boolean mouseDown(Mouse.Button button, int x, int y)

On Mar 7, 2011, at 3:21 PM, lello wrote:

> My row editor extends the standard TableViewRowEditor that doesn't seem to
> have a mouseDown() method...
> 
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647740.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
My row editor extends the standard TableViewRowEditor that doesn't seem to
have a mouseDown() method...


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647740.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
If it extends Window, you could override mouseDown() (components don't need to register for mouse listeners on themselves).

On Mar 7, 2011, at 3:04 PM, lello wrote:

> The problem I am facing is that when you have any value which is not valid
> you can still select a row of the table with the mouse and trigger a store()
> method. This shouldn't be allowed whether or not strictValidation() is true
> or false, since it returns an exception in any case. I should overwrite some
> MouseListener in my custom TableRowEditor, but I can't find any.
> Any suggestion?
> 
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Nice!

On Mar 7, 2011, at 4:57 PM, anton dos santos wrote:

> I have done it this way:
> 
> public class ValidationRowEditor extends TableViewRowEditor {
> 
>  @Override
>  public void endEdit(boolean result) {
>    boolean valid = true;
>    if (result) {
>      Dictionary<String, Component> editors = getCellEditors();
>      Sequence<Column> cols = getTableView().getColumns();
>      for (int i = 0, n = cols.getLength(); i < n; i++) {
>        Component comp = editors.get(cols.get(i).getName());
>        if (comp instanceof TextInput) {
>          valid &= ((TextInput) comp).isTextValid();
>        }
>      }
>    }
>    if (!(result && !valid)) {
>      super.endEdit(result);
>    }
>  }
> }
> 
> if one of the TextInput editors is not valid, user can only leave the editor by pressing ESC.
> It seems to work also with mouse, so it may also solve Lello's issues ( haven't tested completely)
> 
> On 07/03/2011 22:11, Greg Brown wrote:
>> Sorry - I haven't looked at this code in a while. You should just be able to override endEdit() and perform your validation. Don't call the base endEdit() if validation fails.
>> 
>> On Mar 7, 2011, at 4:06 PM, anton dos santos wrote:
>> 
>>> Ok
>>> but how can my app (my validators) tell the TableViewRowEditor that it should not store the row?
>>> it happens in TableViewRowEditor#endEdit(boolean result) which is invoked by keyPressed().
>>> So I should override keyPressed() and check the status of all validators before invoking endEdit() ?
>>> 
>>> On 07/03/2011 21:41, Greg Brown wrote:
>>>> That's true, but it is up to your app to decide what constitutes "valid".
>>>> 
>>>> On Mar 7, 2011, at 3:36 PM, anton dos santos wrote:
>>>> 
>>>>> when a value is not valid you can still press return and then editing ends and store() is also called.
>>>>> So modifying the MouseListener will not be enough.
>>>>> I believe that store() should not be invoked when there is a non valid value in the row
>>>>> 
>>>>> 
>>>>> On 07/03/2011 21:04, lello wrote:
>>>>>> The problem I am facing is that when you have any value which is not valid
>>>>>> you can still select a row of the table with the mouse and trigger a store()
>>>>>> method. This shouldn't be allowed whether or not strictValidation() is true
>>>>>> or false, since it returns an exception in any case. I should overwrite some
>>>>>> MouseListener in my custom TableRowEditor, but I can't find any.
>>>>>> Any suggestion?
>>>>>> 
>>>>>> --
>>>>>> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
>>>>>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>>>>>> 
>>>>>> 
>>>> 
>> 
>> 
> 


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
anton dos santos wrote:
> 
> I have done it this way:
> 
> public class ValidationRowEditor extends TableViewRowEditor {
> 
>    @Override
>    public void endEdit(boolean result) {
>      boolean valid = true;
>      if (result) {
>        Dictionary editors = getCellEditors();
>        Sequence cols = getTableView().getColumns();
>        for (int i = 0, n = cols.getLength(); i < n; i++) {
>          Component comp = editors.get(cols.get(i).getName());
>          if (comp instanceof TextInput) {
>            valid &= ((TextInput) comp).isTextValid();
>          }
>        }
>      }
>      if (!(result && !valid)) {
>        super.endEdit(result);
>      }
>    }
> }
> 
> if one of the TextInput editors is not valid, user can only leave the
> editor by pressing ESC.
> It seems to work also with mouse, so it may also solve Lello's issues (
> haven't tested completely)
> 
> 

The code suffers in any case the problem that I have: text not valid +
double click outside the row editor and you get a null pointer error.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2648202.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by anton dos santos <ad...@free.fr>.
I have done it this way:

public class ValidationRowEditor extends TableViewRowEditor {

   @Override
   public void endEdit(boolean result) {
     boolean valid = true;
     if (result) {
       Dictionary<String, Component> editors = getCellEditors();
       Sequence<Column> cols = getTableView().getColumns();
       for (int i = 0, n = cols.getLength(); i < n; i++) {
         Component comp = editors.get(cols.get(i).getName());
         if (comp instanceof TextInput) {
           valid &= ((TextInput) comp).isTextValid();
         }
       }
     }
     if (!(result && !valid)) {
       super.endEdit(result);
     }
   }
}

if one of the TextInput editors is not valid, user can only leave the editor by pressing ESC.
It seems to work also with mouse, so it may also solve Lello's issues ( haven't tested completely)

On 07/03/2011 22:11, Greg Brown wrote:
> Sorry - I haven't looked at this code in a while. You should just be able to override endEdit() and perform your validation. Don't call the base endEdit() if validation fails.
>
> On Mar 7, 2011, at 4:06 PM, anton dos santos wrote:
>
>> Ok
>> but how can my app (my validators) tell the TableViewRowEditor that it should not store the row?
>> it happens in TableViewRowEditor#endEdit(boolean result) which is invoked by keyPressed().
>> So I should override keyPressed() and check the status of all validators before invoking endEdit() ?
>>
>> On 07/03/2011 21:41, Greg Brown wrote:
>>> That's true, but it is up to your app to decide what constitutes "valid".
>>>
>>> On Mar 7, 2011, at 3:36 PM, anton dos santos wrote:
>>>
>>>> when a value is not valid you can still press return and then editing ends and store() is also called.
>>>> So modifying the MouseListener will not be enough.
>>>> I believe that store() should not be invoked when there is a non valid value in the row
>>>>
>>>>
>>>> On 07/03/2011 21:04, lello wrote:
>>>>> The problem I am facing is that when you have any value which is not valid
>>>>> you can still select a row of the table with the mouse and trigger a store()
>>>>> method. This shouldn't be allowed whether or not strictValidation() is true
>>>>> or false, since it returns an exception in any case. I should overwrite some
>>>>> MouseListener in my custom TableRowEditor, but I can't find any.
>>>>> Any suggestion?
>>>>>
>>>>> --
>>>>> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
>>>>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>
>
>


Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Sorry - I haven't looked at this code in a while. You should just be able to override endEdit() and perform your validation. Don't call the base endEdit() if validation fails.

On Mar 7, 2011, at 4:06 PM, anton dos santos wrote:

> Ok
> but how can my app (my validators) tell the TableViewRowEditor that it should not store the row?
> it happens in TableViewRowEditor#endEdit(boolean result) which is invoked by keyPressed().
> So I should override keyPressed() and check the status of all validators before invoking endEdit() ?
> 
> On 07/03/2011 21:41, Greg Brown wrote:
>> That's true, but it is up to your app to decide what constitutes "valid".
>> 
>> On Mar 7, 2011, at 3:36 PM, anton dos santos wrote:
>> 
>>> when a value is not valid you can still press return and then editing ends and store() is also called.
>>> So modifying the MouseListener will not be enough.
>>> I believe that store() should not be invoked when there is a non valid value in the row
>>> 
>>> 
>>> On 07/03/2011 21:04, lello wrote:
>>>> The problem I am facing is that when you have any value which is not valid
>>>> you can still select a row of the table with the mouse and trigger a store()
>>>> method. This shouldn't be allowed whether or not strictValidation() is true
>>>> or false, since it returns an exception in any case. I should overwrite some
>>>> MouseListener in my custom TableRowEditor, but I can't find any.
>>>> Any suggestion?
>>>> 
>>>> --
>>>> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
>>>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>>>> 
>>>> 
>> 
>> 
> 


Re: TextInput and Validator issues

Posted by anton dos santos <ad...@free.fr>.
Ok
but how can my app (my validators) tell the TableViewRowEditor that it should not store the row?
it happens in TableViewRowEditor#endEdit(boolean result) which is invoked by keyPressed().
So I should override keyPressed() and check the status of all validators before invoking endEdit() ?

On 07/03/2011 21:41, Greg Brown wrote:
> That's true, but it is up to your app to decide what constitutes "valid".
>
> On Mar 7, 2011, at 3:36 PM, anton dos santos wrote:
>
>> when a value is not valid you can still press return and then editing ends and store() is also called.
>> So modifying the MouseListener will not be enough.
>> I believe that store() should not be invoked when there is a non valid value in the row
>>
>>
>> On 07/03/2011 21:04, lello wrote:
>>> The problem I am facing is that when you have any value which is not valid
>>> you can still select a row of the table with the mouse and trigger a store()
>>> method. This shouldn't be allowed whether or not strictValidation() is true
>>> or false, since it returns an exception in any case. I should overwrite some
>>> MouseListener in my custom TableRowEditor, but I can't find any.
>>> Any suggestion?
>>>
>>> --
>>> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
>>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>>>
>>>
>
>


Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
That's true, but it is up to your app to decide what constitutes "valid".

On Mar 7, 2011, at 3:36 PM, anton dos santos wrote:

> when a value is not valid you can still press return and then editing ends and store() is also called.
> So modifying the MouseListener will not be enough.
> I believe that store() should not be invoked when there is a non valid value in the row
> 
> 
> On 07/03/2011 21:04, lello wrote:
>> The problem I am facing is that when you have any value which is not valid
>> you can still select a row of the table with the mouse and trigger a store()
>> method. This shouldn't be allowed whether or not strictValidation() is true
>> or false, since it returns an exception in any case. I should overwrite some
>> MouseListener in my custom TableRowEditor, but I can't find any.
>> Any suggestion?
>> 
>> --
>> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
>> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>> 
>> 
> 


Re: TextInput and Validator issues

Posted by anton dos santos <ad...@free.fr>.
when a value is not valid you can still press return and then editing ends and store() is also called.
So modifying the MouseListener will not be enough.
I believe that store() should not be invoked when there is a non valid value in the row


On 07/03/2011 21:04, lello wrote:
> The problem I am facing is that when you have any value which is not valid
> you can still select a row of the table with the mouse and trigger a store()
> method. This shouldn't be allowed whether or not strictValidation() is true
> or false, since it returns an exception in any case. I should overwrite some
> MouseListener in my custom TableRowEditor, but I can't find any.
> Any suggestion?
>
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>
>


Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
The problem I am facing is that when you have any value which is not valid
you can still select a row of the table with the mouse and trigger a store()
method. This shouldn't be allowed whether or not strictValidation() is true
or false, since it returns an exception in any case. I should overwrite some
MouseListener in my custom TableRowEditor, but I can't find any.
Any suggestion?

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2647704.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Greg Brown <gk...@verizon.net>.
Those are basically the tradeoffs you need to make, though - if you allow an empty string, it is up to your app to define a meaning for it, since the empty string does not represent a valid number.

On Mar 7, 2011, at 12:55 PM, anton dos santos wrote:

> Hi Chris
> 
> you are right, this works:
> 
>   @Override
>   public boolean isValid(String text) {
>     return (text.length() == 0) || super.isValid(text); 
>   }
> 
> I was referencing a wrong validator in my bxml file :(
> but now when my input is empty, it can't be converted into a number when store() is invoked:
> 
> java.lang.NumberFormatException: For input string: ""
>         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
>         at java.lang.Long.parseLong(Long.java:431)
>         at java.lang.Long.parseLong(Long.java:468)
>         at org.apache.pivot.beans.BeanAdapter.coerce(BeanAdapter.java:897)
>         at org.apache.pivot.beans.BeanAdapter.put(BeanAdapter.java:286)
>         at org.apache.pivot.json.JSON.put(JSON.java:152)
>         at org.apache.pivot.wtk.TextInput.store(TextInput.java:841)
>         at org.apache.pivot.wtk.Container.store(Container.java:613)
>         at org.apache.pivot.wtk.content.TableViewRowEditor.endEdit(TableViewRowEditor.java:254)
> 
> I solved this issue with a custom BindMapping, but now this gets a little bit complex ... 
> 
> Regards
> Anton
> 
> On 06/03/2011 20:26, Chris Bartlett wrote:
>> 
>> On 6 March 2011 16:11, anton dos santos <ad...@free.fr> wrote:
>> I also tried StrictValidation="true" and an intValidator that accepts empty input by overriding isValid():
>>    public boolean isValid(String text) {
>>        final ParsePosition pos = new ParsePosition(0);
>>        Object obj = format.parseObject(text, pos);
>>        if( obj == null || text.length() == 0) {
>>          return true;
>>        }
>> 
>>        // the text is only valid is we successfully parsed ALL of it. Don't want trailing bits of
>>        // not-valid text.
>>        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == text.length();
>>    }
>> this works fine until user erases everything in the TextInput, from then on use can enter any character, he is no longer limited to digits.
>> 
>> What else could I try ?
>> 
>> 
>> This seems to work for me.
>> 
>> 
>> public class XXXIntRangeValidator extends IntRangeValidator {
>>   // Constructors...
>> 
>>   @Override
>>   public boolean isValid(String text) {
>>     return (text.length() == 0) || super.isValid(text); 
>>   }
>> }
> 


Re: TextInput and Validator issues

Posted by anton dos santos <ad...@free.fr>.
Hi Chris

you are right, this works:

   @Override
   public boolean isValid(String text) {
     return (text.length() == 0) || super.isValid(text);
   }

I was referencing a wrong validator in my bxml file :(
but now when my input is empty, it can't be converted into a number when store() is invoked:

java.lang.NumberFormatException: For input string: ""
         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
         at java.lang.Long.parseLong(Long.java:431)
         at java.lang.Long.parseLong(Long.java:468)
         at org.apache.pivot.beans.BeanAdapter.coerce(BeanAdapter.java:897)
         at org.apache.pivot.beans.BeanAdapter.put(BeanAdapter.java:286)
         at org.apache.pivot.json.JSON.put(JSON.java:152)
         at org.apache.pivot.wtk.TextInput.store(TextInput.java:841)
         at org.apache.pivot.wtk.Container.store(Container.java:613)
         at org.apache.pivot.wtk.content.TableViewRowEditor.endEdit(TableViewRowEditor.java:254)

I solved this issue with a custom BindMapping, but now this gets a little bit complex ...

Regards
Anton

On 06/03/2011 20:26, Chris Bartlett wrote:
> On 6 March 2011 16:11, anton dos santos <adsantos@free.fr <ma...@free.fr>> wrote:
>
>     I also tried StrictValidation="true" and an intValidator that accepts empty input by
>     overriding isValid():
>        public boolean isValid(String text) {
>            final ParsePosition pos = new ParsePosition(0);
>            Object obj = format.parseObject(text, pos);
>            if( obj == null || text.length() == 0) {
>              return true;
>            }
>
>            // the text is only valid is we successfully parsed ALL of it. Don't want trailing bits of
>            // not-valid text.
>            return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == text.length();
>        }
>     this works fine until user erases everything in the TextInput, from then on use can enter any
>     character, he is no longer limited to digits.
>
>     What else could I try ?
>
>
> This seems to work for me.
>
>
> public class XXXIntRangeValidator extends IntRangeValidator {
>   // Constructors...
>
>   @Override
>   public boolean isValid(String text) {
>     return (text.length() == 0) || super.isValid(text);
>   }
> }


Re: TextInput and Validator issues

Posted by Chris Bartlett <cb...@gmail.com>.
The example overridden isValid() method was just an example of allowing an
empty string without the weird issues that Anton saw with other characters
suddenly being accepted.

I haven't tested it within a row editor, just as a stand alone TextInput
with a custom IntRangeValidator.


A few months back, there were a couple of fairly long threads which resulted
in changes to RowEditors.  Greg tried a few variations, but I can't remember
how it ended up.  I think that it might still need a couple of tweaks to
allow developers to target a specific row for editing.

I think it should be possible to prevent the store() occurring if any values
are invalid. Perhaps with a WindowStateListener on the RowEditor window to
veto the close?  I'll try to take a look tomorrow, but in the mean time, try
searching the nabble archives.

http://apache-pivot-developers.417237.n3.nabble.com/
http://apache-pivot-users.399431.n3.nabble.com/

Chris

On 7 March 2011 03:08, lello <rb...@gmail.com> wrote:

> Hi all,
> I was having the same issue in the past days, and I was trying to find a
> solution, but without success.
> I'll try what Chris suggests.
>
> Another issue with the validation is the following: if you have no strict
> validation (setStrictvalidation(false)) you can type and invalid character,
> but if you select a second row with the
> mouse the store() method is called, which of course result in an exception.
> I don't know if this is a bug or it is intended to be like this. I'd prefer
> the table to be not responsive when any of its field is not valid.
>
>
>
> --
> View this message in context:
> http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2643010.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>

Re: TextInput and Validator issues

Posted by Chris Bartlett <cb...@gmail.com>.
I don't have the source in front of me but I think that the main issue here
is that the strict validation of TextInputs doesn't work properly due to it
processing text replacement as 2 separate operations.
 TextInputContentListener will preview and possibly veto the text removal
before then previewing and possibly vetoing the text insertion.

http://pivot.apache.org/2.0/docs/api/org/apache/pivot/wtk/TextInputContentListener.html

If so, it shouldn't be too hard to calculate what the resulting string would
look like after a combined (remove + insert) operation, and then pass that
to the validator.  Without access to the source though, I can't be sure.

Chris

On 7 March 2011 03:13, lello <rb...@gmail.com> wrote:

> Just a note,
> the solution suggeste by Chris does work, in the sense that it allows to
> have an empty field.
> The problem is that in my case the empty field should not be valid, and the
> use can press ENTER, call
> for store() and trigger an exception. So the solution is not really that
> immediate.
>
> --
> View this message in context:
> http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2643037.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>

Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Just a note,
the solution suggeste by Chris does work, in the sense that it allows to
have an empty field.
The problem is that in my case the empty field should not be valid, and the
use can press ENTER, call
for store() and trigger an exception. So the solution is not really that
immediate.

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2643037.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by lello <rb...@gmail.com>.
Hi all,
I was having the same issue in the past days, and I was trying to find a
solution, but without success.
I'll try what Chris suggests.

Another issue with the validation is the following: if you have no strict
validation (setStrictvalidation(false)) you can type and invalid character,
but if you select a second row with the
mouse the store() method is called, which of course result in an exception.
I don't know if this is a bug or it is intended to be like this. I'd prefer
the table to be not responsive when any of its field is not valid.



--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/TextInput-and-Validator-issues-tp2641141p2643010.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: TextInput and Validator issues

Posted by Chris Bartlett <cb...@gmail.com>.
On 6 March 2011 16:11, anton dos santos <ad...@free.fr> wrote:

> I also tried StrictValidation="true" and an intValidator that accepts empty
> input by overriding isValid():
>    public boolean isValid(String text) {
>        final ParsePosition pos = new ParsePosition(0);
>        Object obj = format.parseObject(text, pos);
>        if( obj == null || text.length() == 0) {
>          return true;
>        }
>
>        // the text is only valid is we successfully parsed ALL of it. Don't
> want trailing bits of
>        // not-valid text.
>        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() ==
> text.length();
>    }
> this works fine until user erases everything in the TextInput, from then on
> use can enter any character, he is no longer limited to digits.
>
> What else could I try ?
>
>
This seems to work for me.


public class XXXIntRangeValidator extends IntRangeValidator {
  // Constructors...

  @Override
  public boolean isValid(String text) {
    return (text.length() == 0) || super.isValid(text);
  }
}