001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 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.mlm.debug.ui;
028:
029: import java.util.Enumeration;
030:
031: import javax.swing.tree.DefaultMutableTreeNode;
032:
033: import org.cougaar.core.blackboard.IncrementalSubscription;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.planning.ldm.plan.PlanElement;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /** An object passed to UITreeNode (a dynamically expandable
039: tree node) must extend the UITreeNode class and override these methods:
040: isLeaf: true if it has no children;
041: loadChildren: inserts children into tree;
042: toString: for rendering itself
043: */
044:
045: public class UIPlanDetailsNode extends UITreeNode implements
046: UISubscriber {
047: private UIPlugin uiPlugin;
048: private String planName;
049: private boolean childrenLoaded = false;
050:
051: /** Creates a tree node for a plan by calling the UITreeNode constructor.
052: Listens on the plan elements collection (plan elements are the children
053: nodes of the plan) to dynamically change the tree as the plan elements
054: change.
055: @param uiPlugin this user interface plug in
056: @param planName name of plan to display
057: @param clusterId cluster from which to obtain plan
058: @exception UINoPlanException thrown when the plan does not exist
059: */
060:
061: public UIPlanDetailsNode(UIPlugin uiPlugin, String planName,
062: MessageAddress clusterId) throws UINoPlanException {
063: super ();
064: this .uiPlugin = uiPlugin;
065: this .planName = planName;
066: super .setUserObject(uiPlugin.getPlan(planName));
067: }
068:
069: /** The plan is never a leaf.
070: @return false
071: */
072:
073: public boolean isLeaf() {
074: return false;
075: }
076:
077: /** Get this cluster's plan elements. */
078:
079: private static UnaryPredicate planElementPredicate() {
080: return new UnaryPredicate() {
081: public boolean execute(Object o) {
082: //System.out.println("Predicate called with: " + o.toString());
083: return (o instanceof PlanElement);
084: }
085: };
086: }
087:
088: /** Get the plan elements (children of the plan node) for this plan.
089: Don't do this at node creation time, because the tree model
090: that is used in subscriptionChanged won't be set.
091: */
092:
093: public void loadChildren() {
094: if (!childrenLoaded) {
095: uiPlugin.subscribe(this , planElementPredicate());
096: childrenLoaded = true;
097: }
098: }
099:
100: /** UISubscriber interface.
101: Notified when a plan element is added or removed or changed;
102: update the tree.
103: */
104:
105: public void subscriptionChanged(IncrementalSubscription container) {
106: boolean reload = false;
107: Enumeration added = container.getAddedList();
108: Enumeration removed = container.getRemovedList();
109: Enumeration changed = container.getChangedList();
110: while (added.hasMoreElements()) {
111: PlanElement planElement = (PlanElement) added.nextElement();
112: UIObjectNode node = new UIObjectNode(planElement, "");
113: if (node != null) {
114: treeModel.insertNodeInto(node, this , getChildCount());
115: reload = true;
116: }
117: }
118: if (reload)
119: treeModel.reload();
120: while (removed.hasMoreElements()) {
121: PlanElement planElement = (PlanElement) removed
122: .nextElement();
123: removeObjectFromTree(planElement);
124: }
125: // if a plan element changed, remove the node and re-add it
126: // and redisplay the tree, as we don't know
127: // what in the plan element changed
128: while (changed.hasMoreElements()) {
129: PlanElement planElement = (PlanElement) changed
130: .nextElement();
131: DefaultMutableTreeNode node = findUserObject(planElement);
132: if (node != null) {
133: treeModel.removeNodeFromParent(node);
134: treeModel.insertNodeInto(node, this , getChildCount());
135: } else
136: System.out
137: .println("Warning: could not find changed node.");
138: }
139: }
140:
141: /** Display the plan name in the tree.
142: @return the plan name
143: */
144:
145: public String toString() {
146: return planName;
147: }
148:
149: }
|