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 15 24 class BasicDragGestureRecognizer implements MouseListener, MouseMotionListener { 25 26 private MouseEvent dndArmedEvent = null; 27 28 private static int getMotionThreshold() { 29 return DragSource.getDragThreshold(); 30 } 31 32 protected int mapDragOperationFromModifiers(MouseEvent e) { 33 int mods = e.getModifiersEx(); 34 35 if ((mods & InputEvent.BUTTON1_DOWN_MASK) != InputEvent.BUTTON1_DOWN_MASK) { 36 return TransferHandler.NONE; 37 } 38 39 JComponent c = getComponent(e); 40 TransferHandler th = c.getTransferHandler(); 41 return SunDragSourceContextPeer.convertModifiersToDropAction(mods, th.getSourceActions(c)); 42 } 43 44 public void mouseClicked(MouseEvent e) { 45 } 46 47 public void mousePressed(MouseEvent e) { 48 dndArmedEvent = null; 49 50 if (isDragPossible(e) && mapDragOperationFromModifiers(e) != TransferHandler.NONE) { 51 dndArmedEvent = e; 52 e.consume(); 53 } 54 } 55 56 public void mouseReleased(MouseEvent e) { 57 dndArmedEvent = null; 58 } 59 60 public void mouseEntered(MouseEvent e) { 61 } 63 64 public void mouseExited(MouseEvent e) { 65 } 69 70 public void mouseDragged(MouseEvent e) { 71 if (dndArmedEvent != null) { 72 e.consume(); 73 74 int action = mapDragOperationFromModifiers(e); 75 76 if (action == TransferHandler.NONE) { 77 return; 78 } 79 80 int dx = Math.abs(e.getX() - dndArmedEvent.getX()); 81 int dy = Math.abs(e.getY() - dndArmedEvent.getY()); 82 if ((dx > getMotionThreshold()) || (dy > getMotionThreshold())) { 83 JComponent c = getComponent(e); 85 TransferHandler th = c.getTransferHandler(); 86 th.exportAsDrag(c, dndArmedEvent, action); 87 dndArmedEvent = null; 88 } 89 } 90 } 91 92 public void mouseMoved(MouseEvent e) { 93 } 94 95 private TransferHandler getTransferHandler(MouseEvent e) { 96 JComponent c = getComponent(e); 97 return c == null ? null : c.getTransferHandler(); 98 } 99 100 111 protected boolean isDragPossible(MouseEvent e) { 112 JComponent c = getComponent(e); 113 return (c == null) ? true : (c.getTransferHandler() != null); 114 } 115 116 protected JComponent getComponent(MouseEvent e) { 117 Object src = e.getSource(); 118 if (src instanceof JComponent) { 119 JComponent c = (JComponent) src; 120 return c; 121 } 122 return null; 123 } 124 125 } 126 | Popular Tags |