KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > dnd > TreeDropTarget


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.dnd;
20
21 import java.awt.Point JavaDoc;
22 import java.awt.dnd.DropTarget JavaDoc;
23 import java.awt.dnd.DropTargetDragEvent JavaDoc;
24 import java.awt.dnd.DropTargetDropEvent JavaDoc;
25 import java.awt.dnd.DropTargetEvent JavaDoc;
26 import java.awt.dnd.DropTargetListener JavaDoc;
27
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JTree JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import org.openharmonise.him.swing.resourcetree.*;
33 import org.openharmonise.him.window.menus.*;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.webdav.client.*;
36
37
38 /**
39  * Drop target for the resource tree. Used for copying/moving resources.
40  *
41  * @author Matthew Large
42  * @version $Revision: 1.1 $
43  *
44  */

45 public class TreeDropTarget implements DropTargetListener JavaDoc {
46
47     DropTarget JavaDoc target;
48     JTree JavaDoc targetTree;
49
50     /**
51      * Constructs a new tree drop target.
52      *
53      * @param tree Resource tree
54      */

55     public TreeDropTarget(ResourceTree tree) {
56         targetTree = tree.getTree();
57         target = new DropTarget JavaDoc(targetTree, this);
58       }
59
60     /* (non-Javadoc)
61      * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
62      */

63     public void dragEnter(DropTargetDragEvent JavaDoc arg0) {
64     }
65
66     /* (non-Javadoc)
67      * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
68      */

69     public void dragOver(DropTargetDragEvent JavaDoc dtde) {
70         Point JavaDoc pt = dtde.getLocation();
71         TreePath JavaDoc path = targetTree.getPathForLocation(pt.x, pt.y);
72         targetTree.setSelectionPath(path);
73         targetTree.expandPath(path);
74     }
75
76     /* (non-Javadoc)
77      * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
78      */

79     public void dropActionChanged(DropTargetDragEvent JavaDoc arg0) {
80     }
81     
82     /**
83      * Returns the popup menu which appears at the end of a drag and
84      * drop from a table view to the resource tree.
85      *
86      * @param src Source virtual file
87      * @param dst Destination virtual file
88      * @return Popup menu
89      */

90     private JPopupMenu JavaDoc getPopup(VirtualFile src, VirtualFile dst) {
91      return new MoveMenu(src, dst);
92     }
93
94     /* (non-Javadoc)
95      * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
96      */

97     public void drop(DropTargetDropEvent JavaDoc dtde) {
98         Point JavaDoc pt = dtde.getLocation();
99         TreePath JavaDoc path = targetTree.getClosestPathForLocation(pt.x,pt.y);
100         TreeNode node = (TreeNode)path.getLastPathComponent();
101         while(node.isLeaf()) {
102             node = (TreeNode) node.getParent();
103         }
104
105         VirtualFile src = null;
106
107         try {
108           src = (VirtualFile)dtde.getTransferable().getTransferData( VirtualFileTransferable.DAV_FLAVOR ); /*ex.getSelectedTableFile();*/
109         } catch (Exception JavaDoc e) {
110           e.printStackTrace(System.out);
111         }
112         
113         String JavaDoc sDestinationPath = node.getFilePath();
114         
115         WebDAVFileSystem webVFS = (WebDAVFileSystem) node.getVFS();
116         
117         VirtualFile dst = webVFS.getVirtualFile( sDestinationPath + "/" ).getResource();
118
119         JPopupMenu JavaDoc popper = this.getPopup(src, dst);
120
121         popper.show(this.targetTree, pt.x, pt.y);
122     }
123
124     /* (non-Javadoc)
125      * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
126      */

127     public void dragExit(DropTargetEvent JavaDoc arg0) {
128     }
129
130 }
Popular Tags