List of usage examples for java.awt.dnd DropTargetDropEvent acceptDrop
public void acceptDrop(int dropAction)
From source file:EditorDropTarget.java
public void drop(DropTargetDropEvent dtde) { DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction())); // Check the drop action if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { // Accept the drop and get the transfer data dtde.acceptDrop(dtde.getDropAction()); Transferable transferable = dtde.getTransferable(); try {//ww w . java 2 s . c o m boolean result = dropFile(transferable); dtde.dropComplete(result); DnDUtils.debugPrintln("Drop completed, success: " + result); } catch (Exception e) { DnDUtils.debugPrintln("Exception while handling drop " + e); dtde.dropComplete(false); } } else { DnDUtils.debugPrintln("Drop target rejected drop"); dtde.rejectDrop(); } }
From source file:PanelDropTarget.java
public void drop(DropTargetDropEvent dtde) { DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction())); // Check the drop action if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { // Accept the drop and get the transfer data dtde.acceptDrop(dtde.getDropAction()); Transferable transferable = dtde.getTransferable(); try {//from w w w . j a v a2 s .c om boolean result = dropComponent(transferable); dtde.dropComplete(result); DnDUtils.debugPrintln("Drop completed, success: " + result); } catch (Exception e) { DnDUtils.debugPrintln("Exception while handling drop " + e); dtde.dropComplete(false); } } else { DnDUtils.debugPrintln("Drop target rejected drop"); dtde.rejectDrop(); } }
From source file:ColorSink.java
/** This method is invoked when the user drops something on us */ public void drop(DropTargetDropEvent e) { this.setBorder(null); // Restore the default border Transferable t = e.getTransferable(); // Get the data that was dropped // Check for types of data that we support if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) { // If it was a color, accept it, and use it as the background color e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); try {// w ww . j a va2 s .com Color c = (Color) t.getTransferData(TransferableColor.colorFlavor); this.setBackground(c); e.dropComplete(true); } catch (Exception ex) { e.dropComplete(false); } } else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { // If it was a file list, accept it, read the first file in the list // and display the file contents e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); try { List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor); File f = (File) files.get(0); BufferedReader in = new BufferedReader(new FileReader(f)); String s; this.setText(""); while ((s = in.readLine()) != null) this.append(s); e.dropComplete(true); } catch (Exception ex) { e.dropComplete(false); } } else { // If it wasn't a color or a file list, reject it. e.rejectDrop(); return; } }
From source file:net.rptools.tokentool.ui.TokenCompositionPanel.java
public void drop(DropTargetDropEvent dtde) { Transferable transferable = dtde.getTransferable(); dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); try {//from w w w . j a va 2s . c om if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { List<URL> urls = new FileTransferableHandler().getTransferObject(transferable); for (URL url : urls) { String baseName = java.net.URLDecoder.decode(FilenameUtils.getBaseName(url.getFile()), "UTF-8"); TokenTool.getFrame().getControlPanel().setNamePrefixField(baseName); } } } catch (UnsupportedFlavorException | IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Image image = new ImageTransferableHandler().getTransferObject(transferable); if (!(image instanceof BufferedImage)) { // Convert to buffered image image = ImageUtil.createCompatibleImage(image); } setToken((BufferedImage) image); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } fireCompositionChanged(); }
From source file:DropTest.java
public void drop(DropTargetDropEvent dtde) { try {/*w ww. java2 s . c o m*/ // Ok, get the dropped object and try to figure out what it is Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { System.out.println("Possible flavor: " + flavors[i].getMimeType()); // Check for file lists specifically if (flavors[i].isFlavorJavaFileListType()) { // Great! Accept copy drops... dtde.acceptDrop(DnDConstants.ACTION_COPY); ta.setText("Successful file list drop.\n\n"); // And add the list of file names to our text area java.util.List list = (java.util.List) tr.getTransferData(flavors[i]); for (int j = 0; j < list.size(); j++) { ta.append(list.get(j) + "\n"); } // If we made it this far, everything worked. dtde.dropComplete(true); return; } } // Hmm, the user must not have dropped a file list System.out.println("Drop failed: " + dtde); dtde.rejectDrop(); } catch (Exception e) { e.printStackTrace(); dtde.rejectDrop(); } }
From source file:HostPanel.java
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor. *//*w ww . j av a2 s.co m*/ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { dropTargetScroll = new javax.swing.JScrollPane(); dropTargetArea = new javax.swing.JTextArea(); dropTargetTitle = new javax.swing.JLabel(); globalProgress = new javax.swing.JProgressBar(); currentProgress = new javax.swing.JProgressBar(); transferButton = new javax.swing.JButton(); dropTargetValue = new javax.swing.JLabel(); dropTargetClear = new javax.swing.JLabel(); statusDisplay1 = new javax.swing.JLabel(); statusDisplay2 = new javax.swing.JLabel(); setFocusable(false); dropTargetScroll.setFocusable(false); dropTargetArea.setDropTarget(new DropTarget() { @Override public synchronized void drop(DropTargetDropEvent evt) { if (transfer == null || transfer.isDone()) { evt.acceptDrop(DnDConstants.ACTION_LINK); Transferable transferable = evt.getTransferable(); try { if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { ProcessDropTarget pdt = new ProcessDropTarget( (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor)); pdt.execute(); } } catch (UnsupportedFlavorException | IOException ex) { Logger.getLogger(HostPanel.class.getName()).log(Level.SEVERE, null, ex); } } } }); dropTargetArea.setEditable(false); dropTargetArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background")); dropTargetArea.setFocusable(false); dropTargetScroll.setViewportView(dropTargetArea); dropTargetTitle.setText("Drag files to host onto box below"); dropTargetTitle.setFocusable(false); globalProgress.setFocusable(false); globalProgress.setRequestFocusEnabled(false); globalProgress.setStringPainted(true); currentProgress.setFocusable(false); currentProgress.setRequestFocusEnabled(false); currentProgress.setStringPainted(true); transferButton.setText("Host"); transferButton.setActionCommand("host"); transferButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { transferButtonActionPerformed(evt); } }); dropTargetValue.setText("Files to Share: 0"); dropTargetValue.setFocusable(false); dropTargetClear.setText("Clear"); dropTargetClear.setFocusable(false); dropTargetClear.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(java.awt.event.MouseEvent evt) { dropTargetClearMouseReleased(evt); } }); statusDisplay1.setText(""); statusDisplay2.setText(""); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dropTargetScroll) .addGroup(layout.createSequentialGroup().addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(globalProgress, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE) .addComponent(currentProgress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(statusDisplay1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE) .addComponent(statusDisplay2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addComponent(transferButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(dropTargetTitle) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(dropTargetValue, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(dropTargetClear))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dropTargetTitle).addComponent(dropTargetValue) .addComponent(dropTargetClear)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(dropTargetScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(currentProgress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(statusDisplay1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(globalProgress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(statusDisplay2))) .addComponent(transferButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); }
From source file:DropTest2.java
public void drop(DropTargetDropEvent dtde) { try {/*ww w. j a v a 2 s.c o m*/ // Ok, get the dropped object and try to figure out what it is Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { System.out.println("Possible flavor: " + flavors[i].getMimeType()); // Check for file lists specifically if (flavors[i].isFlavorJavaFileListType()) { // Great! Accept copy drops... dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); ta.setText("Successful file list drop.\n\n"); // And add the list of file names to our text area java.util.List list = (java.util.List) tr.getTransferData(flavors[i]); for (int j = 0; j < list.size(); j++) { ta.append(list.get(j) + "\n"); } // If we made it this far, everything worked. dtde.dropComplete(true); return; } // Ok, is it another Java object? else if (flavors[i].isFlavorSerializedObjectType()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); ta.setText("Successful text drop.\n\n"); Object o = tr.getTransferData(flavors[i]); ta.append("Object: " + o); dtde.dropComplete(true); return; } // How about an input stream? else if (flavors[i].isRepresentationClassInputStream()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); ta.setText("Successful text drop.\n\n"); ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])), "from system clipboard"); dtde.dropComplete(true); return; } } // Hmm, the user must not have dropped a file list System.out.println("Drop failed: " + dtde); dtde.rejectDrop(); } catch (Exception e) { e.printStackTrace(); dtde.rejectDrop(); } }
From source file:EditorDropTarget3.java
public void drop(DropTargetDropEvent dtde) { DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction())); // Check the drop action if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) { // Accept the drop and get the transfer data dtde.acceptDrop(dtde.getDropAction()); Transferable transferable = dtde.getTransferable(); try {/* w w w.j a v a 2s . c o m*/ boolean result = false; if (draggingFile) { result = dropFile(transferable); } else { result = dropContent(transferable, dtde); } dtde.dropComplete(result); DnDUtils.debugPrintln("Drop completed, success: " + result); } catch (Exception e) { DnDUtils.debugPrintln("Exception while handling drop " + e); dtde.dropComplete(false); } } else { DnDUtils.debugPrintln("Drop target rejected drop"); dtde.rejectDrop(); } }
From source file:com.ftb2om2.view.MultiplePane.java
private String getDragAndDropPath(DropTargetDropEvent evt) throws IOException, UnsupportedFlavorException { evt.acceptDrop(DnDConstants.ACTION_COPY); List<File> droppedFile = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); return droppedFile.get(0).getAbsolutePath(); }
From source file:com.mirth.connect.client.ui.TemplatePanel.java
public void drop(DropTargetDropEvent dtde) { try {/*from ww w.j av a2 s .co m*/ Transferable tr = dtde.getTransferable(); if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); File file = ((List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)).get(0); String dataType = PlatformUI.MIRTH_FRAME.displayNameToDataType.get(getDataType()); DataTypeClientPlugin dataTypePlugin = LoadedExtensions.getInstance().getDataTypePlugins() .get(dataType); if (dataTypePlugin.isBinary()) { byte[] content = FileUtils.readFileToByteArray(file); // The plugin should decide how to convert the byte array to string pasteBox.setText(dataTypePlugin.getTemplateString(content)); } else { pasteBox.setText(FileUtils.readFileToString(file, UIConstants.CHARSET)); } parent.modified = true; } } catch (Exception e) { dtde.rejectDrop(); } }