KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > controllers > GridPortletController


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

16
17 package org.apache.jetspeed.portal.controllers;
18
19 //ECS stuff
20
import org.apache.ecs.html.Table;
21 import org.apache.ecs.html.TD;
22 import org.apache.ecs.html.TR;
23
24 import org.apache.ecs.ConcreteElement;
25 import org.apache.ecs.ElementContainer;
26
27 //turbine RunData
28
import org.apache.turbine.util.RunData;
29
30 //jetspeed stuff
31
import org.apache.jetspeed.portal.Portlet;
32 import org.apache.jetspeed.portal.PortletConfig;
33 import org.apache.jetspeed.portal.PortletControllerConfig;
34 import org.apache.jetspeed.portal.PortletSet;
35 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
36 import org.apache.jetspeed.services.logging.JetspeedLogger;
37
38 //standard Java stuff
39
import java.util.Vector JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import java.util.Enumeration JavaDoc;
42
43
44 /**
45 Layouts the portlets in a grid and apply the following constraints
46 <ul>
47 <li>all cells within the same column have the same width</li>
48 <li>all cells within the same row have the same minimum height</li>
49 </ul>
50
51 <p>This controller expects the following parameters in its configuration
52 file :
53
54 <ul>
55 <li><b>columns<b> optional, number of columns of the grid. If not specified,
56 determined by the layout information in each portlet</li>
57 <li><b>columnWidths</b> optional, the size of the columns, separated by a colon</li>
58 <li><b>rows<b> optional, number of rows of the grid. If not specified,
59 determined by the layout information in each portlet</li>
60 <li><b>rowHeights</b> optional, the minimum size of the rows, separated by a colon</li>
61 </ul>
62
63 </p>
64 <p>The controller expects each portlet to have a row number and a column number
65 layout information. If this information is not found, put the Portlet in the
66 first cell of the table</p>
67
68 @author <a HREF="mailto:raphael@apache.org">Raphaël Luta</a>
69 @version $Id: GridPortletController.java,v 1.17 2004/02/23 03:25:06 jford Exp $
70 */

