001: /*
002: * (C) Copyright 2000 - 2006 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: package com.nabhinc.portal.model;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.portlet.ActionRequest;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.xml.bind.Unmarshaller;
028: import javax.xml.bind.annotation.XmlElement;
029:
030: import com.nabhinc.portal.core.PortalConstants;
031: import com.nabhinc.util.StringUtil;
032:
033: /**
034: *
035: *
036: * @author Padmanabh Dabke
037: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
038: */
039: public abstract class NavigationalLayout extends
040: BaseCompositeRenderable {
041:
042: public transient int rowIndex = -1;
043:
044: @XmlElement(name="option")
045: public NavigationOption[] navOptions = new NavigationOption[0];
046:
047: public void setRowIndex(int ri) {
048: this .rowIndex = computeRowIndex(ri);
049: }
050:
051: public int computeRowIndex(int ri) {
052: if (navOptions == null || navOptions.length == 0) {
053: return -1;
054: }
055: if (ri >= navOptions.length) {
056: ri = navOptions.length - 1;
057: for (; ri > -1 && navOptions[ri].renderable == null; ri--) {
058: }
059:
060: } else {
061: while (ri < this .navOptions.length
062: && this .navOptions[ri].renderable == null) {
063: ri++;
064: }
065: if (ri == this .navOptions.length)
066: ri = -1;
067: }
068: return ri;
069: }
070:
071: public Renderable getCurrentRenderable(HttpServletRequest request) {
072: int currentRowIndex = getCurrentRowIndex(request);
073: if (navOptions != null && currentRowIndex > -1
074: && navOptions.length > currentRowIndex)
075: return navOptions[currentRowIndex].renderable;
076: return null;
077: }
078:
079: public int getCurrentRowIndex(HttpServletRequest request) {
080: String currentIndexStr = (String) request
081: .getAttribute(PortalConstants.NAV_LAYOUT_SELECTION_ATTRIB
082: + this .getNumericId());
083: if (currentIndexStr != null) {
084: return computeRowIndex(Integer.parseInt(currentIndexStr));
085: } else {
086: return this .rowIndex;
087: }
088: }
089:
090: public NavigationalLayout() {
091: }
092:
093: public NavigationalLayout(PortalPage p) {
094: super (p);
095: }
096:
097: public void create(ActionRequest req) {
098:
099: String temp = req.getParameter("template");
100: if (temp == null || temp.equals("")) {
101: // do nothing
102: } else {
103: setTemplate(temp);
104: }
105:
106: }
107:
108: public void afterUnmarshal(Unmarshaller um, Object parent) {
109: setRowIndex(0);
110: if (navOptions != null) {
111: for (int i = 0; i < navOptions.length; i++) {
112: if (navOptions[i].renderable != null
113: && navOptions[i].renderable instanceof PortletWindow) {
114: ((PortletWindow) navOptions[i].renderable)
115: .setTemplate(bprComponentTemplate);
116: }
117: }
118: }
119: }
120:
121: public void reorderRows(int oldIndex, int newIndex, int parentIndex) {
122: if (oldIndex < 0 || oldIndex >= navOptions.length)
123: return;
124: if (newIndex < 0 || newIndex >= navOptions.length)
125: return;
126: if (oldIndex == newIndex)
127: return;
128:
129: NavigationOption oldTab = navOptions[oldIndex];
130: if (oldIndex < newIndex) {
131: for (int i = oldIndex; i < newIndex; i++) {
132: navOptions[i] = navOptions[i + 1];
133: }
134: navOptions[newIndex] = oldTab;
135: /*
136: if (this.rowIndex > oldIndex && this.rowIndex <= newIndex)
137: this.rowIndex--;
138: */
139: } else {
140: for (int i = oldIndex; i > newIndex; i--) {
141: navOptions[i] = navOptions[i - 1];
142: }
143: navOptions[newIndex] = oldTab;
144: /*
145: if (this.rowIndex >= newIndex && this.rowIndex < oldIndex)
146: this.rowIndex++;
147: */
148: }
149: /*
150: if (oldIndex == this.rowIndex)
151: this.rowIndex = newIndex;
152: */
153: setRowIndex(0);
154:
155: }
156:
157: protected void addRenderable(Renderable r, String optionLabel,
158: int level) {
159: NavigationOption newOption = new NavigationOption();
160: newOption.label = StringUtil.isNullOrEmpty(optionLabel) ? "sb.portal.new_option_label"
161: : optionLabel;
162: newOption.renderable = r;
163: newOption.level = level;
164: newOption.type = NavigationOption.OPTION_TYPE_PORTLET;
165: newOption.url = null;
166: addOption(newOption);
167: }
168:
169: public void addExternalLink(String optionLabel, String url) {
170: NavigationOption newOption = new NavigationOption();
171: newOption.label = StringUtil.isNullOrEmpty(optionLabel) ? "sb.portal.new_option_label"
172: : optionLabel;
173: newOption.renderable = null;
174: newOption.level = 0;
175: newOption.type = NavigationOption.OPTION_TYPE_URL;
176: newOption.url = url;
177: addOption(newOption);
178:
179: }
180:
181: public void addHeader(String optionLabel) {
182: NavigationOption newOption = new NavigationOption();
183: newOption.label = StringUtil.isNullOrEmpty(optionLabel) ? "sb.portal.new_option_label"
184: : optionLabel;
185: newOption.renderable = null;
186: newOption.level = 0;
187: newOption.type = NavigationOption.OPTION_TYPE_HEADER;
188: newOption.url = null;
189: addOption(newOption);
190:
191: }
192:
193: protected void addOption(NavigationOption opt) {
194: NavigationOption[] newOptions = new NavigationOption[this .navOptions.length + 1];
195: for (int i = 0; i < this .navOptions.length; i++) {
196: newOptions[i] = this .navOptions[i];
197: }
198: newOptions[this .navOptions.length] = opt;
199: this .navOptions = newOptions;
200:
201: if (opt.renderable != null
202: && opt.renderable instanceof PortletWindow) {
203: opt.renderable.setTemplate(this .bprComponentTemplate);
204: }
205:
206: if (this .rowIndex == -1)
207: setRowIndex(0);
208: }
209:
210: public void deleteOption(int rowId) {
211: NavigationOption[] newOptions = new NavigationOption[this .navOptions.length - 1];
212: for (int i = 0; i < rowId; i++) {
213: newOptions[i] = this .navOptions[i];
214: }
215: for (int i = rowId; i < newOptions.length; i++) {
216: newOptions[i] = this .navOptions[i + 1];
217: }
218: /*
219: if (this.rowIndex == rowId) setRowIndex(0);
220: else if (this.rowIndex > rowId) this.rowIndex--;
221: */
222: this .navOptions = newOptions;
223: setRowIndex(0);
224: }
225:
226: public void renameOption(int rowId, String newName) {
227: if (StringUtil.isNotNullOrEmpty(newName))
228: this .navOptions[rowId].label = newName;
229: }
230:
231: public void addContent(Renderable r, String optionLabel) {
232: addRenderable(r, optionLabel, 0);
233: setRowIndex(0);
234: }
235:
236: public Iterator<Renderable> getComponents() {
237: List<Renderable> rList = new ArrayList<Renderable>();
238:
239: if (this .navOptions != null) {
240: for (int i = 0; i < this .navOptions.length; i++) {
241: if (this .navOptions[i].renderable != null)
242: rList.add(this .navOptions[i].renderable);
243: }
244:
245: }
246: return rList.iterator();
247: }
248:
249: public boolean removePortletWindow(PortletWindow pWindow) {
250:
251: for (int i = 0; i < navOptions.length; i++) {
252: if (navOptions[i].renderable == pWindow) {
253: deleteRow(i);
254: return true;
255: } else {
256: if (navOptions[i].renderable
257: .removePortletWindow(pWindow))
258: return true;
259: }
260: }
261:
262: return false;
263:
264: }
265:
266: /**
267: * Delete a portlet at the specified index.
268: */
269: public void deleteRow(int portletIndex) {
270:
271: if (portletIndex < 0 || portletIndex >= navOptions.length) {
272: // ignore invalid indices
273: //warn("Attempt to delete a portlet with invalid portlet index.");
274: return;
275: }
276:
277: NavigationOption[] newOptions = new NavigationOption[navOptions.length - 1];
278:
279: for (int i = 0; i < portletIndex; i++) {
280: newOptions[i] = navOptions[i];
281: }
282:
283: int newLen = newOptions.length;
284:
285: for (int i = portletIndex; i < newLen; i++) {
286: newOptions[i] = navOptions[i + 1];
287: }
288:
289: navOptions = newOptions;
290: setRowIndex(this .rowIndex);
291: }
292:
293: public void addPortletWindow(PortletWindow pWindow) {
294: addRenderable(pWindow, "New Option", 0);
295: }
296:
297: }
|