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: package org.cougaar.qos.qrs.gui;
027:
028: import java.util.List;
029:
030: import javax.swing.tree.DefaultTreeModel;
031: import javax.swing.tree.TreeNode;
032:
033: import org.cougaar.qos.qrs.DataFormula;
034: import org.cougaar.qos.qrs.DataValue;
035: import org.cougaar.qos.qrs.DataValueChangedCallbackListener;
036:
037: public class DataFormulaNode extends VectorTreeNode {
038: private final DataFormula formula;
039: private SubscribersNode subscribers;
040: private DependsOnNode dependsOn;
041:
042: public DataFormulaNode(TreeNode parent, DataFormula formula) {
043: super (parent, formula);
044: this .formula = formula;
045: }
046:
047: public void updateChildren(DefaultTreeModel model) {
048: // force a repaint ?
049: if (subscribers == null) {
050: subscribers = new SubscribersNode();
051: addChild(subscribers, model);
052: }
053: subscribers.updateChildren(model);
054: if (dependsOn == null) {
055: dependsOn = new DependsOnNode();
056: addChild(dependsOn, model);
057: }
058: dependsOn.updateChildren(model);
059: }
060:
061: public String toString() {
062: DataValue value = formula.query();
063: // String name = formula.getName();
064: // String[] args = formula.getArgs();
065: // String prefix = name + "(";
066: // if (args != null) {
067: // if (args.length > 0) prefix += args[0];
068: // for (int i=1; i<args.length; i++) {
069: // prefix += ", "+args[i];
070: // }
071: // }
072: // prefix += ")";
073:
074: String prefix = formula.toString();
075:
076: if (value == null) {
077: return prefix + ":<no value>";
078: } else {
079: return prefix + ":" + value.toString();
080: }
081: }
082:
083: class SubscribersNode extends VectorTreeNode {
084: private SubscribersNode() {
085: super (DataFormulaNode.this , null);
086: }
087:
088: public String toString() {
089: return "Subscribers";
090: }
091:
092: public void updateChildren(DefaultTreeModel model) {
093: List<DataValueChangedCallbackListener> subscribers = formula
094: .getSubscribers();
095: synchronized (subscribers) {
096: for (DataValueChangedCallbackListener subscriber : subscribers) {
097: DataTreeNode node = getChild(subscriber);
098: if (node == null) {
099: node = new LeafNode(this , subscriber);
100: addChild(node, model);
101: }
102: }
103: }
104: }
105:
106: }
107:
108: class DependsOnNode extends VectorTreeNode {
109: private DependsOnNode() {
110: super (DataFormulaNode.this , null);
111: }
112:
113: public String toString() {
114: return "Depends On";
115: }
116:
117: public void updateChildren(DefaultTreeModel model) {
118: List<DataFormula> subscribers = formula.getDependencies();
119: synchronized (subscribers) {
120: for (DataFormula dependency : subscribers) {
121: DataTreeNode node = getChild(dependency);
122: if (node == null) {
123: node = new LeafNode(this, dependency);
124: addChild(node, model);
125: }
126:
127: }
128: }
129: }
130:
131: }
132:
133: }
|