KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > model > DesignAdult


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.model;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Observable JavaDoc;
9 import java.util.Observer JavaDoc;
10
11 import org.oddjob.designer.arooa.ElementWrapper;
12 import org.oddjob.designer.factory.DesignFactory;
13
14 /**
15  * A DesignAdult may or may not be capable of having children.
16  * If a DesignAdult does have children they must be design
17  * elements.
18  *
19  */

20 abstract public class DesignAdult extends Observable JavaDoc {
21
22     private final List JavaDoc children = new ArrayList JavaDoc();
23     
24     protected DesignAdult parent;
25     
26     private String JavaDoc[] supportedTypes;
27     
28     private Observer JavaDoc observer = new Observer JavaDoc() {
29         public void update(Observable JavaDoc o, Object JavaDoc 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 JavaDoc[] supportedTypes) {
99         this.supportedTypes = supportedTypes;
100     }
101     
102     public String JavaDoc[] supportedTypes() {
103         if (supportedTypes == null) {
104             return DesignFactory.supportedTypes(getClass());
105         }
106         return this.supportedTypes;
107     }
108     
109     public DesignElementType createType(String JavaDoc type) {
110         return DesignFactory.createType(type);
111     }
112 }
113
Popular Tags