]> source.dussan.org Git - vaadin-framework.git/commitdiff
fixes #932, implemented maxlength for textfield
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 29 Dec 2008 10:58:16 +0000 (10:58 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 29 Dec 2008 10:58:16 +0000 (10:58 +0000)
svn changeset:6360/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java
src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java
src/com/itmill/toolkit/tests/tickets/Ticket932.java [new file with mode: 0644]
src/com/itmill/toolkit/ui/RichTextArea.java
src/com/itmill/toolkit/ui/TextField.java

index 94bccd7bfdb74c68de6744686203577dea4bfeaf..d3538698d73f1355329698926849f5d347b93f1e 100644 (file)
@@ -4,8 +4,11 @@
 \r
 package com.itmill.toolkit.terminal.gwt.client.ui;\r
 \r
+import com.google.gwt.user.client.Command;\r
 import com.google.gwt.user.client.DOM;\r
+import com.google.gwt.user.client.DeferredCommand;\r
 import com.google.gwt.user.client.Element;\r
+import com.google.gwt.user.client.Event;\r
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;\r
 import com.itmill.toolkit.terminal.gwt.client.UIDL;\r
 \r
@@ -26,6 +29,7 @@ public class ITextArea extends ITextField {
         setStyleName(CLASSNAME);\r
     }\r
 \r
+    @Override\r
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {\r
         // Call parent renderer explicitly\r
         super.updateFromUIDL(uidl, client);\r
@@ -33,6 +37,10 @@ public class ITextArea extends ITextField {
         if (uidl.hasAttribute("rows")) {\r
             setRows(new Integer(uidl.getStringAttribute("rows")).intValue());\r
         }\r
+\r
+        if (getMaxLength() >= 0) {\r
+            sinkEvents(Event.ONKEYPRESS);\r
+        }\r
     }\r
 \r
     public void setRows(int rows) {\r
@@ -47,4 +55,18 @@ public class ITextArea extends ITextField {
     } catch (e) {}\r
     }-*/;\r
 \r
+    @Override\r
+    public void onBrowserEvent(Event event) {\r
+        if (getMaxLength() >= 0 && event.getTypeInt() == Event.ONKEYPRESS) {\r
+            DeferredCommand.addCommand(new Command() {\r
+                public void execute() {\r
+                    if (getText().length() > getMaxLength()) {\r
+                        setText(getText().substring(0, getMaxLength()));\r
+                    }\r
+                }\r
+            });\r
+        }\r
+        super.onBrowserEvent(event);\r
+    }\r
+\r
 }\r
index 3f07ba27d1f10cf0a746060d8e57973e7b04e033..a67e9e7e07d0f41c5a4ecd4cf5bc5aad76bdc950 100644 (file)
@@ -45,6 +45,7 @@ public class ITextField extends TextBoxBase implements Paintable, Field,
     private boolean immediate = false;
     private int extraHorizontalPixels = -1;
     private int extraVerticalPixels = -1;
+    private int maxLength;
 
     public ITextField() {
         this(DOM.createInputText());
@@ -63,6 +64,7 @@ public class ITextField extends TextBoxBase implements Paintable, Field,
         sinkEvents(ITooltip.TOOLTIP_EVENTS);
     }
 
+    @Override
     public void onBrowserEvent(Event event) {
         super.onBrowserEvent(event);
         if (client != null) {
@@ -84,6 +86,9 @@ public class ITextField extends TextBoxBase implements Paintable, Field,
             setReadOnly(false);
         }
 
+        setMaxLength(uidl.hasAttribute("maxLength") ? uidl
+                .getIntAttribute("maxLength") : -1);
+
         immediate = uidl.getBooleanAttribute("immediate");
 
         if (uidl.hasAttribute("cols")) {
@@ -94,6 +99,29 @@ public class ITextField extends TextBoxBase implements Paintable, Field,
         valueBeforeEdit = uidl.getStringVariable("text");
     }
 
+    private void setMaxLength(int newMaxLength) {
+        if (newMaxLength > 0) {
+            maxLength = newMaxLength;
+            if (getElement().getTagName().toLowerCase().equals("textarea")) {
+                // NOP no maxlenght property for textarea
+            } else {
+                getElement().setAttribute("maxlength", "" + maxLength);
+            }
+        } else if (maxLength != -1) {
+            if (getElement().getTagName().toLowerCase().equals("textarea")) {
+                // NOP no maxlenght property for textarea
+            } else {
+                getElement().setAttribute("maxlength", "");
+            }
+            maxLength = -1;
+        }
+
+    }
+
+    protected int getMaxLength() {
+        return maxLength;
+    }
+
     public void onChange(Widget sender) {
         if (client != null && id != null) {
             String newText = getText();
index 539993cc803e7f3848ce7e658a4861a447e3db26..dab5fd9dd54ed032e23bc269e07b99e35678113e 100644 (file)
@@ -4,13 +4,16 @@
 
 package com.itmill.toolkit.terminal.gwt.client.ui.richtextarea;
 
+import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.DeferredCommand;
 import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.ui.ChangeListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.FocusListener;
 import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.KeyboardListener;
 import com.google.gwt.user.client.ui.RichTextArea;
 import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
@@ -27,7 +30,7 @@ import com.itmill.toolkit.terminal.gwt.client.ui.Field;
  * 
  */
 public class IRichTextArea extends Composite implements Paintable, Field,
-        ChangeListener, FocusListener {
+        ChangeListener, FocusListener, KeyboardListener {
 
     /**
      * The input node CSS classname.
@@ -53,6 +56,8 @@ public class IRichTextArea extends Composite implements Paintable, Field,
     private int extraHorizontalPixels = -1;
     private int extraVerticalPixels = -1;
 
+    private int maxLength = -1;
+
     public IRichTextArea() {
         fp.add(formatter);
 
@@ -104,7 +109,18 @@ public class IRichTextArea extends Composite implements Paintable, Field,
         }
 
         immediate = uidl.getBooleanAttribute("immediate");
-
+        int newMaxLength = uidl.hasAttribute("maxLength") ? uidl
+                .getIntAttribute("maxLength") : -1;
+        if (newMaxLength >= 0) {
+            if (maxLength == -1) {
+                rta.addKeyboardListener(this);
+            }
+            maxLength = newMaxLength;
+        } else if (maxLength != -1) {
+            getElement().setAttribute("maxlength", "");
+            maxLength = -1;
+            rta.removeKeyboardListener(this);
+        }
     }
 
     public void onChange(Widget sender) {
@@ -206,4 +222,24 @@ public class IRichTextArea extends Composite implements Paintable, Field,
         }
     }
 
+    public void onKeyDown(Widget sender, char keyCode, int modifiers) {
+        // NOP
+    }
+
+    public void onKeyPress(Widget sender, char keyCode, int modifiers) {
+        if (maxLength >= 0) {
+            DeferredCommand.addCommand(new Command() {
+                public void execute() {
+                    if (rta.getHTML().length() > maxLength) {
+                        rta.setHTML(rta.getHTML().substring(0, maxLength));
+                    }
+                }
+            });
+        }
+    }
+
+    public void onKeyUp(Widget sender, char keyCode, int modifiers) {
+        // NOP
+    }
+
 }
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket932.java b/src/com/itmill/toolkit/tests/tickets/Ticket932.java
new file mode 100644 (file)
index 0000000..c0fd9c6
--- /dev/null
@@ -0,0 +1,58 @@
+package com.itmill.toolkit.tests.tickets;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.RichTextArea;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+
+public class Ticket932 extends Application {
+
+    @Override
+    public void init() {
+
+        final Window mainWin = new Window("Test app for max length feature");
+        setMainWindow(mainWin);
+
+        final TextField tx = new TextField("Textfield with maxlenght 10");
+        mainWin.addComponent(tx);
+        tx.setImmediate(true);
+        tx.setRows(5);
+        tx.setMaxLength(10);
+
+        final Label l = new Label();
+
+        Button b = new Button("Check value");
+        b.addListener(new Button.ClickListener() {
+
+            public void buttonClick(ClickEvent event) {
+                l.setValue("Length: " + tx.getValue().toString().length()
+                        + " Content: " + tx.getValue());
+            }
+        });
+
+        mainWin.addComponent(tx);
+        mainWin.addComponent(b);
+        mainWin.addComponent(l);
+
+        final RichTextArea rta = new RichTextArea();
+        rta.setCaption("RTA with max lenght 10");
+
+        rta.setMaxLength(10);
+
+        b = new Button("Check value");
+        b.addListener(new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                l.setValue("Length: " + rta.getValue().toString().length()
+                        + " Content: " + rta.getValue());
+            }
+        });
+
+        mainWin.addComponent(rta);
+        mainWin.addComponent(b);
+
+    }
+
+}
index 10996edb1812504d735259a33e7d870fbeb540cf..213eb2635b62f797c30595929344b8e79fb1a952 100644 (file)
@@ -10,6 +10,9 @@ import com.itmill.toolkit.terminal.PaintTarget;
 /**
  * A simple RichTextEditor to edit HTML format text.
  * 
+ * Note, that using {@link TextField#setMaxLength(int)} method in
+ * {@link RichTextArea} may produce unexpected results as formatting is counted
+ * into length of field.
  */
 public class RichTextArea extends TextField {
 
index 5523b8d36053034692a3d83be6e86052072682b8..dd8596f087a72e302758380795751d06b9f76595 100644 (file)
@@ -73,6 +73,11 @@ public class TextField extends AbstractField {
      */
     private boolean nullSettingAllowed = false;
 
+    /**
+     * Maximum character count in text field.
+     */
+    private int maxLength = -1;
+
     /* Constructors */
 
     /**
@@ -141,6 +146,7 @@ public class TextField extends AbstractField {
      * Paints this component. Don't add a JavaDoc comment here, we use the
      * default documentation from implemented interface.
      */
+    @Override
     public void paintContent(PaintTarget target) throws PaintException {
         super.paintContent(target);
 
@@ -149,6 +155,10 @@ public class TextField extends AbstractField {
             target.addAttribute("secret", true);
         }
 
+        if (getMaxLength() >= 0) {
+            target.addAttribute("maxLength", getMaxLength());
+        }
+
         // Adds the number of column and rows
         final int c = getColumns();
         final int r = getRows();
@@ -184,6 +194,7 @@ public class TextField extends AbstractField {
      * @see Format
      * @deprecated
      */
+    @Deprecated
     protected String getFormattedValue() {
         Object v = getValue();
         if (v == null) {
@@ -197,6 +208,7 @@ public class TextField extends AbstractField {
      * JavaDoc comment here, we use the default documentation from implemented
      * interface.
      */
+    @Override
     public Object getValue() {
         Object v = super.getValue();
         if (format == null || v == null) {
@@ -213,6 +225,7 @@ public class TextField extends AbstractField {
      * Gets the components UIDL tag string. Don't add a JavaDoc comment here, we
      * use the default documentation from implemented interface.
      */
+    @Override
     public String getTag() {
         return "textfield";
     }
@@ -222,6 +235,7 @@ public class TextField extends AbstractField {
      * comment here, we use the default documentation from implemented
      * interface.
      */
+    @Override
     public void changeVariables(Object source, Map variables) {
 
         super.changeVariables(source, variables);
@@ -232,6 +246,11 @@ public class TextField extends AbstractField {
             // Only do the setting if the string representation of the value
             // has been updated
             String newValue = (String) variables.get("text");
+
+            // server side check for max length
+            if (getMaxLength() != -1 && newValue.length() > getMaxLength()) {
+                newValue = newValue.substring(0, getMaxLength());
+            }
             final String oldValue = getFormattedValue();
             if (newValue != null
                     && (oldValue == null || isNullSettingAllowed())
@@ -336,6 +355,7 @@ public class TextField extends AbstractField {
      * Gets the edited property's type. Don't add a JavaDoc comment here, we use
      * the default documentation from implemented interface.
      */
+    @Override
     public Class getType() {
         return String.class;
     }
@@ -460,6 +480,7 @@ public class TextField extends AbstractField {
      * @return the Format used to format the value.
      * @deprecated
      */
+    @Deprecated
     public Format getFormat() {
         return format;
     }
@@ -472,13 +493,37 @@ public class TextField extends AbstractField {
      *            formatting.
      * @deprecated
      */
+    @Deprecated
     public void setFormat(Format format) {
         this.format = format;
         requestRepaint();
     }
 
+    @Override
     protected boolean isEmpty() {
         return super.isEmpty() || toString().length() == 0;
     }
 
+    /**
+     * Returns the maximum number of characters in the field. Value -1 is
+     * considered unlimited. Terminal may however have some technical limits.
+     * 
+     * @return the maxLength
+     */
+    public int getMaxLength() {
+        return maxLength;
+    }
+
+    /**
+     * Sets the maximum number of characters in the field. Value -1 is
+     * considered unlimited. Terminal may however have some technical limits.
+     * 
+     * @param maxLength
+     *            the maxLength to set
+     */
+    public void setMaxLength(int maxLength) {
+        this.maxLength = maxLength;
+        requestRepaint();
+    }
+
 }