001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.model;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Vector;
026:
027: import javax.portlet.ActionRequest;
028: import javax.xml.bind.annotation.XmlElement;
029:
030: /**
031: * A page layout that arranges individual portlets in a grid.
032: *
033: * @author Padmanabh Dabke
034: * (c) 2001, 2003 Nabh Information Systems, Inc. All Rights Reserved.
035: */
036: public class GridLayout extends BaseCompositeRenderable {
037:
038: /**
039: * Page columns.
040: */
041: @XmlElement(name="column")
042: public ColumnLayout[] columns = new ColumnLayout[0];
043:
044: @XmlElement(name="top-row-portlet")
045: public Renderable topRowPortlet = null;
046:
047: @XmlElement(name="bottom-row-portlet")
048: public Renderable bottomRowPortlet = null;
049:
050: /**
051: * GridConfig constructor comment.
052: */
053: public GridLayout() {
054: setTemplate("column_layout.jsp");
055: }
056:
057: /**
058: * Adds a portlet at the given column.
059: */
060: protected void addPortletInfo(int colIndex, Renderable pi) {
061:
062: columns[colIndex].addPortlet(pi);
063: }
064:
065: /**
066: * Delete a portlet at the given column and portlet index.
067: */
068: public void deletePortlet(int colIndex, int portletIndex) {
069: if (colIndex == -1 && portletIndex == 0) {
070: topRowPortlet = null;
071: return;
072: }
073:
074: if (colIndex == -1 && portletIndex == 2) {
075: bottomRowPortlet = null;
076: return;
077: }
078:
079: if (colIndex < 0 || colIndex >= columns.length) {
080: // ignore invalid indices
081: //warn("Attempt to delete a portlet with invalid column index.");
082: return;
083: } else {
084: ColumnLayout colLayout = columns[colIndex];
085: colLayout.deletePortlet(portletIndex);
086: }
087: }
088:
089: /**
090: * Get the portlet located at the given coordinates.
091: * @return net.nabh.portal.Portlet
092: * @param colIndex int
093: * @param portletIndex int
094: */
095: public Renderable getPortletWindow(int colIndex, int portletIndex) {
096: if (colIndex == -1 && portletIndex == 0) {
097: return topRowPortlet;
098: }
099:
100: if (colIndex == -1 && portletIndex == 2) {
101: return bottomRowPortlet;
102: }
103:
104: if (colIndex < 0 || colIndex >= columns.length) {
105: // ignore invalid indices
106: //warn("Attempt to delete a portlet with invalid column index.");
107: return null;
108: } else {
109: return columns[colIndex].getPortletWindow(portletIndex);
110: }
111:
112: }
113:
114: @SuppressWarnings("unchecked")
115: public void create(ActionRequest req) {
116: int numColumns = Integer.parseInt(req
117: .getParameter("numcolumns"));
118: int maxPortlets = Integer.parseInt(req
119: .getParameter("maxportlets"));
120: String tPortlet = req.getParameter("topportlet");
121: String bPortlet = req.getParameter("bottomportlet");
122:
123: if (tPortlet != null && !"".equals(tPortlet)) {
124: topRowPortlet = createRenderable(tPortlet, req,
125: "topborder", "toptitle");
126: }
127:
128: if (bPortlet != null && !"".equals(bPortlet)) {
129: bottomRowPortlet = createRenderable(tPortlet, req,
130: "bottomborder", "bottomtitle");
131: }
132:
133: columns = new ColumnLayout[numColumns];
134: for (int i = 0; i < numColumns; i++) {
135: columns[i] = new ColumnLayout();
136: Vector portletList = new Vector(maxPortlets);
137: for (int j = 0; j < maxPortlets; j++) {
138: String portletName = req.getParameter("col" + i + "p"
139: + j);
140: if (portletName == null || portletName.equals(""))
141: continue;
142: PortletWindow r = createPortletWindow(portletName, req,
143: "border" + i + "_" + j, "title" + i + "_" + j);
144:
145: portletList.addElement(r);
146:
147: }
148: PortletWindow[] portletArray = new PortletWindow[portletList
149: .size()];
150: portletList.copyInto(portletArray);
151: columns[i].portletInfo = portletArray;
152: }
153:
154: try {
155: int rs = Integer.parseInt(req.getParameter("refresh"));
156: setRefreshSeconds(rs);
157: } catch (Exception ex) {
158: setRefreshSeconds(-1);
159: }
160:
161: }
162:
163: /**
164: * Modify the contents of this layout based on the form data submitted
165: * via HttpServletRequest.
166: * Creation date: (4/9/2002 12:12:34 AM)
167: * @param req javax.servlet.http.HttpServletRequest
168: */
169: public void modify(ActionRequest req) {
170:
171: String action = req.getParameter("action");
172: int rowIndex = 0;
173: int colIndex = 0;
174: PortletWindow pInfo = null;
175:
176: try {
177: int rs = Integer.parseInt(req.getParameter("refresh"));
178: setRefreshSeconds(rs);
179: } catch (Exception ex) {
180: setRefreshSeconds(-1);
181: }
182:
183: try {
184: rowIndex = Integer.parseInt(req.getParameter("rowindex"));
185: } catch (Exception ex) {
186: }
187:
188: try {
189: colIndex = Integer.parseInt(req.getParameter("colindex"));
190: } catch (Exception ex) {
191: }
192:
193: if ("moveup".equals(action)) {
194: pInfo = columns[colIndex].portletInfo[rowIndex];
195: columns[colIndex].portletInfo[rowIndex] = columns[colIndex].portletInfo[rowIndex - 1];
196: columns[colIndex].portletInfo[rowIndex - 1] = pInfo;
197: return;
198: } else if ("movedown".equals(action)) {
199: pInfo = columns[colIndex].portletInfo[rowIndex];
200: columns[colIndex].portletInfo[rowIndex] = columns[colIndex].portletInfo[rowIndex + 1];
201: columns[colIndex].portletInfo[rowIndex + 1] = pInfo;
202: return;
203: } else if ("moveleft".equals(action)) {
204: pInfo = columns[colIndex].portletInfo[rowIndex];
205: deletePortlet(colIndex, rowIndex);
206: addPortletInfo(colIndex - 1, pInfo);
207: return;
208: } else if ("moveright".equals(action)) {
209: pInfo = columns[colIndex].portletInfo[rowIndex];
210: deletePortlet(colIndex, rowIndex);
211: addPortletInfo(colIndex + 1, pInfo);
212: return;
213: } else if ("showtitle".equals(action)) {
214: if (colIndex == -1 && rowIndex == 0)
215: ((PortletWindow) topRowPortlet).showTitle = true;
216: else if (colIndex == -1 && rowIndex == 2)
217: ((PortletWindow) bottomRowPortlet).showTitle = true;
218: else
219: ((PortletWindow) columns[colIndex].portletInfo[rowIndex]).showTitle = true;
220: return;
221: } else if ("hidetitle".equals(action)) {
222: if (colIndex == -1 && rowIndex == 0)
223: ((PortletWindow) topRowPortlet).showTitle = false;
224: else if (colIndex == -1 && rowIndex == 2)
225: ((PortletWindow) bottomRowPortlet).showTitle = false;
226: else
227: ((PortletWindow) columns[colIndex].portletInfo[rowIndex]).showTitle = false;
228: return;
229: } else if ("showborder".equals(action)) {
230: if (colIndex == -1 && rowIndex == 0)
231: ((PortletWindow) topRowPortlet).borderWidth = 1;
232: else if (colIndex == -1 && rowIndex == 2)
233: ((PortletWindow) bottomRowPortlet).borderWidth = 1;
234: else
235: ((PortletWindow) columns[colIndex].portletInfo[rowIndex]).borderWidth = 1;
236: return;
237: } else if ("hideborder".equals(action)) {
238: if (colIndex == -1 && rowIndex == 0)
239: ((PortletWindow) topRowPortlet).borderWidth = 0;
240: else if (colIndex == -1 && rowIndex == 2)
241: ((PortletWindow) bottomRowPortlet).borderWidth = 0;
242: else
243: ((PortletWindow) columns[colIndex].portletInfo[rowIndex]).borderWidth = 0;
244: return;
245: } else if ("delete".equals(action)) {
246: deletePortlet(colIndex, rowIndex);
247: return;
248: } else {
249:
250: // User is saving the configuration
251:
252: String newPageName = req.getParameter("pagename");
253: if (newPageName != null && (!newPageName.trim().equals(""))) {
254: getPortalPage().setName(newPageName.trim());
255: }
256: boolean addedPortlet = false;
257: for (int i = 0; i < columns.length; i++) {
258: String pName = req.getParameter("add_" + i);
259: if (pName == null || pName.equals(""))
260: continue;
261: pName = req.getParameter("portlet_" + i);
262: if (pName == null || pName.equals(""))
263: continue;
264:
265: String portletID = addPortlet(i, pName, req);
266: if (portletID == null)
267: return;
268: addedPortlet = true;
269:
270: }
271:
272: if (req.getParameter("add_top") != null) {
273: String pName = req.getParameter("topportlet");
274: if (pName != null && !"".equals(pName)) {
275: Renderable r = createRenderable(pName, req,
276: "showborder", "showtitle");
277: if (r == null)
278: return;
279: topRowPortlet = r;
280: addedPortlet = true;
281: }
282: }
283:
284: if (req.getParameter("add_bottom") != null) {
285: String pName = req.getParameter("bottomportlet");
286: if (pName != null && !"".equals(pName)) {
287: Renderable r = createRenderable(pName, req,
288: "showborder", "showtitle");
289: if (r == null)
290: return;
291: bottomRowPortlet = r;
292: addedPortlet = true;
293: }
294: }
295:
296: if (addedPortlet)
297: return;
298: }
299:
300: }
301:
302: /* (non-Javadoc)
303: * @see com.nabhinc.portal.core.PageLayout#getColumnCount()
304: */
305: public int getColumnCount() {
306: return columns.length;
307: }
308:
309: /* (non-Javadoc)
310: * @see com.nabhinc.portal.core.PageLayout#getRowCount()
311: */
312: public int getRowCount(int columnNumber) {
313: return columns[columnNumber].portletInfo.length;
314: }
315:
316: public boolean removePortletWindow(PortletWindow pWindow) {
317: if (pWindow == topRowPortlet) {
318: topRowPortlet = null;
319: return true;
320: } else if (pWindow == bottomRowPortlet) {
321: bottomRowPortlet = null;
322: return true;
323: } else {
324: for (int i = 0; i < columns.length; i++) {
325: if (columns[i].removePortletWindow(pWindow))
326: return true;
327: }
328: }
329: return false;
330: }
331:
332: public void setRowIndex(int rowIndex) {
333: // Has no use for this method
334:
335: }
336:
337: public String getLayoutType() {
338: return "grid";
339: }
340:
341: protected void reorderPortlets(List newColList,
342: String movedPortletID, PortletWindow movedWindow) {
343: for (int i = 0; i < newColList.size(); i++) {
344: String[] pIDs = (String[]) newColList.get(i);
345: ColumnLayout colLayout = this .columns[i];
346: if (colLayout.portletInfo.length == pIDs.length) {
347: int newIndex = -1;
348: int oldIndex = -1;
349: for (int j = 0; j < pIDs.length; j++) {
350: if (pIDs[j].equals(movedPortletID))
351: newIndex = j;
352: }
353: if (newIndex == -1)
354: continue;
355: for (int j = 0; j < colLayout.portletInfo.length; j++) {
356: if (colLayout.portletInfo[j].id
357: .equals(movedPortletID)) {
358: oldIndex = j;
359: break;
360: }
361: }
362: colLayout.deletePortlet(oldIndex);
363: colLayout.addPortletWindow(movedWindow, newIndex);
364: return;
365: } else if (colLayout.portletInfo.length > pIDs.length) {
366: // Portlet has been deleted from this column
367: colLayout.removePortletWindow(movedWindow);
368: } else {
369: int addIndex = colLayout.portletInfo.length;
370: for (int j = 0; j < colLayout.portletInfo.length; j++) {
371: String pID = pIDs[j];
372: if (!colLayout.portletInfo[j].id.equals(pID)) {
373: addIndex = j;
374: break;
375: }
376: }
377: colLayout.addPortletWindow(movedWindow, addIndex);
378: }
379: }
380: }
381:
382: public String addPortlet(int i, String pName, ActionRequest req) {
383: Renderable r = createRenderable(pName, req, "showborder",
384: "showtitle");
385: if (r == null)
386: return null;
387: addPortletInfo(i, r);
388: return ((PortletWindow) r).id;
389:
390: }
391:
392: public Iterator<Renderable> getComponents() {
393: List<Renderable> portletList = new ArrayList<Renderable>();
394: if (this .columns != null) {
395: for (int i = 0; i < columns.length; i++) {
396: ColumnLayout cl = columns[i];
397: if (cl.portletInfo != null) {
398: for (int j = 0; j < cl.portletInfo.length; j++) {
399: portletList.add(cl.portletInfo[j]);
400: }
401: }
402: }
403: }
404: return portletList.iterator();
405: }
406:
407: public void addContent(Renderable r, String optionLabel) {
408: // TODO Auto-generated method stub
409:
410: }
411:
412: public void addPortletWindow(PortletWindow pWindow) {
413: // TODO Auto-generated method stub
414:
415: }
416:
417: }
|