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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.controllers;
1819//ECS stuff20import org.apache.ecs.html.Table;
21import org.apache.ecs.html.TD;
22import org.apache.ecs.html.TR;
2324import org.apache.ecs.ConcreteElement;
25import org.apache.ecs.ElementContainer;
2627//turbine RunData28import org.apache.turbine.util.RunData;
2930//jetspeed stuff31import org.apache.jetspeed.portal.Portlet;
32import org.apache.jetspeed.portal.PortletConfig;
33import org.apache.jetspeed.portal.PortletControllerConfig;
34import org.apache.jetspeed.portal.PortletSet;
35import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
36import org.apache.jetspeed.services.logging.JetspeedLogger;
3738//standard Java stuff39import java.util.Vector;
40import java.util.StringTokenizer;
41import java.util.Enumeration;
424344/***45Layouts the portlets in a grid and apply the following constraints46<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>5051<p>This controller expects the following parameters in its configuration52file :5354<ul>55<li><b>columns<b> optional, number of columns of the grid. If not specified,56determined 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,59determined 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>6263</p>64<p>The controller expects each portlet to have a row number and a column number65layout information. If this information is not found, put the Portlet in the66first cell of the table</p>6768@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*/71publicclassGridPortletControllerextendsAbstractPortletController72 {
737475/***76 * Static initialization of the logger for this class77 */78privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(GridPortletController.class.getName());
7980privateint columns = 0;
81privateint rows = 0;
82private Vector rowHeights = null;
83private Vector colWidths = null;
8485/***86 */87publicGridPortletController() {
88 rowHeights = new Vector();
89 colWidths = new Vector();
90 }
9192/***93 */94privatevoid calculateControllerLayout( Portlet portlet ) {
9596if ( portlet instanceof PortletSet ) {
9798 Enumeration more = ((PortletSet)portlet).getPortlets();
99100while ( more.hasMoreElements() ) {
101 calculateControllerLayout( (Portlet) more.nextElement() );
102 }
103104return;
105 }
106107108PortletConfig portletConf = portlet.getPortletConfig();
109 Integer colObj = portletConf.getConstraints().getColumn();
110 Integer rowObj = portletConf.getConstraints().getRow();
111112int col = (colObj!=null)?colObj.intValue():0;
113int row = (rowObj!=null)?rowObj.intValue():0;
114115if ( col + 1 > this.getColumn() ) {
116this.setColumn( col + 1 );
117 }
118119if ( row + 1 > this.getRow() ) {
120this.setRow( row + 1 );
121 }
122123124 }
125126/***127 Get the content for this PortletController128 */129public ConcreteElement getContent( RunData rundata ) {
130131 ElementContainer base = new ElementContainer();
132133try134 {
135PortletSet portlets = getPortlets();
136PortletConfig pc = portlets.getPortletConfig();
137138// first get the number of columns and rows to display139 Enumeration en = portlets.getPortlets();
140141//see if any or the Portlets you want to add have a larger column or142//row number than that defined in PSML143while ( en.hasMoreElements() ) {
144145Portlet portlet = (Portlet)en.nextElement();
146147 calculateControllerLayout( portlet );
148149 }
150151 setWidth( pc.getLayout( "width", getWidth() ) );
152153int rows = getRow();
154int cols = getColumn();
155156if (0 == rows || 0 == cols)
157return base; // empty container158159 Table t = new Table()
160 .setWidth( this.getWidth() )
161 .setCellPadding( this.getPadding() )
162 .setAlign("center");
163164 base.addElement( t );
165166 ElementContainer[][] elements = new ElementContainer[rows][cols];
167168for( int i = 0; i < rows; i++ ) {
169for ( int j = 0 ; j < cols; j++ ) {
170 elements[i][j]=new ElementContainer();
171 }
172 }
173174// populate the elements array175 en = portlets.getPortlets();
176while (en.hasMoreElements() ) {
177178Portlet p = (Portlet)en.nextElement();
179PortletConfig pConf = p.getPortletConfig();
180181 Integer colObj = pConf.getConstraints().getColumn();
182 Integer rowObj = pConf.getConstraints().getRow();
183int colnum = (colObj!=null)?colObj.intValue():0;
184int rownum = (rowObj!=null)?rowObj.intValue():0;
185186 elements[rownum % rows][colnum % cols]
187 .addElement( p.getContent( rundata ) );
188189 }
190191// build the table192193for (int i = 0; i < rows; ++i) {
194195 TR row = new TR();
196 TD td = null;
197198for(int j=0; j < cols ; ++j) {
199 row.addElement( td= new TD().setVAlign("top")
200 .addElement( elements[i][j] ) );
201if (getRowHeight(i)!=null) td.setHeight(getRowHeight(i));
202if (getColumnWidth(j)!=null) td.setWidth(getColumnWidth(j));
203 }
204205 t.addElement(row);
206 }
207208 }
209catch (Exception e)
210 {
211 logger.error("getContent():", e);
212 }
213214return base;
215216 }
217218/***219 */220publicvoid init() {
221super.init();
222PortletControllerConfig conf = getConfig();
223224if (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 }
230231 }
232233/***234 Set the number of columns used in this controller235 */236publicvoid setColumn(int col) {
237this.columns=col;
238 }
239240/***241 Get the number of columns used in this controller242 */243publicint getColumn() {
244returnthis.columns;
245 }
246247/***248 Set the number of rows used by this controller249 */250publicvoid setRow(int row) {
251this.rows=row;
252 }
253254/***255 Get the number of rows used by this controll256 */257publicint getRow() {
258returnthis.rows;
259 }
260261/***262 */263publicvoid setColumnsWidth(Vector widths) {
264this.colWidths = widths;
265 }
266267/***268 */269public Enumeration getColumnsWidth() {
270return colWidths.elements();
271 }
272273/***274 */275public String getColumnWidth(int pos) {
276if (pos < colWidths.size()) return (String)colWidths.elementAt(pos);
277returnnull;
278 }
279280/***281 */282publicvoid setRowsHeight(Vector heights) {
283this.rowHeights = heights;
284 }
285286/***287 */288public Enumeration getRowsHeight() {
289return rowHeights.elements();
290 }
291292/***293 */294public String getRowHeight(int pos) {
295if (pos < rowHeights.size()) return (String)rowHeights.elementAt(pos);
296returnnull;
297 }
298299/***300 */301private Vector parseList(String list) {
302 Vector v = new Vector();
303if (list!=null) {
304 StringTokenizer st = new StringTokenizer(list,",");
305while (st.hasMoreTokens())
306 v.addElement(st.nextToken());
307 }
308309return v;
310 }
311312 }
313