001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.tools.copletManagement;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.ListIterator;
024:
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.forms.formmodel.Repeater;
027: import org.apache.cocoon.forms.formmodel.Widget;
028: import org.apache.cocoon.forms.formmodel.Repeater.RepeaterRow;
029: import org.apache.cocoon.portal.coplet.CopletData;
030: import org.apache.cocoon.portal.coplet.CopletFactory;
031: import org.apache.cocoon.portal.coplet.CopletInstanceData;
032: import org.apache.cocoon.portal.layout.CompositeLayout;
033: import org.apache.cocoon.portal.layout.Item;
034: import org.apache.cocoon.portal.layout.Layout;
035: import org.apache.cocoon.portal.layout.LayoutFactory;
036: import org.apache.cocoon.portal.layout.NamedItem;
037: import org.apache.cocoon.portal.layout.impl.CopletLayout;
038: import org.apache.cocoon.portal.profile.ProfileManager;
039:
040: /**
041: *
042: * @version CVS $Id: LayoutActions.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public class LayoutActions {
045:
046: private final Layout layout;
047: private final LayoutFactory lf;
048: private final ProfileManager pm;
049: private final CopletFactory cf;
050:
051: public LayoutActions(Layout layout, LayoutFactory lf,
052: CopletFactory cf, ProfileManager pm) {
053: this .layout = layout;
054: this .lf = lf;
055: this .pm = pm;
056: this .cf = cf;
057: }
058:
059: // FIXME - where is this used?
060: public static int line = 1;
061:
062: /**
063: * Delets the Object with the id in the layout
064: * @param id
065: * @return true if the object could be deleted.
066: */
067: public boolean del(String id) {
068:
069: // get layout element:
070: Object layoutObj = getLayoutElement(layout, id, "", 1);
071: if (layoutObj == null)
072: return false;
073:
074: // do the job:
075: Layout lay;
076: if (layoutObj instanceof NamedItem)
077: lay = ((NamedItem) layoutObj).getLayout();
078: else
079: lay = (Layout) layoutObj;
080:
081: try {
082: // an empty item can not be handled by the LayoutFactory, do the job manual:
083: if (lay == null) {
084: List items = ((NamedItem) layoutObj).getParent()
085: .getItems();
086: for (ListIterator iter = items.listIterator(); iter
087: .hasNext();) {
088:
089: Item itemElem = (Item) iter.next();
090:
091: if (itemElem.equals(layoutObj)) {
092: items.remove(iter.nextIndex() - 1);
093: return true;
094: }
095: }
096: } else if (lay.getParent() instanceof NamedItem) {
097: // FIXME: Causes that only the contents inside a tab are deleted instead of the tab
098: NamedItem par = (NamedItem) lay.getParent();
099: par.setLayout(null);
100: } else {
101: lf.remove(lay);
102: }
103:
104: } catch (ProcessingException e) {
105: e.printStackTrace();
106: }
107:
108: return true;
109: }
110:
111: /**
112: * Moves the object one position up or down
113: * @param id id of the element
114: * @param moveUp set 'true', to move the element up ('false' to move it down)
115: * @return true if the object could be moved.
116: */
117: public boolean move(String id, boolean moveUp) {
118:
119: // get layout element:
120: Object layoutObj = getLayoutElement(layout, id, "", 1);
121: if (layoutObj == null)
122: return false;
123:
124: // do the job:
125: Layout lay;
126: Item item;
127: if (layoutObj instanceof NamedItem) {
128: lay = ((NamedItem) layoutObj).getLayout();
129: if (lay == null)
130: item = (NamedItem) layoutObj;
131: else
132: item = lay.getParent();
133: } else {
134: lay = (Layout) layoutObj;
135: item = lay.getParent();
136: }
137:
138: // find element in the list and move it:
139: List items = item.getParent().getItems();
140: for (ListIterator iter = items.listIterator(); iter.hasNext();) {
141:
142: Item itemElem = (Item) iter.next();
143:
144: if (itemElem.equals(item)) {
145:
146: int pos = iter.nextIndex() - 1;
147: int newpos = pos;
148: if (moveUp)
149: newpos--;
150: else
151: newpos++;
152:
153: if (newpos >= items.size())
154: newpos = 0;
155: if (newpos < 0)
156: newpos = items.size() - 1;
157:
158: Object obj = items.remove(pos);
159: items.add(newpos, obj);
160:
161: return true;
162: }
163: }
164: return false;
165: }
166:
167: /**
168: * Adds the object to the layout
169: * @param parent Object to which the new Object should be added
170: * @param type Type of the Object (row, col ...)
171: */
172: public void add(String parent, String type) {
173:
174: Object layoutObj = getLayoutElement(layout, parent, "", 1);
175: if (layoutObj == null)
176: return;
177:
178: Layout lay;
179: if (layoutObj instanceof NamedItem)
180: lay = ((NamedItem) layoutObj).getLayout();
181:
182: else
183: lay = (Layout) layoutObj;
184:
185: try {
186: Layout nObj = lf.newInstance(type);
187: pm.register(nObj);
188:
189: Item e = new Item();
190: nObj.setParent(e);
191: e.setLayout(nObj);
192:
193: if (lay != null)
194: ((CompositeLayout) lay).addItem(e);
195: else {
196: NamedItem ni = (NamedItem) layoutObj;
197: nObj.setParent(ni);
198: ni.setLayout(nObj);
199: }
200:
201: } catch (ProcessingException e) {
202: e.printStackTrace();
203: }
204: }
205:
206: /**
207: * Adds a new Tab
208: * @param parent Parent Object
209: * @param name Name of the Tab
210: */
211: public void addTab(String parent, String name) {
212:
213: // get layout element:
214: Object layoutObj = getLayoutElement(layout, parent, "", 1);
215: if (layoutObj == null)
216: return;
217:
218: Layout lay;
219:
220: if (layoutObj instanceof NamedItem)
221: lay = ((NamedItem) layoutObj).getLayout();
222: else
223: lay = (Layout) layoutObj;
224:
225: // add tab:
226: if (lay != null && lay.getName().equals("tab")) {
227:
228: NamedItem tab = new NamedItem();
229: tab.setName(name);
230: ((CompositeLayout) lay).addItem(tab);
231:
232: } else {
233:
234: try {
235:
236: Layout tab = lf.newInstance("tab");
237: pm.register(tab);
238:
239: NamedItem e = new NamedItem();
240: e.setName(name);
241:
242: ((CompositeLayout) tab).addItem(e);
243:
244: if (lay == null) {
245:
246: ((NamedItem) layoutObj).setLayout(tab);
247: } else {
248: Item m = new Item();
249: m.setParent((CompositeLayout) lay);
250: ((CompositeLayout) lay).addItem(m);
251: m.setLayout(tab);
252: }
253:
254: } catch (ProcessingException e) {
255: e.printStackTrace();
256: }
257: }
258: }
259:
260: public Collection getSelectedCoplets(Repeater r, Collection lets,
261: String parent) {
262:
263: // get layout element:
264: Object obj = getLayoutElement(layout, parent, "", 1);
265: if (obj == null)
266: return null;
267:
268: ArrayList coplets = new ArrayList();
269: ArrayList copletDatas = new ArrayList();
270:
271: int size = r.getSize();
272: for (int i = 0; i < size; i++) {
273: RepeaterRow row = r.getRow(i);
274: Widget widget = row.getChild("selected");
275: Boolean val = (Boolean) widget.getValue();
276: if (val.booleanValue()) {
277: coplets.add(row.getChild("coplet").getValue());
278: }
279: }
280: for (Iterator it = lets.iterator(); it.hasNext();) {
281: CopletData cd = (CopletData) it.next();
282: String cdid = cd.getId();
283: for (Iterator it2 = coplets.iterator(); it2.hasNext();) {
284: String cdidTmp = (String) it2.next();
285: if (cdidTmp.equals(cdid))
286: copletDatas.add(cd);
287: }
288: }
289:
290: for (Iterator it = copletDatas.iterator(); it.hasNext();) {
291: CopletData cd = (CopletData) it.next();
292:
293: try {
294: CopletInstanceData cinst = cf.newInstance(cd);
295: CopletLayout lay = (CopletLayout) lf
296: .newInstance("coplet");
297: lay.setCopletInstanceData(cinst);
298:
299: if (obj instanceof Item) {
300: Item item = (Item) obj;
301: item.setLayout(lay);
302: lay.setParent(item);
303: } else if (obj instanceof CompositeLayout) {
304: CompositeLayout cl = (CompositeLayout) obj;
305: Item item = new Item();
306: item.setLayout(lay);
307: lay.setParent(item);
308: cl.addItem(item);
309: }
310:
311: } catch (ProcessingException e) {
312: // ignore it
313: }
314: }
315: return copletDatas;
316: }
317:
318: public CopletInstanceData getCopletInstanceData(String id) {
319: Object obj = getLayoutElement(layout, id, "", 1);
320: if (obj instanceof CopletLayout) {
321: return ((CopletLayout) obj).getCopletInstanceData();
322: }
323: return null;
324: }
325:
326: /**
327: * interal method; search for a Layout or an Item Object
328: */
329: private Object getLayoutElement(Layout layout, String id,
330: String prefix, int pos) {
331:
332: if (layout != null) {
333:
334: if (id.equals((prefix + pos)))
335: return layout;
336:
337: if (layout instanceof CompositeLayout) {
338: Iterator i = ((CompositeLayout) layout).getItems()
339: .iterator();
340:
341: int currentpos = pos;
342: pos = 1;
343: while (i.hasNext()) {
344:
345: Item current = (Item) i.next();
346:
347: if (id.equals((prefix + currentpos + "." + pos)))
348: return current;
349:
350: Object lay = getLayoutElement(current.getLayout(),
351: id, prefix + currentpos + "." + pos + ".",
352: 1);
353: if (lay != null)
354: return lay;
355:
356: pos++;
357: }
358: }
359: }
360: return null;
361: }
362: }
|