001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.ui.tree;
028:
029: import org.cougaar.tools.csmart.core.property.BaseComponent;
030: import org.cougaar.tools.csmart.experiment.HostComponent;
031: import org.cougaar.tools.csmart.experiment.NodeComponent;
032: import org.cougaar.tools.csmart.society.AgentComponent;
033: import org.cougaar.tools.csmart.ui.viewer.CSMART;
034: import org.cougaar.util.log.Logger;
035:
036: import java.awt.datatransfer.DataFlavor;
037: import java.io.IOException;
038: import java.io.ObjectInputStream;
039: import java.io.ObjectOutputStream;
040: import java.io.Serializable;
041:
042: public class ConsoleTreeObject implements Serializable {
043: String name;
044: private boolean root = false;
045: private Class allowedClass = null;
046: BaseComponent component = null;
047: private transient Logger log;
048:
049: public ConsoleTreeObject(BaseComponent component) {
050: createLogger();
051: name = component.toString();
052: this .component = component;
053: }
054:
055: /**
056: * Constructor for root.
057: * @param name the name for the root node
058: * @param allowedClassName the allowed class of the children
059: */
060: public ConsoleTreeObject(String name, String allowedClassName) {
061: createLogger();
062: this .name = name;
063: try {
064: allowedClass = Class.forName(allowedClassName);
065: } catch (Exception e) {
066: if (log.isErrorEnabled()) {
067: log.error("Exception", e);
068: }
069: }
070: root = true;
071: }
072:
073: private void createLogger() {
074: log = CSMART.createLogger(this .getClass().getName());
075: }
076:
077: public String getName() {
078: return name;
079: }
080:
081: public void setName(String name) {
082: this .name = name;
083: if (component != null)
084: component.setName(name);
085: }
086:
087: public String toString() {
088: if (component == null)
089: return name;
090: return component.toString();
091: }
092:
093: public BaseComponent getComponent() {
094: return component;
095: }
096:
097: public boolean isRoot() {
098: return root;
099: }
100:
101: public boolean isHost() {
102: return component instanceof HostComponent;
103: }
104:
105: public boolean isNode() {
106: return component instanceof NodeComponent;
107: }
108:
109: public boolean isAgent() {
110: return component instanceof AgentComponent;
111: }
112:
113: public static boolean flavorEquals(CSMARTDataFlavor flavor1,
114: CSMARTDataFlavor flavor2) {
115: if (flavor1.equals(flavor2)) {
116: String c1 = flavor1.getUserObjectClassName();
117: String c2 = flavor2.getUserObjectClassName();
118: if (c1 != null)
119: return c1.equals(c2);
120: }
121: return false;
122: }
123:
124: public boolean allowsDataFlavor(DataFlavor flavor) {
125: if (flavor instanceof CSMARTDataFlavor) {
126: CSMARTDataFlavor cflavor = (CSMARTDataFlavor) flavor;
127: // if dropping an agent
128: // if the drop site is a Node component or
129: // the drop site is the root of a tree that allows agents
130: // then its ok
131: if (flavorEquals(cflavor, ConsoleDNDTree.agentFlavor)) {
132: return isNode() || isRoot()
133: && allowedClass == AgentComponent.class;
134: }
135: // same as above, except dropping multiple agents
136: if (flavorEquals(cflavor, ConsoleDNDTree.agentArrayFlavor)) {
137: return isNode() || isRoot()
138: && allowedClass == AgentComponent.class;
139: }
140: // if dropping a Node component
141: // if the drop site is a Host component or
142: // the drop site is the root of a tree that allows node components
143: // then its ok
144: if (flavorEquals(cflavor, ConsoleDNDTree.nodeFlavor)) {
145: return isHost() || isRoot()
146: && allowedClass == NodeComponent.class;
147: }
148: // same as above, except dropping multiple nodes
149: if (flavorEquals(cflavor, ConsoleDNDTree.nodeArrayFlavor)) {
150: return isHost() || isRoot()
151: && allowedClass == NodeComponent.class;
152: }
153: // if dropping a Host component
154: // if the drop site is the root of a tree that allows host components
155: // then its ok
156: if (flavorEquals(cflavor, ConsoleDNDTree.hostFlavor)) {
157: return isRoot() && allowedClass == HostComponent.class;
158: }
159: }
160: return false;
161: }
162:
163: private void writeObject(ObjectOutputStream os) throws IOException {
164: throw new IOException(
165: "Attempt to serialize ConsoleTreeObject, check DataFlavor");
166: }
167:
168: private void readObject(ObjectInputStream is) throws IOException,
169: ClassNotFoundException {
170: createLogger();
171: }
172: }
|