001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.contrib.table.model.common;
016    
017    import org.apache.tapestry.IComponent;
018    import org.apache.tapestry.IRender;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.components.Block;
021    import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
022    import org.apache.tapestry.contrib.table.model.ITableModelSource;
023    import org.apache.tapestry.contrib.table.model.ITableRendererSource;
024    import org.apache.tapestry.valid.RenderString;
025    
026    import java.io.Serializable;
027    import java.util.Comparator;
028    
029    /**
030     * A base implementation of
031     * {@link org.apache.tapestry.contrib.table.model.ITableColumn} that allows
032     * renderers to be set via aggregation.
033     *
034     * @see org.apache.tapestry.contrib.table.model.ITableRendererSource
035     * @author mindbridge
036     * @since 2.3
037     */
038    public class AbstractTableColumn implements IAdvancedTableColumn, Serializable
039    {
040        /**
041         * The suffix of the name of the Block that will be used as the column
042         * renderer for this column.
043         */
044        public static final String COLUMN_RENDERER_BLOCK_SUFFIX = "ColumnHeader";
045    
046        /**
047         * The suffix of the name of the Block that will be used as the value
048         * renderer for this column.
049         */
050        public static final String VALUE_RENDERER_BLOCK_SUFFIX = "ColumnValue";
051    
052        private static final long serialVersionUID = 1L;
053    
054        private String m_strColumnName;
055        private boolean m_bSortable;
056        private Comparator m_objComparator;
057    
058        private ITableRendererSource m_objColumnRendererSource;
059        private ITableRendererSource m_objValueRendererSource;
060    
061        public AbstractTableColumn()
062        {
063            this("", false, null);
064        }
065    
066        public AbstractTableColumn(String strColumnName, boolean bSortable,
067                                   Comparator objComparator)
068        {
069            this(strColumnName, bSortable, objComparator, null, null);
070        }
071    
072        public AbstractTableColumn(String strColumnName, boolean bSortable,
073                                   Comparator objComparator,
074                                   ITableRendererSource objColumnRendererSource,
075                                   ITableRendererSource objValueRendererSource)
076        {
077            setColumnName(strColumnName);
078            setSortable(bSortable);
079            setComparator(objComparator);
080            setColumnRendererSource(objColumnRendererSource);
081            setValueRendererSource(objValueRendererSource);
082        }
083    
084        /**
085         * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnName()
086         */
087        public String getColumnName()
088        {
089            return m_strColumnName;
090        }
091    
092        /**
093         * Sets the columnName.
094         *
095         * @param columnName
096         *            The columnName to set
097         */
098        public void setColumnName(String columnName)
099        {
100            m_strColumnName = columnName;
101        }
102    
103        /**
104         * @see org.apache.tapestry.contrib.table.model.ITableColumn#getSortable()
105         */
106        public boolean getSortable()
107        {
108            return m_bSortable;
109        }
110    
111        /**
112         * Sets whether the column is sortable.
113         *
114         * @param sortable
115         *            The sortable flag to set
116         */
117        public void setSortable(boolean sortable)
118        {
119            m_bSortable = sortable;
120        }
121    
122        /**
123         * @see org.apache.tapestry.contrib.table.model.ITableColumn#getComparator()
124         */
125        public Comparator getComparator()
126        {
127            return m_objComparator;
128        }
129    
130        /**
131         * Sets the comparator.
132         *
133         * @param comparator
134         *            The comparator to set
135         */
136        public void setComparator(Comparator comparator)
137        {
138            m_objComparator = comparator;
139        }
140    
141        /**
142         * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnRenderer(IRequestCycle,
143         *      ITableModelSource)
144         */
145        public IRender getColumnRenderer(IRequestCycle objCycle,
146                                         ITableModelSource objSource)
147        {
148            ITableRendererSource objRendererSource = getColumnRendererSource();
149    
150            if (objRendererSource == null)
151            {
152                // log error
153                return new RenderString("");
154            }
155    
156            return objRendererSource.getRenderer(objCycle, objSource, this, null);
157        }
158    
159        /**
160         * @see org.apache.tapestry.contrib.table.model.ITableColumn#getValueRenderer(IRequestCycle,
161         *      ITableModelSource, Object)
162         */
163        public IRender getValueRenderer(IRequestCycle objCycle,
164                                        ITableModelSource objSource, Object objRow)
165        {
166            ITableRendererSource objRendererSource = getValueRendererSource();
167    
168            if (objRendererSource == null)
169            {
170                // log error
171                return new RenderString("");
172            }
173    
174            return objRendererSource.getRenderer(objCycle, objSource, this, objRow);
175        }
176    
177        /**
178         * Returns the columnRendererSource.
179         *
180         * @return ITableColumnRendererSource
181         */
182        public ITableRendererSource getColumnRendererSource()
183        {
184            return m_objColumnRendererSource;
185        }
186    
187        /**
188         * Sets the columnRendererSource.
189         *
190         * @param columnRendererSource
191         *            The columnRendererSource to set
192         */
193        public void setColumnRendererSource(ITableRendererSource columnRendererSource)
194        {
195            m_objColumnRendererSource = columnRendererSource;
196        }
197    
198        /**
199         * Returns the valueRendererSource.
200         *
201         * @return the valueRendererSource of this column
202         */
203        public ITableRendererSource getValueRendererSource()
204        {
205            return m_objValueRendererSource;
206        }
207    
208        /**
209         * Sets the valueRendererSource.
210         *
211         * @param valueRendererSource
212         *            The valueRendererSource to set
213         */
214        public void setValueRendererSource(ITableRendererSource valueRendererSource)
215        {
216            m_objValueRendererSource = valueRendererSource;
217        }
218    
219        /**
220         * Use the column name to get the column and value renderer sources from the
221         * provided component. Use the column and value renderer sources for all
222         * columns if necessary.
223         *
224         * @param container
225         *            The component from which to get the settings.
226         */
227        public void loadSettings(IComponent container)
228        {
229            String columnName = getColumnName();
230            
231            IComponent objColumnRendererSource =
232                    (IComponent) container.getComponents().get(columnName + COLUMN_RENDERER_BLOCK_SUFFIX);
233    
234            if (objColumnRendererSource == null && columnName.indexOf(".") > -1)
235            {
236                columnName = columnName.replace('.', '_');
237                
238                objColumnRendererSource =
239                    (IComponent) container.getComponents().get(columnName + COLUMN_RENDERER_BLOCK_SUFFIX);
240            }
241    
242            if (objColumnRendererSource == null)
243            {
244                objColumnRendererSource = (IComponent) container.getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
245            }
246    
247            if (objColumnRendererSource != null && objColumnRendererSource instanceof Block)
248            {
249                setColumnRendererSource(new BlockTableRendererSource((Block) objColumnRendererSource));
250            }
251    
252            IComponent objValueRendererSource =
253                    (IComponent) container.getComponents().get(columnName + VALUE_RENDERER_BLOCK_SUFFIX);
254    
255            if (objValueRendererSource == null && columnName.indexOf(".") > -1)
256            {
257                columnName = columnName.replace('.', '_');
258    
259                objValueRendererSource =
260                    (IComponent) container.getComponents().get(columnName + VALUE_RENDERER_BLOCK_SUFFIX);
261            }
262    
263            if (objValueRendererSource == null)
264            {
265                objValueRendererSource = (IComponent) container.getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
266            }
267    
268            if (objValueRendererSource != null && objValueRendererSource instanceof Block)
269            {
270                setValueRendererSource(new BlockTableRendererSource((Block) objValueRendererSource));
271            }
272        }
273    
274    }