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.grid;
018
019import org.apache.wicket.markup.html.basic.Label;
020import org.apache.wicket.markup.repeater.Item;
021import org.apache.wicket.model.IModel;
022import org.apache.wicket.model.PropertyModel;
023
024/**
025 * A convenience implementation of {@link ICellPopulator} that adds a label that will display the
026 * value of the specified property. Non-string properties will be converted to a string before
027 * display.
028 * <p>
029 * Example
030 * 
031 * <pre>
032 * ICellPopulator cityPopulator = new PropertyPopulator(&quot;address.city&quot;);
033 * </pre>
034 * 
035 * @param <T>
036 * @author Igor Vaynberg (ivaynberg)
037 */
038public class PropertyPopulator<T> implements ICellPopulator<T>
039{
040        private static final long serialVersionUID = 1L;
041        private final String property;
042
043        /**
044         * Constructor
045         * 
046         * @param property
047         *            property whose value will be displayed in the cell. uses wicket's
048         *            {@link PropertyModel} notation.
049         */
050        public PropertyPopulator(final String property)
051        {
052                if (property == null)
053                {
054                        throw new IllegalArgumentException("argument [property] cannot be null");
055                }
056                this.property = property;
057        }
058
059        /**
060         * @see org.apache.wicket.model.IDetachable#detach()
061         */
062        @Override
063        public void detach()
064        {
065        }
066
067        /**
068         * @see org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator#populateItem(org.apache.wicket.markup.repeater.Item,
069         *      java.lang.String, org.apache.wicket.model.IModel)
070         */
071        @Override
072        public void populateItem(final Item<ICellPopulator<T>> cellItem, final String componentId,
073                final IModel<T> rowModel)
074        {
075                cellItem.add(new Label(componentId, new PropertyModel<>(rowModel, property)));
076        }
077}