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