Author: noelgrandin
Date: Wed Jun  1 14:03:58 2011
New Revision: 1130155

URL: http://svn.apache.org/viewvc?rev=1130155&view=rev
Log:
PIVOT-695 TextInput should support editable property

Modified:
    
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/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=1130155&r1=1130154&r2=1130155&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
 Wed Jun  1 14:03:58 2011
@@ -42,6 +42,8 @@ import org.apache.pivot.wtk.FocusTravers
 import org.apache.pivot.wtk.GraphicsUtilities;
 import org.apache.pivot.wtk.Insets;
 import org.apache.pivot.wtk.Keyboard;
+import org.apache.pivot.wtk.Keyboard.KeyCode;
+import org.apache.pivot.wtk.Keyboard.Modifier;
 import org.apache.pivot.wtk.Mouse;
 import org.apache.pivot.wtk.Orientation;
 import org.apache.pivot.wtk.Platform;
@@ -51,8 +53,6 @@ import org.apache.pivot.wtk.TextInputLis
 import org.apache.pivot.wtk.TextInputSelectionListener;
 import org.apache.pivot.wtk.Theme;
 import org.apache.pivot.wtk.Window;
-import org.apache.pivot.wtk.Keyboard.KeyCode;
-import org.apache.pivot.wtk.Keyboard.Modifier;
 import org.apache.pivot.wtk.skin.ComponentSkin;
 import org.apache.pivot.wtk.validation.Validator;
 
@@ -369,7 +369,7 @@ public class TerraTextInputSkin extends 
                     Color selectionColor;
                     Color selectionBackgroundColor;
 
