001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.generic.data.GenericCompositeGroup
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.generic.data;
020:
021: import de.gulden.framework.amoda.generic.behaviour.*;
022: import de.gulden.framework.amoda.generic.core.GenericApplicationMemberAbstract;
023: import de.gulden.framework.amoda.model.data.*;
024: import de.gulden.framework.amoda.model.data.CompositeGroup;
025: import java.lang.*;
026: import java.util.*;
027:
028: /**
029: * Class GenericCompositeGroup.
030: *
031: * @author Jens Gulden
032: * @version snapshot-beautyj-1.1
033: */
034: public class GenericCompositeGroup extends
035: GenericApplicationMemberAbstract implements CompositeGroup {
036:
037: // ------------------------------------------------------------------------
038: // --- field ---
039: // ------------------------------------------------------------------------
040:
041: public Map all = new de.gulden.util.OrderedHashMap();
042:
043: // ------------------------------------------------------------------------
044: // --- methods ---
045: // ------------------------------------------------------------------------
046:
047: public void add(CompositeMember member) {
048: member.setParent(this );
049: all.put(member.getId(), member);
050: }
051:
052: public void clear() {
053: for (Iterator it = all.values().iterator(); it.hasNext();) {
054: CompositeMember m = (CompositeMember) it.next();
055: m.setParent(null);
056: }
057: all.clear();
058: }
059:
060: public CompositeMember get(String id) {
061: CompositeMember result = get(id, true);
062: if (result == null) {
063: if (id.indexOf('.') != -1) {
064: result = getAbsolute(id);
065: }
066: }
067: return result;
068: }
069:
070: public CompositeMember get(String id, boolean deep) {
071: CompositeMember m = (CompositeMember) getAll().get(id); // NO Direct access on all to allow overwriting getAll()
072: if ((m == null) && (deep)) {
073: for (Iterator it = getGroups().values().iterator(); it
074: .hasNext();) {
075: CompositeGroup g = (CompositeGroup) it.next();
076: m = g.get(id, true);
077: if (m != null) {
078: return m;
079: }
080: }
081: }
082: return m;
083: }
084:
085: public Map getAll(Class type, boolean deep) {
086: // type may be null
087: de.gulden.util.OrderedHashMap all = (de.gulden.util.OrderedHashMap) getAll(); // don't use this.all, as getAll might be re-implemented
088: Map map = (Map) all.clone();
089: if (type != null) {
090: for (Iterator it = all.values().iterator(); it.hasNext();) {
091: Object o = it.next();
092: if (!type.isAssignableFrom(o.getClass())) {
093: map.remove(((CompositeMember) o).getId());
094: }
095: }
096: }
097: if (deep) {
098: Map groups = getGroups();
099: for (Iterator it = groups.values().iterator(); it.hasNext();) {
100: CompositeGroup g = (CompositeGroup) it.next();
101: map.putAll(g.getAll(type, true));
102: }
103: }
104: return map;
105: }
106:
107: public Map getEntries() {
108: Map map = (Map) ((de.gulden.util.OrderedHashMap) all).clone();
109: for (Iterator it = all.values().iterator(); it.hasNext();) {
110: Object o = it.next();
111: if (o instanceof CompositeGroup) {
112: map.remove(((CompositeMember) o).getId());
113: }
114: }
115: return map;
116: }
117:
118: public Map getGroups() {
119: Map map = (Map) ((de.gulden.util.OrderedHashMap) all).clone();
120: for (Iterator it = all.values().iterator(); it.hasNext();) {
121: Object o = it.next();
122: if (!(o instanceof CompositeGroup)) {
123: map.remove(((CompositeMember) o).getId());
124: }
125: }
126: return map;
127: }
128:
129: public Map getAll() {
130: return all;
131: }
132:
133: public void setAll(Map _all) {
134: clear();
135: addAll(_all.values());
136: }
137:
138: public void addAll(Collection members) {
139: for (Iterator it = members.iterator(); it.hasNext();) {
140: CompositeMember member = (CompositeMember) it.next();
141: this .add(member);
142: }
143: }
144:
145: public int countMaxDepth() {
146: de.gulden.framework.amoda.generic.behaviour.GenericCondition cond = new de.gulden.framework.amoda.generic.behaviour.GenericCondition() {
147: public boolean test() {
148: return true;
149: }
150: };
151: return countMaxDepth(cond);
152: }
153:
154: public int countMaxDepth(GenericCondition cond) {
155: int depth = 0;
156: Map subGroups = getGroups();
157: for (Iterator it = subGroups.values().iterator(); it.hasNext();) {
158: GenericCompositeGroup g = (GenericCompositeGroup) it.next();
159: if (cond.test(g)) {
160: int gDepth = g.countMaxDepth(cond);
161: if ((gDepth > 0) && ((gDepth + 1) > depth)) {
162: depth = gDepth + 1;
163: }
164: }
165: }
166: if (depth == 0) { // no groups, or only empty ones
167: Map m = getEntries();
168: for (Iterator it = m.values().iterator(); (depth == 0)
169: && it.hasNext();) {
170: if (cond.test(it.next())) {
171: depth = 1; // set to 1 constantly to indicate that this group has art leaat one element
172: }
173: }
174: }
175: return depth;
176: }
177:
178: protected CompositeMember getAbsolute(String id) {
179: StringTokenizer st = new StringTokenizer(id, ".", false);
180: String tok = st.nextToken();
181: CompositeGroup g = this ;
182: while (st.hasMoreTokens()) { // not last entry yet: group name
183: Object member = g.get(tok, false);
184: if (member instanceof CompositeGroup) {
185: g = (CompositeGroup) member; // drill into child group
186: } else { // also covers case member==null
187: return null;
188: }
189: tok = st.nextToken();
190: }
191: return g.get(tok);
192: }
193:
194: } // end GenericCompositeGroup
|