Search in sources :

Example 16 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project sldeditor by robward-scisys.

the class TreeTransferHandler method importData.

/*
     * (non-Javadoc)
     * 
     * @see javax.swing.TransferHandler#importData(javax.swing.JComponent, java.awt.datatransfer.Transferable)
     */
@Override
public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof JTree) || (t == null)) {
        return false;
    }
    boolean result = false;
    JTree tree = (JTree) comp;
    NodeInterface destinationTreeNode = (NodeInterface) tree.getLastSelectedPathComponent();
    DataFlavor destDataFlavour = destinationTreeNode.getDataFlavour();
    TransferredData transferredData = null;
    try {
        transferredData = (TransferredData) t.getTransferData(destDataFlavour);
        result = DataFlavourManager.copy(destinationTreeNode, transferredData);
    } catch (UnsupportedFlavorException e) {
        ConsoleManager.getInstance().exception(this, e);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return result;
}
Also used : JTree(javax.swing.JTree) TransferredData(com.sldeditor.datasource.extension.filesystem.dataflavour.TransferredData) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) NodeInterface(com.sldeditor.common.NodeInterface) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 17 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project sldeditor by robward-scisys.

the class TreeTransferHandler method canImport.

/*
     * (non-Javadoc)
     * 
     * @see javax.swing.TransferHandler#canImport(javax.swing.TransferHandler.TransferSupport)
     */
@Override
public boolean canImport(TransferSupport support) {
    if (support == null) {
        return false;
    }
    JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
    TreePath destinationPath = dropLocation.getPath();
    NodeInterface destinationTreeNode = null;
    try {
        destinationTreeNode = (NodeInterface) destinationPath.getLastPathComponent();
    } catch (ClassCastException e) {
        return false;
    }
    Transferable transferable = support.getTransferable();
    DataFlavor destDataFlavour = destinationTreeNode.getDataFlavour();
    TransferredData transferredData = null;
    try {
        transferredData = (TransferredData) transferable.getTransferData(destDataFlavour);
    } catch (UnsupportedFlavorException e) {
        ConsoleManager.getInstance().exception(this, e);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    if (transferredData == null) {
        return false;
    }
    TreePath treePath = transferredData.getTreePath(0);
    if (isSameTreePath(treePath, destinationPath)) {
        return false;
    }
    return DataFlavourManager.isSupported(transferredData.getDataFlavour(0), destDataFlavour);
}
Also used : JTree(javax.swing.JTree) TransferredData(com.sldeditor.datasource.extension.filesystem.dataflavour.TransferredData) TreePath(javax.swing.tree.TreePath) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) NodeInterface(com.sldeditor.common.NodeInterface) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 18 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project sldeditor by robward-scisys.

the class TransferableDataItemTest method testTransferableDataItem.

/**
 * Test method for
 * {@link com.sldeditor.datasource.extension.filesystem.node.TransferableDataItem#TransferableDataItem()}.
 */
@Test
public void testTransferableDataItem() {
    Map<NodeInterface, TreePath> selectedData = new HashMap<NodeInterface, TreePath>();
    FileTreeNode fileTreeNode1 = null;
    FileTreeNode fileTreeNode2 = null;
    FileTreeNode fileTreeNode3 = null;
    FileTreeNode fileTreeNode4 = null;
    try {
        fileTreeNode1 = new FileTreeNode(new File(System.getProperty("user.dir")), "file1.txt");
        fileTreeNode2 = new FileTreeNode(new File(System.getProperty("user.dir")), "file2.txt");
        fileTreeNode3 = new FileTreeNode(new File(System.getProperty("user.dir")), "file3.txt");
        fileTreeNode4 = new FileTreeNode(new File(System.getProperty("user.dir")), "file4.txt");
        selectedData.put(fileTreeNode1, new TreePath(new DefaultMutableTreeNode("branch1")));
        selectedData.put(fileTreeNode2, new TreePath(new DefaultMutableTreeNode("branch2")));
        selectedData.put(fileTreeNode3, new TreePath(new DefaultMutableTreeNode("branch3")));
        selectedData.put(fileTreeNode4, new TreePath(new DefaultMutableTreeNode("branch4")));
        TransferableDataItem dataItem = new TransferableDataItem(selectedData);
        DataFlavor dataFlavour = dataItem.getDataFlavour();
        assertEquals(FileTreeNode.class, dataFlavour.getRepresentationClass());
        try {
            Object data = dataItem.getTransferData(DataFlavourManager.FOLDER_DATAITEM_FLAVOR);
            assertTrue(data instanceof TransferredData);
            assertEquals(selectedData.size(), ((TransferredData) data).getDataListSize());
            assertFalse(dataItem.isDataFlavorSupported(BuiltInDataFlavour.GEOSERVER_DATAITEM_FLAVOUR));
            assertTrue(dataItem.isDataFlavorSupported(DataFlavourManager.FOLDER_DATAITEM_FLAVOR));
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
            fail(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    } catch (SecurityException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) DataFlavor(java.awt.datatransfer.DataFlavor) TransferredData(com.sldeditor.datasource.extension.filesystem.dataflavour.TransferredData) TreePath(javax.swing.tree.TreePath) TransferableDataItem(com.sldeditor.datasource.extension.filesystem.node.TransferableDataItem) File(java.io.File) NodeInterface(com.sldeditor.common.NodeInterface) Test(org.junit.Test)

Example 19 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project keystore-explorer by kaikramer.

the class ExamineClipboardAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    // get clipboard contents, but only string types, not files
    Transferable t = clipboard.getContents(null);
    try {
        if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data;
            data = (String) t.getTransferData(DataFlavor.stringFlavor);
            show(data);
        }
    // TODO handle other flavor types
    } catch (UnsupportedFlavorException e) {
    // ignore
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) Clipboard(java.awt.datatransfer.Clipboard) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 20 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project cytoscape-impl by cytoscape.

the class DragHandler method dragEnter.

@SuppressWarnings("unchecked")
@Override
public void dragEnter(DropTargetDragEvent event) {
    JComponent lastComponent = controller.getLastHoveredComponent();
    if (lastComponent != null) {
        setBackground(lastComponent, ViewUtil.UNSELECTED_BACKGROUND_COLOR);
    }
    JComponent target = getPrimaryView(view);
    try {
        List<Integer> sourcePath = (List<Integer>) event.getTransferable().getTransferData(PathDataFlavor.instance);
        List<Integer> targetPath = controller.getPath(parent, target);
        JComponent sourceView = controller.getChild(parent, sourcePath);
        if (!controller.supportsDrop(parent, sourcePath, sourceView, targetPath, target) || isEquivalentLocation(sourcePath, targetPath, target)) {
            setCursor(DragSource.DefaultCopyNoDrop, target);
            return;
        }
        if (controller.isDropMove(parent, sourceView, sourcePath, target, targetPath)) {
            // Move
            event.acceptDrag(DnDConstants.ACTION_MOVE);
            setCursor(DragSource.DefaultMoveDrop, view);
        } else {
            // Group (+)
            event.acceptDrag(DnDConstants.ACTION_COPY);
            setCursor(DragSource.DefaultCopyDrop, view);
        }
    } catch (UnsupportedFlavorException e) {
        logger.error("Unexpected error", e);
    } catch (IOException e) {
        logger.error("Unexpected error", e);
    } finally {
        controller.setLastHoveredComponent(view);
    }
    target.setBackground(ViewUtil.SELECTED_BACKGROUND_COLOR);
}
Also used : JComponent(javax.swing.JComponent) List(java.util.List) 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