From c2aa19f4f488818d4aed799248e69b9fb735bea8 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 7 Dec 2010 10:26:15 +0000 Subject: [PATCH] #3752 - Refactor TextFields svn changeset:16348/svn branch:6.5 --- src/com/vaadin/ui/RichTextArea.java | 230 +++++++++++++++++- .../richtextarea/RichTextAreaTest.java | 54 ++++ .../richtextarea/RichTextAreasGeneric.java | 13 - .../com/vaadin/tests/tickets/Ticket932.java | 17 -- 4 files changed, 281 insertions(+), 33 deletions(-) create mode 100644 tests/src/com/vaadin/tests/components/richtextarea/RichTextAreaTest.java delete mode 100644 tests/src/com/vaadin/tests/components/richtextarea/RichTextAreasGeneric.java diff --git a/src/com/vaadin/ui/RichTextArea.java b/src/com/vaadin/ui/RichTextArea.java index 10e5259bc2..d371e3c181 100644 --- a/src/com/vaadin/ui/RichTextArea.java +++ b/src/com/vaadin/ui/RichTextArea.java @@ -4,6 +4,9 @@ package com.vaadin.ui; +import java.text.Format; +import java.util.Map; + import com.vaadin.data.Property; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; @@ -17,11 +20,31 @@ import com.vaadin.ui.ClientWidget.LoadStyle; * {@link RichTextArea} may produce unexpected results as formatting is counted * into length of field. */ -@SuppressWarnings({ "serial", "unchecked" }) @ClientWidget(value = VRichTextArea.class, loadStyle = LoadStyle.LAZY) -public class RichTextArea extends AbstractTextField { +public class RichTextArea extends AbstractField { + + /** + * Value formatter used to format the string contents. + */ + @Deprecated + private Format format; - private boolean selectAll; + /** + * Null representation. + */ + private String nullRepresentation = "null"; + + /** + * Is setting to null from non-null value allowed by setting with null + * representation . + */ + private boolean nullSettingAllowed = false; + + /** + * Temporary flag that indicates all content will be selected after the next + * paint. Reset to false after painted. + */ + private boolean selectAll = false; /** * Constructs an empty RichTextArea with no caption. @@ -87,6 +110,18 @@ public class RichTextArea extends AbstractTextField { target.addAttribute("selectAll", true); selectAll = false; } + + // Adds the content as variable + String value = getFormattedValue(); + if (value == null) { + value = getNullRepresentation(); + } + if (value == null) { + throw new IllegalStateException( + "Null values are not allowed if the null-representation is null"); + } + target.addVariable(this, "text", value); + super.paintContent(target); } @@ -121,4 +156,193 @@ public class RichTextArea extends AbstractTextField { requestRepaint(); } + /** + * Gets the formatted string value. Sets the field value by using the + * assigned Format. + * + * @return the Formatted value. + * @see #setFormat(Format) + * @see Format + * @deprecated + */ + @Deprecated + protected String getFormattedValue() { + Object v = getValue(); + if (v == null) { + return null; + } + return v.toString(); + } + + @Override + public Object getValue() { + Object v = super.getValue(); + if (format == null || v == null) { + return v; + } + try { + return format.format(v); + } catch (final IllegalArgumentException e) { + return v; + } + } + + @Override + public void changeVariables(Object source, Map variables) { + + super.changeVariables(source, variables); + + // Sets the text + if (variables.containsKey("text") && !isReadOnly()) { + + // Only do the setting if the string representation of the value + // has been updated + String newValue = (String) variables.get("text"); + + final String oldValue = getFormattedValue(); + if (newValue != null + && (oldValue == null || isNullSettingAllowed()) + && newValue.equals(getNullRepresentation())) { + newValue = null; + } + if (newValue != oldValue + && (newValue == null || !newValue.equals(oldValue))) { + boolean wasModified = isModified(); + setValue(newValue, true); + + // If the modified status changes, or if we have a formatter, + // repaint is needed after all. + if (format != null || wasModified != isModified()) { + requestRepaint(); + } + } + } + + } + + @Override + public Class getType() { + return String.class; + } + + /** + * Gets the null-string representation. + * + *

+ * The null-valued strings are represented on the user interface by + * replacing the null value with this string. If the null representation is + * set null (not 'null' string), painting null value throws exception. + *

+ * + *

+ * The default value is string 'null'. + *

+ * + * @return the String Textual representation for null strings. + * @see TextField#isNullSettingAllowed() + */ + public String getNullRepresentation() { + return nullRepresentation; + } + + /** + * Is setting nulls with null-string representation allowed. + * + *

+ * If this property is true, writing null-representation string to text + * field always sets the field value to real null. If this property is + * false, null setting is not made, but the null values are maintained. + * Maintenance of null-values is made by only converting the textfield + * contents to real null, if the text field matches the null-string + * representation and the current value of the field is null. + *

+ * + *

+ * By default this setting is false + *

