001: package org.enhydra.jawe.components;
002:
003: import java.util.Enumeration;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Set;
007:
008: import javax.swing.tree.DefaultTreeModel;
009: import javax.swing.tree.TreeNode;
010:
011: import org.enhydra.jawe.JaWEComponent;
012: import org.enhydra.jawe.Utils;
013: import org.enhydra.shark.xpdl.XMLBaseForCollectionAndComplex;
014: import org.enhydra.shark.xpdl.XMLCollection;
015: import org.enhydra.shark.xpdl.XMLComplexChoice;
016: import org.enhydra.shark.xpdl.XMLComplexElement;
017: import org.enhydra.shark.xpdl.XMLElement;
018: import org.enhydra.shark.xpdl.elements.DataTypes;
019: import org.enhydra.shark.xpdl.elements.Package;
020:
021: /**
022: * TreeView hierarchy tree model.
023: *
024: * @author Zoran Milakovic
025: * @author Sasa Bojanic
026: */
027: public class XPDLTreeModel extends DefaultTreeModel {
028:
029: JaWEComponent comp;
030:
031: public XPDLTreeModel(JaWEComponent comp) {
032: super (new XPDLTreeNode(), true);
033: this .comp = comp;
034: }
035:
036: public XPDLTreeNode getRootNode() {
037: return (XPDLTreeNode) super .getRoot();
038: }
039:
040: public void clearTree() {
041: getRootNode().removeAllChildren();
042: }
043:
044: public XPDLTreeNode insertNode(XMLElement el) {
045: XPDLTreeNode n = null;
046: if (el instanceof Package) {
047: n = findNode(getRootNode(), el);
048: if (n == null) {
049: n = insertNode(getRootNode(), el);
050: }
051: } else {
052: XPDLTreeNode parent = findNode(el.getParent());
053: if (parent != null) {
054: if (parent.getXPDLElement() instanceof XMLCollection) {
055: Set hidden = getHiddenElements((XMLCollection) parent
056: .getXPDLElement());
057: if (hidden.contains(el)) {
058: return null;
059: }
060: }
061: n = findNode(parent, el);
062: if (n == null) {
063: n = insertNode(parent, el);
064: } else {
065: n = null;
066: }
067: } else {
068: // System.err.println("Can't find parent node for element "+el);
069: }
070: }
071: return n;
072: }
073:
074: protected XPDLTreeNode insertNode(XPDLTreeNode parent, XMLElement el) {
075: XPDLTreeNode n = createNode(el);
076: insertNodeInto(n, parent, parent.getChildCount());
077: if (el instanceof XMLBaseForCollectionAndComplex) {
078: XMLBaseForCollectionAndComplex xbcc = (XMLBaseForCollectionAndComplex) el;
079: Set hidden = null;
080: if (xbcc instanceof XMLComplexElement) {
081: hidden = getHiddenElements((XMLComplexElement) xbcc);
082: } else {
083: hidden = getHiddenElements((XMLCollection) xbcc);
084: }
085: if (hidden.size() == 1 && hidden.contains("*")) {
086: return n;
087: }
088: Iterator it = xbcc.toElements().iterator();
089: while (it.hasNext()) {
090: XMLElement sbel = (XMLElement) it.next();
091: if (hidden.contains(sbel)) {
092: continue;
093: }
094: insertNode(n, sbel);
095: }
096: } else if (el instanceof XMLComplexChoice
097: && !(el instanceof DataTypes)) {
098: XMLComplexChoice xcc = (XMLComplexChoice) el;
099: Iterator it = xcc.getChoices().iterator();
100: while (it.hasNext()) {
101: XMLElement sbel = (XMLElement) it.next();
102: insertNode(n, sbel);
103: }
104: }
105: return n;
106: }
107:
108: public XPDLTreeNode removeNode(XMLElement el) {
109: XPDLTreeNode n = findNode(el);
110: if (n != null) {
111: //XPDLTreeNode parent=(XPDLTreeNode)n.getParent();
112: //parent.rem
113: // n.removeFromParent();
114: removeNodeFromParent(n);
115: }
116: return n;
117: }
118:
119: public void repositionNode(XMLElement el, int newpos) {
120: XPDLTreeNode n = findNode(el);
121: if (n != null) {
122: XPDLTreeNode parent = (XPDLTreeNode) n.getParent();
123: // TODO: repositioning should now consider the possible hidden elements
124: if (parent.getChildCount() < newpos + 1) {
125: return;
126: }
127: removeNodeFromParent(n);
128: // n.removeFromParent();
129: insertNodeInto(n, parent, newpos);
130: }
131: }
132:
133: public XPDLTreeNode findNode(XMLElement el) {
134: XPDLTreeNode lRoot = getRootNode();
135: XPDLTreeNode n = findNode(lRoot, el);
136: // if (n==null) {
137: // System.err.println("Can't find node for element "+el);
138: // }
139: return n;
140: }
141:
142: protected XPDLTreeNode findNode(XPDLTreeNode start, XMLElement el) {
143: if (start.getUserObject() == el) {
144: return start;
145: }
146: Enumeration nodes = start.children();
147: while (nodes.hasMoreElements()) {
148: XPDLTreeNode chld = (XPDLTreeNode) nodes.nextElement();
149: XPDLTreeNode fn = findNode(chld, el);
150: if (fn != null) {
151: return fn;
152: }
153: }
154: return null;
155: }
156:
157: protected XPDLTreeNode createNode(XMLElement el) {
158: return new XPDLTreeNode(el);
159: }
160:
161: public boolean isLeaf(Object node) {
162: return ((TreeNode) node).isLeaf();
163: }
164:
165: protected Set getHiddenElements(XMLComplexElement cel) {
166: Set hidden = new HashSet();
167: String hstr = (String) comp.getSettings().getSetting(
168: "HideSubElements." + cel.toName());
169:
170: String[] hstra = Utils.tokenize(hstr, " ");
171: if (hstra != null) {
172: for (int i = 0; i < hstra.length; i++) {
173: XMLElement el = cel.get(hstra[i]);
174: if (el != null) {
175: hidden.add(el);
176: } else if (cel instanceof Package) {
177: Package pkg = (Package) cel;
178: if (hstra[i].equals(pkg.getNamespaces().toName())) {
179: hidden.add(pkg.getNamespaces());
180: }
181: }
182: }
183: }
184: return hidden;
185: }
186:
187: protected Set getHiddenElements(XMLCollection col) {
188: Set hidden = new HashSet();
189: String elAttr = (String) comp.getSettings().getSetting(
190: "HideElements." + col.toName());
191: String[] els = Utils.tokenize(elAttr, " ");
192: for (int i = 0; i < els.length; i++) {
193: String key = els[i];
194: String hstr = (String) comp.getSettings().getSetting(
195: "HideElements." + col.toName() + "." + key);
196: String[] hstra = Utils.tokenize(hstr, " ");
197: if (hstra != null) {
198: for (int k = 0; k < hstra.length; k++) {
199: if (hstra[k].equals("*")) {
200: hidden.addAll(col.toElements());
201: return hidden;
202: }
203: Iterator ci = col.toElements().iterator();
204: while (ci.hasNext()) {
205: XMLElement el = (XMLElement) ci.next();
206: if (key.equals("")) {
207: if (el.toValue().equals(hstra[k])) {
208: hidden.add(el);
209: }
210: } else {
211: if (el instanceof XMLComplexElement) {
212: XMLElement sel = ((XMLComplexElement) el)
213: .get(key);
214: if (sel != null) {
215: if (sel.toValue().equals(hstra[k])) {
216: hidden.add(el);
217: }
218: }
219: }
220: }
221: }
222: }
223: }
224: }
225: return hidden;
226: }
227:
228: }
|