Skip to content

Commit

Permalink
Parse readonly values for AbstractFields correctly (#18850)
Browse files Browse the repository at this point in the history
Change-Id: I1128ec0bda9b0fdad721f084c1e78cfe78e984f2
  • Loading branch information
johndevs authored and Henri Sara committed Oct 4, 2015
1 parent e941e19 commit 76b42e6
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 30 deletions.
32 changes: 27 additions & 5 deletions server/src/com/vaadin/ui/AbstractField.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ public void commit() throws Buffered.SourceException, InvalidValueException {
committingValueToDataSource = false;
}
} else {
/* An invalid value and we don't allow them, throw the exception */
/*
* An invalid value and we don't allow them, throw the exception
*/
validate();
}
}
Expand Down Expand Up @@ -459,15 +461,35 @@ public void setValue(T newFieldValue) throws Property.ReadOnlyException,
* @param repaintIsNotNeeded
* True iff caller is sure that repaint is not needed.
* @throws Property.ReadOnlyException
* @throws Converter.ConversionException
* @throws InvalidValueException
*/
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded) {
setValue(newFieldValue, repaintIsNotNeeded, false);
}

/**
* Sets the value of the field.
*
* @since 7.5.7
* @param newFieldValue
* the New value of the field.
* @param repaintIsNotNeeded
* True iff caller is sure that repaint is not needed.
* @param ignoreReadOnly
* True iff if the read-only check should be ignored
* @throws Property.ReadOnlyException
* @throws Converter.ConversionException
* @throws InvalidValueException
*/
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded)
throws Property.ReadOnlyException, Converter.ConversionException,
InvalidValueException {
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded,
boolean ignoreReadOnly) throws Property.ReadOnlyException,
Converter.ConversionException, InvalidValueException {

if (!SharedUtil.equals(newFieldValue, getInternalValue())) {

// Read only fields can not be changed
if (isReadOnly()) {
if (!ignoreReadOnly && isReadOnly()) {
throw new Property.ReadOnlyException();
}
try {
Expand Down
31 changes: 20 additions & 11 deletions server/src/com/vaadin/ui/AbstractSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.Converter.ConversionException;
import com.vaadin.data.util.converter.ConverterUtil;
import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.Transferable;
Expand Down Expand Up @@ -697,26 +699,33 @@ public void setValue(Object newValue) throws Property.ReadOnlyException {
* multiselect mode all collections of id:s can be assigned.
* </p>
*
* @since 7.5.7
* @param newValue
* the New selected item or collection of selected items.
* @param repaintIsNotNeeded
* True if caller is sure that repaint is not needed.
* @param ignoreReadOnly
* True if read-only check should be omitted.
* @see com.vaadin.ui.AbstractField#setValue(java.lang.Object,
* java.lang.Boolean)
*/
@Override
protected void setValue(Object newValue, boolean repaintIsNotNeeded)
throws Property.ReadOnlyException {

protected void setValue(Object newFieldValue, boolean repaintIsNotNeeded,
boolean ignoreReadOnly)
throws com.vaadin.data.Property.ReadOnlyException,
ConversionException, InvalidValueException {
if (isMultiSelect()) {
if (newValue == null) {
super.setValue(new LinkedHashSet<Object>(), repaintIsNotNeeded);
} else if (Collection.class.isAssignableFrom(newValue.getClass())) {
if (newFieldValue == null) {
super.setValue(new LinkedHashSet<Object>(), repaintIsNotNeeded,
ignoreReadOnly);
} else if (Collection.class.isAssignableFrom(newFieldValue
.getClass())) {
super.setValue(new LinkedHashSet<Object>(
(Collection<?>) newValue), repaintIsNotNeeded);
(Collection<?>) newFieldValue), repaintIsNotNeeded,
ignoreReadOnly);
}
} else if (newValue == null || items.containsId(newValue)) {
super.setValue(newValue, repaintIsNotNeeded);
} else if (newFieldValue == null || items.containsId(newFieldValue)) {
super.setValue(newFieldValue, repaintIsNotNeeded, ignoreReadOnly);
}
}

Expand Down Expand Up @@ -2196,9 +2205,9 @@ protected void readItems(Element design, DesignContext context) {
}
if (!selected.isEmpty()) {
if (isMultiSelect()) {
setValue(selected);
setValue(selected, false, true);
} else if (selected.size() == 1) {
setValue(selected.iterator().next());
setValue(selected.iterator().next(), false, true);
} else {
throw new DesignException(
"Multiple values selected for a single select component");
Expand Down
5 changes: 3 additions & 2 deletions server/src/com/vaadin/ui/CheckBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ public boolean booleanValue() {
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
if (design.hasAttr("checked")) {
this.setValue(DesignAttributeHandler.readAttribute("checked",
design.attributes(), Boolean.class));
this.setValue(
DesignAttributeHandler.readAttribute("checked",
design.attributes(), Boolean.class), false, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/com/vaadin/ui/DateField.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ public void readDesign(Element design, DesignContext designContext) {
Logger.getLogger(DateField.class.getName()).info(
"cannot parse " + design.attr("value") + " as date");
}
this.setValue(date);
this.setValue(date, false, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/com/vaadin/ui/PasswordField.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void readDesign(Element design, DesignContext designContext) {
Attributes attr = design.attributes();
if (attr.hasKey("value")) {
setValue(DesignAttributeHandler.readAttribute("value", attr,
String.class));
String.class), false, true);
}
}

Expand Down
5 changes: 3 additions & 2 deletions server/src/com/vaadin/ui/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ protected void setInternalValue(Float newValue) {
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
if (design.hasAttr("value") && !design.attr("value").isEmpty()) {
setValue(DesignAttributeHandler.readAttribute("value",
design.attributes(), Float.class));
setValue(
DesignAttributeHandler.readAttribute("value",
design.attributes(), Float.class), false, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/com/vaadin/ui/RichTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void clear() {
@Override
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
setValue(design.html());
setValue(design.html(), false, true);
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions server/src/com/vaadin/ui/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ public void readDesign(Element design, DesignContext context) {
if (attr.hasKey("vertical")) {
setOrientation(SliderOrientation.VERTICAL);
}
if (!attr.get("value").isEmpty()) {
setValue(DesignAttributeHandler.readAttribute("value", attr,
Double.class));
if (attr.hasKey("value")) {
Double newFieldValue = DesignAttributeHandler.readAttribute(
"value", attr, Double.class);
setValue(newFieldValue, false, true);
}
}

Expand Down
3 changes: 2 additions & 1 deletion server/src/com/vaadin/ui/TextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public boolean isWordwrap() {
@Override
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
setValue(DesignFormatter.unencodeFromTextNode(design.html()));
setValue(DesignFormatter.unencodeFromTextNode(design.html()), false,
true);
}

/*
Expand Down
5 changes: 3 additions & 2 deletions server/src/com/vaadin/ui/TextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);
Attributes attr = design.attributes();
if (attr.hasKey("value")) {
setValue(DesignAttributeHandler.readAttribute("value", attr,
String.class));
String newFieldValue = DesignAttributeHandler.readAttribute(
"value", attr, String.class);
setValue(newFieldValue, false, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public void testUnchecked() {
testRead(design, checkBox);
testWrite(design, checkBox);
}

@Test
public void testReadOnlyValue() {
String design = "<v-check-box readonly='true' checked='true' />";
CheckBox checkBox = new CheckBox();
checkBox.setValue(true);
checkBox.setReadOnly(true);
testRead(design, checkBox);
testWrite(design, checkBox);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public void testBasicWrite() {
testWrite(getBasicDesign(), getBasicExpected());
}

@Test
public void testReadOnlyValue() {
String design = "<v-combo-box readonly='true' value='foo'><option selected>foo</option></v-combo-box>";

ComboBox comboBox = new ComboBox();
comboBox.addItems("foo", "bar");
comboBox.setValue("foo");
comboBox.setReadOnly(true);

testRead(design, comboBox);

// Selects items are not written out by default
String design2 = "<v-combo-box readonly='true'></v-combo-box>";
testWrite(design2, comboBox);
}

private String getBasicDesign() {
return "<v-combo-box input-prompt=\"Select something\" filtering-mode=\"off\" scroll-to-selected-item='false'>";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ public void writeYearResolution() {
"2020-01-01 00:00:00+0200"),
getYearResolutionExpected());
}

@Test
public void testReadOnlyValue() {
String design = "<v-date-field readonly='true' resolution='year' value='2020-01-01 00:00:00+0200'/>";
DateField df = new DateField();
df.setResolution(Resolution.YEAR);
df.setValue(new Date(2020 - 1900, 1 - 1, 1));
df.setReadOnly(true);

testRead(design, df);
testWrite(design, df);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@ public void testWriteBasic() {
testWrite(stripOptionTags(getBasicDesign()), getBasicExpected());
}

@Test
public void testReadOnlyValue() {
String design = "<v-native-select readonly='true'><option selected>foo</option><option>bar</option></v-native-select>";

NativeSelect ns = new NativeSelect();
ns.addItems("foo", "bar");
ns.setValue("foo");
ns.setReadOnly(true);

testRead(design, ns);

// Selects items are not written out by default
String design2 = "<v-native-select readonly='true'></v-native-select>";
testWrite(design2, ns);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.server.component.passwordfield;

import org.junit.Test;

import com.vaadin.tests.design.DeclarativeTestBase;
import com.vaadin.ui.PasswordField;

/**
*
* @since
* @author Vaadin Ltd
*/
public class PasswordFieldDeclarativeTest extends
DeclarativeTestBase<PasswordField> {

@Test
public void testReadOnlyValue() {
String design = "<v-password-field readonly=\"true\" value=\"test value\"/>";
PasswordField tf = new PasswordField();
tf.setValue("test value");
tf.setReadOnly(true);
testRead(design, tf);
testWrite(design, tf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ public void testWriteEmpty() {
testWrite("<v-progress-bar>", new ProgressBar());
}

}
@Test
public void testReadOnlyValue() {
String design = "<v-progress-bar readonly='true' value=0.5 indeterminate='true'>";
ProgressBar progressBar = new ProgressBar();
progressBar.setIndeterminate(true);
progressBar.setValue(0.5f);
progressBar.setReadOnly(true);

testRead(design, progressBar);
testWrite(design, progressBar);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ public void testReadEmpty() {
public void testWriteEmpty() {
testWrite("<v-rich-text-area />", new RichTextArea());
}

@Test
public void testReadOnlyValue() {
String design = "<v-rich-text-area readonly='true' style-name='v-richtextarea-readonly'>Hello World!</v-text-area>";
RichTextArea ta = new RichTextArea();
ta.setValue("Hello World!");
ta.setReadOnly(true);

testRead(design, ta);
testWrite(design, ta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ public void testVertical() {
testRead(design, expected);
testWrite(design, expected);
}

@Test
public void testReadOnlyValue() {
String design = "<v-slider readonly='true' min=10 max=20 resolution=1 value=12.3>";

Slider expected = new Slider();
expected.setMin(10.0);
expected.setMax(20.0);
expected.setResolution(1);
expected.setValue(12.3);
expected.setReadOnly(true);

testRead(design, expected);
testWrite(design, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public void testHtmlEntities() throws IOException {
read.writeDesign(root, dc);

Assert.assertEquals("&amp;amp; Test", root.html());
}

@Test
public void testReadOnlyValue() {
String design = "<v-text-area readonly='true' rows=6 wordwrap=false>Hello World!</v-text-area>";
TextArea ta = new TextArea();
ta.setRows(6);
ta.setWordwrap(false);
ta.setValue("Hello World!");
ta.setReadOnly(true);
testRead(design, ta);
testWrite(design, ta);
}
}
Loading

0 comments on commit 76b42e6

Please sign in to comment.