1 7 8 package java.awt.dnd; 9 10 import java.util.TooManyListenersException ; 11 12 import java.io.IOException ; 13 import java.io.ObjectInputStream ; 14 import java.io.ObjectOutputStream ; 15 import java.io.Serializable ; 16 17 import java.awt.AWTEvent ; 18 import java.awt.Component ; 19 import java.awt.Dimension ; 20 import java.awt.GraphicsEnvironment ; 21 import java.awt.HeadlessException ; 22 import java.awt.Insets ; 23 import java.awt.Point ; 24 import java.awt.Rectangle ; 25 import java.awt.Toolkit ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.datatransfer.FlavorMap ; 29 import java.awt.datatransfer.SystemFlavorMap ; 30 import javax.swing.Timer ; 31 import java.awt.peer.ComponentPeer; 32 import java.awt.peer.LightweightPeer; 33 import java.awt.dnd.peer.DropTargetPeer; 34 35 36 50 51 public class DropTarget implements DropTargetListener , Serializable { 52 53 private static final long serialVersionUID = -6283860791671019047L; 54 55 74 public DropTarget(Component c, int ops, DropTargetListener dtl, 75 boolean act, FlavorMap fm) 76 throws HeadlessException 77 { 78 if (GraphicsEnvironment.isHeadless()) { 79 throw new HeadlessException (); 80 } 81 82 component = c; 83 84 setDefaultActions(ops); 85 86 if (dtl != null) try { 87 addDropTargetListener(dtl); 88 } catch (TooManyListenersException tmle) { 89 } 91 92 if (c != null) { 93 c.setDropTarget(this); 94 setActive(act); 95 } 96 97 if (fm != null) flavorMap = fm; 98 } 99 100 117 public DropTarget(Component c, int ops, DropTargetListener dtl, 118 boolean act) 119 throws HeadlessException 120 { 121 this(c, ops, dtl, act, null); 122 } 123 124 130 public DropTarget() throws HeadlessException { 131 this(null, DnDConstants.ACTION_COPY_OR_MOVE, null, true, null); 132 } 133 134 146 public DropTarget(Component c, DropTargetListener dtl) 147 throws HeadlessException 148 { 149 this(c, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true, null); 150 } 151 152 166 public DropTarget(Component c, int ops, DropTargetListener dtl) 167 throws HeadlessException 168 { 169 this(c, ops, dtl, true); 170 } 171 172 182 183 public synchronized void setComponent(Component c) { 184 if (component == c || component != null && component.equals(c)) 185 return; 186 187 Component old; 188 ComponentPeer oldPeer = null; 189 190 if ((old = component) != null) { 191 clearAutoscroll(); 192 193 component = null; 194 195 if (componentPeer != null) { 196 oldPeer = componentPeer; 197 removeNotify(componentPeer); 198 } 199 200 old.setDropTarget(null); 201 202 } 203 204 if ((component = c) != null) try { 205 c.setDropTarget(this); 206 } catch (Exception e) { if (old != null) { 208 old.setDropTarget(this); 209 addNotify(oldPeer); 210 } 211 } 212 } 213 214 220 221 public synchronized Component getComponent() { 222 return component; 223 } 224 225 232 233 public void setDefaultActions(int ops) { 234 getDropTargetContext().setTargetActions(ops & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_REFERENCE)); 235 } 236 237 241 void doSetDefaultActions(int ops) { 242 actions = ops; 243 } 244 245 251 252 public int getDefaultActions() { 253 return actions; 254 } 255 256 262 263 public synchronized void setActive(boolean isActive) { 264 if (isActive != active) { 265 active = isActive; 266 } 267 268 if (!active) clearAutoscroll(); 269 } 270 271 278 279 public boolean isActive() { 280 return active; 281 } 282 283 292 293 public synchronized void addDropTargetListener(DropTargetListener dtl) throws TooManyListenersException { 294 if (dtl == null) return; 295 296 if (equals(dtl)) throw new IllegalArgumentException ("DropTarget may not be its own Listener"); 297 298 if (dtListener == null) 299 dtListener = dtl; 300 else 301 throw new TooManyListenersException (); 302 } 303 304 309 310 public synchronized void removeDropTargetListener(DropTargetListener dtl) { 311 if (dtl != null && dtListener != null) { 312 if(dtListener.equals(dtl)) 313 dtListener = null; 314 else 315 throw new IllegalArgumentException ("listener mismatch"); 316 } 317 } 318 319 333 public synchronized void dragEnter(DropTargetDragEvent dtde) { 334 if (!active) return; 335 336 if (dtListener != null) { 337 dtListener.dragEnter(dtde); 338 } else 339 dtde.getDropTargetContext().setTargetActions(DnDConstants.ACTION_NONE); 340 341 initializeAutoscrolling(dtde.getLocation()); 342 } 343 344 358 public synchronized void dragOver(DropTargetDragEvent dtde) { 359 if (!active) return; 360 361 if (dtListener != null && active) dtListener.dragOver(dtde); 362 363 updateAutoscroll(dtde.getLocation()); 364 } 365 366 380 public synchronized void dropActionChanged(DropTargetDragEvent dtde) { 381 if (!active) return; 382 383 if (dtListener != null) dtListener.dropActionChanged(dtde); 384 385 updateAutoscroll(dtde.getLocation()); 386 } 387 388 403 public synchronized void dragExit(DropTargetEvent dte) { 404 if (!active) return; 405 406 if (dtListener != null && active) dtListener.dragExit(dte); 407 408 clearAutoscroll(); 409 } 410 411 426 public synchronized void drop(DropTargetDropEvent dtde) { 427 clearAutoscroll(); 428 429 if (dtListener != null && active) 430 dtListener.drop(dtde); 431 else { dtde.rejectDrop(); 433 } 434 } 435 436 445 446 public FlavorMap getFlavorMap() { return flavorMap; } 447 448 455 456 public void setFlavorMap(FlavorMap fm) { 457 flavorMap = fm == null ? SystemFlavorMap.getDefaultFlavorMap() : fm; 458 } 459 460 476 477 public void addNotify(ComponentPeer peer) { 478 if (peer == componentPeer) return; 479 480 componentPeer = peer; 481 482 for (Component c = component; 483 c != null && peer instanceof LightweightPeer; c = c.getParent()) { 484 peer = c.getPeer(); 485 } 486 487 if (peer instanceof DropTargetPeer) { 488 nativePeer = peer; 489 ((DropTargetPeer)peer).addDropTarget(this); 490 } else { 491 nativePeer = null; 492 } 493 } 494 495 510 511 public void removeNotify(ComponentPeer peer) { 512 if (nativePeer != null) 513 ((DropTargetPeer)nativePeer).removeDropTarget(this); 514 515 componentPeer = nativePeer = null; 516 } 517 518 524 525 public DropTargetContext getDropTargetContext() { 526 return dropTargetContext; 527 } 528 529 539 540 protected DropTargetContext createDropTargetContext() { 541 return new DropTargetContext (this); 542 } 543 544 555 private void writeObject(ObjectOutputStream s) throws IOException { 556 s.defaultWriteObject(); 557 558 s.writeObject(SerializationTester.test(dtListener) 559 ? dtListener : null); 560 } 561 562 574 private void readObject(ObjectInputStream s) 575 throws ClassNotFoundException , IOException 576 { 577 ObjectInputStream.GetField f = s.readFields(); 578 579 try { 580 dropTargetContext = 581 (DropTargetContext )f.get("dropTargetContext", null); 582 } catch (IllegalArgumentException e) { 583 } 585 if (dropTargetContext == null) { 586 dropTargetContext = createDropTargetContext(); 587 } 588 589 component = (Component )f.get("component", null); 590 actions = f.get("actions", DnDConstants.ACTION_COPY_OR_MOVE); 591 active = f.get("active", true); 592 593 try { 595 dtListener = (DropTargetListener )f.get("dtListener", null); 596 } catch (IllegalArgumentException e) { 597 dtListener = (DropTargetListener )s.readObject(); 599 } 600 } 601 602 603 604 607 608 protected static class DropTargetAutoScroller implements ActionListener { 609 610 616 617 protected DropTargetAutoScroller(Component c, Point p) { 618 super(); 619 620 component = c; 621 autoScroll = (Autoscroll )component; 622 623 Toolkit t = Toolkit.getDefaultToolkit(); 624 625 Integer initial = new Integer (100); 626 Integer interval = new Integer (100); 627 628 try { 629 initial = (Integer )t.getDesktopProperty("DnD.Autoscroll.initialDelay"); 630 } catch (Exception e) { 631 } 633 634 try { 635 interval = (Integer )t.getDesktopProperty("DnD.Autoscroll.interval"); 636 } catch (Exception e) { 637 } 639 640 timer = new Timer (interval.intValue(), this); 641 642 timer.setCoalesce(true); 643 timer.setInitialDelay(initial.intValue()); 644 645 locn = p; 646 prev = p; 647 648 try { 649 hysteresis = ((Integer )t.getDesktopProperty("DnD.Autoscroll.cursorHysteresis")).intValue(); 650 } catch (Exception e) { 651 } 653 654 timer.start(); 655 } 656 657 660 661 private void updateRegion() { 662 Insets i = autoScroll.getAutoscrollInsets(); 663 Dimension size = component.getSize(); 664 665 if (size.width != outer.width || size.height != outer.height) 666 outer.reshape(0, 0, size.width, size.height); 667 668 if (inner.x != i.left || inner.y != i.top) 669 inner.setLocation(i.left, i.top); 670 671 int newWidth = size.width - (i.left + i.right); 672 int newHeight = size.height - (i.top + i.bottom); 673 674 if (newWidth != inner.width || newHeight != inner.height) 675 inner.setSize(newWidth, newHeight); 676 677 } 678 679 684 685 protected synchronized void updateLocation(Point newLocn) { 686 prev = locn; 687 locn = newLocn; 688 689 if (Math.abs(locn.x - prev.x) > hysteresis || 690 Math.abs(locn.y - prev.y) > hysteresis) { 691 if (timer.isRunning()) timer.stop(); 692 } else { 693 if (!timer.isRunning()) timer.start(); 694 } 695 } 696 697 700 701 protected void stop() { timer.stop(); } 702 703 708 709 public synchronized void actionPerformed(ActionEvent e) { 710 updateRegion(); 711 712 if (outer.contains(locn) && !inner.contains(locn)) 713 autoScroll.autoscroll(locn); 714 } 715 716 719 720 private Component component; 721 private Autoscroll autoScroll; 722 723 private Timer timer; 724 725 private Point locn; 726 private Point prev; 727 728 private Rectangle outer = new Rectangle (); 729 private Rectangle inner = new Rectangle (); 730 731 private int hysteresis = 10; 732 } 733 734 735 736 742 743 protected DropTargetAutoScroller createDropTargetAutoScroller(Component c, Point p) { 744 return new DropTargetAutoScroller(c, p); 745 } 746 747 752 753 protected void initializeAutoscrolling(Point p) { 754 if (component == null || !(component instanceof Autoscroll )) return; 755 756 autoScroller = createDropTargetAutoScroller(component, p); 757 } 758 759 764 765 protected void updateAutoscroll(Point dragCursorLocn) { 766 if (autoScroller != null) autoScroller.updateLocation(dragCursorLocn); 767 } 768 769 772 773 protected void clearAutoscroll() { 774 if (autoScroller != null) { 775 autoScroller.stop(); 776 autoScroller = null; 777 } 778 } 779 780 785 private DropTargetContext dropTargetContext = createDropTargetContext(); 786 787 792 private Component component; 793 794 797 private transient ComponentPeer componentPeer; 798 799 802 private transient ComponentPeer nativePeer; 803 804 805 812 int actions = DnDConstants.ACTION_COPY_OR_MOVE; 813 814 819 boolean active = true; 820 821 824 825 private transient DropTargetAutoScroller autoScroller; 826 827 830 831 private transient DropTargetListener dtListener; 832 833 836 837 private transient FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap(); 838 } 839 | Popular Tags |