Changeset 8443 in josm for trunk/src/org/openstreetmap/josm/gui/FileDrop.java
- Timestamp:
- 2015-06-02T16:40:38+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/FileDrop.java
r8419 r8443 34 34 import org.openstreetmap.josm.Main; 35 35 import org.openstreetmap.josm.actions.OpenFileAction; 36 import org.openstreetmap.josm.gui.FileDrop.TransferableObject;37 36 38 37 /** … … 85 84 86 85 // Default border color 87 private static Color defaultBorderColor = new Color( 0f, 0f, 1f, 0.25f);86 private static Color defaultBorderColor = new Color(0f, 0f, 1f, 0.25f); 88 87 89 88 /** … … 91 90 * @param c The drop target 92 91 */ 93 public FileDrop(final Component c) {92 public FileDrop(final Component c) { 94 93 this( 95 94 c, // Drop target 96 BorderFactory.createMatteBorder( 2, 2, 2, 2, defaultBorderColor), // Drag border95 BorderFactory.createMatteBorder(2, 2, 2, 2, defaultBorderColor), // Drag border 97 96 true, // Recursive 98 new FileDrop.Listener() {97 new FileDrop.Listener() { 99 98 @Override 100 public void filesDropped( File[] files){99 public void filesDropped(File[] files){ 101 100 // start asynchronous loading of files 102 101 OpenFileAction.OpenFileTask task = new OpenFileAction.OpenFileTask(Arrays.asList(files), null); … … 126 125 final Listener listener) { 127 126 128 if ( supportsDnD()) {127 if (supportsDnD()) { 129 128 // Make a drop listener 130 129 dropListener = new DropTargetListener() { 131 130 @Override 132 public void dragEnter( DropTargetDragEvent evt) {131 public void dragEnter(DropTargetDragEvent evt) { 133 132 Main.trace("FileDrop: dragEnter event." ); 134 133 135 134 // Is this an acceptable drag event? 136 if ( isDragOk( evt) ) {135 if (isDragOk(evt) ) { 137 136 // If it's a Swing component, set its border 138 if ( c instanceof JComponent) {137 if (c instanceof JComponent) { 139 138 JComponent jc = (JComponent) c; 140 139 normalBorder = jc.getBorder(); 141 Main.trace("FileDrop: normal border saved." 142 jc.setBorder( dragBorder);143 Main.trace("FileDrop: drag border set." 140 Main.trace("FileDrop: normal border saved."); 141 jc.setBorder(dragBorder); 142 Main.trace("FileDrop: drag border set."); 144 143 } 145 144 146 145 // Acknowledge that it's okay to enter 147 evt.acceptDrag( DnDConstants.ACTION_COPY 148 Main.trace("FileDrop: event accepted." 146 evt.acceptDrag( DnDConstants.ACTION_COPY); 147 Main.trace("FileDrop: event accepted."); 149 148 } else { 150 149 // Reject the drag event 151 150 evt.rejectDrag(); 152 Main.trace("FileDrop: event rejected." 151 Main.trace("FileDrop: event rejected."); 153 152 } 154 153 } 155 154 156 155 @Override 157 public void dragOver( DropTargetDragEvent evt) {156 public void dragOver(DropTargetDragEvent evt) { 158 157 // This is called continually as long as the mouse is over the drag target. 159 158 } 160 159 161 160 @Override 162 public void drop( DropTargetDropEvent evt) {163 Main.trace("FileDrop: drop event." 161 public void drop(DropTargetDropEvent evt) { 162 Main.trace("FileDrop: drop event."); 164 163 try { 165 164 // Get whatever was dropped … … 180 179 181 180 // Alert listener to drop. 182 if ( listener != null) {181 if (listener != null) { 183 182 listener.filesDropped(files); 184 183 } … … 214 213 } 215 214 } 216 if (!handled){217 Main.trace("FileDrop: not a file list or reader - abort." 215 if (!handled) { 216 Main.trace("FileDrop: not a file list or reader - abort."); 218 217 evt.rejectDrop(); 219 218 } … … 221 220 } 222 221 } catch (IOException | UnsupportedFlavorException e) { 223 Main.warn("FileDrop: "+e.getClass().getSimpleName()+" - abort:" 222 Main.warn("FileDrop: "+e.getClass().getSimpleName()+" - abort:"); 224 223 Main.error(e); 225 224 try { … … 231 230 } finally { 232 231 // If it's a Swing component, reset its border 233 if ( c instanceof JComponent) {232 if (c instanceof JComponent) { 234 233 JComponent jc = (JComponent) c; 235 jc.setBorder( normalBorder);236 Main.debug("FileDrop: normal border restored." 234 jc.setBorder(normalBorder); 235 Main.debug("FileDrop: normal border restored."); 237 236 } 238 237 } … … 240 239 241 240 @Override 242 public void dragExit( DropTargetEvent evt) {243 Main.debug("FileDrop: dragExit event." 241 public void dragExit(DropTargetEvent evt) { 242 Main.debug("FileDrop: dragExit event."); 244 243 // If it's a Swing component, reset its border 245 if ( c instanceof JComponent) {246 JComponent jc = (JComponent) c;247 jc.setBorder( normalBorder);248 Main.debug("FileDrop: normal border restored." 244 if (c instanceof JComponent) { 245 JComponent jc = (JComponent) c; 246 jc.setBorder(normalBorder); 247 Main.debug("FileDrop: normal border restored."); 249 248 } 250 249 } 251 250 252 251 @Override 253 public void dropActionChanged( DropTargetDragEvent evt) {254 Main.debug("FileDrop: dropActionChanged event." 252 public void dropActionChanged(DropTargetDragEvent evt) { 253 Main.debug("FileDrop: dropActionChanged event."); 255 254 // Is this an acceptable drag event? 256 if ( isDragOk( evt) ) {257 evt.acceptDrag( DnDConstants.ACTION_COPY 258 Main.debug("FileDrop: event accepted." 255 if (isDragOk(evt) ) { 256 evt.acceptDrag( DnDConstants.ACTION_COPY); 257 Main.debug("FileDrop: event accepted."); 259 258 } else { 260 259 evt.rejectDrag(); 261 Main.debug("FileDrop: event rejected." 260 Main.debug("FileDrop: event rejected."); 262 261 } 263 262 } … … 265 264 266 265 // Make the component (and possibly children) drop targets 267 makeDropTarget( c, recursive);266 makeDropTarget(c, recursive); 268 267 } else { 269 Main.info("FileDrop: Drag and drop is not supported with this JVM" 268 Main.info("FileDrop: Drag and drop is not supported with this JVM"); 270 269 } 271 270 } 272 271 273 272 private static synchronized boolean supportsDnD() { 274 if ( supportsDnD == null) {273 if (supportsDnD == null) { 275 274 boolean support = false; 276 275 try { 277 Class.forName( "java.awt.dnd.DnDConstants");276 Class.forName("java.awt.dnd.DnDConstants"); 278 277 support = true; 279 } catch( Exception e) {278 } catch(Exception e) { 280 279 support = false; 281 280 } … … 313 312 // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added. 314 313 315 private void makeDropTarget( final Component c, boolean recursive) {314 private void makeDropTarget(final Component c, boolean recursive) { 316 315 // Make drop target 317 316 final DropTarget dt = new DropTarget(); 318 317 try { 319 dt.addDropTargetListener( dropListener);320 } catch( TooManyListenersException e) {318 dt.addDropTargetListener(dropListener); 319 } catch(TooManyListenersException e) { 321 320 Main.error(e); 322 Main.warn("FileDrop: Drop will not work due to previous error. Do you have another listener attached?" 321 Main.warn("FileDrop: Drop will not work due to previous error. Do you have another listener attached?"); 323 322 } 324 323 325 324 // Listen for hierarchy changes and remove the drop target when the parent gets cleared out. 326 c.addHierarchyListener( 325 c.addHierarchyListener(new HierarchyListener() { 327 326 @Override 328 public void hierarchyChanged( HierarchyEvent evt) {329 Main.trace("FileDrop: Hierarchy changed." 327 public void hierarchyChanged(HierarchyEvent evt) { 328 Main.trace("FileDrop: Hierarchy changed."); 330 329 Component parent = c.getParent(); 331 if ( parent == null) {332 c.setDropTarget( null);333 Main.trace("FileDrop: Drop target cleared from component." 330 if (parent == null) { 331 c.setDropTarget(null); 332 Main.trace("FileDrop: Drop target cleared from component."); 334 333 } else { 335 334 new DropTarget(c, dropListener); 336 Main.trace("FileDrop: Drop target added to component." 335 Main.trace("FileDrop: Drop target added to component."); 337 336 } 338 337 } 339 338 }); 340 if ( c.getParent() != null) {339 if (c.getParent() != null) { 341 340 new DropTarget(c, dropListener); 342 341 } 343 342 344 if ( recursive && (c instanceof Container )) {343 if (recursive && (c instanceof Container)) { 345 344 // Get the container 346 345 Container cont = (Container) c; … … 351 350 // Set it's components as listeners also 352 351 for (Component comp : comps) { 353 makeDropTarget( 352 makeDropTarget(comp, recursive); 354 353 } 355 354 } … … 357 356 358 357 /** Determine if the dragged data is a file list. */ 359 private boolean isDragOk( final DropTargetDragEvent evt) {358 private boolean isDragOk(final DropTargetDragEvent evt) { 360 359 boolean ok = false; 361 360 … … 365 364 // See if any of the flavors are a file list 366 365 int i = 0; 367 while( !ok && i < flavors.length) {366 while(!ok && i < flavors.length) { 368 367 // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added. 369 368 // Is the flavor a file list? 370 369 final DataFlavor curFlavor = flavors[i]; 371 if (curFlavor.equals( DataFlavor.javaFileListFlavor ) ||370 if (curFlavor.equals( DataFlavor.javaFileListFlavor ) || 372 371 curFlavor.isRepresentationClassReader()){ 373 372 ok = true; … … 378 377 379 378 // show data flavors 380 if ( flavors.length == 0) {381 Main.trace("FileDrop: no data flavors." 382 } 383 for ( i = 0; i < flavors.length; i++) {384 Main.trace(flavors[i].toString() 379 if (flavors.length == 0) { 380 Main.trace("FileDrop: no data flavors."); 381 } 382 for (i = 0; i < flavors.length; i++) { 383 Main.trace(flavors[i].toString()); 385 384 } 386 385 … … 398 397 * @return {@code true} if at least one item has been removed, {@code false} otherwise 399 398 */ 400 public static boolean remove( 401 return remove( c, true);399 public static boolean remove(Component c) { 400 return remove(c, true); 402 401 } 403 402 … … 411 410 * @return {@code true} if at least one item has been removed, {@code false} otherwise 412 411 */ 413 public static boolean remove( Component c, boolean recursive) {412 public static boolean remove(Component c, boolean recursive) { 414 413 // Make sure we support dnd. 415 414 if (supportsDnD()) { … … 449 448 * @param files An array of <tt>File</tt>s that were dropped. 450 449 */ 451 public abstract void filesDropped( File[] files);450 public abstract void filesDropped(File[] files); 452 451 } 453 452 … … 512 511 */ 513 512 public static final DataFlavor DATA_FLAVOR = 514 new DataFlavor( FileDrop.TransferableObject.class, MIME_TYPE);513 new DataFlavor(FileDrop.TransferableObject.class, MIME_TYPE); 515 514 516 515 private Fetcher fetcher; … … 528 527 * @param data The data to transfer 529 528 */ 530 public TransferableObject( Object data) {529 public TransferableObject(Object data) { 531 530 this.data = data; 532 this.customFlavor = new DataFlavor( data.getClass(), MIME_TYPE);531 this.customFlavor = new DataFlavor(data.getClass(), MIME_TYPE); 533 532 } 534 533 … … 542 541 * @param fetcher The {@link Fetcher} that will return the data object 543 542 */ 544 public TransferableObject( Fetcher fetcher) {543 public TransferableObject(Fetcher fetcher) { 545 544 this.fetcher = fetcher; 546 545 } … … 558 557 * @see Fetcher 559 558 */ 560 public TransferableObject(Class<?> dataClass, Fetcher fetcher 559 public TransferableObject(Class<?> dataClass, Fetcher fetcher) { 561 560 this.fetcher = fetcher; 562 this.customFlavor = new DataFlavor( dataClass, MIME_TYPE);561 this.customFlavor = new DataFlavor(dataClass, MIME_TYPE); 563 562 } 564 563 … … 587 586 @Override 588 587 public DataFlavor[] getTransferDataFlavors() { 589 if ( customFlavor != null)588 if (customFlavor != null) 590 589 return new DataFlavor[] { 591 590 customFlavor, … … 609 608 */ 610 609 @Override 611 public Object getTransferData( DataFlavor flavor)610 public Object getTransferData(DataFlavor flavor) 612 611 throws UnsupportedFlavorException, IOException { 613 612 // Native object 614 if ( flavor.equals( DATA_FLAVOR ))613 if (flavor.equals(DATA_FLAVOR)) 615 614 return fetcher == null ? data : fetcher.getObject(); 616 615 617 616 // String 618 if ( flavor.equals( DataFlavor.stringFlavor ))617 if (flavor.equals(DataFlavor.stringFlavor)) 619 618 return fetcher == null ? data.toString() : fetcher.getObject().toString(); 620 619 … … 631 630 */ 632 631 @Override 633 public boolean isDataFlavorSupported( DataFlavor flavor) {632 public boolean isDataFlavorSupported(DataFlavor flavor) { 634 633 // Native object 635 if ( flavor.equals( DATA_FLAVOR ))634 if (flavor.equals(DATA_FLAVOR)) 636 635 return true; 637 636 638 637 // String 639 if ( flavor.equals( DataFlavor.stringFlavor ))638 if (flavor.equals( DataFlavor.stringFlavor)) 640 639 return true; 641 640
Note:
See TracChangeset
for help on using the changeset viewer.