71 public class GridPortletController extends AbstractPortletController
72 {
73
74     
75     /**
76      * Static initialization of the logger for this class
77      */

78     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(GridPortletController.class.getName());
79     
80     private int columns = 0;
81     private int rows = 0;
82     private Vector JavaDoc rowHeights = null;
83     private Vector JavaDoc colWidths = null;
84     
85     /**
86     */

87     public GridPortletController() {
88         rowHeights = new Vector JavaDoc();
89         colWidths = new Vector JavaDoc();
90     }
91
92     /**
93     */

94     private void calculateControllerLayout( Portlet portlet ) {
95
96         if ( portlet instanceof PortletSet ) {
97
98             Enumeration JavaDoc more = ((PortletSet)portlet).getPortlets();
99
100             while ( more.hasMoreElements() ) {
101                 calculateControllerLayout( (Portlet) more.nextElement() );
102             }
103
104             return;
105         }
106
107            
108         PortletConfig portletConf = portlet.getPortletConfig();
109         Integer JavaDoc colObj = portletConf.getConstraints().getColumn();
110         Integer JavaDoc rowObj = portletConf.getConstraints().getRow();
111
112         int col = (colObj!=null)?colObj.intValue():0;
113         int row = (rowObj!=null)?rowObj.intValue():0;
114         
115         if ( col + 1 > this.getColumn() ) {
116             this.setColumn( col + 1 );
117         }
118         
119         if ( row + 1 > this.getRow() ) {
120             this.setRow( row + 1 );
121         }
122
123         
124     }
125     
126     /**
127     Get the content for this PortletController
128     */

129     public ConcreteElement getContent( RunData rundata ) {
130
131         ElementContainer base = new ElementContainer();
132
133         try
134         {
135         PortletSet portlets = getPortlets();
136         PortletConfig pc = portlets.getPortletConfig();
137
138         // first get the number of columns and rows to display
139
Enumeration JavaDoc en = portlets.getPortlets();
140       
141         //see if any or the Portlets you want to add have a larger column or
142
//row number than that defined in PSML
143
while ( en.hasMoreElements() ) {
144
145             Portlet portlet = (Portlet)en.nextElement();
146
147             calculateControllerLayout( portlet );
148             
149         }
150
151         setWidth( pc.getLayout( "width", getWidth() ) );
152
153         int rows = getRow();
154         int cols = getColumn();
155
156         if (0 == rows || 0 == cols)
157             return base; // empty container
158

159         Table t = new Table()
160                        .setWidth( this.getWidth() )
161                        .setCellPadding( this.getPadding() )
162                        .setAlign("center");
163
164         base.addElement( t );
165
166         ElementContainer[][] elements = new ElementContainer[rows][cols];
167
168         for( int i = 0; i < rows; i++ ) {
169             for ( int j = 0 ; j < cols; j++ ) {
170                 elements[i][j]=new ElementContainer();
171             }
172         }
173
174         // populate the elements array
175
en = portlets.getPortlets();
176         while (en.hasMoreElements() ) {
177
178             Portlet p = (Portlet)en.nextElement();
179             PortletConfig pConf = p.getPortletConfig();
180
181             Integer JavaDoc colObj = pConf.getConstraints().getColumn();
182             Integer JavaDoc rowObj = pConf.getConstraints().getRow();
183             int colnum = (colObj!=null)?colObj.intValue():0;
184             int rownum = (rowObj!=null)?rowObj.intValue():0;
185
186             elements[rownum % rows][colnum % cols]
187                 .addElement( p.getContent( rundata ) );
188
189         }
190
191         // build the table
192

193         for (int i = 0; i < rows; ++i) {
194
195             TR row = new TR();
196             TD td = null;
197
198             for(int j=0; j < cols ; ++j) {
199                 row.addElement( td= new TD().setVAlign("top")
200                                        .addElement( elements[i][j] ) );
201                 if (getRowHeight(i)!=null) td.setHeight(getRowHeight(i));
202                 if (getColumnWidth(j)!=null) td.setWidth(getColumnWidth(j));
203             }
204
205             t.addElement(row);
206         }
207
208         }
209         catch (Exception JavaDoc e)
210         {
211             logger.error("getContent():", e);
212         }
213         
214         return base;
215
216     }
217
218     /**
219     */

220     public void init() {
221         super.init();
222         PortletControllerConfig conf = getConfig();
223         
224         if (conf!=null) {
225             setColumn(Integer.parseInt(conf.getInitParameter("column","0")));
226             setRow(Integer.parseInt(conf.getInitParameter("row","0")));
227             setColumnsWidth(parseList(conf.getInitParameter("columnWidths")));
228             setRowsHeight(parseList(conf.getInitParameter("rowHeights")));
229         }
230             
231     }
232     
233     /**
234     Set the number of columns used in this controller
235     */

236     public void setColumn(int col) {
237         this.columns=col;
238     }
239     
240     /**
241     Get the number of columns used in this controller
242     */

243     public int getColumn() {
244         return this.columns;
245     }
246     
247     /**
248     Set the number of rows used by this controller
249     */

250     public void setRow(int row) {
251         this.rows=row;
252     }
253     
254     /**
255     Get the number of rows used by this controll
256     */

257     public int getRow() {
258         return this.rows;
259     }
260     
261     /**
262     */

263     public void setColumnsWidth(Vector JavaDoc widths) {
264         this.colWidths = widths;
265     }
266     
267     /**
268     */

269     public Enumeration JavaDoc getColumnsWidth() {
270         return colWidths.elements();
271     }
272     
273     /**
274     */

275     public String JavaDoc getColumnWidth(int pos) {
276         if (pos < colWidths.size()) return (String JavaDoc)colWidths.elementAt(pos);
277         return null;
278     }
279     
280     /**
281     */

282     public void setRowsHeight(Vector JavaDoc heights) {
283         this.rowHeights = heights;
284     }
285     
286     /**
287     */

288     public Enumeration JavaDoc getRowsHeight() {
289         return rowHeights.elements();
290     }
291     
292     /**
293     */

294     public String JavaDoc getRowHeight(int pos) {
295         if (pos < rowHeights.size()) return (String JavaDoc)rowHeights.elementAt(pos);
296         return null;
297     }
298     
299     /**
300     */

301     private Vector JavaDoc parseList(String JavaDoc list) {
302         Vector JavaDoc v = new Vector JavaDoc();
303         if (list!=null) {
304             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(list,",");
305             while (st.hasMoreTokens())
306                 v.addElement(st.nextToken());
307         }
308
309         return v;
310     }
311
312 }
313
314
Popular Tags