1 16 package org.apache.cocoon.portal.tools.copletManagement; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.ListIterator ; 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 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 public static int line = 1; 59 60 65 public boolean del(String id) { 66 67 Object layoutObj = getLayoutElement (layout, id, "", 1); 69 if (layoutObj == null) return false; 70 71 Layout lay; 73 if (layoutObj instanceof NamedItem) 74 lay = ((NamedItem)layoutObj).getLayout(); 75 else 76 lay = (Layout) layoutObj; 77 78 try { 79 if (lay == null) { 81 List items = ((NamedItem)layoutObj).getParent().getItems(); 82 for (ListIterator 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 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 113 public boolean move(String id, boolean moveUp) { 114 115 Object layoutObj = getLayoutElement (layout, id, "", 1); 117 if (layoutObj == null) return false; 118 119 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 List items = item.getParent().getItems(); 136 for (ListIterator 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 obj = items.remove (pos); 153 items.add(newpos,obj); 154 155 return true; 156 } 157 } 158 return false; 159 } 160 161 166 public void add(String parent, String type) { 167 168 Object 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 205 public void addTab(String parent, String name) { 206 207 Object 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 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 getSelectedCoplets(Repeater r, Collection lets, String parent) { 255 256 Object obj = getLayoutElement (layout, parent, "", 1); 258 if (obj == null) return null; 259 260 ArrayList coplets = new ArrayList (); 261 ArrayList copletDatas = new ArrayList (); 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 val = (Boolean ) widget.getValue(); 268 if(val.booleanValue()) { 269 coplets.add(row.getChild("coplet").getValue()); 270 } 271 } 272 for(Iterator it = lets.iterator(); it.hasNext();) { 273 CopletData cd = (CopletData) it.next(); 274 String cdid = cd.getId(); 275 for(Iterator it2 = coplets.iterator(); it2.hasNext();) { 276 String cdidTmp = (String ) it2.next(); 277 if(cdidTmp.equals(cdid)) 278 copletDatas.add(cd); 279 } 280 } 281 282 for(Iterator 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 } 305 } 306 return copletDatas; 307 } 308 309 public CopletInstanceData getCopletInstanceData(String id) { 310 Object obj = getLayoutElement(layout, id, "", 1); 311 if(obj instanceof CopletLayout) { 312 return ((CopletLayout) obj).getCopletInstanceData(); 313 } 314 return null; 315 } 316 317 320 private Object getLayoutElement (Layout layout, String id, String 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 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 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 |