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.experiment.HostComponent;
030: import org.cougaar.tools.csmart.society.AgentComponent;
031: import org.cougaar.tools.csmart.ui.tree.CSMARTDataFlavor;
032: import org.cougaar.tools.csmart.ui.tree.DMTNArray;
033: import org.cougaar.tools.csmart.ui.tree.DNDTree;
034: import org.cougaar.tools.csmart.ui.viewer.CSMART;
035: import org.cougaar.util.log.Logger;
036:
037: import javax.swing.tree.DefaultMutableTreeNode;
038: import javax.swing.tree.DefaultTreeModel;
039: import javax.swing.tree.TreePath;
040: import java.awt.datatransfer.DataFlavor;
041: import java.awt.datatransfer.Transferable;
042: import java.awt.dnd.DnDConstants;
043: import java.awt.event.MouseEvent;
044: import java.io.IOException;
045: import java.io.ObjectInputStream;
046:
047: /**
048: * Provides method definitions for abstract methods in DNDTree
049: * to support Host, Node and Agent trees in the Community Panel
050: * of the Experiment Builder.
051: */
052:
053: public class EntityDNDTree extends DNDTree {
054:
055: private transient Logger log;
056:
057: protected static CSMARTDataFlavor agentFlavor = new CSMARTDataFlavor(
058: DefaultMutableTreeNode.class, AgentComponent.class,
059: EntityDNDTree.class, "CSMART Agent");
060: protected static CSMARTDataFlavor hostFlavor = new CSMARTDataFlavor(
061: DefaultMutableTreeNode.class, HostComponent.class,
062: EntityDNDTree.class, "CSMART Host");
063: protected static CSMARTDataFlavor agentArrayFlavor = new CSMARTDataFlavor(
064: DMTNArray.class, AgentComponent.class, EntityDNDTree.class,
065: "CSMART Agent");
066: protected static CSMARTDataFlavor hostArrayFlavor = new CSMARTDataFlavor(
067: DMTNArray.class, HostComponent.class, EntityDNDTree.class,
068: "CSMART Host");
069:
070: public EntityDNDTree(DefaultTreeModel model) {
071: super (model);
072: setToolTipText("");
073: createLogger();
074: }
075:
076: private void createLogger() {
077: log = CSMART.createLogger(this .getClass().getName());
078: }
079:
080: public String getToolTipText(MouseEvent evt) {
081: if (getRowForLocation(evt.getX(), evt.getY()) == -1)
082: return null;
083: TreePath path = getPathForLocation(evt.getX(), evt.getY());
084: DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
085: .getLastPathComponent();
086: Object o = node.getUserObject();
087: if (o != null && o instanceof CommunityTreeObject)
088: return ((CommunityTreeObject) o).getToolTip();
089: else
090: return "";
091: }
092:
093: protected boolean supportsMultiDrag() {
094: return true;
095: }
096:
097: /**
098: * Return true if object can be dragged and false otherwise.
099: * Anything but roots can be dragged.
100: */
101:
102: public boolean isDraggable(Object selected) {
103: DefaultMutableTreeNode node = (DefaultMutableTreeNode) selected;
104: return (node.getParent() != null);
105: }
106:
107: /**
108: * Make a draggable object from a DefaultMutableTreeNode.
109: */
110:
111: public Transferable makeDraggableObject(Object o) {
112: if (o instanceof DefaultMutableTreeNode) {
113: return new EntityTreeObjectTransferable(
114: (DefaultMutableTreeNode) o);
115: } else if (o instanceof DMTNArray) {
116: DMTNArray nodeArray = (DMTNArray) o;
117: CommunityTreeObject userObject = (CommunityTreeObject) nodeArray.nodes[0]
118: .getUserObject();
119: if (userObject == null)
120: throw new IllegalArgumentException("null user object");
121: CSMARTDataFlavor[] theFlavors = null;
122: if (userObject.isAgent())
123: theFlavors = new CSMARTDataFlavor[] { agentArrayFlavor };
124: else if (userObject.isHost())
125: theFlavors = new CSMARTDataFlavor[] { hostArrayFlavor };
126: return new CommunityArrayTransferable(nodeArray, theFlavors);
127: }
128: return null;
129: }
130:
131: /**
132: * You can't drop anything in the host-node-agent trees.
133: */
134:
135: protected int isDroppable(DataFlavor[] flavors,
136: DefaultMutableTreeNode target) {
137: return DnDConstants.ACTION_NONE;
138: }
139:
140: /**
141: * Adds dropped object to this tree; called by drop method.
142: * Object is either a CommunityTreeObject or a DefaultMutableTreeNode
143: * depending on whether the user is dragging a leaf or a node.
144: * Returns true if successful.
145: */
146:
147: public int addElement(Transferable transferable,
148: DefaultMutableTreeNode target, DefaultMutableTreeNode before) {
149: // this will never be called
150: // because you can't drop anything in these trees
151: return DnDConstants.ACTION_NONE;
152: }
153:
154: private void readObject(ObjectInputStream ois) throws IOException,
155: ClassNotFoundException {
156: ois.defaultReadObject();
157: createLogger();
158: }
159:
160: }
|