You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by sm...@apache.org on 2013/02/20 12:55:20 UTC

svn commit: r1448109 - in /pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk: Label.java LabelListener.java skin/LabelSkin.java

Author: smartini
Date: Wed Feb 20 11:55:20 2013
New Revision: 1448109

URL: http://svn.apache.org/r1448109
Log:
PIVOT-893: add maximumLength property to Label (like TextInput and others)

Modified:
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/Label.java
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/LabelListener.java
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/Label.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/Label.java?rev=1448109&r1=1448108&r2=1448109&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/Label.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/Label.java Wed Feb 20 11:55:20 2013
@@ -52,6 +52,14 @@ public class Label extends Component {
                 listener.textChanged(label, previousText);
             }
         }
+
+        @Override
+        public void maximumLengthChanged(Label label, int previousMaximumLength) {
+            for (LabelListener listener : this) {
+                listener.maximumLengthChanged(label, previousMaximumLength);
+            }
+        }
+
     }
 
     private static class LabelBindingListenerList extends WTKListenerList<LabelBindingListener>
@@ -79,6 +87,7 @@ public class Label extends Component {
     }
 
     private String text = null;
+    private int maximumLength = 32767;
 
     private String textKey = null;
     private BindType textBindType = BindType.BOTH;
@@ -102,7 +111,17 @@ public class Label extends Component {
     }
 
     public void setText(String text) {
-        String previousText = this.text;
+        // TODO: in 2.1 verify if enable this constraint (like in other classes)
+        // if (text == null) {
+        //     throw new IllegalArgumentException();
+        // }
+        //
+        // if (text.length() > maximumLength) {
+        if (text != null && text.length() > maximumLength) {
+            throw new IllegalArgumentException("Text length is greater than maximum length.");
+        }
+
+       String previousText = this.text;
         if (previousText != text) {
             this.text = text;
             labelListeners.textChanged(this, previousText);
@@ -120,6 +139,44 @@ public class Label extends Component {
     }
 
     /**
+     * Returns the maximum length of the label text.
+     *
+     * @return
+     * The maximum length of the label text.
+     */
+    public int getMaximumLength() {
+        return maximumLength;
+    }
+
+    /**
+     * Sets the maximum length of the label text.
+     *
+     * @param maximumLength
+     * The maximum length of the label text.
+     */
+    public void setMaximumLength(int maximumLength) {
+        if (maximumLength < 0) {
+            throw new IllegalArgumentException("maximumLength is negative.");
+        }
+
+        int previousMaximumLength = this.maximumLength;
+        if (previousMaximumLength != maximumLength) {
+            this.maximumLength = maximumLength;
+
+            if (text != null) {
+                // Truncate the text, if necessary (do not allow listeners to vote on this change)
+                int length = text.length();
+                if (length > maximumLength) {
+                    setText(text.substring(0, maximumLength));
+                }
+
+            }
+
+            labelListeners.maximumLengthChanged(this, previousMaximumLength);
+        }
+    }
+
+    /**
      * Sets the label's text key.
      *
      * @param textKey

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/LabelListener.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/LabelListener.java?rev=1448109&r1=1448108&r2=1448109&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/LabelListener.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/LabelListener.java Wed Feb 20 11:55:20 2013
@@ -16,15 +16,42 @@
  */
 package org.apache.pivot.wtk;
 
+
 /**
  * Label listener interface.
  */
 public interface LabelListener {
     /**
+     * Label listener adapter.
+     */
+    public static class Adapter implements LabelListener {
+
+        @Override
+        public void textChanged(Label label, String previousText) {
+            // empty block
+        }
+
+        @Override
+        public void maximumLengthChanged(Label label, int previousMaximumLength) {
+            // empty block
+        }
+    }
+
+
+   /**
      * Called when a label's text has changed.
      *
      * @param label
      * @param previousText
      */
     public void textChanged(Label label, String previousText);
+
+    /**
+     * Called when a label text maximum length has changed.
+     *
+     * @param label
+     * @param previousMaximumLength
+     */
+    public void maximumLengthChanged(Label label, int previousMaximumLength);
+
 }

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java?rev=1448109&r1=1448108&r2=1448109&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/skin/LabelSkin.java Wed Feb 20 11:55:20 2013
@@ -675,4 +675,10 @@ public class LabelSkin extends Component
     public void textChanged(Label label, String previousText) {
         invalidateComponent();
     }
+
+    @Override
+    public void maximumLengthChanged(Label label, int previousMaximumLength) {
+        invalidateComponent();
+    }
+
 }