/* ************************************************************************* IT Mill Toolkit Development of Browser User Interfaces Made Easy Copyright (C) 2000-2006 IT Mill Ltd ************************************************************************* This product is distributed under commercial license that can be found from the product package on license.pdf. Use of this product might require purchasing a commercial license from IT Mill Ltd. For guidelines on usage, see licensing-guidelines.html ************************************************************************* For more information, contact: IT Mill Ltd phone: +358 2 4802 7180 Ruukinkatu 2-4 fax: +358 2 4802 7181 20540, Turku email: info@itmill.com Finland company www: www.itmill.com Primary source for information and releases: www.itmill.com ********************************************************************** */ package com.itmill.toolkit.ui; import java.lang.reflect.Method; import com.itmill.toolkit.data.Property; import com.itmill.toolkit.data.util.ObjectProperty; import com.itmill.toolkit.terminal.PaintException; import com.itmill.toolkit.terminal.PaintTarget; /** * Label component for showing non-editable short texts. * * The label content can be set to the modes specified by the final members * CONTENT_* * *
* The contents of the label may contain simple formatting: *
true
if the component is in read only mode.
*/
public boolean isReadOnly() {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be se");
}
return dataSource.isReadOnly();
}
/**
* Paints the content of this component.
*
* @param target
* the Paint Event.
* @throws PaintException
* if the Paint Operation fails.
*/
public void paintContent(PaintTarget target) throws PaintException {
if (contentMode != CONTENT_TEXT) {
target.addAttribute("mode", CONTENT_MODE_NAME[contentMode]);
}
if (contentMode == CONTENT_TEXT) {
target.addText(toString());
} else if (contentMode == CONTENT_UIDL) {
target.addUIDL(toString());
} else if (contentMode == CONTENT_XHTML) {
target.startTag("data");
target.addXMLSection("div", toString(),
"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.endTag("pre");
} else if (contentMode == CONTENT_XML) {
target.addXMLSection("data", toString(), null);
} else if (contentMode == CONTENT_RAW) {
target.startTag("data");
target.addAttribute("escape", false);
target.addCharacterData(toString());
target.endTag("data");
}
}
/**
* Gets the value of the label. Value of the label is the XML contents of
* the label.
*
* @return the Value of the label.
*/
public Object getValue() {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be se");
}
return dataSource.getValue();
}
/**
* Set the value of the label. Value of the label is the XML contents of the
* label.
*
* @param newValue
* the New value of the label.
*/
public void setValue(Object newValue) {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be se");
}
dataSource.setValue(newValue);
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be se");
}
return dataSource.toString();
}
/**
* Gets the type of the Property.
*
* @see com.itmill.toolkit.data.Property#getType()
*/
public Class getType() {
if (dataSource == null) {
throw new IllegalStateException("Datasource must be se");
}
return dataSource.getType();
}
/**
* Gets the viewing data-source property.
*
* @return the data source property.
* @see com.itmill.toolkit.data.Property.Viewer#getPropertyDataSource()
*/
public Property getPropertyDataSource() {
return dataSource;
}
/**
* Sets the property as data-source for viewing.
*
* @param newDataSource
* the new data source Property
* @see com.itmill.toolkit.data.Property.Viewer#setPropertyDataSource(com.itmill.toolkit.data.Property)
*/
public void setPropertyDataSource(Property newDataSource) {
// Stops listening the old data source changes
if (dataSource != null
&& Property.ValueChangeNotifier.class
.isAssignableFrom(dataSource.getClass())) {
((Property.ValueChangeNotifier) dataSource).removeListener(this);
}
// Sets the new data source
dataSource = newDataSource;
// Listens the new data source if possible
if (dataSource != null
&& Property.ValueChangeNotifier.class
.isAssignableFrom(dataSource.getClass())) {
((Property.ValueChangeNotifier) dataSource).addListener(this);
}
}
/**
* Gets the content mode of the Label.
*
* * Possible content modes include: *
* Possible content modes include: *
* 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) */ public int compareTo(Object other) { String thisValue; String otherValue; if (contentMode == CONTENT_XML || contentMode == CONTENT_UIDL || contentMode == CONTENT_XHTML) { thisValue = stripTags(toString()); } else { thisValue = toString(); } if (other instanceof Label && (((Label) other).getContentMode() == CONTENT_XML || ((Label) other).getContentMode() == CONTENT_UIDL || ((Label) other) .getContentMode() == CONTENT_XHTML)) { otherValue = stripTags(other.toString()); } else { otherValue = other.toString(); } 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) { StringBuffer res = new StringBuffer(); int processed = 0; 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(); } }