Search in sources :

Example 66 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project Spark by igniterealtime.

the class SparkTabbedPane method enableDragAndDrop.

/**
 * Drag and Drop
 */
public void enableDragAndDrop() {
    final DragSourceListener dsl = new DragSourceListener() {

        @Override
        public void dragDropEnd(DragSourceDropEvent event) {
            dragTabIndex = -1;
        }

        @Override
        public void dragEnter(DragSourceDragEvent event) {
            event.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
        }

        @Override
        public void dragExit(DragSourceEvent event) {
        }

        @Override
        public void dragOver(DragSourceDragEvent event) {
        }

        @Override
        public void dropActionChanged(DragSourceDragEvent event) {
        }
    };
    final Transferable t = new Transferable() {

        private final DataFlavor FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType, NAME);

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            return pane;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            DataFlavor[] f = new DataFlavor[1];
            f[0] = this.FLAVOR;
            return f;
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.getHumanPresentableName().equals(NAME);
        }
    };
    final DragGestureListener dgl = event -> {
        dragTabIndex = pane.indexAtLocation(event.getDragOrigin().x, event.getDragOrigin().y);
        try {
            event.startDrag(DragSource.DefaultMoveDrop, t, dsl);
        } catch (Exception idoe) {
            Log.error(idoe);
        }
    };
    final DropTargetListener dtl = new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetDragEvent event) {
        }

        @Override
        public void dragExit(DropTargetEvent event) {
        }

        @Override
        public void dragOver(DropTargetDragEvent event) {
        }

        @Override
        public void drop(DropTargetDropEvent event) {
            int dropTabIndex = getTargetTabIndex(event.getLocation());
            moveTab(dragTabIndex, dropTabIndex);
        }

        @Override
        public void dropActionChanged(DropTargetDragEvent event) {
        }
    };
    new DropTarget(pane, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true);
    new DragSource().createDefaultDragGestureRecognizer(pane, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
}
Also used : ModelUtil(org.jivesoftware.spark.util.ModelUtil) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) java.awt.dnd(java.awt.dnd) Iterator(java.util.Iterator) DataFlavor(java.awt.datatransfer.DataFlavor) SparkRes(org.jivesoftware.resource.SparkRes) Transferable(java.awt.datatransfer.Transferable) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) IOException(java.io.IOException) Log(org.jivesoftware.spark.util.log.Log) MouseEvent(java.awt.event.MouseEvent) Spark(org.jivesoftware.Spark) ArrayList(java.util.ArrayList) java.awt(java.awt) List(java.util.List) SparkManager(org.jivesoftware.spark.SparkManager) MouseAdapter(java.awt.event.MouseAdapter) ChangeListener(javax.swing.event.ChangeListener) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 67 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project freeplane by freeplane.

the class MClipboardController method getFlavorHandlers.

