Search in sources :

Example 96 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class FileDropListener method drop.

@Override
public void drop(DropTargetDropEvent e) {
    try {
        if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
            Transferable t = e.getTransferable();
            List<?> list = (List<?>) t.getTransferData(DataFlavor.javaFileListFlavor);
            for (Object o : list) {
                if (o instanceof File) {
                    File f = (File) o;
                    System.out.println(f.getAbsolutePath());
                }
            }
            e.dropComplete(true);
        } else {
            e.rejectDrop();
        }
    } catch (UnsupportedFlavorException | IOException ex) {
        e.rejectDrop();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) List(java.util.List) IOException(java.io.IOException) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 97 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class TreeTransferHandler method createTransferable.

@Override
protected Transferable createTransferable(JComponent c) {
    c.getRootPane().getGlassPane().setVisible(true);
    JTable table = (JTable) c;
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    for (int i : table.getSelectedRows()) {
        indices.add(i);
    }
    return new Transferable() {

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] { FLAVOR };
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return Objects.equals(FLAVOR, flavor);
        }

        @SuppressWarnings("JdkObsolete")
        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
            if (isDataFlavorSupported(flavor)) {
                return indices.stream().map(model.getDataVector()::get).collect(Collectors.toList());
            } else {
                throw new UnsupportedFlavorException(flavor);
            }
        }
    };
}
Also used : DefaultTableModel(javax.swing.table.DefaultTableModel) Transferable(java.awt.datatransfer.Transferable) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 98 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class TableRowTransferHandler method importData.

@Override
public boolean importData(TransferHandler.TransferSupport info) {
    TransferHandler.DropLocation tdl = info.getDropLocation();
    if (!(tdl instanceof JTable.DropLocation)) {
        return false;
    }
    JTable.DropLocation dl = (JTable.DropLocation) tdl;
    JTable target = (JTable) info.getComponent();
    DefaultTableModel model = (DefaultTableModel) target.getModel();
    // boolean insert = dl.isInsert();
    int max = model.getRowCount();
    int index = dl.getRow();
    // index = index < 0 ? max : index; // If it is out of range, it is appended to the end
    // index = Math.min(index, max);
    index = index >= 0 && index < max ? index : max;
    addIndex = index;
    // target.setCursor(Cursor.getDefaultCursor());
    try {
        List<?> values = (List<?>) info.getTransferable().getTransferData(FLAVOR);
        if (Objects.equals(source, target)) {
            addCount = values.size();
        }
        Object[] type = new Object[0];
        for (Object o : values) {
            int row = index++;
            // model.insertRow(row, (Vector<?>) o);
            model.insertRow(row, ((List<?>) o).toArray(type));
            target.getSelectionModel().addSelectionInterval(row, row);
        }
        return true;
    } catch (UnsupportedFlavorException | IOException ex) {
        return false;
    }
}
Also used : DefaultTableModel(javax.swing.table.DefaultTableModel) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 99 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project java-swing-tips by aterai.

the class BasicTransferable method createInputStream.

private InputStream createInputStream(DataFlavor flavor, String data) throws IOException, UnsupportedFlavorException {
    String s = getTextCharset(flavor);
    String cs = Optional.ofNullable(s).orElseThrow(() -> new UnsupportedFlavorException(flavor));
    return new ByteArrayInputStream(data.getBytes(cs));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 100 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project jmeter by apache.

the class HeaderPanel method addFromClipboard.

/**
 * Add values from the clipboard.
 * The clipboard is first split into lines, and the lines are then split on ':' or '\t'
 * to produce the header name and value.
 * Lines without a ':' are tested with '\t' and ignored if not found.
 */
protected void addFromClipboard() {
    GuiUtils.stopTableEditing(this.headerTable);
    int rowCount = headerTable.getRowCount();
    try {
        String clipboardContent = GuiUtils.getPastedText();
        if (clipboardContent == null) {
            return;
        }
        // $NON-NLS-1$
        String[] clipboardLines = clipboardContent.split(CLIPBOARD_LINE_DELIMITER);
        for (String clipboardLine : clipboardLines) {
            // $NON-NLS-1$
            int index = clipboardLine.indexOf(CLIPBOARD_COLON_DELIMITER);
            if (index < 0) {
                // when pasting from another header panel the values are separated with '\t'
                index = clipboardLine.indexOf(CLIPBOARD_TAB_DELIMITER);
            }
            if (index > 0) {
                Header header = new Header(clipboardLine.substring(0, index).trim(), clipboardLine.substring(index + 1).trim());
                headerManager.add(header);
            }
        }
        tableModel.fireTableDataChanged();
        if (headerTable.getRowCount() > rowCount) {
            // Highlight (select) the appropriate rows.
            int rowToSelect = tableModel.getRowCount() - 1;
            headerTable.setRowSelectionInterval(rowCount, rowToSelect);
        }
        checkButtonsStatus();
    } catch (IOException ioe) {
        JOptionPane.showMessageDialog(this, "Could not add read headers from clipboard:\n" + ioe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    } catch (UnsupportedFlavorException ufe) {
        JOptionPane.showMessageDialog(this, "Could not add retrieved " + DataFlavor.stringFlavor.getHumanPresentableName() + " from clipboard" + ufe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)106 IOException (java.io.IOException)92 Transferable (java.awt.datatransfer.Transferable)55 List (java.util.List)32 DataFlavor (java.awt.datatransfer.DataFlavor)26 File (java.io.File)25 Point (java.awt.Point)18 ArrayList (java.util.ArrayList)15 Clipboard (java.awt.datatransfer.Clipboard)14 JTree (javax.swing.JTree)9 TreePath (javax.swing.tree.TreePath)9 JComponent (javax.swing.JComponent)7 TransferHandler (javax.swing.TransferHandler)7 DropTargetContext (java.awt.dnd.DropTargetContext)6 URL (java.net.URL)5 DefaultTableModel (javax.swing.table.DefaultTableModel)5 Image (java.awt.Image)4 ActionEvent (java.awt.event.ActionEvent)4 MouseEvent (java.awt.event.MouseEvent)4 NodeInterface (com.sldeditor.common.NodeInterface)3