private static void addListener(JComponent comp, DropTargetListener listener) { DropTarget target = comp.getDropTarget(); if (target == null) { comp.setDropTarget(new DropTarget(comp, listener)); } else { try { target.addDropTargetListener(listener); } catch (TooManyListenersException e) { Log.log(e); } } }
private void makeDropTarget( final java.io.PrintStream out, final java.awt.Component c, boolean recursive) { // Make drop target final java.awt.dnd.DropTarget dt = new java.awt.dnd.DropTarget(); try { dt.addDropTargetListener(dropListener); } // end try catch (java.util.TooManyListenersException e) { e.printStackTrace(); log( out, "FileDrop: Drop will not work due to previous error. Do you have another listener attached?"); } // end catch // Listen for hierarchy changes and remove the drop target when the parent gets cleared out. c.addHierarchyListener( new java.awt.event.HierarchyListener() { public void hierarchyChanged(java.awt.event.HierarchyEvent evt) { log(out, "FileDrop: Hierarchy changed."); java.awt.Component parent = c.getParent(); if (parent == null) { c.setDropTarget(null); log(out, "FileDrop: Drop target cleared from component."); } // end if: null parent else { new java.awt.dnd.DropTarget(c, dropListener); log(out, "FileDrop: Drop target added to component."); } // end else: parent not null } // end hierarchyChanged }); // end hierarchy listener if (c.getParent() != null) { new java.awt.dnd.DropTarget(c, dropListener); } if (recursive && (c instanceof java.awt.Container)) { // Get the container java.awt.Container cont = (java.awt.Container) c; // Get it's components java.awt.Component[] comps = cont.getComponents(); // Set it's components as listeners also for (int i = 0; i < comps.length; i++) { makeDropTarget(out, comps[i], recursive); } } // end if: recursively set components as listener } // end dropListener
/** @return */ private static Component getActiveArea() { if (activeArea == null) { activeArea = new BufferedImagePanel() { public void paintComponent(Graphics g) { super.paintComponent(g); if (onlyPDFs == null) { return; } // draw list of file names final Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints()); g2d.setColor(TRANSLUCENT_GRAY); g2d.setFont(FONT); int stringY = (int) ((activeArea.getHeight() - onlyPDFs.size() * 40) / 2.0); for (File f : onlyPDFs) { g2d.drawString(f.getName(), 50, stringY); stringY += 50; } } }; activeArea.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.LIGHT_GRAY)); activeArea.setBackground(Color.DARK_GRAY); fileTransferHandler = new FileTransferHandler(); activeArea.setTransferHandler(fileTransferHandler); try { dropTarget = new DropTarget(); dropTarget.setComponent(activeArea); dropTarget.addDropTargetListener(getDropTargetAdapter()); } catch (TooManyListenersException e) { e.printStackTrace(); } pdfLogo = ImageCache.loadBufferedImage(PaperToolkit.getDataFile("icons/pdfIcon.png")); } return activeArea; }
public DragDropTest_ModeMemoryStream() throws Exception { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(0, 0, 400, 300); this.setVisible(true); this.setTitle("Example for MODE_MEMORY_STREAM"); JLabel txt = new JLabel("Drop here"); this.getContentPane().add(txt); txt.setVisible(true); JButton button = new JButton("Paste from clipboard"); this.getContentPane().add(button, "South"); DropTarget dropTarget = new DropTarget(); dropTarget.setComponent(this); dropTarget.addDropTargetListener( new DropTargetAdapter() { @Override public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde) { boolean dropFiles = dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor); dropFiles |= OutlookDD.isDataAvail(); // true, if this is a D&D operation from Outlook System.out.println("enter: file list avail=" + dropFiles); super.dragEnter(dtde); }; @Override public void dragOver(java.awt.dnd.DropTargetDragEvent dtde) { boolean dropFiles = dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor); dropFiles |= OutlookDD.isDataAvail(); // true, if this is a D&D operation from Outlook System.out.println("over: file list avail=" + dropFiles); int action = dtde.getDropAction(); if (!dropFiles) action = DnDConstants.ACTION_NONE; // Un-comment this line, if you do not want to remove the Email from Outlook: // if (action == DnDConstants.ACTION_MOVE) action = DnDConstants.ACTION_COPY; dtde.acceptDrag(action); super.dragOver(dtde); }; public void drop(DropTargetDropEvent dtde) { try { int action = dtde.getDropAction(); // Un-comment this line, if you do not want to remove the Email from Outlook: // if (action == DnDConstants.ACTION_MOVE) action = DnDConstants.ACTION_COPY; dtde.acceptDrop(action); Transferable t = dtde.getTransferable(); if (OutlookDD.isDataAvail()) { // true, if this is a D&D operation from Outlook try { OutlookData outlData = OutlookDD.getData(); handleOutlookData(outlData); } finally { OutlookDD.release(); } // // Test: other heavyweight components do not disturb the D&D // functionality // SwingUtilities.invokeLater(new Runnable() { // public void run() { // for (int i = 0; i < 3; i++) { // JDialog dlg = new JDialog(); // dlg.setBounds(10,10, 300, 300); // dlg.setTitle("Dialog"); // JTextField txt = new JTextField(); // dlg.add(txt); // dlg.setVisible(true); // } // } // }); } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { List<?> list = (List<?>) t.getTransferData(DataFlavor.javaFileListFlavor); for (Object o : list) { System.out.println("file=" + o); } } } catch (Exception e) { e.printStackTrace(); } finally { dtde.dropComplete(true); } } }); // On-click-handler for button "Paste..." button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { // First check for Outlook clipboard data if (OutlookDD.isClipboardDataAvail()) { try { OutlookData outlData = OutlookDD.getClipboardData(); handleOutlookData(outlData); } catch (Exception e) { e.printStackTrace(); } finally { OutlookDD.release(); } } // Handle other clipboard formats using the // AWT clipboard functions. else { String text = getTextFromSystemClipboard(); System.out.println("text from clipboard=" + text); } } }); }
public GraphDesktopController( GraphDesktopListener listener, ISession session, GraphPlugin plugin, ModeManager modeManager, boolean showDndDesktopImageAtStartup) { _listener = listener; _session = session; _plugin = plugin; _graphPluginResources = new GraphPluginResources(_plugin); ImageIcon startUpImage = null; if (showDndDesktopImageAtStartup) { startUpImage = _graphPluginResources.getIcon(GraphPluginResources.IKeys.DND); } _desktopPane = new GraphDesktopPane(_session.getApplication(), startUpImage); _desktopPane.setBackground(Color.white); _modeManager = modeManager; DropTarget dt = new DropTarget(); try { dt.addDropTargetListener( new DropTargetAdapter() { public void drop(DropTargetDropEvent dtde) { onTablesDroped(dtde); } }); } catch (TooManyListenersException e) { throw new RuntimeException(e); } _desktopPane.setDropTarget(dt); _desktopPane.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { onMouseClicked(e); } public void mousePressed(MouseEvent e) { onMousePressed(e); } public void mouseReleased(MouseEvent e) { onMouseReleased(e); } }); _desktopPane.addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { onMouseDragged(e); } }); createPopUp(); }