Collection<IDataFlavorHandler> getFlavorHandlers() {
    final Transferable t = getClipboardContents();
    final Collection<IDataFlavorHandler> handlerList = new LinkedList<IDataFlavorHandler>();
    if (t == null) {
        return handlerList;
    }
    if (t.isDataFlavorSupported(MindMapNodesSelection.mindMapNodesFlavor)) {
        try {
            final String textFromClipboard = t.getTransferData(MindMapNodesSelection.mindMapNodesFlavor).toString();
            handlerList.add(new MindMapNodesFlavorHandler(textFromClipboard));
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    DataFlavor supportedHtmlFlavor = getSupportedHtmlFlavor(t);
    if (supportedHtmlFlavor != null) {
        try {
            final String textFromClipboard = t.getTransferData(supportedHtmlFlavor).toString();
            if (textFromClipboard.charAt(0) != 65533) {
                if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    handlerList.add(new StructuredHtmlFlavorHandler(textFromClipboard));
                    handlerList.add(new DirectHtmlFlavorHandler(textFromClipboard));
                }
            }
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            final String plainTextFromClipboard = t.getTransferData(DataFlavor.stringFlavor).toString();
            handlerList.add(new StringFlavorHandler(plainTextFromClipboard));
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(MindMapNodesSelection.fileListFlavor)) {
        try {
            final List<File> fileList = castToFileList(t.getTransferData(MindMapNodesSelection.fileListFlavor));
            handlerList.add(new FileListFlavorHandler(fileList));
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        try {
            Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
            handlerList.add(new ImageFlavorHandler(image));
        } catch (final UnsupportedFlavorException e) {
        } catch (final IOException e) {
        }
    }
    return handlerList;
}
Also used : Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) Image(java.awt.Image) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) LinkedList(java.util.LinkedList) DataFlavor(java.awt.datatransfer.DataFlavor) File(java.io.File)

Example 68 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.

the class EffectTreeDropTarget method drop.

@Override
public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    Object node = parentpath.getLastPathComponent();
    EffectsLibrary eLibrary = EffectsLibrary.getInstance();
    if (dtde.isDataFlavorSupported(TransferableEffect.EFFECT_CAT_FLAVOR)) {
        if (!(node instanceof EffectCategory)) {
            dtde.rejectDrop();
            return;
        }
        if (dtde.getDropAction() == DnDConstants.ACTION_MOVE) {
            dtde.acceptDrop(dtde.getDropAction());
            Transferable tr = dtde.getTransferable();
            try {
                Object transferNode = tr.getTransferData(TransferableEffect.EFFECT_CAT_FLAVOR);
                EffectCategory effectCategory = (EffectCategory) transferNode;
                EffectCategory parentNode = (EffectCategory) node;
                eLibrary.addCategory(parentNode, effectCategory);
                dtde.dropComplete(true);
            } catch (UnsupportedFlavorException | IOException e) {
                dtde.dropComplete(false);
            }
        } else {
            dtde.rejectDrop();
        }
    } else if (dtde.isDataFlavorSupported(TransferableEffect.EFFECT_FLAVOR)) {
        dtde.acceptDrop(dtde.getDropAction());
        try {
            Transferable tr = dtde.getTransferable();
            Object transferNode = tr.getTransferData(TransferableEffect.EFFECT_FLAVOR);
            Effect effect = (Effect) transferNode;
            effect.clearParameters();
            // iLibrary.removeInstrument(instrument);
            if (node instanceof EffectCategory) {
                EffectCategory parentNode = (EffectCategory) node;
                eLibrary.addEffect(parentNode, effect);
            } else if (node instanceof Effect) {
                EffectCategory parentNode = (EffectCategory) parentpath.getPathComponent(parentpath.getPathCount() - 2);
                int index = ListUtil.indexOfByRef(parentNode.getEffects(), node);
                int closestRow = tree.getClosestRowForLocation(pt.x, pt.y);
                Rectangle bounds = tree.getRowBounds(closestRow);
                if (pt.y > bounds.y + bounds.height) {
                    eLibrary.addEffect(parentNode, effect);
                } else {
                    eLibrary.addEffect(parentNode, index, effect);
                }
            }
            dtde.dropComplete(true);
        } catch (UnsupportedFlavorException | IOException e) {
            dtde.dropComplete(false);
        }
    } else {
        dtde.rejectDrop();
    }
}
Also used : DropTargetContext(java.awt.dnd.DropTargetContext) Transferable(java.awt.datatransfer.Transferable) Rectangle(java.awt.Rectangle) Point(java.awt.Point) EffectCategory(blue.ui.core.mixer.EffectCategory) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) EffectsLibrary(blue.ui.core.mixer.EffectsLibrary)

Example 69 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.

the class PresetsTreeDropTarget method drop.

@Override
public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    Object node = parentpath.getLastPathComponent();
    if (!(node instanceof PresetGroup)) {
        dtde.rejectDrop();
        return;
    }
    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            if (tr.isDataFlavorSupported(flavors[i])) {
                dtde.acceptDrop(dtde.getDropAction());
                Object transferNode = tr.getTransferData(flavors[i]);
                PresetsTreeModel presetsTreeModel = (PresetsTreeModel) tree.getModel();
                PresetGroup parentNode = (PresetGroup) node;
                if (transferNode instanceof Preset) {
                    Preset preset = (Preset) transferNode;
                    // presetsTreeModel.removePreset(preset);
                    presetsTreeModel.addPreset(parentNode, preset);
                    dtde.dropComplete(true);
                } else if (transferNode instanceof PresetGroup) {
                    PresetGroup presetGroup = (PresetGroup) transferNode;
                    // presetsTreeModel.removePresetGroup(presetGroup);
                    presetsTreeModel.addPresetGroup(parentNode, presetGroup);
                    dtde.dropComplete(true);
                } else {
                    dtde.rejectDrop();
                }
                return;
            }
        }
        dtde.rejectDrop();
    } catch (UnsupportedFlavorException | IOException e) {
        e.printStackTrace();
    }
}
Also used : Preset(blue.orchestra.blueSynthBuilder.Preset) DropTargetContext(java.awt.dnd.DropTargetContext) Transferable(java.awt.datatransfer.Transferable) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) DataFlavor(java.awt.datatransfer.DataFlavor) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) PresetGroup(blue.orchestra.blueSynthBuilder.PresetGroup)

