c = ConverterUtil.getConverter(String.class,
newDataSource.getType(), getApplication());
setConverter(c);
}
dataSource = newDataSource;
// Listens the new data source if possible
if (dataSource != null
&& Property.ValueChangeNotifier.class
.isAssignableFrom(dataSource.getClass())) {
((Property.ValueChangeNotifier) dataSource).addListener(this);
}
requestRepaint();
}
/**
* Gets the content mode of the Label.
*
* @return the Content mode of the label.
*
* @see ContentMode
*/
public ContentMode getContentMode() {
return getState().getContentMode();
}
/**
* Sets the content mode of the Label.
*
* @param contentMode
* the New content mode of the label.
*
* @see ContentMode
*/
public void setContentMode(ContentMode contentMode) {
if (contentMode == null) {
throw new IllegalArgumentException("Content mode can not be null");
}
getState().setContentMode(contentMode);
requestRepaint();
}
/* Value change events */
private static final Method VALUE_CHANGE_METHOD;
static {
try {
VALUE_CHANGE_METHOD = Property.ValueChangeListener.class
.getDeclaredMethod("valueChange",
new Class[] { Property.ValueChangeEvent.class });
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error finding methods in Label");
}
}
/**
* Value change event
*
* @author Vaadin Ltd.
* @version
* @VERSION@
* @since 3.0
*/
public static class ValueChangeEvent extends Component.Event implements
Property.ValueChangeEvent {
/**
* New instance of text change event
*
* @param source
* the Source of the event.
*/
public ValueChangeEvent(Label source) {
super(source);
}
/**
* Gets the Property that has been modified.
*
* @see com.vaadin.data.Property.ValueChangeEvent#getProperty()
*/
@Override
public Property getProperty() {
return (Property) getSource();
}
}
/**
* Adds the value change listener.
*
* @param listener
* the Listener to be added.
* @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com.vaadin.data.Property.ValueChangeListener)
*/
@Override
public void addListener(Property.ValueChangeListener listener) {
addListener(Label.ValueChangeEvent.class, listener, VALUE_CHANGE_METHOD);
}
/**
* Removes the value change listener.
*
* @param listener
* the Listener to be removed.
* @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com.vaadin.data.Property.ValueChangeListener)
*/
@Override
public void removeListener(Property.ValueChangeListener listener) {
removeListener(Label.ValueChangeEvent.class, listener,
VALUE_CHANGE_METHOD);
}
/**
* Emits the options change event.
*/
protected void fireValueChange() {
// Set the error message
fireEvent(new Label.ValueChangeEvent(this));
}
/**
* Listens the value change events from data source.
*
* @see com.vaadin.data.Property.ValueChangeListener#valueChange(Property.ValueChangeEvent)
*/
@Override
public void valueChange(Property.ValueChangeEvent event) {
// Update the internal value from the data source
getState().setText(getValue());
requestRepaint();
fireValueChange();
}
private String getComparableValue() {
String stringValue = getValue();
if (stringValue == null) {
stringValue = "";
}
if (getContentMode() == ContentMode.XHTML
|| getContentMode() == ContentMode.XML) {
return stripTags(stringValue);
} else {
return stringValue;
}
}
/**
* Compares the Label to other objects.
*
*
* Labels can be compared to other labels for sorting label contents. This
* is especially handy for sorting table columns.
*
*
*
* In RAW, PREFORMATTED and TEXT modes, the label contents are compared as
* is. In XML, UIDL and XHTML modes, only CDATA is compared and tags
* ignored. If the other object is not a Label, its toString() return value
* is used in comparison.
*
*
* @param other
* the Other object to compare to.
* @return a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Label other) {
String thisValue = getComparableValue();
String otherValue = other.getComparableValue();
return thisValue.compareTo(otherValue);
}
/**
* Strips the tags from the XML.
*
* @param xml
* the String containing a XML snippet.
* @return the original XML without tags.
*/
private String stripTags(String xml) {
final StringBuffer res = new StringBuffer();
int processed = 0;
final int xmlLen = xml.length();
while (processed < xmlLen) {
int next = xml.indexOf('<', processed);
if (next < 0) {
next = xmlLen;
}
res.append(xml.substring(processed, next));
if (processed < xmlLen) {
next = xml.indexOf('>', processed);
if (next < 0) {
next = xmlLen;
}
processed = next + 1;
}
}
return res.toString();
}
/**
* Gets the converter used to convert the property data source value to the
* label value.
*
* @return The converter or null if none is set.
*/
public Converter getConverter() {
return converter;
}
/**
* Sets the converter used to convert the label value to the property data
* source type. The converter must have a presentation type of String.
*
* @param converter
* The new converter to use.
*/
public void setConverter(Converter converter) {
this.converter = (Converter) converter;
requestRepaint();
}
}