KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > treetable > TreeTableDragGestureRecognizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.treetable;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.dnd.DnDConstants JavaDoc;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import javax.swing.tree.TreePath JavaDoc;
28 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
29
30 /**
31  * Parts of this class were copied from the Linux JDK 1.4
32  * BasicDragGestureRecognizer and BasicTableUI.
33  *
34  * Drag gesture recognizer for TreeTable. Default swing drag gesture
35  * recognizer also starts a DnD operation if the user pressed his mouse
36  * over ther JTree's toggle. This makes it impossible to collapse/expand
37  * selected nodes.
38  *
39  * @author tl
40  */

41 public class TreeTableDragGestureRecognizer implements MouseListener,
42         MouseMotionListener {
43     /**
44      * Enables Drag&Drop for a TreeTable
45      *
46      * @param tt a TreeTable
47      */

48     public static void enableDnD(TreeTable tt) {
49         TreeTableDragGestureRecognizer l = new TreeTableDragGestureRecognizer();
50         tt.addMouseListener(l);
51         tt.addMouseMotionListener(l);
52     }
53     
54     private MouseEvent dndArmedEvent = null;
55     
56     private static int motionThreshold;
57     
58     private static boolean checkedMotionThreshold = false;
59
60     private static int getMotionThreshold() {
61         if (checkedMotionThreshold) {
62             return motionThreshold;
63         } else {
64             checkedMotionThreshold = true;
65             try {
66                 motionThreshold = ((Integer JavaDoc)Toolkit.getDefaultToolkit().
67                         getDesktopProperty(
68                         "DnD.gestureMotionThreshold")).intValue();
69             } catch (Exception JavaDoc e) {
70                 motionThreshold = 5;
71             }
72         }
73         return motionThreshold;
74     }
75     
76     protected int mapDragOperationFromModifiers(MouseEvent e) {
77         int mods = e.getModifiersEx();
78         
79         if ((mods & InputEvent.BUTTON1_DOWN_MASK) !=
80                 InputEvent.BUTTON1_DOWN_MASK) {
81             return TransferHandler.NONE;
82         }
83
84         JComponent c = getComponent(e);
85         TransferHandler th = c.getTransferHandler();
86         return convertModifiersToDropAction(mods, th.getSourceActions(c));
87     }
88     
89     /**
90      * Many thanks to JReversePro
91      *
92      * sun.awt.dnd.SunDragSourceContextPeer
93      *
94      * @param modifiers keyboard modifiers
95      * @param sourceActions available source actions
96      */

97     private static int convertModifiersToDropAction(int modifiers,
98             int sourceActions)
99     {
100          int k = 0;
101          switch (modifiers & (InputEvent.SHIFT_DOWN_MASK |
102                  InputEvent.CTRL_DOWN_MASK)) {
103               case InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK:
104                    k = DnDConstants.ACTION_LINK;
105                    break;
106               case InputEvent.CTRL_DOWN_MASK:
107                    k = DnDConstants.ACTION_COPY;
108                    break;
109               case InputEvent.SHIFT_DOWN_MASK:
110                    k = DnDConstants.ACTION_MOVE;
111                    break;
112               // without a modifier
113
default:
114                    if ((sourceActions & DnDConstants.ACTION_MOVE) != 0) {
115                         k = DnDConstants.ACTION_MOVE;
116                         break;
117                    }
118                    else if ((sourceActions & DnDConstants.ACTION_COPY) != 0) {
119                         k = DnDConstants.ACTION_COPY;
120                         break;
121                    }
122                    else if ((sourceActions & DnDConstants.ACTION_LINK) != 0)
123                         k = DnDConstants.ACTION_LINK;
124
125
126          }
127          return (k & sourceActions);
128     }
129
130     public void mouseClicked(MouseEvent e) {
131     }
132
133     public void mousePressed(MouseEvent e) {
134         dndArmedEvent = null;
135
136     if (isDragPossible(e) && mapDragOperationFromModifiers(e) !=
137                 TransferHandler.NONE) {
138             dndArmedEvent = e;
139         e.consume();
140     }
141     }
142     
143     public void mouseReleased(MouseEvent e) {
144         dndArmedEvent = null;
145     }
146     
147     public void mouseEntered(MouseEvent e) {
148     }
149     
150     public void mouseExited(MouseEvent e) {
151     }
152
153     public void mouseDragged(MouseEvent e) {
154     if (dndArmedEvent != null) {
155             e.consume();
156             
157             int action = mapDragOperationFromModifiers(e);
158             
159             if (action == TransferHandler.NONE) {
160                 return;
161             }
162             
163         int dx = Math.abs(e.getX() - dndArmedEvent.getX());
164         int dy = Math.abs(e.getY() - dndArmedEvent.getY());
165             if ((dx > getMotionThreshold()) || (dy > getMotionThreshold())) {
166         // start transfer... shouldn't be a click at this point
167
JComponent c = getComponent(e);
168         TransferHandler th = c.getTransferHandler();
169         th.exportAsDrag(c, dndArmedEvent, action);
170         dndArmedEvent = null;
171         }
172     }
173     }
174     
175     public void mouseMoved(MouseEvent e) {
176     }
177     
178     private TransferHandler getTransferHandler(MouseEvent e) {
179         JComponent c = getComponent(e);
180         return c == null ? null : c.getTransferHandler();
181     }
182     
183     /**
184      * Determines if the following are true:
185      * <ul>
186      * <li>the press event is located over a selection
187      * <li>the dragEnabled property is true
188      * <li>A TranferHandler is installed
189      * </ul>
190      * <p>
191      * This is implemented to perform the superclass behavior
192      * followed by a check if the dragEnabled
193      * property is set and if the location picked is selected.
194      */

195     protected boolean isDragPossible(MouseEvent e) {
196         JComponent c = getComponent(e);
197         if ((c == null) ? true : (c.getTransferHandler() != null)) {
198             TreeTable table = (TreeTable) this.getComponent(e);
199             //if (table.getDragEnabled()) {
200
Point JavaDoc p = e.getPoint();
201                 int row = table.rowAtPoint(p);
202                 int column = table.columnAtPoint(p);
203                 if ((column != -1) && (row != -1) &&
204                         table.isCellSelected(row, column)) {
205                     if (table.isTreeColumn(column)) {
206                         TreePath JavaDoc tp = table.getTree().getPathForLocation(
207                                 e.getX() -
208                                 table.getCellRect(0, column, true).x,
209                                 e.getY());
210                         return tp != null;
211                     } else {
212                         return true;
213                     }
214                 }
215             //}
216
}
217         return false;
218     }
219
220     protected JComponent getComponent(MouseEvent e) {
221     Object JavaDoc src = e.getSource();
222     if (src instanceof JComponent) {
223         JComponent c = (JComponent) src;
224         return c;
225     }
226     return null;
227     }
228
229 }
230
Popular Tags