1 package net.sf.mmapps.applications.developer.project;
2
3 import java.awt.Cursor;
4 import java.awt.dnd.*;
5 import java.awt.event.InputEvent;
6 import java.beans.*;
7
8 import javax.swing.*;
9 import javax.swing.event.*;
10 import javax.swing.tree.*;
11
12 import net.sf.mmapps.applications.developer.dnd.XMLConfigurationTransferable;
13 import net.sf.mmapps.modules.config.ConfigurationXMLWriter;
14
15 import org.apache.commons.logging.*;
16 /***
17 *
18 * @author Kees Jongenburger
19 * @version $Id: ProjectTree.java,v 1.2 2004/07/16 20:55:48 keesj Exp $
20 **/
21 public class ProjectTree extends JTree implements PropertyChangeListener, TreeSelectionListener,DragGestureListener, DropTargetListener,DragSourceListener {
22 private static Log log = LogFactory.getLog(ProjectTree.class);
23 /*** Variables needed for DnD */
24 private DragSource dragSource = null;
25 private DragSourceContext dragSourceContext = null;
26
27
28 /*** Creates a new instance of ProjectTree */
29 public ProjectTree(TreeNode treeNode) {
30 super(treeNode);
31
32
33 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
34 setRootVisible(false);
35 setScrollsOnExpand(true);
36 setCellRenderer(new ProjectCelRenderer());
37
38 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
39
40 addMouseListener(new ProjectTreeSelectionListener(this));
41 UIManager.addPropertyChangeListener(this);
42
43
44 dragSource = DragSource.getDefaultDragSource() ;
45 DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this );
46 dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);
47 DropTarget dropTarget = new DropTarget(this, this);
48 }
49 public void propertyChange(PropertyChangeEvent e) {
50 String name = e.getPropertyName();
51 if (name.equals("lookAndFeel")) {
52 setCellRenderer(new ProjectCelRenderer());
53 }
54 }
55 /***
56 * Called whenever the value of the selection changes.
57 * @param e the event that characterizes the change.
58 *
59 */
60 public void valueChanged(TreeSelectionEvent e) {
61
62 }
63
64 /*** This method is invoked to signify that the Drag and Drop
65 * operation is complete. The getDropSuccess() method of
66 * the <code>DragSourceDropEvent</code> can be used to
67 * determine the termination state. The getDropAction() method
68 * returns the operation that the drop site selected
69 * to apply to the Drop operation. Once this method is complete, the
70 * current <code>DragSourceContext</code> and
71 * associated resources become invalid.
72 *
73 * @param dsde the <code>DragSourceDropEvent</code>
74 *
75 */
76 public void dragDropEnd(DragSourceDropEvent dsde) {
77 }
78
79 /*** Called while a drag operation is ongoing, when the mouse pointer enters
80 * the operable part of the drop site for the <code>DropTarget</code>
81 * registered with this listener.
82 *
83 * @param dtde the <code>DropTargetDragEvent</code>
84 *
85 */
86 public void dragEnter(DropTargetDragEvent dtde) {
87 }
88
89 /*** Called as the cursor's hotspot enters a platform-dependent drop site.
90 * This method is invoked when all the following conditions are true:
91 * <UL>
92 * <LI>The cursor's hotspot enters the operable part of a platform-
93 * dependent drop site.
94 * <LI>The drop site is active.
95 * <LI>The drop site accepts the drag.
96 * </UL>
97 *
98 * @param dsde the <code>DragSourceDragEvent</code>
99 *
100 */
101 public void dragEnter(DragSourceDragEvent dsde) {
102 }
103
104 /*** Called while a drag operation is ongoing, when the mouse pointer has
105 * exited the operable part of the drop site for the
106 * <code>DropTarget</code> registered with this listener.
107 *
108 * @param dte the <code>DropTargetEvent</code>
109 *
110 */
111 public void dragExit(DropTargetEvent dte) {
112 }
113
114 /*** Called as the cursor's hotspot exits a platform-dependent drop site.
115 * This method is invoked when any of the following conditions are true:
116 * <UL>
117 * <LI>The cursor's hotspot no longer intersects the operable part
118 * of the drop site associated with the previous dragEnter() invocation.
119 * </UL>
120 * OR
121 * <UL>
122 * <LI>The drop site associated with the previous dragEnter() invocation
123 * is no longer active.
124 * </UL>
125 * OR
126 * <UL>
127 * <LI> The current drop site has rejected the drag.
128 * </UL>
129 *
130 * @param dse the <code>DragSourceEvent</code>
131 *
132 */
133 public void dragExit(DragSourceEvent dse) {
134 }
135
136 /*** A <code>DragGestureRecognizer</code> has detected
137 * a platform-dependent drag initiating gesture and
138 * is notifying this listener
139 * in order for it to initiate the action for the user.
140 * <P>
141 * @param dge the <code>DragGestureEvent</code> describing
142 * the gesture that has just occurred
143 *
144 */
145 public void dragGestureRecognized(DragGestureEvent dge) {
146 log.debug("selection count" + getSelectionCount());
147 Object node = getSelectionPath().getLastPathComponent();
148 if (node != null){
149 if (node instanceof NodeManagerConfigurationMutableTreeNode){
150 NodeManagerConfigurationMutableTreeNode nodeManagerTreeNode = (NodeManagerConfigurationMutableTreeNode)node;
151 String data = ConfigurationXMLWriter.createNodeManagerConfiguration(nodeManagerTreeNode.getNodeManagerConfiguration());
152 log.info("put nodemanager configuration in dnd");
153 XMLConfigurationTransferable tr = new XMLConfigurationTransferable("nodemanager",nodeManagerTreeNode.getNodeManagerConfiguration().getName(), data);
154 Cursor cursor = DragSource.DefaultCopyNoDrop;
155 dragSource.startDrag(dge, cursor, tr, this);
156 }
157 }
158
159 }
160
161 /*** Called as the cursor's hotspot moves over a platform-dependent drop site.
162 * This method is invoked when all the following conditions are true:
163 * <UL>
164 * <LI>The cursor's hotspot has moved, but still intersects the
165 * operable part of the drop site associated with the previous
166 * dragEnter() invocation.
167 * <LI>The drop site is still active.
168 * <LI>The drop site accepts the drag.
169 * </UL>
170 *
171 * @param dsde the <code>DragSourceDragEvent</code>
172 *
173 */
174 public void dragOver(DragSourceDragEvent dsde) {
175 }
176
177 /*** Called when a drag operation is ongoing, while the mouse pointer is still
178 * over the operable part of the drop site for the <code>DropTarget</code>
179 * registered with this listener.
180 *
181 * @param dtde the <code>DropTargetDragEvent</code>
182 *
183 */
184 public void dragOver(DropTargetDragEvent dtde) {
185 }
186
187 /*** Called when the drag operation has terminated with a drop on
188 * the operable part of the drop site for the <code>DropTarget</code>
189 * registered with this listener.
190 * <p>
191 * This method is responsible for undertaking
192 * the transfer of the data associated with the
193 * gesture. The <code>DropTargetDropEvent</code>
194 * provides a means to obtain a <code>Transferable</code>
195 * object that represents the data object(s) to
196 * be transfered.<P>
197 * From this method, the <code>DropTargetListener</code>
198 * shall accept or reject the drop via the
199 * acceptDrop(int dropAction) or rejectDrop() methods of the
200 * <code>DropTargetDropEvent</code> parameter.
201 * <P>
202 * Subsequent to acceptDrop(), but not before,
203 * <code>DropTargetDropEvent</code>'s getTransferable()
204 * method may be invoked, and data transfer may be
205 * performed via the returned <code>Transferable</code>'s
206 * getTransferData() method.
207 * <P>
208 * At the completion of a drop, an implementation
209 * of this method is required to signal the success/failure
210 * of the drop by passing an appropriate
211 * <code>boolean</code> to the <code>DropTargetDropEvent</code>'s
212 * dropComplete(boolean success) method.
213 * <P>
214 * Note: The data transfer should be completed before the call to the
215 * <code>DropTargetDropEvent</code>'s dropComplete(boolean success) method.
216 * After that, a call to the getTransferData() method of the
217 * <code>Transferable</code> returned by
218 * <code>DropTargetDropEvent.getTransferable()</code> is guaranteed to
219 * succeed only if the data transfer is local; that is, only if
220 * <code>DropTargetDropEvent.isLocalTransfer()</code> returns
221 * <code>true</code>. Otherwise, the behavior of the call is
222 * implementation-dependent.
223 * <P>
224 * @param dtde the <code>DropTargetDropEvent</code>
225 *
226 */
227 public void drop(DropTargetDropEvent dtde) {
228 }
229
230 /*** Called when the user has modified the drop gesture.
231 * This method is invoked when the state of the input
232 * device(s) that the user is interacting with changes.
233 * Such devices are typically the mouse buttons or keyboard
234 * modifiers that the user is interacting with.
235 *
236 * @param dsde the <code>DragSourceDragEvent</code>
237 *
238 */
239 public void dropActionChanged(DragSourceDragEvent dsde) {
240 }
241
242 /*** Called if the user has modified
243 * the current drop gesture.
244 * <P>
245 * @param dtde the <code>DropTargetDragEvent</code>
246 *
247 */
248 public void dropActionChanged(DropTargetDragEvent dtde) {
249 }
250 }