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
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 /**
042 * The suffix of the name of the Block that will be used as the column
043 * renderer 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
049 * renderer for this column.
050 */
051 public static final String VALUE_RENDERER_BLOCK_SUFFIX = "ColumnValue";
052
053 private static final long serialVersionUID = 1L;
054
055 private String m_strColumnName;
056 private boolean m_bSortable;
057 private Comparator m_objComparator;
058
059 private ITableRendererSource m_objColumnRendererSource;
060 private ITableRendererSource m_objValueRendererSource;
061
062 public AbstractTableColumn()
063 {
064 this("", false, null);
065 }
066
067 public AbstractTableColumn(String strColumnName, boolean bSortable,
068 Comparator objComparator)
069 {
070 this(strColumnName, bSortable, objComparator, null, null);
071 }
072
073 public AbstractTableColumn(String strColumnName, boolean bSortable,
074 Comparator objComparator,
075 ITableRendererSource objColumnRendererSource,
076 ITableRendererSource objValueRendererSource)
077 {
078 setColumnName(strColumnName);
079 setSortable(bSortable);
080 setComparator(objComparator);
081 setColumnRendererSource(objColumnRendererSource);
082 setValueRendererSource(objValueRendererSource);
083 }
084
085 /**
086 * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnName()
087 */
088 public String getColumnName()
089 {
090 return m_strColumnName;
091 }
092
093 /**
094 * Sets the columnName.
095 *
096 * @param columnName
097 * 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 *
115 * @param sortable
116 * The sortable flag to set
117 */
118 public void setSortable(boolean sortable)
119 {
120 m_bSortable = sortable;
121 }
122
123 /**
124 * @see org.apache.tapestry.contrib.table.model.ITableColumn#getComparator()
125 */
126 public Comparator getComparator()
127 {
128 return m_objComparator;
129 }
130
131 /**
132 * Sets the comparator.
133 *
134 * @param comparator
135 * The comparator to set
136 */
137 public void setComparator(Comparator comparator)
138 {
139 m_objComparator = comparator;
140 }
141
142 /**
143 * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnRenderer(IRequestCycle,
144 * ITableModelSource)
145 */
146 public IRender getColumnRenderer(IRequestCycle objCycle,
147 ITableModelSource objSource)
148 {
149 ITableRendererSource objRendererSource = getColumnRendererSource();
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 if (objRendererSource == null)
168 {
169 // log error
170 return new RenderString("");
171 }
172
173 return objRendererSource.getRenderer(objCycle, objSource, this, objRow);
174 }
175
176 /**
177 * Returns the columnRendererSource.
178 *
179 * @return ITableColumnRendererSource
180 */
181 public ITableRendererSource getColumnRendererSource()
182 {
183 return m_objColumnRendererSource;
184 }
185
186 /**
187 * Sets the columnRendererSource.
188 *
189 * @param columnRendererSource
190 * The columnRendererSource to set
191 */
192 public void setColumnRendererSource(
193 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 objSettingsContainer
225 * the component from which to get the settings
226 */
227 public void loadSettings(IComponent objSettingsContainer)
228 {
229 IComponent objColumnRendererSource = (IComponent) objSettingsContainer
230 .getComponents().get(
231 getColumnName() + COLUMN_RENDERER_BLOCK_SUFFIX);
232 if (objColumnRendererSource == null)
233 objColumnRendererSource = (IComponent) objSettingsContainer
234 .getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
235 if (objColumnRendererSource != null
236 && objColumnRendererSource instanceof Block)
237 setColumnRendererSource(new BlockTableRendererSource(
238 (Block) objColumnRendererSource));
239
240 IComponent objValueRendererSource = (IComponent) objSettingsContainer
241 .getComponents().get(
242 getColumnName() + VALUE_RENDERER_BLOCK_SUFFIX);
243 if (objValueRendererSource == null)
244 objValueRendererSource = (IComponent) objSettingsContainer
245 .getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
246 if (objValueRendererSource != null
247 && objValueRendererSource instanceof Block)
248 setValueRendererSource(new BlockTableRendererSource(
249 (Block) objValueRendererSource));
250 }
251
252 }