001: /*
002: * $Id$ $Revision$ $Date$
003: *
004: * ==============================================================================
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package wicket.extensions.markup.html.tree.table;
018:
019: import java.util.Locale;
020:
021: import javax.swing.tree.TreeNode;
022:
023: import wicket.Session;
024: import wicket.util.convert.IConverter;
025: import wicket.util.lang.PropertyResolver;
026:
027: /**
028: * Lightweight column that uses a property expression to get the value from the
029: * node.
030: *
031: * @author Matej Knopp
032: */
033: public class PropertyRenderableColumn extends AbstractRenderableColumn {
034: private static final long serialVersionUID = 1L;
035:
036: private IConverter converter;
037:
038: private Locale locale;
039:
040: private String propertyExpression;
041:
042: /**
043: * Creates the columns.
044: *
045: * @param location
046: * Specifies how the column should be aligned and what his size
047: * should be
048: *
049: * @param header
050: * Header caption
051: *
052: * @param propertyExpression
053: * Expression for property access
054: */
055: public PropertyRenderableColumn(ColumnLocation location,
056: String header, String propertyExpression) {
057: super (location, header);
058: this .propertyExpression = propertyExpression;
059: }
060:
061: /**
062: * Returns the converter or null if no converter is specified.
063: *
064: * @return The converter or null
065: */
066: public IConverter getConverter() {
067: return converter;
068: }
069:
070: /**
071: * Returns the locale or null if no locale is specified.
072: *
073: * @return The locale or null
074: */
075: public Locale getLocale() {
076: return locale;
077: }
078:
079: /**
080: * @see AbstractRenderableColumn#getNodeValue(TreeNode)
081: */
082: public String getNodeValue(TreeNode node) {
083: Object result = PropertyResolver.getValue(propertyExpression,
084: node);
085: if (converter != null) {
086: Locale locale = this .locale;
087: if (locale == null) {
088: locale = Session.get().getLocale();
089: }
090: converter.setLocale(locale);
091: return (String) converter.convert(result, String.class);
092: } else {
093: return result != null ? result.toString() : "";
094: }
095: }
096:
097: /**
098: * By default the property is converted to string using
099: * <code>toString</code> method. If you want to alter this behavior, you
100: * can specify a custom converter.
101: *
102: * @param converter
103: * any converter
104: */
105: public void setConverter(IConverter converter) {
106: this .converter = converter;
107: }
108:
109: /**
110: * Sets the locale to be used as parameter for custom converter (if one is
111: * specified). If no locale is set, session locale is used.
112: *
113: * @param locale
114: * Any locale
115: */
116: public void setLocale(Locale locale) {
117: this .locale = locale;
118: }
119:
120: /**
121: * Returns the property expression.
122: *
123: * @return The property expression
124: */
125: protected String getPropertyExpression() {
126: return propertyExpression;
127: }
128: }
|