1 7 8 package java.awt.dnd; 9 10 import java.awt.event.InputEvent ; 11 import java.awt.Component ; 12 import java.awt.Point ; 13 14 import java.util.TooManyListenersException ; 15 import java.util.ArrayList ; 16 17 import java.io.IOException ; 18 import java.io.ObjectInputStream ; 19 import java.io.ObjectOutputStream ; 20 import java.io.Serializable ; 21 22 63 64 public abstract class DragGestureRecognizer implements Serializable { 65 66 private static final long serialVersionUID = 8996673345831063337L; 67 68 100 101 protected DragGestureRecognizer(DragSource ds, Component c, int sa, DragGestureListener dgl) { 102 super(); 103 104 if (ds == null) throw new IllegalArgumentException ("null DragSource"); 105 106 dragSource = ds; 107 component = c; 108 sourceActions = sa & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); 109 110 try { 111 if (dgl != null) addDragGestureListener(dgl); 112 } catch (TooManyListenersException tmle) { 113 } 115 } 116 117 143 144 protected DragGestureRecognizer(DragSource ds, Component c, int sa) { 145 this(ds, c, sa, null); 146 } 147 148 171 172 protected DragGestureRecognizer(DragSource ds, Component c) { 173 this(ds, c, DnDConstants.ACTION_NONE); 174 } 175 176 188 189 protected DragGestureRecognizer(DragSource ds) { 190 this(ds, null); 191 } 192 193 198 199 protected abstract void registerListeners(); 200 201 206 207 protected abstract void unregisterListeners(); 208 209 217 218 public DragSource getDragSource() { return dragSource; } 219 220 229 230 public synchronized Component getComponent() { return component; } 231 232 240 241 public synchronized void setComponent(Component c) { 242 if (component != null && dragGestureListener != null) 243 unregisterListeners(); 244 245 component = c; 246 247 if (component != null && dragGestureListener != null) 248 registerListeners(); 249 } 250 251 258 259 public synchronized int getSourceActions() { return sourceActions; } 260 261 267 268 public synchronized void setSourceActions(int actions) { 269 sourceActions = actions & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); 270 } 271 272 279 280 public InputEvent getTriggerEvent() { return events.isEmpty() ? null : (InputEvent )events.get(0); } 281 282 286 287 public void resetRecognizer() { events.clear(); } 288 289 298 299 public synchronized void addDragGestureListener(DragGestureListener dgl) throws TooManyListenersException { 300 if (dragGestureListener != null) 301 throw new TooManyListenersException (); 302 else { 303 dragGestureListener = dgl; 304 305 if (component != null) registerListeners(); 306 } 307 } 308 309 318 319 public synchronized void removeDragGestureListener(DragGestureListener dgl) { 320 if (dragGestureListener == null || !dragGestureListener.equals(dgl)) 321 throw new IllegalArgumentException (); 322 else { 323 dragGestureListener = null; 324 325 if (component != null) unregisterListeners(); 326 } 327 } 328 329 336 protected synchronized void fireDragGestureRecognized(int dragAction, Point p) { 337 try { 338 if (dragGestureListener != null) { 339 dragGestureListener.dragGestureRecognized(new DragGestureEvent (this, dragAction, p, events)); 340 } 341 } finally { 342 events.clear(); 343 } 344 } 345 346 363 364 protected synchronized void appendEvent(InputEvent awtie) { 365 events.add(awtie); 366 } 367 368 379 private void writeObject(ObjectOutputStream s) throws IOException { 380 s.defaultWriteObject(); 381 382 s.writeObject(SerializationTester.test(dragGestureListener) 383 ? dragGestureListener : null); 384 } 385 386 394 private void readObject(ObjectInputStream s) 395 throws ClassNotFoundException , IOException 396 { 397 s.defaultReadObject(); 398 399 dragGestureListener = (DragGestureListener )s.readObject(); 400 } 401 402 405 406 413 protected DragSource dragSource; 414 415 421 protected Component component; 422 423 427 protected transient DragGestureListener dragGestureListener; 428 429 436 protected int sourceActions; 437 438 445 protected ArrayList <InputEvent > events = new ArrayList <InputEvent >(1); 446 } 447 | Popular Tags |