1 7 package javax.swing.plaf.basic; 8 9 import java.awt.*; 10 import java.awt.dnd.*; 11 import javax.swing.*; 12 import javax.swing.plaf.UIResource ; 13 14 import java.awt.event.*; 15 import javax.swing.Timer ; 16 17 45 class BasicDropTargetListener implements DropTargetListener, UIResource , ActionListener { 46 47 53 protected BasicDropTargetListener() { 54 } 55 56 57 61 protected void saveComponentState(JComponent c) { 62 } 63 64 68 protected void restoreComponentState(JComponent c) { 69 } 70 71 75 protected void restoreComponentStateForDrop(JComponent c) { 76 } 77 78 82 protected void updateInsertionLocation(JComponent c, Point p) { 83 } 84 85 private static final int AUTOSCROLL_INSET = 10; 86 87 99 void updateAutoscrollRegion(JComponent c) { 100 Rectangle visible = c.getVisibleRect(); 102 outer.reshape(visible.x, visible.y, visible.width, visible.height); 103 104 Insets i = new Insets(0, 0, 0, 0); 106 if (c instanceof Scrollable) { 107 int minSize = 2 * AUTOSCROLL_INSET; 108 109 if (visible.width >= minSize) { 110 i.left = i.right = AUTOSCROLL_INSET; 111 } 112 113 if (visible.height >= minSize) { 114 i.top = i.bottom = AUTOSCROLL_INSET; 115 } 116 } 117 118 inner.reshape(visible.x + i.left, 120 visible.y + i.top, 121 visible.width - (i.left + i.right), 122 visible.height - (i.top + i.bottom)); 123 } 124 125 131 void autoscroll(JComponent c, Point pos) { 132 if (c instanceof Scrollable) { 133 Scrollable s = (Scrollable) c; 134 if (pos.y < inner.y) { 135 int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, -1); 137 Rectangle r = new Rectangle(inner.x, outer.y - dy, inner.width, dy); 138 c.scrollRectToVisible(r); 139 } else if (pos.y > (inner.y + inner.height)) { 140 int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, 1); 142 Rectangle r = new Rectangle(inner.x, outer.y + outer.height, inner.width, dy); 143 c.scrollRectToVisible(r); 144 } 145 146 if (pos.x < inner.x) { 147 int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, -1); 149 Rectangle r = new Rectangle(outer.x - dx, inner.y, dx, inner.height); 150 c.scrollRectToVisible(r); 151 } else if (pos.x > (inner.x + inner.width)) { 152 int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, 1); 154 Rectangle r = new Rectangle(outer.x + outer.width, inner.y, dx, inner.height); 155 c.scrollRectToVisible(r); 156 } 157 } 158 } 159 160 164 private void initPropertiesIfNecessary() { 165 if (timer == null) { 166 Toolkit t = Toolkit.getDefaultToolkit(); 167 Integer initial = new Integer (100); 168 Integer interval = new Integer (100); 169 170 try { 171 initial = (Integer )t.getDesktopProperty( 172 "DnD.Autoscroll.initialDelay"); 173 } catch (Exception e) { 174 } 176 try { 177 interval = (Integer )t.getDesktopProperty( 178 "DnD.Autoscroll.interval"); 179 } catch (Exception e) { 180 } 182 timer = new Timer (interval.intValue(), this); 183 184 timer.setCoalesce(true); 185 timer.setInitialDelay(initial.intValue()); 186 187 try { 188 hysteresis = ((Integer )t.getDesktopProperty( 189 "DnD.Autoscroll.cursorHysteresis")).intValue(); 190 } catch (Exception e) { 191 } 193 } 194 } 195 196 static JComponent getComponent(DropTargetEvent e) { 197 DropTargetContext context = e.getDropTargetContext(); 198 return (JComponent) context.getComponent(); 199 } 200 201 203 209 public synchronized void actionPerformed(ActionEvent e) { 210 updateAutoscrollRegion(component); 211 if (outer.contains(lastPosition) && !inner.contains(lastPosition)) { 212 autoscroll(component, lastPosition); 213 } 214 } 215 216 218 public void dragEnter(DropTargetDragEvent e) { 219 component = getComponent(e); 220 TransferHandler th = component.getTransferHandler(); 221 canImport = th.canImport(component, e.getCurrentDataFlavors()); 222 if (canImport) { 223 saveComponentState(component); 224 lastPosition = e.getLocation(); 225 updateAutoscrollRegion(component); 226 initPropertiesIfNecessary(); 227 } 228 } 229 230 public void dragOver(DropTargetDragEvent e) { 231 if (canImport) { 232 Point p = e.getLocation(); 233 updateInsertionLocation(component, p); 234 235 236 synchronized(this) { 238 if (Math.abs(p.x - lastPosition.x) > hysteresis || 239 Math.abs(p.y - lastPosition.y) > hysteresis) { 240 if (timer.isRunning()) timer.stop(); 242 } else { 243 if (!timer.isRunning()) timer.start(); 244 } 245 lastPosition = p; 246 } 247 } 248 } 249 250 public void dragExit(DropTargetEvent e) { 251 if (canImport) { 252 restoreComponentState(component); 253 } 254 cleanup(); 255 } 256 257 public void drop(DropTargetDropEvent e) { 258 if (canImport) { 259 restoreComponentStateForDrop(component); 260 } 261 cleanup(); 262 } 263 264 public void dropActionChanged(DropTargetDragEvent e) { 265 } 266 267 271 private void cleanup() { 272 if (timer != null) { 273 timer.stop(); 274 } 275 component = null; 276 lastPosition = null; 277 } 278 279 281 private Timer timer; 282 private Point lastPosition; 283 private Rectangle outer = new Rectangle(); 284 private Rectangle inner = new Rectangle(); 285 private int hysteresis = 10; 286 private boolean canImport; 287 288 292 private JComponent component; 293 294 } 295 296 | Popular Tags |