001: package org.enhydra.jawe.components.graph;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import org.jgraph.graph.ParentMap;
010:
011: /**
012: * Describes relations between childs and parents.
013: *
014: */
015: public class JaWEParentMap extends ParentMap {
016:
017: /**
018: * Returns the list of parents that will be empty when
019: * this parent map is applied.
020: */
021: public ArrayList emptyParentList() {
022: ArrayList list = new ArrayList();
023: Iterator it = childCount.entrySet().iterator();
024: while (it.hasNext()) {
025: Map.Entry entry = (Map.Entry) it.next();
026: if (entry.getValue() instanceof Integer) {
027: if (((Integer) entry.getValue()).intValue() == 0)
028: list.add(entry.getKey());
029: }
030: }
031: return list;
032: }
033:
034: /**
035: * Returns true if Participant that is in changed nodes set has other Participants.
036: * NOTE: because this method is not used when Participants are added to each other,
037: * method doesn't check for added participants but only for removed.
038: */
039: public boolean hasAnyParticipant(Object p) {
040: if ((p == null) || !(p instanceof DefaultGraphParticipant)) {
041: return false;
042: }
043: // getting current state in model
044: Set childParticipants = new HashSet(
045: ((DefaultGraphParticipant) p).getChildParticipants());
046: // removing from it all changedNodes - this means (considering a NOTE)
047: // that these participants will be deleted after parent map is applied
048: childParticipants.removeAll(changedNodes);
049:
050: return (childParticipants.size() > 0);
051:
052: }
053:
054: /**
055: * Returns all children which will belong to given parent
056: * when this parent map is applied.
057: */
058: public ArrayList getNewChildren(Object parent) {
059: ArrayList list = new ArrayList();
060: if (parent != null) {
061: Iterator it = entries.iterator();
062: while (it.hasNext()) {
063: Entry entry = (Entry) it.next();
064: Object parentFromEntry = entry.getParent();
065: if (parentFromEntry != null
066: && parentFromEntry.equals(parent)) {
067: list.add(entry.getChild());
068: }
069: }
070: }
071: return list;
072: }
073:
074: /**
075: * Returns parent of given child. The child will belong to returned
076: * parent when this parent map is applied.
077: */
078: public Object getNewParent(Object child) {
079: Object parent = null;
080: if (child != null) {
081: Iterator it = entries.iterator();
082: while (it.hasNext()) {
083: Entry entry = (Entry) it.next();
084: Object childFromEntry = entry.getChild();
085: if (childFromEntry != null
086: && childFromEntry.equals(child)) {
087: parent = entry.getParent();
088: break;
089: }
090: }
091: }
092: return parent;
093: }
094:
095: /**
096: * Returns nodes that will be removed after this map is applied (These
097: * are entries which parent part is null).
098: */
099: public ArrayList getRemovedNodes() {
100: ArrayList list = new ArrayList();
101: Iterator it = entries.iterator();
102: while (it.hasNext()) {
103: Entry entry = (Entry) it.next();
104: Object parentFromEntry = entry.getParent();
105: if (parentFromEntry == null) {
106: list.add(entry.getChild());
107: }
108: }
109: return list;
110: }
111:
112: /**
113: * Returns the number of entries in parent map.
114: */
115: public int entryCount() {
116: return entries.size();
117: }
118:
119: }
120:
121: /* End of PEParentMap.java */
|