List of usage examples for java.awt.dnd DropTargetDropEvent getTransferable
public Transferable getTransferable()
From source file:com.declarativa.interprolog.gui.ListenerWindow.java
void handlePrologInputDnD(DropTargetDropEvent dtde) { //System.out.println("drop:"+dtde); try {/* w w w .j a v a2 s.c om*/ Transferable transferable = dtde.getTransferable(); /* DataFlavor[] flavors = transferable.getTransferDataFlavors(); for (int f=0;f<flavors.length;f++) System.out.println("Flavor:"+flavors[f]);*/ int action = dtde.getDropAction(); if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { if (engine.isIdle()) { dtde.acceptDrop(action); final java.util.List files = (java.util.List) transferable .getTransferData(DataFlavor.javaFileListFlavor); dtde.getDropTargetContext().dropComplete(true); boolean allPs = true; for (int f = 0; f < files.size(); f++) { String filename = ((File) files.get(f)).getName(); int dot = filename.lastIndexOf('.'); if (!filename.endsWith(".P")) { allPs = false; break; } } if (!allPs) { errorMessage("All dragged files must be Prolog source files (with a .P extension)"); } else { prologOutput.append("\nReconsulting " + ((files.size() > 1 ? files.size() + " files...\n" : files.size() + " file...\n"))); Runnable r = new Runnable() { public void run() { boolean crashed = false; Toolkit.getDefaultToolkit().sync(); for (int f = 0; f < files.size() && !crashed; f++) { File file = (File) files.get(f); if (!processDraggedFile(file)) { crashed = true; } } if (crashed) { prologOutput.append("...terminated with errors.\n"); } else { prologOutput.append("...done.\n"); } } }; SwingUtilities.invokeLater(r); } } else { dtde.rejectDrop(); errorMessage("You can not consult files while Prolog is working"); } } else { dtde.rejectDrop(); } } catch (Exception e) { throw new IPException("Problem dropping:" + e); } }
From source file:javazoom.jlgui.player.amp.Player.java
/** * DnD : Drop implementation./*from w ww .j ava 2 s . co m*/ * Adds all dropped files to the playlist. */ public void drop(DropTargetDropEvent e) { // Check DataFlavor DataFlavor[] dfs = e.getCurrentDataFlavors(); DataFlavor tdf = null; for (int i = 0; i < dfs.length; i++) { if (DataFlavor.javaFileListFlavor.equals(dfs[i])) { tdf = dfs[i]; break; } } // Is file list ? if (tdf != null) { // Accept COPY DnD only. if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) { e.acceptDrop(DnDConstants.ACTION_COPY); } else return; try { Transferable t = e.getTransferable(); Object data = t.getTransferData(tdf); // How many files ? if (data instanceof java.util.List) { java.util.List al = (java.util.List) data; // Read the first File. if (al.size() > 0) { File file = null; // Stops the player if needed. if ((playerState == PLAY) || (playerState == PAUSE)) { theSoundPlayer.stop(); playerState = STOP; } // Clean the playlist. playlist.removeAllItems(); // Add all dropped files to playlist. ListIterator li = al.listIterator(); while (li.hasNext()) { file = (File) li.next(); PlaylistItem pli = null; if (file != null) { pli = new PlaylistItem(file.getName(), file.getAbsolutePath(), -1, true); if (pli != null) playlist.appendItem(pli); } } // Start the playlist from the top. playlist.nextCursor(); fileList.initPlayList(); this.setCurrentSong(playlist.getCursor()); } } else { log.info("Unknown dropped objects"); } } catch (IOException ioe) { log.info("Drop error", ioe); e.dropComplete(false); return; } catch (UnsupportedFlavorException ufe) { log.info("Drop error", ufe); e.dropComplete(false); return; } catch (Exception ex) { log.info("Drop error", ex); e.dropComplete(false); return; } e.dropComplete(true); } }
From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java
@Override public void drop(DropTargetDropEvent dtde) { Transferable t = dtde.getTransferable(); List<Village> villages = new LinkedList<Village>(); if (dtde.getDropTargetContext().getComponent() == jSourcesTable || dtde.getDropTargetContext().getComponent() == jVictimTable) { if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor)) { //village dnd dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); try { villages = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor); } catch (Exception ex) { }// w w w. j av a 2s . c o m } else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) { //string dnd try { villages = PluginManager.getSingleton() .executeVillageParser((String) t.getTransferData(DataFlavor.stringFlavor)); } catch (Exception e) { } } else { dtde.rejectDrop(); return; } } else { dtde.rejectDrop(); return; } MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor()); if (!villages.isEmpty()) { if (jideTabbedPane1.getSelectedIndex() == 0) { fireAddSourcesEvent(villages); } else if (jideTabbedPane1.getSelectedIndex() == 1) { fireAddTargetsEvent(villages); } } }
From source file:org.apache.jmeter.gui.MainFrame.java
/** * Handler of Top level Dnd//ww w . j a v a2 s . c o m */ @Override public void drop(DropTargetDropEvent dtde) { try { Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (DataFlavor flavor : flavors) { // Check for file lists specifically if (flavor.isFlavorJavaFileListType()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); try { openJmxFilesFromDragAndDrop(tr); } finally { dtde.dropComplete(true); } return; } } } catch (UnsupportedFlavorException | IOException e) { log.warn("Dnd failed", e); } }
From source file:org.kuali.test.ui.dnd.RepositoryDropTargetAdapter.java
@Override public void drop(DropTargetDropEvent dtde) { boolean success = false; try {// www .j a va2 s .c o m if (canDrop(dtde)) { Transferable t = dtde.getTransferable(); Object data = t.getTransferData(t.getTransferDataFlavors()[0]); DefaultMutableTreeNode dropNode = getNodeAtPoint(dtde.getLocation()); if ((dropNode != null) && (data != null) && (data instanceof RepositoryTransferData)) { repositoryTree.handleDataDrop(t.getTransferDataFlavors()[0], (RepositoryTransferData) data, dropNode); success = true; dtde.dropComplete(true); } } } catch (Exception e) { LOG.error(e.toString(), e); } if (!success) { dtde.rejectDrop(); } }
From source file:org.pmedv.blackboard.EditorUtils.java
/** * Configures the drop target for new symbols to be dragged from the symbol * table to the editor./*from w ww. jav a 2 s. com*/ */ public static DropTarget configureDropTarget(final BoardEditor editor, final int raster) { DropTarget dt = new DropTarget(editor, new DropTargetAdapter() { final DataFlavor flavors[] = { DataFlavor.imageFlavor }; @Override public void drop(DropTargetDropEvent e) { int xloc = BoardUtil.snap((int) e.getLocation().getX(), raster); int yloc = BoardUtil.snap((int) e.getLocation().getY(), raster); SymbolBean symbol = null; try { symbol = (SymbolBean) e.getTransferable().getTransferData(flavors[0]); } catch (Exception e1) { e1.printStackTrace(); } Symbol sym = new Symbol(symbol); sym.setXLoc(xloc); sym.setYLoc(yloc); for (Item subItem : sym.getItems()) { if (subItem instanceof Line) { Line line = (Line) subItem; line.setStart(new Point((int) line.getStart().getX() + xloc, (int) line.getStart().getY() + yloc)); line.setEnd( new Point((int) line.getEnd().getX() + +xloc, (int) line.getEnd().getY() + yloc)); line.setOldstart(new Point(line.getStart())); line.setOldEnd(new Point(line.getEnd())); } else { subItem.setXLoc(subItem.getXLoc() + xloc); subItem.setYLoc(subItem.getYLoc() + yloc); subItem.setOldXLoc(subItem.getXLoc()); subItem.setOldYLoc(subItem.getYLoc()); subItem.setOldWidth(subItem.getWidth()); subItem.setOldHeight(subItem.getHeight()); } } editor.getModel().getCurrentLayer().getItems().add(sym); sym.setLayer(editor.getModel().getCurrentLayer().getIndex()); editor.refresh(); e.dropComplete(true); e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); // Switch to select mode after drop ctx.getBean(SetSelectModeCommand.class).execute(null); if (!editor.getUndoManager().addEdit(new AddItemEdit(sym))) { log.info("Could not add edit " + this.getClass()); } editor.setFileState(FileState.DIRTY); ctx.getBean(RedoCommand.class).setEnabled(editor.getUndoManager().canRedo()); ctx.getBean(UndoCommand.class).setEnabled(editor.getUndoManager().canUndo()); } }); return dt; }
From source file:org.rdv.datapanel.AbstractDataPanel.java
@SuppressWarnings("unchecked") public void drop(DropTargetDropEvent e) { try {/*from w w w . j ava2 s. c o m*/ int dropAction = e.getDropAction(); if (dropAction == DnDConstants.ACTION_LINK) { DataFlavor channelListDataFlavor = new ChannelListDataFlavor(); Transferable tr = e.getTransferable(); if (e.isDataFlavorSupported(channelListDataFlavor)) { e.acceptDrop(DnDConstants.ACTION_LINK); e.dropComplete(true); final List<String> channels = (List) tr.getTransferData(channelListDataFlavor); new Thread() { public void run() { for (int i = 0; i < channels.size(); i++) { String channel = channels.get(i); boolean status; if (supportsMultipleChannels()) { status = addChannel(channel); } else { status = setChannel(channel); } if (!status) { // TODO display an error in the UI } } } }.start(); } else { e.rejectDrop(); } } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } }
From source file:org.rdv.datapanel.DigitalTabularDataPanel.java
/** * an overridden method from/* w w w.ja v a2 s . c o m*/ * * @see org.rdv.datapanel.AbstractDataPanel for the * @see DropTargetListener interface */ @SuppressWarnings("unchecked") public void drop(DropTargetDropEvent e) { try { int dropAction = e.getDropAction(); if (dropAction == DnDConstants.ACTION_LINK) { DataFlavor channelListDataFlavor = new ChannelListDataFlavor(); Transferable tr = e.getTransferable(); if (e.isDataFlavorSupported(channelListDataFlavor)) { e.acceptDrop(DnDConstants.ACTION_LINK); e.dropComplete(true); // calculate which table the x coordinate of the mouse // corresponds to double clickX = e.getLocation().getX(); // get the mouse x // coordinate int compWidth = dataComponent.getWidth(); // gets the // width of this // component final int tableNum = (int) (clickX * columnGroupCount / compWidth); final List<String> channels = (List) tr.getTransferData(channelListDataFlavor); new Thread() { public void run() { for (int i = 0; i < channels.size(); i++) { String channel = channels.get(i); boolean status; if (supportsMultipleChannels()) { status = addChannel(channel, tableNum); } else { status = setChannel(channel); } if (!status) { // TODO display an error in the UI } } } }.start(); } else { e.rejectDrop(); } } } catch (IOException ioe) { ioe.printStackTrace(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } }
From source file:pkgrenamer.Main.java
static void onDropFiles(DropTargetDropEvent dtde, FileDropListener onDrop) { try {/* w w w. ja va 2 s.co m*/ Transferable transferable = dtde.getTransferable(); if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_MOVE); java.util.List<?> files = (java.util.List<?>) transferable .getTransferData(DataFlavor.javaFileListFlavor); File[] fa = new File[files.size()]; for (int i = 0; i < fa.length; i++) { fa[i] = (File) files.get(i); } onDrop.onDropFile(fa); dtde.getDropTargetContext().dropComplete(true); } else { if (sNixFileDataFlavor == null) { sNixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String"); } dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); String data = (String) transferable.getTransferData(sNixFileDataFlavor); if (data != null) { ArrayList<File> fs = new ArrayList<>(); for (StringTokenizer st = new StringTokenizer(data, "\r\n"); st.hasMoreTokens();) { String token = st.nextToken().trim(); if (token.startsWith("#") || token.isEmpty()) { continue; } try { fs.add(new File(new URI(token))); } catch (Exception e) { } } onDrop.onDropFile(fs.toArray(new File[0])); dtde.getDropTargetContext().dropComplete(true); } else { dtde.rejectDrop(); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:plugin.notes.gui.NotesTreeNode.java
/** * handles a drop of a java file list//from w w w . j ava 2 s .c o m * * @param dtde * drop target drop even - a java dile list has been dropped on * something that represents this node. * @return returns true if the drop takes place, false if not */ public boolean handleDropJavaFileList(DropTargetDropEvent dtde) { dtde.acceptDrop(dtde.getDropAction()); Transferable t = dtde.getTransferable(); try { List fileList = ((List) t.getTransferData(DataFlavor.javaFileListFlavor)); for (Object aFileList : fileList) { File newFile = (File) aFileList; if (newFile.exists()) { FileUtils.copyFile(newFile, new File(dir.getAbsolutePath() + File.separator + newFile.getName())); } } } catch (Exception e) { Logging.errorPrint(e.getMessage(), e); return false; } return true; }