001: package jtaDiscRack.presentation.delements;
002:
003: import jtaDiscRack.presentation.dpanels.*;
004:
005: import java.util.*;
006:
007: /**
008: * Represents collection of elements which must be instances of
009: * DCollectionElement class.
010: *
011: * @author Sasa Bojanic
012: * @version 1.0
013: */
014: public class DCollection extends DSimpleElement {
015:
016: /** The collection of elements. */
017: protected ArrayList refCollectionElements = new ArrayList();
018:
019: protected transient DPanel controlledPanel;
020:
021: protected transient DControlPanel controlPanel;
022:
023: public DCollection(String name) {
024: super (name);
025: }
026:
027: /** Adds new element to collection. */
028: public void add(DSimpleElement el) {
029: refCollectionElements.add(el);
030: }
031:
032: /** Removes specified element from collection. */
033: public void remove(Object el) {
034: refCollectionElements.remove(el);
035: }
036:
037: /** Gets the element that is placed at specified no. from collection. */
038: public Object get(int no) {
039: try {
040: return refCollectionElements.get(no);
041: } catch (Exception ex) {
042: return null;
043: }
044: }
045:
046: /** Returns the number of elements within collection. */
047: public int size() {
048: return refCollectionElements.size();
049: }
050:
051: /** Clears the collection. */
052: public void clear() {
053: refCollectionElements.clear();
054: }
055:
056: /**
057: * Returns the element specified by ID.
058: *
059: * @param ID ID of wanted element.
060: * @return Wanted element if exist, null otherwise.
061: */
062: public DCollectionElement getCollectionElement(String ID) {
063: DCollectionElement ce = null;
064: String ceID;
065: Iterator it = refCollectionElements.iterator();
066: while (it.hasNext()) {
067: DCollectionElement cetmp = (DCollectionElement) it.next();
068: ceID = cetmp.getID();
069: if (ceID.equals(ID)) {
070: ce = cetmp;
071: break;
072: }
073: }
074: return ce;
075: }
076:
077: /**
078: * Gets the structure of elements contained within collection,
079: * that is the all elements that element is made of.
080: */
081: public Collection getElementStructure() {
082: DSimpleElement el = generateNewElement();
083: if (el instanceof DComplexElement) {
084: return ((DComplexElement) el).toComplexType();
085: } else {
086: java.util.List l = new ArrayList();
087: l.add(el);
088: return l;
089: }
090: }
091:
092: /**
093: * Sets the collection and all contained elements to be read only or not.
094: */
095: public void setReadOnly(boolean ro) {
096: isReadOnly = ro;
097: Iterator it = refCollectionElements.iterator();
098: while (it.hasNext()) {
099: DSimpleElement el = (DSimpleElement) it.next();
100: el.setReadOnly(ro);
101: }
102: }
103:
104: /** Returns the collection of all elements within collection. */
105: public Collection toCollection() {
106: return refCollectionElements;
107: }
108:
109: /**
110: * Gets elements that can be choosed within table. Default
111: * implementation returns all elements within collection.
112: */
113: public Collection getTableElements() {
114: return refCollectionElements;
115: }
116:
117: /**
118: * Generates the new element that made collection. Derived classes
119: * has to implement this method to create it's collection element.
120: */
121: public DSimpleElement generateNewElement() {
122: return new DSimpleElement(name);
123: }
124:
125: /**
126: * Some specific things to be done after some action is canceled.
127: * Default implementation is nothing to do, and derived classes
128: * should implement it's specific actions.
129: */
130: public void onActionCanceled(DSimpleElement el, int act) {
131: return;
132: }
133:
134: /**
135: * Some specific things to be done after element is created.
136: * Default implementation is nothing to do, and derived classes
137: * should implement it's specific actions.
138: */
139: public void onElementCreated(DSimpleElement el) {
140: return;
141: }
142:
143: /**
144: * Some specific things to be done after element from collection
145: * is modified. Default implementation is nothing to do, and derived
146: * classes should implement it's specific actions.
147: */
148: public void onElementModified(DSimpleElement el) {
149: return;
150: }
151:
152: /**
153: * Some specific things to be done after element is deleted from
154: * collection. Default implementation is nothing to do, and derived
155: * classes should implement it's specific actions.
156: */
157: public void onElementDeleted(DSimpleElement el) throws Exception {
158: return;
159: }
160:
161: public DPanel getControlledPanel() {
162: return controlledPanel;
163: }
164:
165: public DPanel getControlPanel() {
166: return controlPanel;
167: }
168:
169: /**
170: * Returns <tt>true</tt> if there is no elements within collection.
171: */
172: public boolean isEmpty() {
173: return size() == 0;
174: }
175:
176: // First, the controlled panel must be created, and then the control panel
177: public DPanel getPanel() {
178: controlledPanel = new DTablePanel(this , "", false);
179: controlPanel = new DTableControlPanel(this , "", true, false);
180: return new DGroupPanel(this , new DPanel[] { controlledPanel,
181: controlPanel }, toName(), false, true);
182: }
183:
184: public String toString() {
185: return name;
186: }
187:
188: }
|