+ * + * @return boolean Should the null-string represenation be always converted + * to null-values. + * @see TextField#getNullRepresentation() + */ + public boolean isNullSettingAllowed() { + return nullSettingAllowed; + } + + /** + * Sets the null-string representation. + * + *

+ * The null-valued strings are represented on the user interface by + * replacing the null value with this string. If the null representation is + * set null (not 'null' string), painting null value throws exception. + *

+ * + *

+ * The default value is string 'null' + *

+ * + * @param nullRepresentation + * Textual representation for null strings. + * @see TextField#setNullSettingAllowed(boolean) + */ + public void setNullRepresentation(String nullRepresentation) { + this.nullRepresentation = nullRepresentation; + } + + /** + * Sets the null conversion mode. + * + *

+ * If this property is true, writing null-representation string to text + * field always sets the field value to real null. If this property is + * false, null setting is not made, but the null values are maintained. + * Maintenance of null-values is made by only converting the textfield + * contents to real null, if the text field matches the null-string + * representation and the current value of the field is null. + *

+ * + *

+ * By default this setting is false. + *

+ * + * @param nullSettingAllowed + * Should the null-string represenation be always converted to + * null-values. + * @see TextField#getNullRepresentation() + */ + public void setNullSettingAllowed(boolean nullSettingAllowed) { + this.nullSettingAllowed = nullSettingAllowed; + } + + /** + * Gets the value formatter of TextField. + * + * @return the Format used to format the value. + * @deprecated replaced by {@link com.vaadin.data.util.PropertyFormatter} + */ + @Deprecated + public Format getFormat() { + return format; + } + + /** + * Gets the value formatter of TextField. + * + * @param format + * the Format used to format the value. Null disables the + * formatting. + * @deprecated replaced by {@link com.vaadin.data.util.PropertyFormatter} + */ + @Deprecated + public void setFormat(Format format) { + this.format = format; + requestRepaint(); + } + + @Override + protected boolean isEmpty() { + return super.isEmpty() || toString().length() == 0; + } + } diff --git a/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreaTest.java b/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreaTest.java new file mode 100644 index 0000000000..ad2f82aca6 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreaTest.java @@ -0,0 +1,54 @@ +package com.vaadin.tests.components.richtextarea; + +import java.util.LinkedHashMap; + +import com.vaadin.tests.components.abstractfield.AbstractFieldTest; +import com.vaadin.ui.RichTextArea; + +public class RichTextAreaTest extends AbstractFieldTest { + + @Override + protected Class getTestClass() { + return RichTextArea.class; + } + + private Command nullSelectionAllowedCommand = new Command() { + + public void execute(RichTextArea c, Boolean value, Object data) { + c.setNullSettingAllowed(value); + + } + }; + private Command nullRepresentationCommand = new Command() { + + public void execute(RichTextArea c, String value, Object data) { + c.setNullRepresentation(value); + } + }; + + @Override + protected void createActions() { + super.createActions(); + + createSetTextValueAction(CATEGORY_ACTIONS); + + createNullSettingAllowedAction(CATEGORY_FEATURES); + createNullRepresentationAction(CATEGORY_FEATURES); + } + + private void createNullSettingAllowedAction(String category) { + createBooleanAction("Null selection allowed", category, true, + nullSelectionAllowedCommand); + } + + private void createNullRepresentationAction(String category) { + LinkedHashMap options = new LinkedHashMap(); + options.put("-", null); + options.put("null", "null"); + options.put("This is empty", "This is empty"); + options.put("- Nothing -", "- Nothing -"); + createSelectAction("Null representation", category, options, "null", + nullRepresentationCommand); + } + +} diff --git a/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreasGeneric.java b/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreasGeneric.java deleted file mode 100644 index ca8b641e5f..0000000000 --- a/tests/src/com/vaadin/tests/components/richtextarea/RichTextAreasGeneric.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.vaadin.tests.components.richtextarea; - -import com.vaadin.tests.components.abstractfield.AbstractTextFieldTest; -import com.vaadin.ui.RichTextArea; - -public class RichTextAreasGeneric extends AbstractTextFieldTest { - - @Override - protected Class getTestClass() { - return RichTextArea.class; - } - -} diff --git a/tests/src/com/vaadin/tests/tickets/Ticket932.java b/tests/src/com/vaadin/tests/tickets/Ticket932.java index 93f8dc3b6e..a9333b5e0f 100644 --- a/tests/src/com/vaadin/tests/tickets/Ticket932.java +++ b/tests/src/com/vaadin/tests/tickets/Ticket932.java @@ -4,7 +4,6 @@ import com.vaadin.Application; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Label; -import com.vaadin.ui.RichTextArea; import com.vaadin.ui.TextArea; import com.vaadin.ui.TextField; import com.vaadin.ui.Window; @@ -57,22 +56,6 @@ public class Ticket932 extends Application { 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); - } } -- 2.39.5