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 */
017package org.apache.wicket.extensions.markup.html.repeater.data.table;
018
019import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
020import org.apache.wicket.extensions.markup.html.repeater.data.table.export.IExportableColumn;
021import org.apache.wicket.markup.html.basic.Label;
022import org.apache.wicket.markup.repeater.Item;
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.model.PropertyModel;
025
026
027/**
028 * A convenience implementation of column that adds a label to the cell whose model is determined by
029 * the provided wicket property expression (same as used by {@link PropertyModel}) that is evaluated
030 * against the current row's model object
031 * <p>
032 * Example
033 * 
034 * <pre>
035 * columns[0] = new PropertyColumn(new Model&lt;String&gt;(&quot;First Name&quot;), &quot;name.first&quot;);
036 * </pre>
037 * 
038 * The above will attach a label to the cell with a property model for the expression
039 * &quot;name.first&quot;
040 * 
041 * @see PropertyModel
042 * 
043 * @author Igor Vaynberg ( ivaynberg )
044 * @param <T>
045 *            The Model object type
046 * @param <S>
047 *            the type of the sort property
048 */
049public class PropertyColumn<T, S> extends AbstractColumn<T, S> implements IExportableColumn<T, S>
050{
051        private static final long serialVersionUID = 1L;
052
053        private final String propertyExpression;
054
055        /**
056         * Creates a property column that is also sortable
057         * 
058         * @param displayModel
059         *            display model
060         * @param sortProperty
061         *            sort property
062         * @param propertyExpression
063         *            wicket property expression used by PropertyModel
064         */
065        public PropertyColumn(final IModel<String> displayModel, final S sortProperty,
066                final String propertyExpression)
067        {
068                super(displayModel, sortProperty);
069                this.propertyExpression = propertyExpression;
070        }
071
072        /**
073         * Creates a non sortable property column
074         * 
075         * @param displayModel
076         *            display model
077         * @param propertyExpression
078         *            wicket property expression
079         * @see PropertyModel
080         */
081        public PropertyColumn(final IModel<String> displayModel, final String propertyExpression)
082        {
083                super(displayModel, null);
084                this.propertyExpression = propertyExpression;
085        }
086
087        /**
088         * Implementation of populateItem which adds a label to the cell whose model is the provided
089         * property expression evaluated against rowModelObject
090         * 
091         * @see ICellPopulator#populateItem(Item, String, IModel)
092         */
093        @Override
094        public void populateItem(final Item<ICellPopulator<T>> item, final String componentId,
095                final IModel<T> rowModel)
096        {
097                item.add(new Label(componentId, getDataModel(rowModel)));
098        }
099
100        /**
101         * @return wicket property expression
102         */
103        public String getPropertyExpression()
104        {
105                return propertyExpression;
106        }
107
108        /**
109         * Factory method for generating a model that will generated the displayed value. Typically the
110         * model is a property model using the {@link #propertyExpression} specified in the constructor.
111         *
112         * @param rowModel
113         * @return model
114         */
115        @Override
116        public IModel<?> getDataModel(IModel<T> rowModel)
117        {
118                PropertyModel<?> propertyModel = new PropertyModel<>(rowModel, propertyExpression);
119                return propertyModel;
120        }
121}