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.glm.ldm.asset.Organization;
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 UIClusterAssetsNode 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: @param uiPlugin this user interface plug in
053: @param planName name of plan to display
054: @param clusterId cluster from which to obtain plan
055: @exception UINoPlanException thrown when the plan does not exist
056: */
057:
058: public UIClusterAssetsNode(UIPlugin uiPlugin, String planName,
059: MessageAddress clusterId) throws UINoPlanException {
060: super ();
061: this .uiPlugin = uiPlugin;
062: this .planName = planName;
063: super .setUserObject(uiPlugin.getPlan(planName));
064: }
065:
066: /** Not a leaf.
067: @return false
068: */
069:
070: public boolean isLeaf() {
071: return false;
072: }
073:
074: /** Get this cluster's cluster assets. */
075:
076: private static UnaryPredicate organizationPredicate() {
077: return new UnaryPredicate() {
078: public boolean execute(Object o) {
079: //System.out.println("Predicate called with: " + o.toString());
080: return (o instanceof Organization);
081: }
082: };
083: }
084:
085: /** Get the cluster assets for the plan.
086: */
087:
088: public synchronized void loadChildren() {
089: // new CCV2 method to obtain cluster assets
090: if (!childrenLoaded) {
091: uiPlugin.subscribe(this , organizationPredicate());
092: childrenLoaded = true;
093: }
094: }
095:
096: /** Display the plan name in the tree.
097: @return the plan name
098: */
099:
100: public String toString() {
101: return planName;
102: }
103:
104: /** UISubscriber interface.
105: Notified when a cluster asset is added or removed and
106: update the tree.
107: */
108:
109: public synchronized void subscriptionChanged(
110: IncrementalSubscription container) {
111: //System.out.println("Container changed");
112: boolean reload = false;
113: Enumeration added = container.getAddedList();
114: Enumeration removed = container.getRemovedList();
115: Enumeration changed = container.getChangedList();
116: while (added.hasMoreElements()) {
117: Organization organization = (Organization) added
118: .nextElement();
119: UIClusterAssetNode node = new UIClusterAssetNode(
120: organization);
121: if (node != null) {
122: //System.out.println("Added: " + organization.toString() + " at " +
123: // getChildCount());
124: treeModel.insertNodeInto(node, this , getChildCount());
125: reload = true;
126: }
127: }
128: if (reload)
129: treeModel.reload();
130: while (removed.hasMoreElements()) {
131: Organization organization = (Organization) removed
132: .nextElement();
133: //System.out.println("Removed: " + organization.toString());
134: removeObjectFromTree(organization);
135: }
136: while (changed.hasMoreElements()) {
137: Organization organization = (Organization) changed
138: .nextElement();
139: DefaultMutableTreeNode node = findUserObject(organization);
140: if (node != null) {
141: treeModel.removeNodeFromParent(node);
142: treeModel.insertNodeInto(node, this , getChildCount());
143: } else
144: System.out
145: .println("Warning: could not find changed node.");
146: }
147: }
148:
149: }
|