1 20 package com.puppycrawl.tools.checkstyle.gui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.awt.datatransfer.DataFlavor ; 26 import java.awt.datatransfer.Transferable ; 27 import java.awt.datatransfer.UnsupportedFlavorException ; 28 import java.awt.dnd.DnDConstants ; 29 import java.awt.dnd.DropTarget ; 30 import java.awt.dnd.DropTargetDragEvent ; 31 import java.awt.dnd.DropTargetDropEvent ; 32 import java.awt.dnd.DropTargetEvent ; 33 import java.awt.dnd.DropTargetListener ; 34 import java.awt.event.HierarchyEvent ; 35 import java.awt.event.HierarchyListener ; 36 import java.io.File ; 37 import java.io.IOException ; 38 import java.util.List ; 39 import java.util.TooManyListenersException ; 40 import javax.swing.BorderFactory ; 41 import javax.swing.JComponent ; 42 import javax.swing.border.Border ; 43 44 75 class FileDrop 76 { 77 81 private transient Border normalBorder; 82 private transient DropTargetListener dropListener; 83 84 86 private static final Color DEFAULT_BORDER_COLOR = 87 new Color (0f, 0f, 1f, 0.25f); 88 89 99 FileDrop( 100 final Component c, 101 final Listener listener) 102 throws TooManyListenersException 103 { 104 this( c, BorderFactory.createMatteBorder(2, 2, 2, 2, DEFAULT_BORDER_COLOR), true, listener); 108 } 109 110 111 124 FileDrop( 125 final Component c, 126 final Border dragBorder, 127 final boolean recursive, 128 final Listener listener) 129 throws TooManyListenersException 130 { 131 dropListener = new FileDropTargetListener(c, dragBorder, listener); 132 makeDropTarget(c, recursive); 133 } 134 135 136 private void makeDropTarget(final Component c, boolean recursive) 137 throws TooManyListenersException 138 { 139 final DropTarget dt = new DropTarget (); 141 dt.addDropTargetListener(dropListener); 142 143 c.addHierarchyListener(new HierarchyListener () 146 { 147 public void hierarchyChanged(HierarchyEvent evt) 148 { 149 final Component parent = c.getParent(); 150 if (parent == null) { 151 c.setDropTarget(null); 152 } 153 else { 154 new DropTarget (c, dropListener); 155 } 156 } 157 }); 158 159 if (c.getParent() != null) { 160 new DropTarget (c, dropListener); 161 } 162 163 if (recursive && (c instanceof Container )) { 164 final Container cont = (Container ) c; 165 final Component [] comps = cont.getComponents(); 166 for (int i = 0; i < comps.length; i++) 167 makeDropTarget(comps[i], recursive); 168 } 169 } 170 171 172 173 private boolean isDragOk(final DropTargetDragEvent evt) 174 { 175 boolean ok = false; 176 final DataFlavor [] flavors = evt.getCurrentDataFlavors(); 177 178 int i = 0; 180 while (!ok && (i < flavors.length)) { if (flavors[i].equals(DataFlavor.javaFileListFlavor)) 182 ok = true; 183 i++; 184 } 185 186 return ok; 187 } 188 189 190 200 static void remove(Component c) 201 { 202 remove(c, true); 203 } 204 205 206 215 static void remove(Component c, boolean recursive) 216 { 217 c.setDropTarget(null); 218 if (recursive && (c instanceof Container )) { 219 final Component [] comps = ((Container ) c).getComponents(); 220 for (int i = 0; i < comps.length; i++) { 221 remove(comps[i], recursive); 222 } 223 } 224 } 225 226 227 242 public interface Listener 243 { 244 250 void filesDropped(File [] files); 251 } 252 253 private class FileDropTargetListener implements DropTargetListener 254 { 255 private final Component mC; 256 private final Border mDragBorder; 257 private final Listener mListener; 258 259 public void dragEnter(DropTargetDragEvent evt) 260 { 261 if (isDragOk(evt)) { 262 if (mC instanceof JComponent ) { 263 final JComponent jc = (JComponent ) mC; 264 normalBorder = jc.getBorder(); 265 jc.setBorder(mDragBorder); 266 } 267 evt.acceptDrag(DnDConstants.ACTION_COPY); 268 } 269 else { 270 evt.rejectDrag(); 271 } 272 } 273 274 public void drop(DropTargetDropEvent evt) 275 { 276 try { 277 final Transferable tr = evt.getTransferable(); 278 279 if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { 280 evt.acceptDrop(DnDConstants.ACTION_COPY); 281 282 final List fileList = (List ) tr.getTransferData( 283 DataFlavor.javaFileListFlavor); 284 final File [] files = new File [fileList.size()]; 285 fileList.toArray(files); 286 287 if (mListener != null) { 288 mListener.filesDropped(files); 289 } 290 291 evt.getDropTargetContext().dropComplete(true); 292 } 293 else { 294 evt.rejectDrop(); 295 } 296 } 297 catch (final IOException io) { 298 evt.rejectDrop(); 299 } 300 catch (final UnsupportedFlavorException ufe) { 301 evt.rejectDrop(); 302 } 303 finally { 304 if (mC instanceof JComponent ) { 305 final JComponent jc = (JComponent ) mC; 306 jc.setBorder(normalBorder); 307 } 308 } 309 } 310 311 public void dragExit(DropTargetEvent evt) 312 { 313 if (mC instanceof JComponent ) { 314 final JComponent jc = (JComponent ) mC; 315 jc.setBorder(normalBorder); 316 } 317 } 318 319 public void dropActionChanged(DropTargetDragEvent evt) 320 { 321 if (isDragOk(evt)) { 322 evt.acceptDrag(DnDConstants.ACTION_COPY); 323 } 324 else { 325 evt.rejectDrag(); 326 } 327 } 328 329 public void dragOver(DropTargetDragEvent dtde) 330 { 331 } 332 333 public FileDropTargetListener(Component aC, Border aDragBorder, Listener aListener) 334 { 335 mC = aC; 336 mDragBorder = aDragBorder; 337 mListener = aListener; 338 } 339 } 340 341 } | Popular Tags |