1 19 20 package org.netbeans.modules.tasklist.usertasks.treetable; 21 22 import java.awt.Point ; 23 import java.awt.Toolkit ; 24 import java.awt.dnd.DnDConstants ; 25 import java.awt.event.*; 26 import javax.swing.*; 27 import javax.swing.tree.TreePath ; 28 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 29 30 41 public class TreeTableDragGestureRecognizer implements MouseListener, 42 MouseMotionListener { 43 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 )Toolkit.getDefaultToolkit(). 67 getDesktopProperty( 68 "DnD.gestureMotionThreshold")).intValue(); 69 } catch (Exception 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 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 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 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 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 Point 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 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 } 217 return false; 218 } 219 220 protected JComponent getComponent(MouseEvent e) { 221 Object 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 |