1 16 17 package org.apache.jetspeed.portal.controllers; 18 19 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 import org.apache.turbine.util.RunData; 29 30 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 import java.util.Vector ; 40 import java.util.StringTokenizer ; 41 import java.util.Enumeration ; 42 43 44 71 public class GridPortletController extends AbstractPortletController 72 { 73 74 75 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 rowHeights = null; 83 private Vector colWidths = null; 84 85 87 public GridPortletController() { 88 rowHeights = new Vector (); 89 colWidths = new Vector (); 90 } 91 92 94 private void calculateControllerLayout( Portlet portlet ) { 95 96 if ( portlet instanceof PortletSet ) { 97 98 Enumeration 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 colObj = portletConf.getConstraints().getColumn(); 110 Integer 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 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 Enumeration en = portlets.getPortlets(); 140 141 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; 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 en = portlets.getPortlets(); 176 while (en.hasMoreElements() ) { 177 178 Portlet p = (Portlet)en.nextElement(); 179 PortletConfig pConf = p.getPortletConfig(); 180 181 Integer colObj = pConf.getConstraints().getColumn(); 182 Integer 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 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 e) 210 { 211 logger.error("getContent():", e); 212 } 213 214 return base; 215 216 } 217 218 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 236 public void setColumn(int col) { 237 this.columns=col; 238 } 239 240 243 public int getColumn() { 244 return this.columns; 245 } 246 247 250 public void setRow(int row) { 251 this.rows=row; 252 } 253 254 257 public int getRow() { 258 return this.rows; 259 } 260 261 263 public void setColumnsWidth(Vector widths) { 264 this.colWidths = widths; 265 } 266 267 269 public Enumeration getColumnsWidth() { 270 return colWidths.elements(); 271 } 272 273 275 public String getColumnWidth(int pos) { 276 if (pos < colWidths.size()) return (String )colWidths.elementAt(pos); 277 return null; 278 } 279 280 282 public void setRowsHeight(Vector heights) { 283 this.rowHeights = heights; 284 } 285 286 288 public Enumeration getRowsHeight() { 289 return rowHeights.elements(); 290 } 291 292 294 public String getRowHeight(int pos) { 295 if (pos < rowHeights.size()) return (String )rowHeights.elementAt(pos); 296 return null; 297 } 298 299 301 private Vector parseList(String list) { 302 Vector v = new Vector (); 303 if (list!=null) { 304 StringTokenizer st = new StringTokenizer (list,","); 305 while (st.hasMoreTokens()) 306 v.addElement(st.nextToken()); 307 } 308 309 return v; 310 } 311 312 } 313 314 | Popular Tags |