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);
}
});
}
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);
}
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);
}
Aggregations