\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
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
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
} 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
private boolean immediate = false;
private int extraHorizontalPixels = -1;
private int extraVerticalPixels = -1;
+ private int maxLength;
public ITextField() {
this(DOM.createInputText());
sinkEvents(ITooltip.TOOLTIP_EVENTS);
}
+ @Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
if (client != null) {
setReadOnly(false);
}
+ setMaxLength(uidl.hasAttribute("maxLength") ? uidl
+ .getIntAttribute("maxLength") : -1);
+
immediate = uidl.getBooleanAttribute("immediate");
if (uidl.hasAttribute("cols")) {
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();
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;
*
*/
public class IRichTextArea extends Composite implements Paintable, Field,
- ChangeListener, FocusListener {
+ ChangeListener, FocusListener, KeyboardListener {
/**
* The input node CSS classname.
private int extraHorizontalPixels = -1;
private int extraVerticalPixels = -1;
+ private int maxLength = -1;
+
public IRichTextArea() {
fp.add(formatter);
}
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) {
}
}
+ 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
+ }
+
}
--- /dev/null
+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);
+
+ }
+
+}
/**
* 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 {
*/
private boolean nullSettingAllowed = false;
+ /**
+ * Maximum character count in text field.
+ */
+ private int maxLength = -1;
+
/* Constructors */
/**
* 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);
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();
* @see Format
* @deprecated
*/
+ @Deprecated
protected String getFormattedValue() {
Object v = getValue();
if (v == null) {
* JavaDoc comment here, we use the default documentation from implemented
* interface.
*/
+ @Override
public Object getValue() {
Object v = super.getValue();
if (format == null || v == null) {
* 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";
}
* comment here, we use the default documentation from implemented
* interface.
*/
+ @Override
public void changeVariables(Object source, Map variables) {
super.changeVariables(source, variables);
// 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())
* 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;
}
* @return the Format used to format the value.
* @deprecated
*/
+ @Deprecated
public Format getFormat() {
return format;
}
* 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();
+ }
+
}