1 10 11 package swingwt.awt.dnd; 12 13 import java.util.ArrayList ; 14 import java.util.TooManyListenersException ; 15 16 import org.eclipse.swt.dnd.DragSource; 17 18 import swingwt.awt.Component; 19 import swingwt.awt.Point; 20 import swingwt.awt.event.InputEvent; 21 22 public abstract class DragGestureRecognizer { 23 24 protected DragGestureRecognizer(DragSource ds, Component c, int sa, DragGestureListener dgl) { 25 26 super(); 27 28 dragSource = ds; 29 component = c; 30 sourceActions = sa & 31 (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); 32 33 try { 34 if (dgl != null) addDragGestureListener(dgl); 35 } 36 catch (Exception e) {} 37 } 38 39 protected DragGestureRecognizer(DragSource ds, Component c, int sa) { 40 this(ds, c, sa, null); 41 } 42 43 protected DragGestureRecognizer(DragSource ds, Component c) { 44 this(ds, c, DnDConstants.ACTION_NONE); 45 } 46 47 protected DragGestureRecognizer(DragSource ds) { 48 this(ds, null); 49 } 50 51 protected abstract void registerListeners(); 52 53 protected abstract void unregisterListeners(); 54 55 public DragSource getDragSource() { 56 return dragSource; 57 } 58 59 public synchronized Component getComponent() { 60 return component; 61 } 62 63 public synchronized void setComponent(Component c) { 64 if (component != null && dragGestureListener != null) 65 unregisterListeners(); 66 67 component = c; 68 69 if (component != null && dragGestureListener != null) 70 registerListeners(); 71 } 72 73 public synchronized int getSourceActions() { 74 return sourceActions; 75 } 76 77 public synchronized void setSourceActions(int actions) { 78 sourceActions = actions & 79 (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); 80 } 81 82 public InputEvent getTriggerEvent() { 83 return events.isEmpty() ? null : (InputEvent) events.get(0); 84 } 85 86 public void resetRecognizer() { 87 events.clear(); 88 } 89 90 public synchronized void addDragGestureListener(DragGestureListener dgl) throws 91 TooManyListenersException { 92 if (dragGestureListener != null) 93 throw new TooManyListenersException (); 94 else { 95 dragGestureListener = dgl; 96 97 if (component != null) registerListeners(); 98 } 99 } 100 101 public synchronized void removeDragGestureListener(DragGestureListener dgl) { 102 if (dragGestureListener == null || !dragGestureListener.equals(dgl)) 103 throw new IllegalArgumentException (); 104 else { 105 dragGestureListener = null; 106 107 if (component != null) unregisterListeners(); 108 } 109 } 110 111 protected synchronized void fireDragGestureRecognized(int dragAction, Point p) { 112 try { 113 if (dragGestureListener != null) { 114 dragGestureListener.dragGestureRecognized(new DragGestureEvent(this, 115 dragAction, p, events)); 116 } 117 } 118 finally { 119 events.clear(); 120 } 121 } 122 123 protected synchronized void appendEvent(InputEvent awtie) { 124 events.add(awtie); 125 } 126 127 protected DragSource dragSource; 128 129 protected Component component; 130 131 protected transient DragGestureListener dragGestureListener; 132 133 protected int sourceActions; 134 protected ArrayList events = new ArrayList (1); 135 136 } 137 | Popular Tags |