001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of 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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.extensions.markup.html.tree.table;
018:
019: import java.util.Locale;
020:
021: import javax.swing.tree.TreeNode;
022:
023: import org.apache.wicket.Session;
024: import org.apache.wicket.util.convert.IConverter;
025: import org.apache.wicket.util.lang.PropertyResolver;
026:
027: /**
028: * TreeColumn class that uses a property expression to get the value from the
029: * node.
030: *
031: * @author Matej Knopp
032: */
033: public class PropertyTreeColumn extends AbstractTreeColumn {
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 PropertyTreeColumn(ColumnLocation location, String header,
056: 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 Any converter
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 Any locale
074: */
075: public Locale getLocale() {
076: return locale;
077: }
078:
079: /**
080: * @see AbstractTreeColumn#renderNode(TreeNode)
081: */
082: public String renderNode(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: return converter.convertToString(result, locale);
091: } else {
092: return result != null ? result.toString() : null;
093: }
094: }
095:
096: /**
097: * By default the property is converted to string using
098: * <code>toString</code> method. If you want to alter this behavior, you
099: * can specify a custom converter.
100: *
101: * @param converter
102: * Any converter
103: */
104: public void setConverter(IConverter converter) {
105: this .converter = converter;
106: }
107:
108: /**
109: * Sets the locale to be used as parameter for custom converter (if one is
110: * specified). If no locale is set, session locale is used.
111: *
112: * @param locale
113: * The locale
114: */
115: public void setLocale(Locale locale) {
116: this.locale = locale;
117: }
118: }
|