1 package org.jahia.layout; 2 3 15 import java.util.Enumeration ; 16 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.utils.JahiaConsole; 19 20 public class PortletBeanGrid extends PortletBeanSet { 21 22 protected int columnCount = 0; 23 protected int rowCount = 0; 24 25 public PortletBeanGrid() { 26 } 27 28 32 public PortletBeanGrid(PortletBeanSet sourceSet) { 33 Enumeration sourceElements = sourceSet.elements(); 34 while (sourceElements.hasMoreElements()) { 35 PortletBean portlet = (PortletBean) sourceElements.nextElement(); 36 this.add(portlet); 37 } 38 } 39 40 47 public PortletBean getPosPortlet(int rowID, int columnID) { 48 Enumeration portletList = this.elements(); 49 while (portletList.hasMoreElements()) { 50 PortletBean curPortlet = (PortletBean) portletList.nextElement(); 51 if ( (curPortlet.getPortletRow() == rowID) && 52 (curPortlet.getPortletColumn() == columnID) ) { 53 return curPortlet; 54 } 55 } 56 return null; } 59 60 67 public PortletBeanSet getRowPortlets(int rowID) { 68 Enumeration portletList = this.elements(); 69 PortletBeanSet resultList = new PortletBeanSet(); 70 while (portletList.hasMoreElements()) { 71 PortletBean curPortlet = (PortletBean) portletList.nextElement(); 72 if (curPortlet.getPortletRow() == rowID) { 73 resultList.add(curPortlet); 74 } 75 } 76 77 PortletBeanSet sortedResultList = new PortletBeanSet(); 80 for (int i = 0; i < resultList.size(); i++) { 81 for (int j = 0; j < resultList.size(); j++) { 82 PortletBean curPortlet = (PortletBean) resultList.elementAt(j); 83 if (curPortlet.getPortletColumn() == i) 84 sortedResultList.add(curPortlet); 85 } 86 } 87 return sortedResultList; 88 } 89 90 96 public PortletBeanSet getColumnPortlets(int columnID) { 97 Enumeration portletList = this.elements(); 98 PortletBeanSet resultList = new PortletBeanSet(); 99 while (portletList.hasMoreElements()) { 100 PortletBean curPortlet = (PortletBean) portletList.nextElement(); 101 if (curPortlet.getPortletColumn() == columnID) { 102 resultList.add(curPortlet); 103 } 104 } 105 106 PortletBeanSet sortedResultList = new PortletBeanSet(); 109 for (int i = 0; i < resultList.size(); i++) { 110 for (int j = 0; j < resultList.size(); j++) { 111 PortletBean curPortlet = (PortletBean) resultList.elementAt(j); 112 if (curPortlet.getPortletRow() == i) 113 sortedResultList.add(curPortlet); 114 } 115 } 116 117 return sortedResultList; 118 } 119 120 128 public void movePortletUp(int portletID) 129 throws JahiaException { 130 PortletBean targetPortlet = findPortlet(portletID); 131 if (targetPortlet.getPortletRow() == 0) { 132 136 } else { 137 PortletBean portletToSwap = getPosPortlet(targetPortlet.getPortletRow() - 1, 138 targetPortlet.getPortletColumn()); 139 if (portletToSwap != null) { 140 portletToSwap.setPortletRow(targetPortlet.getPortletRow()); 141 } 142 targetPortlet.setPortletRow(targetPortlet.getPortletRow() - 1); 143 } 144 } 145 146 151 public void movePortletDown(int portletID) 152 throws JahiaException { 153 PortletBean targetPortlet = findPortlet(portletID); 154 if (targetPortlet.getPortletRow() == getRowCount()-1) { 155 rowCount++; 156 } else { 157 PortletBean portletToSwap = getPosPortlet(targetPortlet.getPortletRow() + 1 , 158 targetPortlet.getPortletColumn()); 159 if (portletToSwap != null) { 160 portletToSwap.setPortletRow(targetPortlet.getPortletRow()); 161 } 162 } 163 targetPortlet.setPortletRow(targetPortlet.getPortletRow() + 1); 164 } 165 166 173 public void movePortletLeft(int portletID) 174 throws JahiaException { 175 PortletBean targetPortlet = findPortlet(portletID); 176 int oldRow = targetPortlet.getPortletRow(); 177 int oldColumn = targetPortlet.getPortletColumn(); 178 if (targetPortlet.getPortletColumn() == 0) { 179 183 } else { 184 185 for (int i = oldRow +1; i <= getRowCount(oldColumn); i++) { 186 PortletBean curPortlet = getPosPortlet(i, oldColumn); 187 if (curPortlet != null) { 188 curPortlet.setPortletRow(i-1); 189 } 190 } 191 192 for (int i = getRowCount(oldColumn - 1) - 1; i >= oldRow; i--) { 193 PortletBean curPortlet = getPosPortlet(i, oldColumn - 1); 194 if (curPortlet != null) { 195 curPortlet.setPortletRow(i+1); 196 } 197 } 198 202 if (targetPortlet.getPortletRow() >= getRowCount(targetPortlet.getPortletColumn() - 1) ) { 203 targetPortlet.setPortletRow(getRowCount(targetPortlet.getPortletColumn() - 1)); 204 } 205 targetPortlet.setPortletColumn(targetPortlet.getPortletColumn() - 1); 206 207 } 208 } 209 210 217 public void movePortletRight(int portletID) 218 throws JahiaException { 219 JahiaConsole.println("PortletBeanGrid.movePortletRight", "Moving portlet " + Integer.toString(portletID) + " to the right..."); 220 PortletBean targetPortlet = findPortlet(portletID); 221 int oldRow = targetPortlet.getPortletRow(); 222 int oldColumn = targetPortlet.getPortletColumn(); 223 if (oldColumn == getColumnCount()-1) { 224 columnCount++; 225 226 JahiaConsole.println("PortletBeanGrid.movePortletRight", "Deleting from column " + 227 Integer.toString(oldColumn) + ", moving " + 228 Integer.toString(getRowCount(oldColumn)) + " portlets up..."); 229 for (int i = oldRow+1; 230 i <= getRowCount(oldColumn); i++) { 231 PortletBean curPortlet = getPosPortlet(i, oldColumn); 232 if (curPortlet != null) { 233 curPortlet.setPortletRow(i-1); 234 } 235 } 236 } else { 237 238 JahiaConsole.println("PortletBeanGrid.movePortletRight", "Deleting from column " + 239 Integer.toString(oldColumn) + ", moving " + 240 Integer.toString(getRowCount(oldColumn)) + " portlets up..."); 241 for (int i = oldRow+1; 242 i <= getRowCount(oldColumn); i++) { 243 PortletBean curPortlet = getPosPortlet(i, oldColumn); 244 if (curPortlet != null) { 245 curPortlet.setPortletRow(i-1); 246 } 247 } 248 249 JahiaConsole.println("PortletBeanGrid.movePortletRight", "Making space in column " + 250 Integer.toString(oldColumn+1) + ", moving " + 251 Integer.toString(getRowCount(oldColumn+1)) + "portlets down..."); 252 for (int i = getRowCount(oldColumn + 1) - 1; 253 i >= targetPortlet.getPortletRow(); i--) { 254 PortletBean curPortlet = getPosPortlet(i, oldColumn + 1); 255 if (curPortlet != null) { 256 curPortlet.setPortletRow(i+1); 257 } 258 } 259 } 260 264 if (targetPortlet.getPortletRow() >= getRowCount(oldColumn + 1) ) { 265 targetPortlet.setPortletRow(getRowCount(oldColumn + 1)); 266 } 267 targetPortlet.setPortletColumn(oldColumn + 1); 268 } 269 270 277 public int getColumnCount() { 278 Enumeration portletBeanEnum = this.elements(); 279 while (portletBeanEnum.hasMoreElements()) { 280 PortletBean portlet = (PortletBean) portletBeanEnum.nextElement(); 281 if (portlet.getPortletColumn() + 1 > columnCount) { 282 columnCount = portlet.getPortletColumn() + 1; 283 } 284 } 285 return columnCount; 286 } 287 288 295 public int getColumnCount (int rowID) throws JahiaException { 296 int resultColumnCount = 0; 297 Enumeration portletBeanEnum = this.elements(); 298 while (portletBeanEnum.hasMoreElements()) { 299 PortletBean portlet = (PortletBean) portletBeanEnum.nextElement(); 300 if (portlet.getPortletRow() == rowID) { 301 if (portlet.getPortletColumn() + 1 > resultColumnCount) { 302 resultColumnCount = portlet.getPortletColumn() + 1; 303 } 304 } 305 } 306 return resultColumnCount; 307 } 308 309 317 public int getRowCount() { 318 Enumeration portletBeanEnum = this.elements(); 319 while (portletBeanEnum.hasMoreElements()) { 320 PortletBean portlet = (PortletBean) portletBeanEnum.nextElement(); 321 if (portlet.getPortletRow() + 1 > rowCount) { 322 rowCount = portlet.getPortletRow() + 1; 323 } 324 } 325 return rowCount; 326 } 327 328 335 public int getRowCount(int columnID) throws JahiaException { 336 int resultRowCount = 0; 337 Enumeration portletBeanEnum = this.elements(); 338 while (portletBeanEnum.hasMoreElements()) { 339 PortletBean portlet = (PortletBean) portletBeanEnum.nextElement(); 340 if (portlet.getPortletColumn() == columnID) { 341 if (portlet.getPortletRow() + 1 > resultRowCount) { 342 resultRowCount = portlet.getPortletRow() + 1; 343 } 344 } 345 } 346 return resultRowCount; 347 } 348 349 359 public boolean add(PortletBean portlet) { 360 Enumeration portletBeanEnum = this.elements(); 362 while (portletBeanEnum.hasMoreElements()) { 363 PortletBean curPortlet = (PortletBean) portletBeanEnum.nextElement(); 364 if ((curPortlet.getPortletRow() == portlet.getPortletRow()) && 365 (curPortlet.getPortletColumn() == portlet.getPortletColumn())) { 366 int maxRow = getRowCount(); 368 portlet.setPortletRow(maxRow); 369 portlet.setPortletColumn(0); 370 JahiaConsole.println("PortletBeanGrid.add", 371 "Conflict found, modifying new portlet position to row=" + maxRow + " column=0"); 372 } 373 } 374 375 return super.add(portlet); 376 } 377 378 382 public void debugDisplayGrid() { 383 String separatorLine = new String (); 384 for (int j=0; j < this.getColumnCount(); j++) { 385 separatorLine = separatorLine + "+-----"; 386 } 387 388 StringBuffer curLine; 389 String idStr; 390 for (int i=0; i < this.getRowCount(); i++) { 391 curLine = new StringBuffer (); 392 curLine = new StringBuffer (); 393 for (int j=0; j < this.getColumnCount(); j++) { 394 PortletBean curPortlet = this.getPosPortlet(i, j); 395 curLine.append("�"); 396 if (curPortlet != null) { 397 idStr = Integer.toString(curPortlet.getPortletID()); 398 while (idStr.length() < 5) { 399 idStr = " " + idStr; 400 } 401 } else { 402 idStr = " null"; 403 } 404 curLine.append(idStr); 405 } 406 JahiaConsole.println("PortletBeanGrid.debugDisplayGrid", separatorLine); 407 JahiaConsole.println("PortletBeanGrid.debugDisplayGrid", 408 curLine.toString()); 409 } 410 } 411 412 } | Popular Tags |