001: //THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: //CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: //BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: //OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: //LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: //EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: //POSSIBILITY OF SUCH DAMAGE.
013: //
014: //Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.designtree;
016:
017: import java.awt.Point;
018: import java.awt.datatransfer.Transferable;
019: import java.awt.dnd.DnDConstants;
020: import java.awt.dnd.DropTarget;
021: import java.awt.dnd.DropTargetContext;
022: import java.awt.dnd.DropTargetDragEvent;
023: import java.awt.dnd.DropTargetDropEvent;
024: import java.awt.dnd.DropTargetEvent;
025: import java.awt.dnd.DropTargetListener;
026:
027: import javax.swing.JTree;
028: import javax.swing.tree.DefaultMutableTreeNode;
029: import javax.swing.tree.TreeNode;
030: import javax.swing.tree.TreePath;
031:
032: import com.metaboss.applications.designstudio.BaseUserObject;
033:
034: public class DesignTreeDropTarget implements DropTargetListener {
035: protected JTree mTree = null;
036: protected DropTarget mTarget = null;
037: protected BaseUserObject mReceivingUserObject = null;
038: private DesignTreeDelayedNodeExpander mDelayedTreeNodeExpander = null;
039:
040: public DesignTreeDropTarget(JTree aTree) {
041: mTree = aTree;
042: mTarget = (mTree != null) ? new DropTarget(mTree, this ) : null;
043: mDelayedTreeNodeExpander = new DesignTreeDelayedNodeExpander(
044: mTree);
045: }
046:
047: // check drag event and allow to drop
048: protected boolean isDropAllowed(TreeNode pEventTreeNode,
049: DropTargetDragEvent e, boolean pExpand) {
050: boolean lResult = false;
051: mReceivingUserObject = null;
052: if (mTree != null) {
053: if (pEventTreeNode != null) {
054: DefaultMutableTreeNode lTreeNode = (DefaultMutableTreeNode) pEventTreeNode;
055: Object lObject = lTreeNode.getUserObject();
056: if (lObject != null
057: && lObject instanceof BaseUserObject) {
058: BaseUserObject lNodeObject = (BaseUserObject) lObject;
059: DropTargetContext lContext = e
060: .getDropTargetContext();
061: if (lContext != null) {
062: DropTargetDropEvent lDropEvent = new DropTargetDropEvent(
063: lContext, new Point(0, 0), 0, 0);
064: Transferable lTransferable = lDropEvent
065: .getTransferable();
066: if (lTransferable != null) {
067: try {
068: lObject = lTransferable
069: .getTransferData(BaseUserObject.USEROBJECT_FLAVOR);
070: if (lObject != null
071: && lObject instanceof BaseUserObject) {
072: int lAction = e.getDropAction();
073: boolean lCopy = (lAction & DnDConstants.ACTION_COPY) != 0;
074: BaseUserObject lUserObject = (BaseUserObject) lObject;
075: if (pExpand) {
076: lResult = lNodeObject
077: .getBOObject()
078: .mayContainWithin(
079: lUserObject
080: .getBOObject());
081: } else {
082: lResult = lNodeObject
083: .canAcceptDropObject(
084: lUserObject,
085: lCopy);
086: if (lResult)
087: mReceivingUserObject = lNodeObject;
088: }
089: }
090: } catch (Exception ex) {
091: ex.printStackTrace();
092: }
093: }
094: }
095: }
096: }
097: }
098: return lResult;
099: }
100:
101: private TreeNode getNodeForEvent(DropTargetDragEvent e) {
102: Point p = e.getLocation();
103: DropTargetContext dtc = e.getDropTargetContext();
104: JTree lTree = (JTree) dtc.getComponent();
105: TreePath lPath = lTree.getClosestPathForLocation(p.x, p.y);
106: return (TreeNode) lPath.getLastPathComponent();
107: }
108:
109: /* DropTargetListener interface implementation */
110:
111: public void dragEnter(DropTargetDragEvent e) {
112: TreeNode lNode = getNodeForEvent(e);
113: if (isDropAllowed(lNode, e, true))
114: mDelayedTreeNodeExpander.scheduleToExpandNode(lNode);
115:
116: if (isDropAllowed(lNode, e, false))
117: e.acceptDrag(e.getDropAction());
118: else
119: e.rejectDrag();
120: }
121:
122: public void dragOver(DropTargetDragEvent e) {
123: TreeNode lNode = getNodeForEvent(e);
124: if (isDropAllowed(lNode, e, true))
125: mDelayedTreeNodeExpander.scheduleToExpandNode(lNode);
126:
127: if (isDropAllowed(lNode, e, false))
128: e.acceptDrag(e.getDropAction());
129: else
130: e.rejectDrag();
131: }
132:
133: public void dragExit(DropTargetEvent e) {
134: mDelayedTreeNodeExpander.stop();
135: }
136:
137: public void dropActionChanged(DropTargetDragEvent e) {
138: TreeNode lNode = getNodeForEvent(e);
139: mDelayedTreeNodeExpander.scheduleToExpandNode(lNode);
140: }
141:
142: public void drop(DropTargetDropEvent e) {
143: mDelayedTreeNodeExpander.stop();
144:
145: Transferable lTransferable = e.getTransferable();
146: if (lTransferable != null && mReceivingUserObject != null) {
147: try {
148: Object lObject = lTransferable
149: .getTransferData(BaseUserObject.USEROBJECT_FLAVOR);
150: if (lObject != null
151: && lObject instanceof BaseUserObject) {
152: int lAction = e.getDropAction();
153: boolean lCopy = (lAction & DnDConstants.ACTION_COPY) != 0;
154: BaseUserObject lUserOBject = (BaseUserObject) lObject;
155: mReceivingUserObject.acceptDropObject(lUserOBject,
156: lCopy);
157: }
158: } catch (Exception ex) {
159: ex.printStackTrace();
160: }
161: }
162: }
163: }
|