1 14 package org.wings; 15 16 import org.wings.event.SContainerEvent; 17 import org.wings.event.SContainerListener; 18 import org.wings.plaf.ContainerCG; 19 import org.wings.style.CSSProperty; 20 import org.wings.style.CSSSelector; 21 import org.wings.util.ComponentVisitor; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 26 27 35 public class SContainer extends SComponent { 36 39 protected SLayoutManager layout; 40 41 44 private SIcon backgroundImage; 45 46 49 private ArrayList componentList; 50 51 54 private ArrayList constraintList; 55 56 60 public static final CSSSelector.Pseudo SELECTOR_CONTENT = new CSSSelector.Pseudo("area of container content"); 61 62 63 68 public SContainer(SLayoutManager l) { 69 setLayout(l); 70 } 71 72 76 public SContainer() {} 77 78 79 public void updateCG() { 80 super.updateCG(); 81 82 if (layout != null) 83 layout.updateCG(); 84 } 85 86 91 public void setLayout(SLayoutManager l) { 92 if (layout != null) { 93 for (int i = 0; i < getComponentCount(); i++) { 94 layout.removeComponent(getComponent(i)); 95 } 96 layout.setContainer(null); 97 } 98 99 layout = l; 100 101 if (layout != null) { 102 for (int i = 0; i < getComponentCount(); i++) { 103 layout.addComponent(getComponent(i), getConstraintAt(i), i); 104 } 105 106 layout.setContainer(this); 107 } 108 } 109 110 115 public void setBackgroundImage(SIcon icon) { 116 backgroundImage = icon; 117 setAttribute(SELECTOR_CONTENT, CSSProperty.BACKGROUND_IMAGE, icon); 118 } 119 120 125 public SIcon getBackgroundImage() { 126 return backgroundImage; 127 } 128 129 134 public SLayoutManager getLayout() { 135 return layout; 136 } 137 138 145 public void addContainerListener(SContainerListener l) { 146 addEventListener(SContainerListener.class, l); 147 } 148 149 156 public void removeContainerListener(SContainerListener l) { 157 removeEventListener(SContainerListener.class, l); 158 } 159 160 protected void fireContainerEvent(int type, SComponent comp) { 161 SContainerEvent event = null; 162 163 Object [] listeners = getListenerList(); 164 for (int i = listeners.length - 2; i >= 0; i -= 2) { 165 if (listeners[i] == SContainerListener.class) { 166 if (event == null) 168 event = new SContainerEvent(this, type, comp); 169 170 processContainerEvent((SContainerListener) listeners[i + 1], 171 event); 172 } 173 } 174 } 175 176 187 protected void processContainerEvent(SContainerListener listener, 188 SContainerEvent e) { 189 switch (e.getID()) { 190 case SContainerEvent.COMPONENT_ADDED: 191 listener.componentAdded(e); 192 break; 193 194 case SContainerEvent.COMPONENT_REMOVED: 195 listener.componentRemoved(e); 196 break; 197 } 198 } 199 200 protected ArrayList getComponentList() { 201 if (componentList == null) { 202 componentList = new ArrayList (3); 203 } 204 return componentList; 205 } 206 207 protected ArrayList getConstraintList() { 208 if (constraintList == null) 209 constraintList = new ArrayList (3); 210 return constraintList; 211 } 212 213 214 219 public int getComponentCount() { 220 return getComponentList().size(); 221 } 222 223 224 225 231 public SComponent getComponent(int i) { 232 return (SComponent) getComponentList().get(i); 233 } 234 235 public SComponent[] getComponents() { 236 return (SComponent[]) getComponentList().toArray(new SComponent[getComponentCount()]); 238 } 239 240 241 247 public Object getConstraintAt(int i) { 248 return getConstraintList().get(i); 249 } 250 251 252 253 259 public void remove(SComponent c) { 260 if (c == null) return; 261 262 if (layout != null) 263 layout.removeComponent(c); 264 265 int index = getComponentList().indexOf(c); 266 if (getComponentList().remove(c)) { 267 getConstraintList().remove(index); 268 269 fireContainerEvent(SContainerEvent.COMPONENT_REMOVED, c); 270 271 c.setParent(null); 272 reload(); 273 } 274 } 275 276 282 public void remove(int index) { 283 SComponent c = getComponent(index); 284 remove(c); 285 } 286 287 290 public void removeAll() { 291 while (getComponentCount() > 0) { 292 remove(0); 293 } 294 } 295 296 297 304 public SComponent add(SComponent c) { 305 return addComponent(c, null); 306 } 307 308 315 public void add(SComponent c, Object constraint) { 316 addComponent(c, constraint); 317 } 318 319 327 public SComponent add(SComponent c, int index) { 328 return addComponent(c, null, index); 329 } 330 331 338 public void add(SComponent c, Object constraint, int index) { 339 addComponent(c, constraint, index); 340 } 341 342 349 public SComponent addComponent(SComponent c) { 350 return addComponent(c, null); 351 } 352 353 361 public SComponent addComponent(SComponent c, Object constraint) { 362 return addComponent(c, constraint, getComponentList().size()); 363 } 364 365 373 public SComponent addComponent(SComponent c, int index) { 374 return addComponent(c, null, index); 375 } 376 377 385 public SComponent addComponent(SComponent c, Object constraint, int index) { 386 if (c != null) { 387 getComponentList().add(index, c); 388 getConstraintList().add(index, constraint); 389 c.setParent(this); 390 391 if (layout != null) { 392 layout.addComponent(c, constraint, index); 393 } 394 fireContainerEvent(SContainerEvent.COMPONENT_ADDED, c); 395 reload(); 396 } 397 398 return c; 399 } 400 401 406 protected void setParentFrame(SFrame f) { 407 if (f != parentFrame) { 408 super.setParentFrame(f); 409 for (int i = 0; i < getComponentCount(); i++) { 410 getComponent(i).setParentFrame(getParentFrame()); 411 } 412 } 413 } 414 415 419 public Object clone() { 420 try { 421 SContainer erg = (SContainer) super.clone(); 422 424 erg.getComponentList().clear(); 427 erg.getConstraintList().clear(); 428 for (int i = 0; i < getComponentCount(); i++) { 429 erg.addComponent((SComponent) getComponent(i).clone()); 430 } 431 return erg; 432 } catch (Exception e) { 433 e.printStackTrace(); 434 return null; 435 } 436 } 437 438 public void setCG(ContainerCG cg) { 439 super.setCG(cg); 440 } 441 442 448 public void invite(ComponentVisitor visitor) 449 throws Exception { 450 visitor.visit(this); 451 } 452 453 460 public void inviteEachComponent(ComponentVisitor visitor) 461 throws Exception { 462 Iterator iterator = getComponentList().iterator(); 463 while (iterator.hasNext()) { 464 ((SComponent) iterator.next()).invite(visitor); 465 } 466 } 467 468 public void removeNotify() { 469 Iterator iterator = getComponentList().iterator(); 470 while (iterator.hasNext()) { 471 ((SComponent)iterator.next()).removeNotify(); 472 } 473 super.removeNotify(); 474 } 475 476 public ArrayList getMenus() { 477 ArrayList menus = new ArrayList (); 478 if (isVisible()) { 479 Iterator iter = getComponentList().iterator(); 480 while (iter.hasNext()) { 481 SComponent comp = (SComponent)iter.next(); 482 ArrayList compMenus = comp.getMenus(); 483 compMenus.removeAll(menus); 484 menus.addAll(compMenus); 485 } 486 SPopupMenu pmenu = getComponentPopupMenu(); 487 if (pmenu != null && !menus.contains(pmenu)) { 488 menus.add(pmenu); 489 } 490 } 491 return menus; 492 } 493 } 494 | Popular Tags |