KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > copletManagement > LayoutActions


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.tools.copletManagement;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.forms.formmodel.Repeater;
26 import org.apache.cocoon.forms.formmodel.Widget;
27 import org.apache.cocoon.forms.formmodel.Repeater.RepeaterRow;
28 import org.apache.cocoon.portal.coplet.CopletData;
29 import org.apache.cocoon.portal.coplet.CopletFactory;
30 import org.apache.cocoon.portal.coplet.CopletInstanceData;
31 import org.apache.cocoon.portal.layout.CompositeLayout;
32 import org.apache.cocoon.portal.layout.Item;
33 import org.apache.cocoon.portal.layout.Layout;
34 import org.apache.cocoon.portal.layout.LayoutFactory;
35 import org.apache.cocoon.portal.layout.NamedItem;
36 import org.apache.cocoon.portal.layout.impl.CopletLayout;
37 import org.apache.cocoon.portal.profile.ProfileManager;
38
39 /**
40  *
41  * @version CVS $Id: LayoutActions.java 278950 2005-09-06 09:15:36Z cziegeler $
42  */

43 public class LayoutActions {
44     
45     private final Layout layout;
46     private final LayoutFactory lf;
47     private final ProfileManager pm;
48     private final CopletFactory cf;
49     
50     public LayoutActions(Layout layout, LayoutFactory lf, CopletFactory cf, ProfileManager pm) {
51         this.layout = layout;
52         this.lf = lf;
53         this.pm = pm;
54         this.cf = cf;
55     }
56     
57     // FIXME - where is this used?
58
public static int line = 1;
59     
60     /**
61      * Delets the Object with the id in the layout
62      * @param id
63      * @return true if the object could be deleted.
64      */

65     public boolean del(String JavaDoc id) {
66         
67         // get layout element:
68
Object JavaDoc layoutObj = getLayoutElement (layout, id, "", 1);
69         if (layoutObj == null) return false;
70         
71         // do the job:
72
Layout lay;
73         if (layoutObj instanceof NamedItem)
74             lay = ((NamedItem)layoutObj).getLayout();
75         else
76             lay = (Layout) layoutObj;
77         
78         try {
79             // an empty item can not be handled by the LayoutFactory, do the job manual:
80
if (lay == null) {
81                 List JavaDoc items = ((NamedItem)layoutObj).getParent().getItems();
82                 for (ListIterator JavaDoc iter = items.listIterator(); iter.hasNext(); ) {
83                     
84                     Item itemElem = (Item) iter.next();
85                     
86                     if( itemElem.equals(layoutObj)) {
87                         items.remove (iter.nextIndex()-1);
88                         return true;
89                     }
90                 }
91             } else if(lay.getParent() instanceof NamedItem) {
92                 // FIXME: Causes that only the contents inside a tab are deleted instead of the tab
93
NamedItem par = (NamedItem) lay.getParent();
94                 par.setLayout(null);
95             } else {
96                 lf.remove(lay);
97             }
98             
99         } catch (ProcessingException e) {
100             e.printStackTrace();
101         }
102         
103         return true;
104     }
105     
106     
107     /**
108      * Moves the object one position up or down
109      * @param id id of the element
110      * @param moveUp set 'true', to move the element up ('false' to move it down)
111      * @return true if the object could be moved.
112      */

113     public boolean move(String JavaDoc id, boolean moveUp) {
114         
115         // get layout element:
116
Object JavaDoc layoutObj = getLayoutElement (layout, id, "", 1);
117         if (layoutObj == null) return false;
118         
119         // do the job:
120
Layout lay;
121         Item item;
122         if (layoutObj instanceof NamedItem) {
123             lay = ((NamedItem)layoutObj).getLayout();
124             if (lay == null)
125                 item = (NamedItem) layoutObj;
126             else
127                 item = lay.getParent();
128         }
129         else {
130             lay = (Layout) layoutObj;
131             item = lay.getParent();
132         }
133         
134         // find element in the list and move it:
135
List JavaDoc items = item.getParent().getItems();
136         for (ListIterator JavaDoc iter = items.listIterator(); iter.hasNext(); ) {
137             
138             Item itemElem = (Item) iter.next();
139             
140             if(itemElem.equals(item)) {
141                 
142                 int pos = iter.nextIndex()-1;
143                 int newpos = pos;
144                 if (moveUp)
145                     newpos --;
146                 else
147                     newpos ++;
148                 
149                 if (newpos >= items.size()) newpos = 0;
150                 if (newpos < 0) newpos = items.size()-1;
151                 
152                 Object JavaDoc obj = items.remove (pos);
153                 items.add(newpos,obj);
154                 
155                 return true;
156             }
157         }
158         return false;
159     }
160     
161     /**
162      * Adds the object to the layout
163      * @param parent Object to which the new Object should be added
164      * @param type Type of the Object (row, col ...)
165      */

166     public void add(String JavaDoc parent, String JavaDoc type) {
167         
168         Object JavaDoc layoutObj = getLayoutElement (layout, parent, "", 1);
169         if (layoutObj == null) return;
170         
171         Layout lay;
172         if (layoutObj instanceof NamedItem)
173             lay = ((NamedItem)layoutObj).getLayout();
174         
175         else
176             lay = (Layout) layoutObj;
177         
178         try {
179             Layout nObj = lf.newInstance(type);
180             pm.register(nObj);
181             
182             Item e = new Item();
183             nObj.setParent(e);
184             e.setLayout(nObj);
185             
186             if (lay != null)
187                 ((CompositeLayout) lay).addItem(e);
188             else
189             {
190                 NamedItem ni = (NamedItem)layoutObj;
191                 nObj.setParent(ni);
192                 ni.setLayout(nObj);
193             }
194             
195         } catch (ProcessingException e) {
196             e.printStackTrace();
197         }
198     }
199     
200     /**
201      * Adds a new Tab
202      * @param parent Parent Object
203      * @param name Name of the Tab
204      */

205     public void addTab(String JavaDoc parent, String JavaDoc name) {
206         
207         // get layout element:
208
Object JavaDoc layoutObj = getLayoutElement (layout, parent, "", 1);
209         if (layoutObj == null) return;
210         
211         Layout lay;
212         
213         if (layoutObj instanceof NamedItem)
214             lay = ((NamedItem)layoutObj).getLayout();
215         else
216             lay = (Layout) layoutObj;
217         
218         // add tab:
219
if(lay != null && lay.getName().equals("tab")) {
220             
221             NamedItem tab = new NamedItem();
222             tab.setName(name);
223             ((CompositeLayout) lay).addItem(tab);
224             
225         } else {
226             
227             try {
228                 
229                 Layout tab = lf.newInstance("tab");
230                 pm.register(tab);
231                 
232                 NamedItem e = new NamedItem();
233                 e.setName(name);
234                 
235                 ((CompositeLayout) tab).addItem(e);
236                 
237                 if (lay == null) {
238                     
239                     ((NamedItem)layoutObj).setLayout(tab);
240                 }
241                 else {
242                     Item m = new Item();
243                     m.setParent((CompositeLayout) lay);
244                     ((CompositeLayout) lay).addItem(m);
245                     m.setLayout(tab);
246                 }
247                 
248             } catch (ProcessingException e) {
249                 e.printStackTrace();
250             }
251         }
252     }
253     
254     public Collection JavaDoc getSelectedCoplets(Repeater r, Collection JavaDoc lets, String JavaDoc parent) {
255         
256         // get layout element:
257
Object JavaDoc obj = getLayoutElement (layout, parent, "", 1);
258         if (obj == null) return null;
259         
260         ArrayList JavaDoc coplets = new ArrayList JavaDoc();
261         ArrayList JavaDoc copletDatas = new ArrayList JavaDoc();
262         
263         int size = r.getSize();
264         for(int i = 0; i < size; i++) {
265             RepeaterRow row = r.getRow(i);
266             Widget widget = row.getChild("selected");
267             Boolean JavaDoc val = (Boolean JavaDoc) widget.getValue();
268             if(val.booleanValue()) {
269                 coplets.add(row.getChild("coplet").getValue());
270             }
271         }
272         for(Iterator JavaDoc it = lets.iterator(); it.hasNext();) {
273             CopletData cd = (CopletData) it.next();
274             String JavaDoc cdid = cd.getId();
275             for(Iterator JavaDoc it2 = coplets.iterator(); it2.hasNext();) {
276                 String JavaDoc cdidTmp = (String JavaDoc) it2.next();
277                 if(cdidTmp.equals(cdid))
278                     copletDatas.add(cd);
279             }
280         }
281         
282         for(Iterator JavaDoc it = copletDatas.iterator(); it.hasNext();) {
283             CopletData cd = (CopletData) it.next();
284             
285             try {
286                 CopletInstanceData cinst = cf.newInstance(cd);
287                 CopletLayout lay = (CopletLayout) lf.newInstance("coplet");
288                 lay.setCopletInstanceData(cinst);
289                 
290                if(obj instanceof Item) {
291                    Item item = (Item) obj;
292                    item.setLayout(lay);
293                    lay.setParent(item);
294                } else if(obj instanceof CompositeLayout) {
295                    CompositeLayout cl = (CompositeLayout) obj;
296                    Item item = new Item();
297                    item.setLayout(lay);
298                    lay.setParent(item);
299                    cl.addItem(item);
300                }
301                
302             } catch (ProcessingException e) {
303                 // ignore it
304
}
305         }
306         return copletDatas;
307     }
308     
309     public CopletInstanceData getCopletInstanceData(String JavaDoc id) {
310         Object JavaDoc obj = getLayoutElement(layout, id, "", 1);
311         if(obj instanceof CopletLayout) {
312             return ((CopletLayout) obj).getCopletInstanceData();
313         }
314         return null;
315     }
316     
317     /**
318      * interal method; search for a Layout or an Item Object
319      */

320     private Object JavaDoc getLayoutElement (Layout layout, String JavaDoc id, String JavaDoc prefix, int pos) {
321         
322         if (layout != null) {
323             
324             if (id.equals((prefix+pos)))
325                 return layout;
326             
327             if (layout instanceof CompositeLayout) {
328                 Iterator JavaDoc i = ((CompositeLayout) layout).getItems().iterator();
329                 
330                 int currentpos = pos;
331                 pos = 1;
332                 while (i.hasNext()) {
333                     
334                     Item current = (Item) i.next();
335                     
336                     if (id.equals((prefix+currentpos+"."+pos)))
337                         return current;
338                     
339                     Object JavaDoc lay = getLayoutElement(current.getLayout(), id, prefix+currentpos+"."+pos+".",1);
340                     if(lay != null)
341                         return lay;
342                     
343                     pos ++;
344                 }
345             }
346         }
347         return null;
348     }
349 }
350
Popular Tags