You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/08/27 18:44:56 UTC

svn commit: r990205 - in /pivot/trunk: demos/src/org/apache/pivot/demos/roweditor/ tutorials/src/org/apache/pivot/tutorials/webqueries/ wtk-terra/src/org/apache/pivot/wtk/skin/terra/ wtk/src/org/apache/pivot/wtk/

Author: gbrown
Date: Fri Aug 27 16:44:56 2010
New Revision: 990205

URL: http://svn.apache.org/viewvc?rev=990205&view=rev
Log:
Re-implement strict validation in TextInput using the new preview events.

Modified:
    pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/row_editor_demo.bxml
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/webqueries/expense_sheet.bxml
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java

Modified: pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/row_editor_demo.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/row_editor_demo.bxml?rev=990205&r1=990204&r2=990205&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/row_editor_demo.bxml (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/roweditor/row_editor_demo.bxml Fri Aug 27 16:44:56 2010
@@ -29,7 +29,7 @@ limitations under the License.
         <ListButton bxml:id="typeListButton" listData="['Hotel', 'Miscellaneous', 'Meals', 'Parking', 'Travel']"
             selectedItemKey="type"/>
 
-        <TextInput bxml:id="amountTextInput" textKey="amount">
+        <TextInput bxml:id="amountTextInput" strictValidation="true" textKey="amount">
             <validator>
                 <demo:CurrencyValidator/>
             </validator>

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/webqueries/expense_sheet.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/webqueries/expense_sheet.bxml?rev=990205&r1=990204&r2=990205&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/webqueries/expense_sheet.bxml (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/webqueries/expense_sheet.bxml Fri Aug 27 16:44:56 2010
@@ -56,7 +56,8 @@ limitations under the License.
                         <ListButton bxml:id="typeListButton" Form.label="%type" listData="@types.json"
                             selectedItemKey="type"/>
 
-                        <TextInput bxml:id="amountTextInput" Form.label="%amount" textKey="amount">
+                        <TextInput bxml:id="amountTextInput" Form.label="%amount" strictValidation="true"
+                            textKey="amount">
                             <validator>
                                 <webqueries:AmountValidator/>
                             </validator>

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java?rev=990205&r1=990204&r2=990205&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraTextInputSkin.java Fri Aug 27 16:44:56 2010
@@ -1289,6 +1289,11 @@ public class TerraTextInputSkin extends 
     }
 
     @Override
+    public void strictValidationChanged(TextInput textInput) {
+        // No-op
+    }
+
+    @Override
     public void textValidChanged(TextInput textInput) {
         repaintComponent();
     }
@@ -1296,7 +1301,27 @@ public class TerraTextInputSkin extends 
     // Text input character events
     @Override
     public Vote previewInsertText(TextInput textInput, String text, int index) {
-        return Vote.APPROVE;
+        Vote vote;
+        if (textInput.isStrictValidation()) {
+            Validator validator = textInput.getValidator();
+            StringBuilder textBuilder = new StringBuilder();
+
+            CharSequence characters = textInput.getCharacters();
+            textBuilder.append(characters.subSequence(0, index));
+            textBuilder.append(text);
+            textBuilder.append(characters.subSequence(index, characters.length()));
+
+            if (validator.isValid(textBuilder.toString())) {
+                vote = Vote.APPROVE;
+            } else {
+                vote = Vote.DENY;
+                Toolkit.getDefaultToolkit().beep();
+            }
+        } else {
+            vote = Vote.APPROVE;
+        }
+
+        return vote;
     }
 
     @Override
@@ -1311,7 +1336,23 @@ public class TerraTextInputSkin extends 
 
     @Override
     public Vote previewRemoveText(TextInput textInput, int index, int count) {
-        return Vote.APPROVE;
+        Vote vote;
+        if (textInput.isStrictValidation()) {
+            Validator validator = textInput.getValidator();
+            StringBuilder textBuilder = new StringBuilder(textInput.getCharacters());
+            textBuilder.delete(index, index + count);
+
+            if (validator.isValid(textBuilder.toString())) {
+                vote = Vote.APPROVE;
+            } else {
+                vote = Vote.DENY;
+                Toolkit.getDefaultToolkit().beep();
+            }
+        } else {
+            vote = Vote.APPROVE;
+        }
+
+        return vote;
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java?rev=990205&r1=990204&r2=990205&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java Fri Aug 27 16:44:56 2010
@@ -114,6 +114,13 @@ public class TextInput extends Component
         }
 
         @Override
+        public void strictValidationChanged(TextInput textInput) {
+            for (TextInputListener listener : this) {
+                listener.strictValidationChanged(textInput);
+            }
+        }
+
+        @Override
         public void textValidChanged(TextInput textInput) {
             for (TextInputListener listener : this) {
                 listener.textValidChanged(textInput);
@@ -232,6 +239,7 @@ public class TextInput extends Component
     private TextBindMapping textBindMapping = null;
 
     private Validator validator = null;
+    private boolean strictValidation = false;
     private boolean textValid = true;
 
     private TextInputListenerList textInputListeners = new TextInputListenerList();
@@ -799,6 +807,26 @@ public class TextInput extends Component
     }
 
     /**
+     * Returns the text input's strict validation flag.
+     */
+    public boolean isStrictValidation() {
+        return strictValidation;
+    }
+
+    /**
+     * Sets the text input's strict validation flag. When enabled, only valid text will be
+     * accepted by the text input.
+     *
+     * @param strictValidation
+     */
+    public void setStrictValidation(boolean strictValidation) {
+        if (this.strictValidation != strictValidation) {
+            this.strictValidation = strictValidation;
+            textInputListeners.strictValidationChanged(this);
+        }
+    }
+
+    /**
      * Reports whether this text input's text is currently valid as defined by
      * its validator.
      *

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java?rev=990205&r1=990204&r2=990205&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java Fri Aug 27 16:44:56 2010
@@ -47,6 +47,10 @@ public interface TextInputListener {
         }
 
         @Override
+        public void strictValidationChanged(TextInput textInput) {
+        }
+
+        @Override
         public void textValidChanged(TextInput textInput) {
         }
     }
@@ -91,6 +95,13 @@ public interface TextInputListener {
     public void textValidatorChanged(TextInput textInput, Validator previousValidator);
 
     /**
+     * Called when a text input's strict validation flag has changed.
+     *
+     * @param textInput
+     */
+    public void strictValidationChanged(TextInput textInput);
+
+    /**
      * Called when the text changes validity.
      *
      * @param textInput