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.community;
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.tree.CSMARTDataFlavor;
034: import org.cougaar.tools.csmart.ui.viewer.CSMART;
035: import org.cougaar.util.log.Logger;
036:
037: import java.awt.datatransfer.DataFlavor;
038: import java.io.IOException;
039: import java.io.ObjectInputStream;
040: import java.io.ObjectOutputStream;
041:
042: public class CommunityTreeObject implements Cloneable {
043: private String label;
044: private String toolTip;
045: private boolean isRoot = false;
046: private boolean isCommunity = false;
047: private boolean isHost = false;
048: private boolean isAgent = false;
049: private boolean allowsChildren = true;
050: private String communityName = null; // community this object is in
051: private transient Logger log;
052:
053: /**
054: * Constructor for root.
055: * @param label for the root node
056: */
057: public CommunityTreeObject(String label) {
058: this (null, label, "Root", null);
059: }
060:
061: public CommunityTreeObject(String label, String type) {
062: this (null, label, type, null);
063: }
064:
065: public CommunityTreeObject(String label, String type,
066: String communityName) {
067: this (null, label, type, communityName);
068: }
069:
070: public CommunityTreeObject(BaseComponent component) {
071: this (component, component.toString(), "", null);
072: }
073:
074: public CommunityTreeObject(BaseComponent component, String label,
075: String type, String communityName) {
076: createLogger();
077: this .communityName = communityName;
078: if (component != null) {
079: if (component instanceof HostComponent)
080: isHost = true;
081: else if (component instanceof NodeComponent)
082: isAgent = true;
083: else if (component instanceof AgentComponent) {
084: isAgent = true;
085: allowsChildren = false;
086: }
087: } else {
088: if (type.equals("Root"))
089: isRoot = true;
090: else if (type.equalsIgnoreCase("Community"))
091: isCommunity = true;
092: else if (type.equals("Host"))
093: isHost = true;
094: else if (type.equalsIgnoreCase("Agent")) {
095: isAgent = true;
096: allowsChildren = false;
097: } else if (type.equalsIgnoreCase("Node")) {
098: isAgent = true;
099: allowsChildren = false;
100: } else {
101: allowsChildren = false;
102: isAgent = true;
103: if (log.isInfoEnabled()) {
104: log.info("CommunityTreeObject " + label
105: + " created with unknown type: " + type
106: + " in community " + communityName
107: + ". Treating as agent.");
108: }
109: }
110: }
111: this .label = label;
112: this .toolTip = toolTip;
113: }
114:
115: protected void setLabel(String label) {
116: this .label = label;
117: }
118:
119: public String toString() {
120: return label;
121: }
122:
123: protected void setToolTip(String toolTip) {
124: this .toolTip = toolTip;
125: }
126:
127: protected String getToolTip() {
128: return toolTip;
129: }
130:
131: private void createLogger() {
132: log = CSMART.createLogger(this .getClass().getName());
133: }
134:
135: protected boolean isRoot() {
136: return isRoot;
137: }
138:
139: protected boolean isCommunity() {
140: return isCommunity;
141: }
142:
143: protected boolean isHost() {
144: return isHost;
145: }
146:
147: protected boolean isAgent() {
148: return isAgent;
149: }
150:
151: protected String getType() {
152: if (isAgent())
153: return "Agent";
154: else if (isHost())
155: return "Host";
156: else if (isCommunity())
157: return "Community";
158: else if (isRoot())
159: return "Root";
160: else
161: return "Unkown";
162: }
163:
164: protected boolean allowsChildren() {
165: return allowsChildren;
166: }
167:
168: protected static boolean flavorEquals(CSMARTDataFlavor flavor1,
169: CSMARTDataFlavor flavor2) {
170: if (flavor1.equals(flavor2)) {
171: String c1 = flavor1.getUserObjectClassName();
172: String c2 = flavor2.getUserObjectClassName();
173: if (c1 != null)
174: return c1.equals(c2);
175: }
176: return false;
177: }
178:
179: protected boolean allowsDataFlavor(DataFlavor flavor) {
180: if (flavor instanceof CSMARTDataFlavor) {
181: CSMARTDataFlavor cflavor = (CSMARTDataFlavor) flavor;
182: // allow dropping hosts, nodes and agents and communities on communities
183: // nodes are actually agents
184: if ((flavorEquals(cflavor, CommunityDNDTree.communityFlavor)
185: || flavorEquals(cflavor,
186: CommunityDNDTree.communityArrayFlavor)
187: || flavorEquals(cflavor,
188: CommunityDNDTree.hostFlavor)
189: || flavorEquals(cflavor,
190: CommunityDNDTree.hostArrayFlavor)
191: || flavorEquals(cflavor,
192: CommunityDNDTree.agentFlavor) || flavorEquals(
193: cflavor, CommunityDNDTree.agentArrayFlavor))
194: && isCommunity())
195: return true;
196: // allow dropping communities on root
197: if ((flavorEquals(cflavor, CommunityDNDTree.communityFlavor) || flavorEquals(
198: cflavor, CommunityDNDTree.communityArrayFlavor))
199: && isRoot())
200: return true;
201: }
202: return false;
203: }
204:
205: public void setCommunityName(String communityName) {
206: this .communityName = communityName;
207: }
208:
209: public String getCommunityName() {
210: return communityName;
211: }
212:
213: public CommunityTreeObject copy() {
214: CommunityTreeObject copiedObject = null;
215: try {
216: copiedObject = (CommunityTreeObject) clone();
217: } catch (CloneNotSupportedException e) {
218: if (log.isErrorEnabled()) {
219: log.error("Clone not supported: " + e);
220: }
221: }
222: return copiedObject;
223: }
224:
225: private void writeObject(ObjectOutputStream os) throws IOException {
226: throw new IOException(
227: "Attempt to serialize CommunityTreeObject, check DataFlavor");
228: }
229:
230: private void readObject(ObjectInputStream is) throws IOException,
231: ClassNotFoundException {
232: createLogger();
233: }
234: }
|