KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > layout > PortletBeanGrid


1 package org.jahia.layout;
2
3 /**
4  * Title: 2D grid of PortletBean object, used to map notably on HTML
5  * tables
6  * Description: This object offers 2D layout of portlets, allowing them to
7  * be added to the grid, as well as interactively moved.
8  * Copyright: Copyright (c) 2002
9  * Company: Jahia Ltd
10  * @author Serge Huber
11  * @version 1.0
12  * @todo Currently this is not optimized, we go through enumerations a lot,
13  * we might want to add a real grid to speed up operations
14  */

15 import java.util.Enumeration JavaDoc;
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     /**
29      * Copy constructor
30      * @param sourceSet original set to copy from
31      */

32     public PortletBeanGrid(PortletBeanSet sourceSet) {
33         Enumeration JavaDoc sourceElements = sourceSet.elements();
34         while (sourceElements.hasMoreElements()) {
35             PortletBean portlet = (PortletBean) sourceElements.nextElement();
36             this.add(portlet);
37         }
38     }
39
40     /**
41      * Retrieves the PortletBean at the specified position in the grid.
42      * @param rowID the number of the row (0-based)
43      * @param columnID the number of the column (0-based)
44      * @returns the portlet bean at the specified position, and if there is none,
45      * returns null
46      */

47     public PortletBean getPosPortlet(int rowID, int columnID) {
48         Enumeration JavaDoc 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; // no portlet at that position ! Might be better to return
57
// an exception here !
58
}
59
60     /**
61      * Retrieves all the portlets on a given row
62      * @param rowID the number of the row to retrieve (0-based)
63      * @returns a PortletBeanSet object containing a set of the PortletBean objects
64      * available on that row. This set might be empty if the row doesn't exist. The
65      * result list is sorted according to the column position
66      */

67     public PortletBeanSet getRowPortlets(int rowID) {
68         Enumeration JavaDoc 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         // let's sort inside the row by column
78
// FIXME : this could be much faster with better sorting algorithms
79
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     /**
91      * Retrieves all the portlets on a given column
92      * @param columnID the number of the column to retrieve (0-based)
93      * @returns a PortletBeanSet object containing a set of the PortletBean objects
94      * available on that column. This set might be empty if the column doesn't exist.
95      */

96     public PortletBeanSet getColumnPortlets(int columnID) {
97         Enumeration JavaDoc 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         // let's sort inside the column by row
107
// FIXME : this could be much faster with better sorting algorithms
108
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     /**
121      * Move the portlet up by one row in the grid. If the row is already the top
122      * one no movement is down. Otherwise the movement is simply done by swapping
123      * positions with the portlet above the one we want to move.
124      * @param portletID the identifier of the portlet to move
125      * @throws JahiaException if the portlet corresponding to the identifier
126      * cannot be found.
127      */

128     public void movePortletUp(int portletID)
129     throws JahiaException {
130         PortletBean targetPortlet = findPortlet(portletID);
131         if (targetPortlet.getPortletRow() == 0) {
132             /** @todo for the moment we just do nothing, but we could also
133              * decide to move everything down and insert a new first row with
134              * our portlet alone on it
135              */

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     /**
147      * Moves the portlet down one row, creating a new row if it doesn't exist
148      * @param portletID identifier of the portlet to be moved
149      * @throws JahiaException if the portlet cannot be found.
150      */

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     /**
167      * Moves the specified portlet one column to the left. If the portlet is
168      * already in the leftmost column, this method does nothing.
169      * @param portletID identifier of the portlet to be moved
170      * @throws JahiaException if the portlet corresponding to the identifier
171      * cannot be found
172      */

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             /** @todo for the moment we just do nothing, but we could also
180              * decide to move everything down and insert a new first row with
181              * our portlet alone on it
182              */

183         } else {
184             /* Let's move all the portlets in the current column up one row */
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             /* Let's move all the portlets in the new column down to insert the portlet */
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             /* Let's check if we are inserting in a column that is smaller than
199                the current row position. If so reduce the row to include at the
200                end of the column, avoiding "hole" creation.
201              */

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     /**
211      * Move the specified portlet one column to the right, creating a new column
212      * if the portlet is already at the rightmost position before the move.
213      * @param portletID identifier of the portlet to be moved
214      * @throws JahiaException if the portlet corresponding to the identifier
215      * cannot be found
216      */

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             /* Let's move all the portlets in the current column up one row */
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             /* Let's move all the portlets in the current column up one row */
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             /* Let's move all the portlets in the new column down to insert the portlet */
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         /* Let's check if we are inserting in a column that is smaller than
261            the current row position. If so reduce the row to include at the
262            end of the column, avoiding "hole" creation.
263          */

264         if (targetPortlet.getPortletRow() >= getRowCount(oldColumn + 1) ) {
265             targetPortlet.setPortletRow(getRowCount(oldColumn + 1));
266         }
267         targetPortlet.setPortletColumn(oldColumn + 1);
268     }
269
270     /**
271      * Retrieves the column count present in the grid. This is obtained by going
272      * through all the portlets and keeping the maximal value.
273      * Note : this might mean that some columns do not contain portlets.
274      * @returns an integer that represents the maximal column value in the grid
275      * @todo investigate how this behaves when column "holes" are present
276      */

277     public int getColumnCount() {
278         Enumeration JavaDoc 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     /**
289      * Retrieves the column count in the specified row.
290      * @param rowID the identifier (0-based) of the row in which we want to count
291      * the columns.
292      * @returns an integer representing the number of columns in the specified
293      * row
294      */

295     public int getColumnCount (int rowID) throws JahiaException {
296         int resultColumnCount = 0;
297         Enumeration JavaDoc 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     /**
310      * Retrieves the row count present in the grid. This is obtained by going
311      * through all the portlets and keeping the maximal value.
312      * Note : this might mean that some rows do not contain portlets.
313      * @returns an integer that represents the maximal row value in the grid
314      * @todo investigate how this behaves when row "holes" are present in the
315      * grid
316      */

317     public int getRowCount() {
318         Enumeration JavaDoc 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     /**
329      * Retrieves the row count present in the grid at the specified column.
330      * @param columnID the identifier (0-based) of the column we want to get the
331      * row count of.
332      * @returns an integer that represents the maximal row value in the specified
333      * column
334      */

335     public int getRowCount(int columnID) throws JahiaException {
336         int resultRowCount = 0;
337         Enumeration JavaDoc 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     /**
350      * Insert a new portlet into the set. What is done here is position checking
351      * to make sure the new portlet does not overlap or conflict with an existing
352      * portlet.
353      * WARNING : this method modifies the position of the portlet bean object !!
354      * @param portlet PortletBean object to insert in the grid. This object will
355      * also be modified so that the correct position is reflected in the portlet
356      * @returns true on success, false on failure (failure is not defined for the
357      * moment)
358      */

359     public boolean add(PortletBean portlet) {
360         // let's do position checking.
361
Enumeration JavaDoc 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                 // we have found a conflict, let's resolve it.
367
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     /**
379      * Displays the grid on the jahia console by inserting the portlet ID into
380      * each cell.
381      */

382     public void debugDisplayGrid() {
383         String JavaDoc separatorLine = new String JavaDoc();
384         for (int j=0; j < this.getColumnCount(); j++) {
385             separatorLine = separatorLine + "+-----";
386         }
387
388         StringBuffer JavaDoc curLine;
389         String JavaDoc idStr;
390         for (int i=0; i < this.getRowCount(); i++) {
391             curLine = new StringBuffer JavaDoc();
392             curLine = new StringBuffer JavaDoc();
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