1 7 package javax.swing.plaf.basic; 8 9 import java.awt.Toolkit ; 10 import java.awt.event.*; 11 import java.awt.dnd.DragSource ; 12 import javax.swing.*; 13 import sun.awt.dnd.SunDragSourceContextPeer; 14 import sun.awt.AppContext; 15 16 26 class DragRecognitionSupport { 27 private int motionThreshold; 28 private MouseEvent dndArmedEvent; 29 private JComponent component; 30 31 35 public static interface BeforeDrag { 36 public void dragStarting(MouseEvent me); 37 } 38 39 42 private static DragRecognitionSupport getDragRecognitionSupport() { 43 DragRecognitionSupport support = 44 (DragRecognitionSupport )AppContext.getAppContext(). 45 get(DragRecognitionSupport .class); 46 47 if (support == null) { 48 support = new DragRecognitionSupport (); 49 AppContext.getAppContext().put(DragRecognitionSupport .class, support); 50 } 51 52 return support; 53 } 54 55 58 public static boolean mousePressed(MouseEvent me) { 59 return ((DragRecognitionSupport )getDragRecognitionSupport()). 60 mousePressedImpl(me); 61 } 62 63 67 public static MouseEvent mouseReleased(MouseEvent me) { 68 return ((DragRecognitionSupport )getDragRecognitionSupport()). 69 mouseReleasedImpl(me); 70 } 71 72 75 public static boolean mouseDragged(MouseEvent me, BeforeDrag bd) { 76 return ((DragRecognitionSupport )getDragRecognitionSupport()). 77 mouseDraggedImpl(me, bd); 78 } 79 80 private void clearState() { 81 dndArmedEvent = null; 82 component = null; 83 } 84 85 private int mapDragOperationFromModifiers(MouseEvent me, 86 TransferHandler th) { 87 88 if (th == null || !SwingUtilities.isLeftMouseButton(me)) { 89 return TransferHandler.NONE; 90 } 91 92 return SunDragSourceContextPeer. 93 convertModifiersToDropAction(me.getModifiersEx(), 94 th.getSourceActions(component)); 95 } 96 97 100 private boolean mousePressedImpl(MouseEvent me) { 101 component = (JComponent)me.getSource(); 102 103 if (mapDragOperationFromModifiers(me, component.getTransferHandler()) 104 != TransferHandler.NONE) { 105 106 motionThreshold = DragSource.getDragThreshold(); 107 dndArmedEvent = me; 108 return true; 109 } 110 111 clearState(); 112 return false; 113 } 114 115 119 private MouseEvent mouseReleasedImpl(MouseEvent me) { 120 121 if (dndArmedEvent == null) { 122 return null; 123 } 124 125 MouseEvent retEvent = null; 126 127 if (me.getSource() == component) { 128 retEvent = dndArmedEvent; 129 } 131 clearState(); 132 return retEvent; 133 } 134 135 138 private boolean mouseDraggedImpl(MouseEvent me, BeforeDrag bd) { 139 140 if (dndArmedEvent == null) { 141 return false; 142 } 143 144 145 if (me.getSource() != component) { 146 clearState(); 147 return false; 148 } 149 150 int dx = Math.abs(me.getX() - dndArmedEvent.getX()); 151 int dy = Math.abs(me.getY() - dndArmedEvent.getY()); 152 if ((dx > motionThreshold) || (dy > motionThreshold)) { 153 TransferHandler th = component.getTransferHandler(); 154 int action = mapDragOperationFromModifiers(me, th); 155 if (action != TransferHandler.NONE) { 156 157 if (bd != null) { 158 bd.dragStarting(dndArmedEvent); 159 } 160 th.exportAsDrag(component, dndArmedEvent, action); 161 clearState(); 162 } 163 } 164 165 return true; 166 } 167 } 168 | Popular Tags |