summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatti Tahvonen <matti.tahvonen@itmill.com>2008-12-29 10:58:16 +0000
committerMatti Tahvonen <matti.tahvonen@itmill.com>2008-12-29 10:58:16 +0000
commit52d88518a1017706f8f9a70ffa7b5b636de7bb92 (patch)
tree547ff1341d4b24444d14beb9bf61d9cc11215126 /src
parent03024e474f9017d259ba54975bd1ba80811eb715 (diff)
downloadvaadin-framework-52d88518a1017706f8f9a70ffa7b5b636de7bb92.tar.gz
vaadin-framework-52d88518a1017706f8f9a70ffa7b5b636de7bb92.zip
fixes #932, implemented maxlength for textfield
svn changeset:6360/svn branch:trunk
Diffstat (limited to 'src')
-rw-r--r--src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java22
-rw-r--r--src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java28
-rw-r--r--src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java40
-rw-r--r--src/com/itmill/toolkit/tests/tickets/Ticket932.java58
-rw-r--r--src/com/itmill/toolkit/ui/RichTextArea.java3
-rw-r--r--src/com/itmill/toolkit/ui/TextField.java45
6 files changed, 194 insertions, 2 deletions
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java
index 94bccd7bfd..d3538698d7 100644
--- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java
+++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextArea.java
@@ -4,8 +4,11 @@
package com.itmill.toolkit.terminal.gwt.client.ui;
+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.Event;
import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
import com.itmill.toolkit.terminal.gwt.client.UIDL;
@@ -26,6 +29,7 @@ public class ITextArea extends ITextField {
setStyleName(CLASSNAME);
}
+ @Override
public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
// Call parent renderer explicitly
super.updateFromUIDL(uidl, client);
@@ -33,6 +37,10 @@ public class ITextArea extends ITextField {
if (uidl.hasAttribute("rows")) {
setRows(new Integer(uidl.getStringAttribute("rows")).intValue());
}
+
+ if (getMaxLength() >= 0) {
+ sinkEvents(Event.ONKEYPRESS);
+ }
}
public void setRows(int rows) {
@@ -47,4 +55,18 @@ public class ITextArea extends ITextField {
} catch (e) {}
}-*/;
+ @Override
+ public void onBrowserEvent(Event event) {
+ if (getMaxLength() >= 0 && event.getTypeInt() == Event.ONKEYPRESS) {
+ DeferredCommand.addCommand(new Command() {
+ public void execute() {
+ if (getText().length() > getMaxLength()) {
+ setText(getText().substring(0, getMaxLength()));
+ }
+ }
+ });
+ }
+ super.onBrowserEvent(event);
+ }
+
}
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java
index 3f07ba27d1..a67e9e7e07 100644
--- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java
+++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextField.java
@@ -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();
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java
index 539993cc80..dab5fd9dd5 100644
--- a/src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java
+++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/richtextarea/IRichTextArea.java
@@ -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
index 0000000000..c0fd9c69bc
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/tickets/Ticket932.java
@@ -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);
+
+ }
+
+}
diff --git a/src/com/itmill/toolkit/ui/RichTextArea.java b/src/com/itmill/toolkit/ui/RichTextArea.java
index 10996edb18..213eb2635b 100644
--- a/src/com/itmill/toolkit/ui/RichTextArea.java
+++ b/src/com/itmill/toolkit/ui/RichTextArea.java
@@ -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 {
diff --git a/src/com/itmill/toolkit/ui/TextField.java b/src/com/itmill/toolkit/ui/TextField.java
index 5523b8d360..dd8596f087 100644
--- a/src/com/itmill/toolkit/ui/TextField.java
+++ b/src/com/itmill/toolkit/ui/TextField.java
@@ -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();
+ }
+
}