001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper.tree;
021:
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.awt.dnd.DragGestureEvent;
025: import java.awt.dnd.DropTargetDropEvent;
026: import java.util.List;
027:
028: import javax.swing.tree.TreePath;
029:
030: import org.netbeans.modules.soa.mapper.basicmapper.MapperLink;
031: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.IMapperCanvasView;
032: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.gtk.ICanvasView;
033: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
034: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeView;
035: import org.netbeans.modules.soa.mapper.common.IMapperLink;
036: import org.netbeans.modules.soa.mapper.common.IMapperNode;
037:
038: /**
039: * <p>
040: *
041: * Title: </p> DestTreeViewController <p>
042: *
043: * Description: </p> Provides an implemenation Destnatated Tree behavior.<p>
044: *
045: * Copyright: Copyright (c) 2002 </p> <p>
046: *
047: * Company: </p>
048: *
049: * @author Un Seng Leong
050: * @created December 4, 2002
051: * @version 1.0
052: */
053: public class DestTreeViewController extends AbstractTreeViewController {
054:
055: /**
056: * Creates a new DestTreeViewController object.
057: */
058: public DestTreeViewController() {
059: }
060:
061: /**
062: * Return a mapper link connected to a mapper tree node that contains the
063: * drag location of the tree path of the tree. If the tree path cannot be
064: * found (drag outside the tree), this method returns null.
065: *
066: * @param event the DragGestureEvent
067: * @return a mapper link connected to a mapper tree node that contains
068: * the drag location of the tree path of the tree.
069: */
070: protected Object getDragObject(DragGestureEvent event) {
071: Point p = event.getDragOrigin();
072: TreePath path = ((IMapperTreeView) getView()).getTree()
073: .getPathForLocation(p.x, p.y);
074:
075: if (path == null) {
076: return null;
077: }
078: if (getDnDCustomizer() != null) {
079: if (!getDnDCustomizer().isMappable(path)) {
080: return null;
081: }
082: }
083:
084: IMapperTreeNode treeAddress = ((AbstractMapperTree) getView())
085: .getMapperTreeNode(path);
086: treeAddress.setHighlightLink(true);
087:
088: MapperLink link = new MapperLink();
089: link.setEndNode(treeAddress);
090:
091: return link;
092: }
093:
094: /**
095: * Return true if specified transfer object is an instance of mapper link
096: * from source tree, false otherwise. This method set the mapper link end
097: * node to a mapper tree node that contains the drop location of the tree
098: * path of the tree. If tree path is not found, return false.
099: *
100: * @param event DropTargetDropEvent
101: * @param transferData the object from the event transferable.
102: * @return Return true if specified transfer object is an
103: * instanceof mapper link that is from source tree
104: */
105: protected boolean handleJLocalObjectDrop(DropTargetDropEvent event,
106: Object transferData) {
107: if (transferData == null) {
108: return false;
109: }
110:
111: if (transferData instanceof IMapperLink) {
112: Point dropPoint = event.getLocation();
113: TreePath dropPath = ((IMapperTreeView) getView()).getTree()
114: .getPathForLocation(dropPoint.x, dropPoint.y);
115:
116: if (dropPath == null) {
117: return false;
118: }
119:
120: IMapperTreeNode treeNode = ((IMapperTreeView) getView())
121: .getMapperTreeNode(dropPath);
122: ((IMapperLink) transferData).setEndNode(treeNode);
123: requestNewLink((IMapperLink) transferData);
124:
125: return true;
126: }
127:
128: return false;
129: }
130:
131: /**
132: * Return true if the method handles JGoObject Drop to this tree
133: * successfully, false otherwise. This method constructs an mapper link
134: * connents to an mapper tree node that contains a tree path is located by
135: * the drop location. Then it calls and return from
136: * ICanvasView.createLinkFromDnD to delgates the actural link creation to
137: * the canvas.
138: *
139: * @param event the DropTargetDropEvent event that supports JGo DataFlavor
140: * in its transferable.
141: * @return true if ICanvasView.createLinkFromDnD handles new link to
142: * this tree successfully, false otherwise.
143: */
144: protected boolean handleJGoObjectDrop(DropTargetDropEvent event) {
145: Point dropPoint = event.getLocation();
146: TreePath dropPath = ((IMapperTreeView) getView()).getTree()
147: .getPathForLocation(dropPoint.x, dropPoint.y);
148:
149: if (dropPath == null) {
150: return false;
151: }
152:
153: IMapperTreeNode treeAddress = ((AbstractMapperTree) getView())
154: .getMapperTreeNode(dropPath);
155: MapperLink newLink = new MapperLink(null, treeAddress);
156:
157: return ((ICanvasView) ((IMapperCanvasView) this
158: .getMapperController().getViewManager().getCanvasView())
159: .getCanvas()).connectLinkByDrag(newLink);
160: }
161:
162: /**
163: * Sets the specified node as the end or start node on the given link,
164: * according to the direction that points to this tree.
165: */
166: protected void setLinkOnNode(IMapperLink link, IMapperTreeNode node) {
167: link.setEndNode(node);
168: }
169:
170: /**
171: * Sets the specified node as the end or start node on the given link,
172: * according to the direction that points to the opposite tree.
173: */
174: protected void setOppositeLinkOnNode(IMapperLink link,
175: IMapperTreeNode node) {
176: link.setStartNode(node);
177: }
178:
179: /**
180: * Returns the tree node linked to the opposite tree.
181: */
182: protected IMapperTreeNode getOriginatingTreeNodeFromLink(
183: IMapperLink link) {
184: IMapperNode node = link.getStartNode();
185: if (node instanceof IMapperTreeNode) {
186: IMapperTreeNode treeNode = (IMapperTreeNode) node;
187: return treeNode.isSourceTreeNode() ? treeNode : null;
188: }
189: return null;
190: }
191:
192: /**
193: * Determines if any of the links on the node link to the node with the
194: * direction of the link pointing to this tree.
195: */
196: protected boolean isNodeAlreadyLinked(IMapperNode node) {
197: boolean isFound = false;
198: List links = node.getLinks();
199: for (int i = 0; i < links.size(); i++) {
200: if (((IMapperLink) links.get(i)).getEndNode().equals(node)) {
201: return true;
202: }
203: }
204: return false;
205: }
206:
207: /**
208: * Returns the x location of the edge of the tree.
209: */
210: protected int getDragPortOriginX() {
211: Rectangle rect = getMapperController().getViewManager()
212: .getCanvasView().getCanvas().getUIComponent()
213: .getBounds();
214: return rect.width - 1;
215: }
216: }
|