1 4 package org.oddjob.designer.model; 5 6 import java.util.ArrayList ; 7 import java.util.List ; 8 import java.util.Observable ; 9 import java.util.Observer ; 10 11 import org.oddjob.designer.arooa.ElementWrapper; 12 import org.oddjob.designer.factory.DesignFactory; 13 14 20 abstract public class DesignAdult extends Observable { 21 22 private final List children = new ArrayList (); 23 24 protected DesignAdult parent; 25 26 private String [] supportedTypes; 27 28 private Observer observer = new Observer () { 29 public void update(Observable o, Object arg) { 30 DEObservable deo = (DEObservable) arg; 31 setChanged(); 32 notifyObservers(DEObservable.childChanged(deo)); 33 }; 34 }; 35 36 public DesignAdult() { 37 38 } 39 40 public void addChild(DesignElementType de) { 41 de.parent = this; 42 children.add(de); 43 setChanged(); 44 notifyObservers(DEObservable.childAdded(de, children.size())); 45 de.addObserver(observer); 46 } 47 48 public void insertChild(int index, DesignElementType de) { 49 de.parent = this; 50 children.add(index, de); 51 setChanged(); 52 notifyObservers(DEObservable.childAdded(de, index)); 53 de.addObserver(observer); 54 } 55 56 public void removeChild(int index) { 57 DesignElementType de = (DesignElementType) children.remove(index); 58 de.parent = null; 59 de.deleteObserver(observer); 60 setChanged(); 61 notifyObservers(DEObservable.childRemoved(de, index)); 62 } 63 64 65 public void clearChildren() { 66 while (children.size() > 0) { 67 removeChild(0); 68 } 69 } 70 71 public DesignAdult parent() { 72 return parent; 73 } 74 75 protected ElementWrapper[] childElements() { 76 ElementWrapper[] results = new ElementWrapper[children.size()]; 77 for (int i = 0; i < children.size(); ++i) { 78 ElementWrapper wrapper = new ElementWrapper( 79 ((DesignElementType) children.get(i)).type(), 80 children.get(i)); 81 results[i] = wrapper; 82 } 83 return results; 84 } 85 86 public int childCount() { 87 return children.size(); 88 } 89 90 public DesignElementType[] children() { 91 return (DesignElementType[]) children.toArray(new DesignElementType[0]); 92 } 93 94 public DesignElementType children(int index) { 95 return (DesignElementType) children.get(index); 96 } 97 98 public void supportedTypes(String [] supportedTypes) { 99 this.supportedTypes = supportedTypes; 100 } 101 102 public String [] supportedTypes() { 103 if (supportedTypes == null) { 104 return DesignFactory.supportedTypes(getClass()); 105 } 106 return this.supportedTypes; 107 } 108 109 public DesignElementType createType(String type) { 110 return DesignFactory.createType(type); 111 } 112 } 113 | Popular Tags |