/** * Removes the drag-and-drop hooks from the component and optionally from * the all children. You should call this if you add and remove components * after you've set up the drag-and-drop. * * @param out Optional {@link PrintStream} for logging drag and drop * messages * @param c The component to unregister * @param recursive Recursively unregister components within a container * @return {@code true} if hooks were removed, and {@code false} otherwise * @since 1.0 */ public static boolean remove(PrintStream out, Component c, boolean recursive) { // Make sure we support dnd. if (supportsDnD()) { log(out, "FileDrop: Removing drag-and-drop hooks."); c.setDropTarget(null); if (recursive && (c instanceof java.awt.Container)) { Component[] comps = ((java.awt.Container) c).getComponents(); for (Component comp : comps) remove(out, comp, recursive); return true; } else { return false; } } else { return false; } }
if (supportsDnD()) { this.out = out; this.c = c; makeDropTarget(out, c, recursive); log(out, "FileDrop: Drag and drop is not supported with this JVM");
/** * Sets the {@code FileDrop} as enabled or disabled. * * @param active {@code true} if the {@code FileDrop} should become active, and {@code false} otherwise * @since 1.1 */ public void setActive(boolean active) { if (supportsDnD()) makeDropTargetActive(out, c, recursive, active); }
private JScrollPane createTrafficMatrixScrollPane(JTable table) { JScrollPane pane = new JScrollPane(table); new FileDrop(pane, new LineBorder(Color.BLACK), new FileDrop.Listener() { @Override public void filesDropped(File[] files) { try { Collection<NetPlan> netPlans = new LinkedList<NetPlan>(); for (int i = 0; i < files.length; i++) { if (i == 0) checkTrafficMatrixFileChooser(files[i].getParentFile()); netPlans.add(new NetPlan(files[i])); } loadTrafficMatrices(files, netPlans); } catch (Throwable ex) { ErrorHandling.showErrorDialog(ex.getMessage(), "Error loading matrix/matrices"); } } }); return pane; }
dt.addDropTargetListener(dropListener); } catch (java.util.TooManyListenersException e) { log(out, "FileDrop: Drop will not work due to previous error. Do you have another listener attached?"); for (Component comp : comps) makeDropTarget(out, comp, recursive);
private static File[] createFileArray(BufferedReader bReader, PrintStream out) { try { List list = new java.util.ArrayList(); String line; while ((line = bReader.readLine()) != null) { try { // kde seems to append a 0 char to the end of the reader if (ZERO_CHAR_STRING.equals(line)) continue; File file = new File(new URI(line)); list.add(file); } catch (Exception ex) { log(out, "Error with " + line + ": " + ex.getMessage()); } } return (java.io.File[]) list.toArray(new File[list.size()]); } catch (IOException ex) { log(out, "FileDrop: IOException"); } return new File[0]; } // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
/** * Removes the drag-and-drop hooks from the component and optionally from * the all children. You should call this if you add and remove components * after you've set up the drag-and-drop. This will recursively unregister * all components contained within * <var>c</var> if <var>c</var> is a {@link java.awt.Container}. * * @param c The component to unregister as a drop target * @return {@code true} if hooks were removed, and {@code false} otherwise * @since 1.0 */ public static boolean remove(Component c) { return remove(null, c, true); }
@Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); algorithmSelector.setEnabled(enabled); load.setEnabled(enabled); parametersPanel.setEnabled(enabled); fd.setActive(enabled); }
private void makeDropTargetActive(final PrintStream out, final Component c, boolean recursive, boolean active) { DropTarget dropTarget = c.getDropTarget(); if (dropTarget != null) dropTarget.setActive(active); if (c.getParent() != null) { DropTarget parentDropTarget = c.getDropTarget(); if (parentDropTarget != null) c.getDropTarget().setActive(active); } if (recursive && c instanceof Container) { // Get the container Container cont = (Container) c; // Get its components Component[] comps = cont.getComponents(); // Set its components as listeners also for (Component comp : comps) makeDropTargetActive(out, comp, recursive, active); } }
getDefaults(); fd = new FileDrop(this, new LineBorder(Color.BLACK), true, new FileDrop.Listener() { @Override public void filesDropped(File[] files) {
/** * Determine if the dragged data is a file list. */ private boolean isDragOk(final PrintStream out, final DropTargetDragEvent evt) { boolean ok = false; // Get data flavors being dragged DataFlavor[] flavors = evt.getCurrentDataFlavors(); // See if any of the flavors are a file list int i = 0; while (!ok && i < flavors.length) { // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added. // Is the flavor a file list? final DataFlavor curFlavor = flavors[i]; if (curFlavor.equals(DataFlavor.javaFileListFlavor) || curFlavor.isRepresentationClassReader()) ok = true; // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added. i++; } // end while: through flavors // If logging is enabled, show data flavors if (out != null) { if (flavors.length == 0) log(out, "FileDrop: no data flavors."); for (i = 0; i < flavors.length; i++) log(out, flavors[i].toString()); } return ok; }
new FileDrop(pane, new LineBorder(Color.BLACK), new FileDrop.Listener() { @Override public void filesDropped(File[] files) {
new FileDrop(canvasComponent, new LineBorder(Color.BLACK), files ->