KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > common > AbstractTableColumn


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.common;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.Comparator JavaDoc;
19
20 import org.apache.tapestry.IComponent;
21 import org.apache.tapestry.IRender;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.components.Block;
24 import org.apache.tapestry.contrib.table.model.IAdvancedTableColumn;
25 import org.apache.tapestry.contrib.table.model.ITableModelSource;
26 import org.apache.tapestry.contrib.table.model.ITableRendererSource;
27 import org.apache.tapestry.valid.RenderString;
28
29 /**
30  * A base implementation of {@link org.apache.tapestry.contrib.table.model.ITableColumn}
31  * that allows renderers to be set via aggregation.
32  *
33  * @see org.apache.tapestry.contrib.table.model.ITableRendererSource
34  * @author mindbridge
35  * @since 2.3
36  */

37 public class AbstractTableColumn implements IAdvancedTableColumn, Serializable JavaDoc
38 {
39     private static final long serialVersionUID = 1L;
40
41     /**
42      * The suffix of the name of the Block that will be used as the column renderer
43      * for this column
44      */

45     public static final String JavaDoc COLUMN_RENDERER_BLOCK_SUFFIX = "ColumnHeader";
46
47     /**
48      * The suffix of the name of the Block that will be used as the value renderer
49      * for this column
50      */

51     public static final String JavaDoc VALUE_RENDERER_BLOCK_SUFFIX = "ColumnValue";
52     
53     private String JavaDoc m_strColumnName;
54     private boolean m_bSortable;
55     private Comparator JavaDoc m_objComparator;
56
57     private ITableRendererSource m_objColumnRendererSource;
58     private ITableRendererSource m_objValueRendererSource;
59
60     public AbstractTableColumn()
61     {
62         this("", false, null);
63     }
64
65     public AbstractTableColumn(
66         String JavaDoc strColumnName,
67         boolean bSortable,
68         Comparator JavaDoc objComparator)
69     {
70         this(strColumnName, bSortable, objComparator, null, null);
71     }
72
73     public AbstractTableColumn(
74         String JavaDoc strColumnName,
75         boolean bSortable,
76         Comparator JavaDoc objComparator,
77         ITableRendererSource objColumnRendererSource,
78         ITableRendererSource objValueRendererSource)
79     {
80         setColumnName(strColumnName);
81         setSortable(bSortable);
82         setComparator(objComparator);
83         setColumnRendererSource(objColumnRendererSource);
84         setValueRendererSource(objValueRendererSource);
85     }
86
87     /**
88      * @see org.apache.tapestry.contrib.table.model.ITableColumn#getColumnName()
89      */

90     public String JavaDoc getColumnName()
91     {
92         return m_strColumnName;
93     }
94
95     /**
96      * Sets the columnName.
97      * @param columnName The columnName to set
98      */

99     public void setColumnName(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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         IComponent objColumnRendererSource = (IComponent) objSettingsContainer.getComponents().get(getColumnName() + COLUMN_RENDERER_BLOCK_SUFFIX);
226         if (objColumnRendererSource == null)
227             objColumnRendererSource = (IComponent) objSettingsContainer.getComponents().get(COLUMN_RENDERER_BLOCK_SUFFIX);
228         if (objColumnRendererSource != null && objColumnRendererSource instanceof Block)
229             setColumnRendererSource(new BlockTableRendererSource((Block) objColumnRendererSource));
230
231         IComponent objValueRendererSource = (IComponent) objSettingsContainer.getComponents().get(getColumnName() + VALUE_RENDERER_BLOCK_SUFFIX);
232         if (objValueRendererSource == null)
233             objValueRendererSource = (IComponent) objSettingsContainer.getComponents().get(VALUE_RENDERER_BLOCK_SUFFIX);
234         if (objValueRendererSource != null && objValueRendererSource instanceof Block)
235             setValueRendererSource(new BlockTableRendererSource((Block) objValueRendererSource));
236     }
237
238 }
239
Popular Tags