Example 70 with UnsupportedFlavorException

use of java.awt.datatransfer.UnsupportedFlavorException in project blue by kunstmusik.

the class AudioLayersDropTargetListener method drop.

/*
     * (non-Javadoc)
     * 
     * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
     */
@Override
public void drop(DropTargetDropEvent dtde) {
    Node node = NodeTransfer.node(dtde.getTransferable(), NodeTransfer.DND_COPY);
    if (node != null) {
        File f = node.getLookup().lookup(File.class);
        if (f == null || !f.isFile()) {
            dtde.dropComplete(false);
            return;
        }
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Point p = dtde.getLocation();
        AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
        int index = audioLayerGroup.getLayerNumForY(p.y);
        AudioClip af = new AudioClip();
        af.setName(f.getName());
        af.setAudioFile(f);
        float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
        af.setStartTime(startTime);
        af.setSubjectiveDuration(af.getAudioDuration());
        AudioLayer layer = audioLayerGroup.get(index);
        layer.add(af);
        dtde.dropComplete(true);
    }
    try {
        Transferable tr = dtde.getTransferable();
        if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_LINK);
            List<?> list = (List<?>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            if (list.size() != 1) {
                dtde.dropComplete(false);
                return;
            }
            String s = list.get(0).toString().trim();
            System.out.println("file flavor found: " + s);
            if (!s.toLowerCase().endsWith("wav") && !s.toLowerCase().endsWith("aif") && !s.toLowerCase().endsWith("aiff")) {
                dtde.dropComplete(false);
                return;
            }
            String sObjName = s.substring(s.lastIndexOf(File.separator) + 1);
            Point p = dtde.getLocation();
            AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
            int index = audioLayerGroup.getLayerNumForY(p.y);
            AudioClip af = new AudioClip();
            af.setName(sObjName);
            af.setAudioFile(new File(s));
            float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
            af.setStartTime(startTime);
            af.setSubjectiveDuration(af.getAudioDuration());
            AudioLayer layer = audioLayerGroup.get(index);
            layer.add(af);
            dtde.dropComplete(true);
            return;
        } else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_LINK);
            String str = (String) tr.getTransferData(DataFlavor.stringFlavor);
            System.out.println("String flavor found: " + str);
            if (!str.startsWith("file://")) {
                dtde.dropComplete(false);
                return;
            }
            str = str.substring(7).trim();
            if (!str.toLowerCase().endsWith("wav") && !str.toLowerCase().endsWith("aif") && !str.toLowerCase().endsWith("aiff")) {
                System.err.println("Could not open file: " + str);
                dtde.dropComplete(false);
                return;
            }
            str = URLDecoder.decode(str);
            str = str.replaceAll(" ", "\\ ");
            File f = new File(str);
            if (!f.exists()) {
                dtde.dropComplete(false);
                return;
            }
            String sObjName = str.substring(str.lastIndexOf(File.separator) + 1);
            Point p = dtde.getLocation();
            AudioLayerGroup audioLayerGroup = audioLayersPanel.getAudioLayerGroup();
            int index = audioLayerGroup.getLayerNumForY(p.y);
            AudioClip af = new AudioClip();
            af.setName(sObjName);
            af.setAudioFile(f);
            float startTime = (float) p.x / audioLayersPanel.getTimeState().getPixelSecond();
            af.setStartTime(startTime);
            af.setSubjectiveDuration(af.getAudioDuration());
            AudioLayer layer = audioLayerGroup.get(index);
            layer.add(af);
            dtde.dropComplete(true);
            return;
        }
        dtde.rejectDrop();
    } catch (UnsupportedFlavorException | IOException | InvalidDnDOperationException e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}
Also used : InvalidDnDOperationException(java.awt.dnd.InvalidDnDOperationException) Node(org.openide.nodes.Node) AudioClip(blue.score.layers.audio.core.AudioClip) Transferable(java.awt.datatransfer.Transferable) AudioLayerGroup(blue.score.layers.audio.core.AudioLayerGroup) Point(java.awt.Point) IOException(java.io.IOException) AudioLayer(blue.score.layers.audio.core.AudioLayer) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) List(java.util.List) File(java.io.File)

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