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 java.io.Serializable;
018    import java.util.Comparator;
019    
020    import org.apache.tapestry.IComponent;
021    import org.apache.tapestry.IRender;
022    import org.apache.tapestry.IRequestCycle;
023    import org.apache.tapestry.components.Block;
024    import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
025    import org.apache.tapestry.contrib.table.model.ITableModelSource;
026    import org.apache.tapestry.contrib.table.model.ITableRendererSource;
027    import org.apache.tapestry.valid.RenderString;
028    
029    /**
030     * A base implementation of {@link org.apache.tapestry.contrib.table.model.ITableColumn}
031     * that allows renderers to be set via aggregation.
032     * 
033     * @see org.apache.tapestry.contrib.table.model.ITableRendererSource
034     * @author mindbridge
035     * @since 2.3
036     */
037    public class AbstractTableColumn implements IAdvancedTableColumn, Serializable
038    {
039            private static final long serialVersionUID = 1L;
040    
041            /**
042         *  The suffix of the name of the Block that will be used as the column renderer
043         *  for this column 
044         */
045        public static final String COLUMN_RENDERER_BLOCK_SUFFIX = "ColumnHeader";
046    
047        /**
048         *  The suffix of the name of the Block that will be used as the value renderer 
049         *  for this column 
050         */
051        public static final String VALUE_RENDERER_BLOCK_SUFFIX = "ColumnValue";
052        
053            private String m_strColumnName;
054            private boolean m_bSortable;
055            private Comparator m_objComparator;
056    
057            private ITableRendererSource m_objColumnRendererSource;
058            private ITableRendererSource m_objValueRendererSource;
059    
060            public AbstractTableColumn()
061            {
062                    this("", false, null);
063            }
064    
065            public AbstractTableColumn(
066                    String strColumnName,
067                    boolean bSortable,
068                    Comparator objComparator)
069            {
070                    this(strColumnName, bSortable, objComparator, null, null);
071            }
072    
073            public AbstractTableColumn(
074                    String strColumnName,
075                    boolean bSortable,
076                    Comparator objComparator,
077                    ITableRendererSource objColumnRendererSource,
078                    ITableRendererSource objValueRendererSource)
079            {
080                    setColumnName(strColumnName);
081                    setSortable(bSortable);
082                    setComparator(objComparator);
083                    setColumnRendererSource(objColumnRendererSource);
084                    setValueRendererSource(objValueRendererSource);
085            }
086    
087            /**
088             * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnName()
089             */
090            public String getColumnName()
091            {
092                    return m_strColumnName;
093            }
094    
095            /**
096             * Sets the columnName.
097             * @param columnName The columnName to set
098             */
099            public void setColumnName(String columnName)
100            {
101                    m_strColumnName = columnName;
102            }
103    
104            /**
105             * @see org.apache.tapestry.contrib.table.model.ITableColumn#getSortable()
106             */
107            public boolean getSortable()
108            {
109                    return m_bSortable;
110            }
111    
112            /**
113             * Sets whether the column is sortable.
114             * @param sortable The sortable flag to set
115             */
116            public void setSortable(boolean sortable)
117            {
118                    m_bSortable = sortable;
119            }
120    
121            /**
122             * @see org.apache.tapestry.contrib.table.model.ITableColumn#getComparator()
123             */
124            public Comparator getComparator()
125            {
126                    return m_objComparator;
127            }
128    
129            /**
130             * Sets the comparator.
131             * @param comparator The comparator to set
132             */
133            public void setComparator(Comparator comparator)
134            {
135                    m_objComparator = comparator;
136            }
137    
138            /**
139             * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnRenderer(IRequestCycle, ITableModelSource)
140             */
141            public IRender getColumnRenderer(
142                    IRequestCycle objCycle,
143                    ITableModelSource objSource)
144            {
145                    ITableRendererSource objRendererSource =
146                            getColumnRendererSource();
147                    if (objRendererSource == null)
148                    {
149                            // log error
150                            return new RenderString("");
151                    }
152    
153                    return objRendererSource.getRenderer(objCycle, objSource, this, null);
154            }
155    
156            /**
157             * @see org.apache.tapestry.contrib.table.model.ITableColumn#getValueRenderer(IRequestCycle, ITableModelSource, Object)
158             */
159            public IRender getValueRenderer(
160                    IRequestCycle objCycle,
161                    ITableModelSource objSource,
162                    Object objRow)
163            {
164                    ITableRendererSource objRendererSource = getValueRendererSource();
165                    if (objRendererSource == null)
166                    {
167                            // log error
168                            return new RenderString("");
169                    }
170    
171                    return objRendererSource.getRenderer(
172                            objCycle,
173                            objSource,
174                            this,
175                            objRow);
176            }
177    
178            /**
179             * Returns the columnRendererSource.
180             * @return ITableColumnRendererSource
181             */
182            public ITableRendererSource getColumnRendererSource()
183            {
184                    return m_objColumnRendererSource;
185            }
186    
187            /**
188             * Sets the columnRendererSource.
189             * @param columnRendererSource The columnRendererSource to set
190             */
191            public void setColumnRendererSource(ITableRendererSource columnRendererSource)
192            {
193                    m_objColumnRendererSource = columnRendererSource;
194            }
195    
196            /**
197             * Returns the valueRendererSource.
198         * 
199             * @return the valueRendererSource of this column
200             */
201            public ITableRendererSource getValueRendererSource()
202            {
203                    return m_objValueRendererSource;
204            }
205    
206            /**
207             * Sets the valueRendererSource.
208         * 
209             * @param valueRendererSource The valueRendererSource to set
210             */
211            public void setValueRendererSource(ITableRendererSource valueRendererSource)
212            {
213                    m_objValueRendererSource = valueRendererSource;
214            }
215    
216        /**
217         *  Use the column name to get the column and value renderer sources 
218         *  from the provided component.
219         *  Use the column and value renderer sources for all columns if necessary.
220         *   
221         *  @param objSettingsContainer the component from which to get the settings 
222         */
223        public void loadSettings(IComponent objSettingsContainer)
224        {
225            // Replace any periods in the column name with underscores so columns can be referenced in a @Block
226            String columnName = getColumnName().replace('.', '_');
227    
228            IComponent objColumnRendererSource = (IComponent) objSettingsContainer.getComponents().get(columnName + COLUMN_RENDERER_BLOCK_SUFFIX);
229            if (objColumnRendererSource == null) 
230                    objColumnRendererSource = (IComponent) objSettingsContainer.getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
231            if (objColumnRendererSource != null && objColumnRendererSource instanceof Block)
232                    setColumnRendererSource(new BlockTableRendererSource((Block) objColumnRendererSource));
233    
234            IComponent objValueRendererSource = (IComponent) objSettingsContainer.getComponents().get(columnName + VALUE_RENDERER_BLOCK_SUFFIX);
235            if (objValueRendererSource == null)
236                    objValueRendererSource = (IComponent) objSettingsContainer.getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
237            if (objValueRendererSource != null && objValueRendererSource instanceof Block)
238                setValueRendererSource(new BlockTableRendererSource((Block) objValueRendererSource));
239        }
240    
241    }