The toString() method should not be used on a property to get its value.
Added getStringValue() to AbstractField, Label etc. Using them
where applicable. Added comments to some problematic locations where
Property.toString() is known to be used.
AbstractField.toString() and Label.toString() still return the same
values as before, but it will throw an exception in future revisions.
Migration needed: Replace explicit and implicit uses of
Property.toString() with use property.getValue() and its
string representation.
Alternatively, use AbstractProperty.getStringValue() and
AbstractField.getStringValue() instead of Property.toString() during
migration.
* should at least understand the format returned by the
* <code>toString</code> method of the Property.
*
+ * TODO correct this comment as eliminating Property.toString()
+ *
* @param newValue
* New value of the Property. This should be assignable to the
* type returned by getType, but also String type should be
public void setValue(Object newValue) throws Property.ReadOnlyException,
Property.ConversionException;
- /**
- * Returns the value of the Property in human readable textual format. The
- * return value should be assignable to the <code>setValue</code> method if
- * the Property is not in read-only mode.
- *
- * @return <code>String</code> representation of the value stored in the
- * Property
- */
- public String toString();
-
/**
* Returns the type of the Property. The methods <code>getValue</code> and
* <code>setValue</code> must be compatible with this type: one must be able
* <code>setValue</code> method if the Property is not in read-only mode.
*
* @return String representation of the value stored in the Property
+ * @deprecated use the property value directly, or {@link #getStringValue()}
+ * during migration period
*/
+ @Deprecated
@Override
public String toString() {
+ return getStringValue();
+ }
+
+ /**
+ * Returns the value of the <code>Property</code> in human readable textual
+ * format.
+ *
+ * @return String representation of the value stored in the Property
+ * @since 7.0
+ */
+ public String getStringValue() {
final Object value = getValue();
if (value == null) {
return null;
for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) {
final Object propertyId = i.next();
+ // TODO do not use Property.toString()
retValue += getItemProperty(propertyId).toString();
if (i.hasNext()) {
retValue += " ";
*
* @return <code>String</code> representation of the value stored in the
* Property
+ * @deprecated use the property value directly, or
+ * {@link #getStringValue()} during migration period
*/
+ @Deprecated
@Override
public String toString() {
+ return getStringValue();
+ }
+
+ /**
+ * Returns the value of the <code>Property</code> in human readable
+ * textual format.
+ *
+ * @return String representation of the value stored in the Property
+ * @since 7.0
+ */
+ public String getStringValue() {
final Object value = getValue();
if (value == null) {
return null;
.removeListener(this);
}
readOnly = isReadOnly();
- prevValue = toString();
+ prevValue = getStringValue();
}
dataSource = newDataSource;
if (isReadOnly() != readOnly) {
fireReadOnlyStatusChange();
}
- String newVal = toString();
+ String newVal = getStringValue();
if ((prevValue == null && newVal != null)
|| (prevValue != null && !prevValue.equals(newVal))) {
fireValueChange();
* String given by format().
*/
public Object getValue() {
- return toString();
+ return getStringValue();
}
/**
- * Get the formatted value.
+ * Get the formatted value. For PropertyFormatter, this is identical with
+ * {@link #getValue()}.
*
* @return If the datasource returns null, this is null. Otherwise this is
* String given by format().
*/
@Override
- public String toString() {
+ public String getStringValue() {
Object value = dataSource == null ? false : dataSource.getValue();
if (value == null) {
return null;
}
/** Reflects the read-only status of the datasource. */
+ @Override
public boolean isReadOnly() {
return dataSource == null ? false : dataSource.isReadOnly();
}
* @param newStatus
* the new read-only status of the Property.
*/
+ @Override
public void setReadOnly(boolean newStatus) {
if (dataSource != null) {
dataSource.setReadOnly(newStatus);
} else {
try {
dataSource.setValue(parse((String) newValue));
- if (!newValue.equals(toString())) {
+ if (!newValue.equals(getStringValue())) {
fireValueChange();
}
} catch (Exception e) {
for (final Iterator<?> i = getItemPropertyIds().iterator(); i.hasNext();) {
final Object propertyId = i.next();
- retValue += getItemProperty(propertyId).toString();
+ retValue += getItemProperty(propertyId).getValue();
if (i.hasNext()) {
retValue += " ";
}
public boolean passesFilter(Object itemId, Item item) {
final Property p = item.getItemProperty(propertyId);
- if (p == null || p.toString() == null) {
+ if (p == null) {
return false;
}
- final String value = ignoreCase ? p.toString().toLowerCase() : p
- .toString();
+ Object propertyValue = p.getValue();
+ if (propertyValue == null) {
+ return false;
+ }
+ final String value = ignoreCase ? propertyValue.toString()
+ .toLowerCase() : propertyValue.toString();
if (onlyMatchPrefix) {
if (!value.startsWith(filterString)) {
return false;
return propertyId;
}
+ /**
+ * Returns the value of the Property in human readable textual format.
+ *
+ * @see java.lang.Object#toString()
+ * @deprecated get the string representation from the value, or use
+ * getStringValue() during migration
+ */
+ @Deprecated
@Override
public String toString() {
- Object val = getValue();
- if (val == null) {
+ return getStringValue();
+ }
+
+ /**
+ * Returns the (UI type) value of the field converted to a String using
+ * toString().
+ *
+ * This method exists to help migration from the use of Property.toString()
+ * to get the field value - for new applications, access getValue()
+ * directly. This method may disappear in future Vaadin versions.
+ *
+ * @return string representation of the field value or null if the value is
+ * null
+ * @since 7.0
+ */
+ public String getStringValue() {
+ final Object value = getValue();
+ if (value == null) {
return null;
}
- return val.toString();
+ return value.toString();
}
public void setOwner(RowItem owner) {
s.append("|");
s.append(propId.toString());
s.append(":");
- s.append(getItemProperty(propId).toString());
+ Object value = getItemProperty(propId).getValue();
+ s.append((null != value) ? value.toString() : null);
}
return s.toString();
}
try {
// Discards buffer by overwriting from datasource
- newValue = String.class == getType() ? dataSource.toString()
- : dataSource.getValue();
+ newValue = dataSource.getValue();
+ if (String.class == getType()) {
+ newValue = newValue.toString();
+ }
// If successful, remove set the buffering state to be ok
if (currentBufferedSourceException != null) {
}
readThroughMode = readThrough;
if (!isModified() && readThroughMode && dataSource != null) {
- setInternalValue(String.class == getType() ? dataSource.toString()
- : dataSource.getValue());
+ Object newValue = dataSource.getValue();
+ if (String.class == getType()) {
+ newValue = newValue.toString();
+ }
+ setInternalValue(newValue);
fireValueChange(false);
}
}
* Returns the value of the Property in human readable textual format.
*
* @see java.lang.Object#toString()
+ * @deprecated get the string representation from the data source, or use
+ * getStringValue() during migration
*/
+ @Deprecated
@Override
public String toString() {
+ return getStringValue();
+ }
+
+ /**
+ * Returns the (UI type) value of the field converted to a String using
+ * toString().
+ *
+ * This method exists to help migration from the use of Property.toString()
+ * to get the field value - for new applications, access getValue()
+ * directly. This method may disappear in future Vaadin versions.
+ *
+ * @return string representation of the field value or null if the value is
+ * null
+ * @since 7.0
+ */
+ public String getStringValue() {
final Object value = getValue();
if (value == null) {
return null;
}
- return getValue().toString();
+ return value.toString();
}
/**
return value;
}
- Object newValue = String.class == getType() ? dataSource.toString()
- : dataSource.getValue();
-
- return newValue;
+ Object result = dataSource.getValue();
+ if (String.class == getType()) {
+ result = result.toString();
+ }
+ return result;
}
/**
// Gets the value from source
try {
if (dataSource != null) {
- setInternalValue(String.class == getType() ? dataSource
- .toString() : dataSource.getValue());
+ Object newValue = dataSource.getValue();
+ if (String.class == getType()) {
+ newValue = newValue.toString();
+ }
+ setInternalValue(newValue);
}
modified = false;
} catch (final Throwable e) {
focusable.focus();
}
}
-}
\ No newline at end of file
+}
@Override
protected boolean isEmpty() {
- return super.isEmpty() || toString().length() == 0;
+ return super.isEmpty() || getStringValue().length() == 0;
}
/**
removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
}
-}
\ No newline at end of file
+}
target.addAttribute("mode", CONTENT_MODE_NAME[contentMode]);
}
if (contentMode == CONTENT_TEXT) {
- target.addText(toString());
+ target.addText(getStringValue());
} else if (contentMode == CONTENT_UIDL) {
- target.addUIDL(toString());
+ target.addUIDL(getStringValue());
} else if (contentMode == CONTENT_XHTML) {
target.startTag("data");
- target.addXMLSection("div", toString(),
+ target.addXMLSection("div", getStringValue(),
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
target.endTag("data");
} else if (contentMode == CONTENT_PREFORMATTED) {
target.startTag("pre");
- target.addText(toString());
+ target.addText(getStringValue());
target.endTag("pre");
} else if (contentMode == CONTENT_XML) {
- target.addXMLSection("data", toString(), null);
+ target.addXMLSection("data", getStringValue(), null);
} else if (contentMode == CONTENT_RAW) {
target.startTag("data");
target.addAttribute("escape", false);
- target.addText(toString());
+ target.addText(getStringValue());
target.endTag("data");
}
/**
* @see java.lang.Object#toString()
+ * @deprecated use the data source value or {@link #getStringValue()}
+ * instead
*/
+ @Deprecated
@Override
public String toString() {
+ return getStringValue();
+ }
+
+ /**
+ * TODO temporary method to help eliminate the use of toString()
+ *
+ * @return
+ */
+ public String getStringValue() {
if (dataSource == null) {
throw new IllegalStateException(DATASOURCE_MUST_BE_SET);
}
+ // TODO do not use Property.toString()
return dataSource.toString();
}
if (contentMode == CONTENT_XML || contentMode == CONTENT_UIDL
|| contentMode == CONTENT_XHTML) {
- thisValue = stripTags(toString());
+ thisValue = stripTags(getStringValue());
} else {
- thisValue = toString();
+ thisValue = getStringValue();
}
if (other instanceof Label
&& (((Label) other).getContentMode() == CONTENT_XML
|| ((Label) other).getContentMode() == CONTENT_UIDL || ((Label) other)
.getContentMode() == CONTENT_XHTML)) {
- otherValue = stripTags(other.toString());
+ otherValue = stripTags(((Label) other).getStringValue());
} else {
+ // TODO not a good idea - and might assume that Field.toString()
+ // returns a string representation of the value
otherValue = other.toString();
}
/**
* @see com.vaadin.ui.AbstractField#toString()
+ * @deprecated use the data source value instead of toString()
*/
+ @Deprecated
@Override
public String toString() {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be set");
}
- return dataSource.toString();
+ Object value = dataSource.getValue();
+ return (null != value) ? value.toString() : null;
}
/**
@Override
protected boolean isEmpty() {
- return super.isEmpty() || toString().length() == 0;
+ return super.isEmpty() || getStringValue().length() == 0;
}
}
if (property == null) {
return "";
}
- return property.toString();
+ Object value = property.getValue();
+ return (null != value) ? value.toString() : "";
}
/* Action container */
import com.vaadin.data.Item.PropertySetChangeEvent;
import com.vaadin.data.Item.PropertySetChangeListener;
-import com.vaadin.data.util.ObjectProperty;
-import com.vaadin.data.util.PropertysetItem;
public class PropertySetItemTest extends TestCase {
item.addItemProperty(ID1, prop1);
- Assert.assertEquals(String.valueOf(prop1), item.toString());
+ Assert.assertEquals(String.valueOf(prop1.getValue()), item.toString());
item.addItemProperty(ID2, prop2);
Assert.assertEquals(
- String.valueOf(prop1) + " " + String.valueOf(prop2),
- item.toString());
+ String.valueOf(prop1.getValue()) + " "
+ + String.valueOf(prop2.getValue()), item.toString());
}
}
Statement statement = conn.createStatement();
statement
.executeUpdate("DELETE FROM people WHERE \"ID\"="
- + item.getItemProperty("ID"));
+ + item.getItemProperty("ID").getValue());
statement.close();
return true;
}
import junit.framework.TestCase;
import com.vaadin.data.Item;
+import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.MethodProperty;
import com.vaadin.data.validator.RegexpValidator;
data));
Serializable s2 = (Serializable) in.readObject();
+ // using special toString(Object) method to avoid calling
+ // Property.toString(), which will be temporarily disabled
if (s.equals(s2)) {
- System.out.println(s + " equals " + s2);
+ System.out.println(toString(s) + " equals " + toString(s2));
} else {
- System.out.println(s + " does NOT equal " + s2);
+ System.out.println(toString(s) + " does NOT equal " + toString(s2));
}
return s2;
}
+ private static String toString(Object o) {
+ if (o instanceof Property) {
+ return String.valueOf(((Property) o).getValue());
+ } else {
+ return String.valueOf(o);
+ }
+ }
+
public static class Data implements Serializable {
private String dummyGetter;
private String dummyGetterAndSetter;
filterButton.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
ic.removeAllContainerFilters();
- if (fooFilter.toString().length() > 0) {
- ic.addContainerFilter("foo", fooFilter.toString(), false,
+ if (fooFilter.getStringValue().length() > 0) {
+ ic.addContainerFilter("foo", fooFilter.getStringValue(), false,
false);
}
- if (barFilter.toString().length() > 0) {
- ic.addContainerFilter("bar", barFilter.toString(), true,
+ if (barFilter.getStringValue().length() > 0) {
+ ic.addContainerFilter("bar", barFilter.getStringValue(), true,
true);
}
count.setValue("Rows in table: " + ic.size());
t.addListener(new Listener() {
public void componentEvent(Event event) {
status.addComponent(new Label(event.getClass().getName()));
+ // TODO should not use Property.toString()
status.addComponent(new Label("selected: "
+ event.getSource().toString()));
}
t.addListener(new Listener() {
public void componentEvent(Event event) {
status.addComponent(new Label(event.getClass().getName()));
+ // TODO should not use Property.toString()
status.addComponent(new Label("selected: "
+ event.getSource().toString()));
}
date.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
if (date.isValid()) {
- getMainWindow().showNotification(date.toString());
+ getMainWindow().showNotification(date.getStringValue());
}
}
tf.addListener(new ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
+ // TODO should not use Property.toString()
l.log("Value change:" + event.getProperty().toString());
}
});
if (transferable instanceof TableTransferable) {
TableTransferable tr = (TableTransferable) transferable;
System.out.println("From table row" + tr.getPropertyId());
+ // TODO should not use Property.toString()
data = tr.getSourceContainer().getItem(tr.getItemId())
.getItemProperty(tr.getPropertyId()).toString();
// if the source is from table (not from tree1 itself),
// transfer Name property and use it as an identifier in
// tree1
+ // TODO should not use Property.toString()
String name = sourceContainer.getItem(itemId)
.getItemProperty("Name").toString();
PopupView.Content content = new PopupView.Content() {
public String getMinimizedValueAsHTML() {
- return prop.toString();
+ return String.valueOf(prop.getValue());
}
public Component getPopupComponent() {
panel2.addComponent(new myButton());
PopupView.Content content2 = new PopupView.Content() {
public String getMinimizedValueAsHTML() {
- return prop2.toString();
+ return String.valueOf(prop2.getValue());
}
public Component getPopupComponent() {
PopupView.Content content3 = new PopupView.Content() {
public String getMinimizedValueAsHTML() {
- return op.toString();
+ return String.valueOf(op.getValue());
}
public Component getPopupComponent() {
final InlineDateField df = new InlineDateField("", new Date());
PopupView pp = new PopupView(new PopupView.Content() {
public String getMinimizedValueAsHTML() {
- return df.toString();
+ return String.valueOf(df.getValue());
}
public Component getPopupComponent() {
+ " and see how the overview-version changes.");
public String getMinimizedValueAsHTML() {
- return "" + tf.toString().length() + " characters of info";
+ return "" + String.valueOf(tf.getValue()).length()
+ + " characters of info";
}
public Component getPopupComponent() {
c.addComponent(tf);
tf.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
+ // TODO should not use Property.toString()
main.addComponent(new Label(name + " send text:"
- + tf.toString()));
+ + tf.getStringValue()));
}
});
for (int i = 0; i < 3; i++) {
height.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
try {
- target.setHeight(height.toString());
+ target.setHeight(height.getStringValue());
height.setComponentError(null);
updateLabel();
} catch (Exception e) {
width.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
try {
- target.setWidth(width.toString());
+ target.setWidth(width.getStringValue());
width.setComponentError(null);
updateLabel();
} catch (Exception e) {