Search in sources :

Example 11 with DragSource

use of java.awt.dnd.DragSource in project gdmatrix by gdmatrix.

the class ElementLabel method initComponents.

private void initComponents() {
    setBorder(new EmptyBorder(2, 4, 2, 4));
    addMouseListener(this);
    DragSource dragSource = DragSource.getDefaultDragSource();
    dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, new DragGestureListener() {

        @Override
        public void dragGestureRecognized(DragGestureEvent event) {
            DragSourceListener dragListener = new DragSourceAdapter() {

                @Override
                public void dragDropEnd(DragSourceDropEvent event) {
                    Palette palette = ElementLabel.this.getCategoryPane().getPalette();
                    palette.clearSelectedElement();
                }
            };
            event.startDrag(DragSource.DefaultCopyDrop, ElementLabel.this, dragListener);
        }
    });
}
Also used : DragSourceAdapter(java.awt.dnd.DragSourceAdapter) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragGestureListener(java.awt.dnd.DragGestureListener) DragSourceDropEvent(java.awt.dnd.DragSourceDropEvent) DragSource(java.awt.dnd.DragSource) EmptyBorder(javax.swing.border.EmptyBorder) DragSourceListener(java.awt.dnd.DragSourceListener)

Example 12 with DragSource

use of java.awt.dnd.DragSource in project Weasis by nroduit.

the class SeriesThumbnail method registerListeners.

@Override
public void registerListeners() {
    super.registerListeners();
    // Reactivate tooltip listener
    ToolTipManager.sharedInstance().registerComponent(this);
    if (dragSource != null) {
        dragSource.removeDragSourceListener(this);
        dragSource.removeDragSourceMotionListener(this);
        removeFocusListener(this);
    }
    addFocusListener(this);
    this.setFocusable(true);
    this.addMouseListener(this);
    dragSource = new DragSource();
    dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
    dragSource.addDragSourceMotionListener(this);
}
Also used : DragSource(java.awt.dnd.DragSource)

Example 13 with DragSource

use of java.awt.dnd.DragSource in project darklaf by weisJ.

the class TransferUtil method setupDnD.

public static <T> Pair<DropTarget, AtomicBoolean> setupDnD(final JComponent c, final int sourceActions, final Class<T> dataType, final Supplier<T> exporter, final Consumer<T> importer, final Function<T, Icon> dragImageCreator) {
    DragSource ds = new DragSource();
    AtomicBoolean dragEnabled = new AtomicBoolean(true);
    TransferHandler handler = new TransferHandler() {

        private final DataFlavor flavor;

        {
            try {
                flavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=" + dataType.getName());
            } catch (final ClassNotFoundException e) {
                throw new IllegalStateException(e);
            }
        }

        @Override
        public int getSourceActions(final JComponent c) {
            return sourceActions;
        }

        @Override
        public boolean canImport(final TransferSupport support) {
            return support.isDataFlavorSupported(flavor);
        }

        @Override
        public Icon getVisualRepresentation(final Transferable t) {
            try {
                // noinspection unchecked
                return dragImageCreator.apply((T) t.getTransferData(flavor));
            } catch (UnsupportedFlavorException | IOException e) {
                return null;
            }
        }

        @Override
        protected Transferable createTransferable(final JComponent c) {
            T value = exporter.get();
            setDragImage(IconUtil.iconToImage(dragImageCreator.apply(value), c));
            return new ObjectTransferable<>(value, dataType);
        }

        @Override
        public boolean importData(final TransferSupport support) {
            if (!support.isDrop())
                return false;
            try {
                // noinspection unchecked
                importer.accept((T) support.getTransferable().getTransferData(flavor));
            } catch (final Exception e) {
                LOGGER.log(Level.SEVERE, "Importing failed", e);
            }
            return false;
        }
    };
    c.setTransferHandler(handler);
    ds.createDefaultDragGestureRecognizer(c, sourceActions, dge -> {
        if (!dragEnabled.get())
            return;
        handler.exportAsDrag(c, dge.getTriggerEvent(), dge.getDragAction());
    });
    return new Pair<>(c.getDropTarget(), dragEnabled);
}
Also used : Transferable(java.awt.datatransfer.Transferable) DragSource(java.awt.dnd.DragSource) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(com.github.weisj.darklaf.util.Pair)

Aggregations

DragSource (java.awt.dnd.DragSource)13 MouseEvent (java.awt.event.MouseEvent)5 DragGestureEvent (java.awt.dnd.DragGestureEvent)4 Point (java.awt.Point)3 DragGestureListener (java.awt.dnd.DragGestureListener)3 MouseAdapter (java.awt.event.MouseAdapter)3 Transferable (java.awt.datatransfer.Transferable)2 DragSourceAdapter (java.awt.dnd.DragSourceAdapter)2 DragSourceListener (java.awt.dnd.DragSourceListener)2 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)2 TreeSelectionListener (javax.swing.event.TreeSelectionListener)2 TreePath (javax.swing.tree.TreePath)2 Pair (com.github.weisj.darklaf.util.Pair)1 com.mxgraph.model.mxGeometry (com.mxgraph.model.mxGeometry)1 com.mxgraph.swing.util.mxGraphTransferable (com.mxgraph.swing.util.mxGraphTransferable)1 com.mxgraph.util.mxPoint (com.mxgraph.util.mxPoint)1 com.mxgraph.util.mxRectangle (com.mxgraph.util.mxRectangle)1 ParameterSetList (gov.sandia.umf.platform.ensemble.params.ParameterSetList)1 FilterableParameterTreePanel (gov.sandia.umf.platform.ui.ensemble.tree.FilterableParameterTreePanel)1 NodeSubdomain (gov.sandia.umf.platform.ui.ensemble.tree.NodeSubdomain)1