KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > presentation > delements > DCollection


1 package discRack.presentation.delements;
2
3 import discRack.presentation.dpanels.*;
4
5 import java.util.*;
6
7 /**
8  * Represents collection of elements which must be instances of
9  * DCollectionElement class.
10  *
11  * @author Sasa Bojanic
12  * @version 1.0
13  */

14 public class DCollection extends DSimpleElement {
15
16    /** The collection of elements. */
17    protected ArrayList refCollectionElements=new ArrayList();
18
19    protected transient DPanel controlledPanel;
20
21    protected transient DControlPanel controlPanel;
22
23    public DCollection (String JavaDoc name) {
24       super(name);
25    }
26
27    /** Adds new element to collection. */
28    public void add (DSimpleElement el) {
29       refCollectionElements.add(el);
30    }
31
32    /** Removes specified element from collection. */
33    public void remove (Object JavaDoc el) {
34       refCollectionElements.remove(el);
35    }
36
37    /** Gets the element that is placed at specified no. from collection. */
38    public Object JavaDoc get (int no) {
39       try {
40          return refCollectionElements.get(no);
41       } catch (Exception JavaDoc ex) {
42          return null;
43       }
44    }
45
46    /** Returns the number of elements within collection. */
47    public int size () {
48       return refCollectionElements.size();
49    }
50
51    /** Clears the collection. */
52    public void clear () {
53       refCollectionElements.clear();
54    }
55
56    /**
57    * Refreshes the collection.
58    *
59    * @param elementsToAddOrRemove Set of elements that has to be added to or
60    * removed from collection.
61    * @param append <tt>true</tt> if adding elements to collection,
62    * <tt>false</tt> otherwise.
63    */

64    public void refreshCollection (Set elementsToAddOrRemove,boolean append) {
65       if (append) {
66          refCollectionElements.addAll(elementsToAddOrRemove);
67       } else {
68          refCollectionElements.removeAll(elementsToAddOrRemove);
69       }
70    }
71
72    /**
73    * Returns the element specified by ID.
74    *
75    * @param ID ID of wanted element.
76    * @return Wanted element if exist, null otherwise.
77    */

78    public DCollectionElement getCollectionElement (String JavaDoc ID) {
79       DCollectionElement ce=null;
80       String JavaDoc ceID;
81       Iterator it=refCollectionElements.iterator();
82       while (it.hasNext()) {
83          DCollectionElement cetmp=(DCollectionElement)it.next();
84          ceID=cetmp.getID();
85          if (ceID.equals(ID)) {
86             ce=cetmp;
87             break;
88          }
89       }
90       return ce;
91    }
92
93    /**
94    * Gets the structure of elements contained within collection,
95    * that is the all elements that element is made of.
96    */

97    public Collection getElementStructure () {
98       DSimpleElement el=generateNewElement();
99       if (el instanceof DComplexElement) {
100          return ((DComplexElement)el).toComplexType();
101       }
102       else {
103          java.util.List JavaDoc l=new ArrayList();
104          l.add(el);
105          return l;
106       }
107    }
108
109    /**
110    * Sets the collection and all contained elements to be read only or not.
111    */

112    public void setReadOnly (boolean ro) {
113       isReadOnly=ro;
114       Iterator it=refCollectionElements.iterator();
115       while (it.hasNext()) {
116          DSimpleElement el=(DSimpleElement)it.next();
117          el.setReadOnly(ro);
118       }
119    }
120
121    /** Returns the collection of all elements within collection. */
122    public Collection toCollection() {
123       return refCollectionElements;
124    }
125
126    /**
127    * Gets elements that can be choosed within table. Default
128    * implementation returns all elements within collection.
129    */

130    public Collection getTableElements() {
131       return refCollectionElements;
132    }
133
134    /**
135    * Generates the new element that made collection. Derived classes
136    * has to implement this method to create it's collection element.
137    */

138    public DSimpleElement generateNewElement() {
139       return new DSimpleElement(name);
140    }
141
142    /**
143    * Some specific things to be done after some action is canceled.
144    * Default implementation is nothing to do, and derived classes
145    * should implement it's specific actions.
146    */

147    public void onActionCanceled (DSimpleElement el,int act) {
148       return;
149    }
150
151    /**
152    * Some specific things to be done after element is created.
153    * Default implementation is nothing to do, and derived classes
154    * should implement it's specific actions.
155    */

156    public void onElementCreated (DSimpleElement el) {
157       return;
158    }
159
160    /**
161    * Some specific things to be done after element from collection
162    * is modified. Default implementation is nothing to do, and derived
163    * classes should implement it's specific actions.
164    */

165    public void onElementModified (DSimpleElement el) {
166       return;
167    }
168
169    /**
170    * Some specific things to be done after element is deleted from
171    * collection. Default implementation is nothing to do, and derived
172    * classes should implement it's specific actions.
173    */

174    public void onElementDeleted (DSimpleElement el) throws Exception JavaDoc {
175       return;
176    }
177
178    public DPanel getControlledPanel () {
179       return controlledPanel;
180    }
181
182    public DPanel getControlPanel () {
183       return controlPanel;
184    }
185
186    /**
187    * Returns <tt>true</tt> if there is no elements within collection.
188    */

189    public boolean isEmpty () {
190       return size()==0;
191    }
192
193    // First, the controlled panel must be created, and then the control panel
194
public DPanel getPanel () {
195       controlledPanel=new DTablePanel(this,"",false);
196       controlPanel=new DTableControlPanel(this,"",true,false);
197       return new DGroupPanel(this,new DPanel[]{
198          controlledPanel,controlPanel},toName(),
199          false,true);
200    }
201
202    public String JavaDoc toString () {
203       return name;
204    }
205
206 }
207
Popular Tags