001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.qos.qrs.gui;
028:
029: import java.util.Enumeration;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.Vector;
033:
034: import javax.swing.tree.DefaultTreeModel;
035: import javax.swing.tree.TreeNode;
036:
037: abstract class VectorTreeNode implements DataTreeNode {
038: static final Enumeration<Object> NULL_ENUM = new NullEnum();
039:
040: private final Vector<DataTreeNode> children;
041: private final Map<Object, DataTreeNode> children_2;
042: private final TreeNode parent;
043: private final Object datum;
044:
045: public VectorTreeNode(TreeNode parent, Object object) {
046: this .parent = parent;
047: this .datum = object;
048: this .children = new Vector<DataTreeNode>();
049: this .children_2 = new HashMap<Object, DataTreeNode>();
050: }
051:
052: protected void addChild(DataTreeNode child, DefaultTreeModel model) {
053: children.add(child);
054: children_2.put(child.getDatum(), child);
055: if (model != null) {
056: model.reload(this );
057: }
058: }
059:
060: // DataTreeNode
061: public Object getDatum() {
062: return datum;
063: }
064:
065: public DataTreeNode getChild(Object datum) {
066: return children_2.get(datum);
067: }
068:
069: // TreeNode
070:
071: public boolean getAllowsChildren() {
072: return true;
073: }
074:
075: public int getChildCount() {
076: return children == null ? 0 : children.size();
077: }
078:
079: public boolean isLeaf() {
080: return false;
081: }
082:
083: public TreeNode getParent() {
084: return parent;
085: }
086:
087: public Enumeration<?> children() {
088: return children == null ? NULL_ENUM : children.elements();
089: }
090:
091: public TreeNode getChildAt(int index) {
092: return children.elementAt(index);
093: }
094:
095: public int getIndex(TreeNode child) {
096: return children.indexOf(child);
097: }
098:
099: static class NullEnum implements Enumeration<Object> {
100: public boolean hasMoreElements() {
101: return false;
102: }
103:
104: public Object nextElement() {
105: return null;
106: }
107: }
108: }
|