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.asset.AggregateAsset;
036: import org.cougaar.planning.ldm.asset.Asset;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /** An object passed to UITreeNode (a dynamically expandable
040: tree node) must extend the UITreeNode class and override these methods:
041: isLeaf: true if it has no children;
042: loadChildren: inserts children into tree;
043: toString: for rendering itself
044: */
045:
046: public class UIAllAssetsNode extends UITreeNode implements UISubscriber {
047: private UIPlugin uiPlugin;
048: private String planName;
049: private int treeIndex = 0;
050: private boolean childrenLoaded = false;
051:
052: /** Creates a tree node for a plan by calling the UITreeNode constructor.
053: @param uiPlugin this user interface plug in
054: @param planName name of plan to display
055: @param clusterId cluster from which to obtain plan
056: @exception UINoPlanException thrown when the plan does not exist
057: */
058:
059: public UIAllAssetsNode(UIPlugin uiPlugin, String planName,
060: MessageAddress clusterId) throws UINoPlanException {
061: super ();
062: this .uiPlugin = uiPlugin;
063: this .planName = planName;
064: super .setUserObject(uiPlugin.getPlan(planName));
065: }
066:
067: /** Not a leaf.
068: @return false
069: */
070:
071: public boolean isLeaf() {
072: return false;
073: }
074:
075: /** Get this cluster's assets. */
076:
077: private static UnaryPredicate assetPredicate() {
078: return new UnaryPredicate() {
079: public boolean execute(Object o) {
080: //System.out.println("Predicate called with: " + o.toString());
081: return (o instanceof Asset);
082: }
083: };
084: }
085:
086: /** Get all assets, properties and attributes.
087: */
088:
089: public synchronized void loadChildren() {
090: if (!childrenLoaded) {
091: uiPlugin.subscribe(this , assetPredicate());
092: childrenLoaded = true;
093: }
094: }
095:
096: /** UISubscriber interface.
097: Notified when an asset is added or removed or changed;
098: update the tree.
099: */
100:
101: public synchronized void subscriptionChanged(
102: IncrementalSubscription container) {
103: boolean reload = false;
104: Enumeration added = container.getAddedList();
105: Enumeration removed = container.getRemovedList();
106: Enumeration changed = container.getChangedList();
107: while (added.hasMoreElements()) {
108: Asset asset = (Asset) added.nextElement();
109: if (asset instanceof AggregateAsset) {
110: AggregateAsset aggregateAsset = (AggregateAsset) asset;
111: asset = aggregateAsset.getAsset();
112: if (asset == null)
113: continue;
114: }
115: UIAssetAttributeNode node = new UIAssetAttributeNode(asset);
116: if (node != null) {
117: //System.out.println("Added: " + asset.toString() + " at " +
118: // getChildCount());
119: treeModel.insertNodeInto(node, this , getChildCount());
120: reload = true;
121: }
122: }
123: if (reload)
124: treeModel.reload();
125: while (removed.hasMoreElements()) {
126: Asset asset = (Asset) removed.nextElement();
127: //System.out.println("Removed: " + asset.toString());
128: removeObjectFromTree(asset);
129: }
130: while (changed.hasMoreElements()) {
131: Asset asset = (Asset) changed.nextElement();
132: DefaultMutableTreeNode node = findUserObject(asset);
133: if (node != null) {
134: treeModel.removeNodeFromParent(node);
135: treeModel.insertNodeInto(node, this , getChildCount());
136: } else
137: System.out
138: .println("Warning: could not find changed node.");
139: }
140: }
141:
142: /** Display the plan name in the tree.
143: @return the plan name
144: */
145:
146: public String toString() {
147: return planName;
148: }
149:
150: }
|