-                    if (textInput.isFocused()) {
+                    if (textInput.isFocused() && textInput.isEditable()) {
                         selectionColor = this.selectionColor;
                         selectionBackgroundColor = 
this.selectionBackgroundColor;
                     } else {
@@ -1036,22 +1036,24 @@ public class TerraTextInputSkin extends 
     @Override
     public boolean keyTyped(Component component, char character) {
         boolean consumed = super.keyTyped(component, character);
+        TextInput textInput = (TextInput)getComponent();
 
-        // Ignore characters in the control range and the ASCII delete
-        // character as well as meta key presses
-        if (character > 0x1F
-            && character != 0x7F
-            && !Keyboard.isPressed(Keyboard.Modifier.META)) {
-            TextInput textInput = (TextInput)getComponent();
-            int selectionLength = textInput.getSelectionLength();
+        if (textInput.isEditable()) {
+            // Ignore characters in the control range and the ASCII delete
+            // character as well as meta key presses
+            if (character > 0x1F
+                && character != 0x7F
+                && !Keyboard.isPressed(Keyboard.Modifier.META)) {
+                int selectionLength = textInput.getSelectionLength();
 
-            if (textInput.getCharacterCount() - selectionLength + 1 > 
textInput.getMaximumLength()) {
-                Toolkit.getDefaultToolkit().beep();
-            } else {
-                // NOTE We explicitly call getSelectionStart() twice here in 
case the remove
-                // event is vetoed
-                textInput.removeText(textInput.getSelectionStart(), 
selectionLength);
-                textInput.insertText(Character.toString(character), 
textInput.getSelectionStart());
+                if (textInput.getCharacterCount() - selectionLength + 1 > 
textInput.getMaximumLength()) {
+                    Toolkit.getDefaultToolkit().beep();
+                } else {
+                    // NOTE We explicitly call getSelectionStart() twice here 
in case the remove
+                    // event is vetoed
+                    textInput.removeText(textInput.getSelectionStart(), 
selectionLength);
+                    textInput.insertText(Character.toString(character), 
textInput.getSelectionStart());
+                }
             }
         }
 
@@ -1116,7 +1118,7 @@ public class TerraTextInputSkin extends 
         Keyboard.Modifier commandModifier = Platform.getCommandModifier();
         Keyboard.Modifier wordNavigationModifier = 
Platform.getWordNavigationModifier();
 
-        if (keyCode == Keyboard.KeyCode.DELETE) {
+        if (keyCode == Keyboard.KeyCode.DELETE && textInput.isEditable()) {
             int index = textInput.getSelectionStart();
 
             if (index < textInput.getCharacterCount()) {
@@ -1125,7 +1127,7 @@ public class TerraTextInputSkin extends 
 
                 consumed = true;
             }
-        } else if (keyCode == Keyboard.KeyCode.BACKSPACE) {
+        } else if (keyCode == Keyboard.KeyCode.BACKSPACE && 
textInput.isEditable()) {
             int index = textInput.getSelectionStart();
             int count = textInput.getSelectionLength();
 
@@ -1270,7 +1272,7 @@ public class TerraTextInputSkin extends 
             if (keyCode == Keyboard.KeyCode.A) {
                 textInput.setSelection(0, textInput.getCharacterCount());
                 consumed = true;
-            } else if (keyCode == Keyboard.KeyCode.X) {
+            } else if (keyCode == Keyboard.KeyCode.X && 
textInput.isEditable()) {
                 if (textInput.isPassword()) {
                     Toolkit.getDefaultToolkit().beep();
                 } else {
@@ -1286,10 +1288,10 @@ public class TerraTextInputSkin extends 
                 }
 
                 consumed = true;
-            } else if (keyCode == Keyboard.KeyCode.V) {
+            } else if (keyCode == Keyboard.KeyCode.V && 
textInput.isEditable()) {
                 textInput.paste();
                 consumed = true;
-            } else if (keyCode == Keyboard.KeyCode.Z) {
+            } else if (keyCode == Keyboard.KeyCode.Z && 
textInput.isEditable()) {
                 if (!Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                     textInput.undo();
                 }
@@ -1459,6 +1461,11 @@ public class TerraTextInputSkin extends 
         repaintComponent();
     }
 
+    @Override
+    public void editableChanged(TextInput textInput) {
+        repaintComponent();
+    }
+
     // Text input selection events
     @Override
     public void selectionChanged(TextInput textInput, int 
previousSelectionStart,

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=1130155&r1=1130154&r2=1130155&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInput.java Wed Jun  1 14:03:58 
2011
@@ -152,6 +152,13 @@ public class TextInput extends Component
                 listener.textValidChanged(textInput);
             }
         }
+
+        @Override
+        public void editableChanged(TextInput textInput) {
+            for (TextInputListener listener : this) {
+                listener.editableChanged(textInput);
+            }
+        }
     }
 
     private static class TextInputContentListenerList extends 
WTKListenerList<TextInputContentListener>
@@ -259,6 +266,7 @@ public class TextInput extends Component
     private int maximumLength = Integer.MAX_VALUE;
     private boolean password = false;
     private String prompt = null;
+    private boolean editable = true;
 
     private String textKey = null;
     private BindType textBindType = BindType.BOTH;
@@ -927,6 +935,32 @@ public class TextInput extends Component
     }
 
     /**
+     * Returns the text area's editable flag.
+     */
+    public boolean isEditable() {
+        return editable;
+    }
+
+    /**
+     * Sets the text area's editable flag.
+     *
+     * @param editable
+     */
+    public void setEditable(boolean editable) {
+        if (this.editable != editable) {
+            if (!editable) {
+                if (isFocused()) {
+                    clearFocus();
+                }
+            }
+
+            this.editable = editable;
+
+            textInputListeners.editableChanged(this);
+        }
+    }
+
+    /**
      * Returns the text input listener list.
      */
     public ListenerList<TextInputListener> getTextInputListeners() {

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=1130155&r1=1130154&r2=1130155&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/TextInputListener.java Wed Jun  1 
14:03:58 2011
@@ -53,6 +53,10 @@ public interface TextInputListener {
         @Override
         public void textValidChanged(TextInput textInput) {
         }
+
+        @Override
+        public void editableChanged(TextInput textInput) {
+        }
     }
 
     /**
@@ -107,4 +111,12 @@ public interface TextInputListener {
      * @param textInput
      */
     public void textValidChanged(TextInput textInput);
+
+    /**
+     * Called when the editable state has changed.
+     *
+     * @param textInput
+     */
+    public void editableChanged(TextInput textInput);
+
 }


Reply via email to