use of java.awt.dnd.DragSource in project WorldPainter by Captain-Chaos.
the class DnDToggleButton method init.
private void init() {
transferHandler = new TransferHandler() {
@Override
protected Transferable createTransferable(JComponent c) {
return new DnDToggleButton(getText(), getIcon());
}
};
setTransferHandler(transferHandler);
source = new DragSource();
source.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
}
use of java.awt.dnd.DragSource in project jdk8u_jdk by JetBrains.
the class TestDragSourceAdapter method main.
public static void main(String[] args) throws Exception {
DragSource ds = new DragSource();
TestDragSourceAdapter dsa1 = new TestDragSourceAdapter(1);
TestDragSourceAdapter dsa2 = new TestDragSourceAdapter(2);
Component c = new Button();
DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_COPY, e -> e.startDrag(null, null));
MouseEvent me = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, 0, InputEvent.CTRL_MASK, 100, 100, 0, false);
DragGestureEvent dge = new DragGestureEvent(dgr, DnDConstants.ACTION_COPY, new Point(100, 100), Arrays.asList(me));
DragSourceContext dsc = new DragSourceContext(Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge), dge, new Cursor(Cursor.HAND_CURSOR), null, null, new StringSelection("TEXT"), null);
ds.addDragSourceListener(dsa1);
ds.addDragSourceListener(dsa2);
ds.addDragSourceListener(dsa2);
ds.addDragSourceMotionListener(dsa1);
ds.addDragSourceMotionListener(dsa1);
ds.addDragSourceMotionListener(dsa2);
dsc.addDragSourceListener(dsa2);
byte[] serialized;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(dsc);
serialized = bos.toByteArray();
}
DragSourceContext dsc_copy;
try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
ObjectInputStream ois = new ObjectInputStream(bis)) {
dsc_copy = (DragSourceContext) ois.readObject();
}
try {
dsc_copy.addDragSourceListener(dsa1);
throw new RuntimeException("Test failed. Listener addition succeeded");
} catch (TooManyListenersException ignored) {
}
try {
dsc_copy.addDragSourceListener(dsa2);
throw new RuntimeException("Test failed. Listener addition succeeded");
} catch (TooManyListenersException ignored) {
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(ds);
serialized = bos.toByteArray();
}
DragSource ds_copy;
try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
ObjectInputStream ois = new ObjectInputStream(bis)) {
ds_copy = (DragSource) ois.readObject();
}
DragSourceListener[] dsls = ds_copy.getDragSourceListeners();
assertEquals(3, dsls.length, "DragSourceListeners number");
assertEquals(1, Stream.of(dsls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
assertEquals(2, Stream.of(dsls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
DragSourceMotionListener[] dsmls = ds_copy.getDragSourceMotionListeners();
assertEquals(3, dsmls.length, "DragSourceMotionListeners number");
assertEquals(2, Stream.of(dsmls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
assertEquals(1, Stream.of(dsmls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
}
use of java.awt.dnd.DragSource in project jdk8u_jdk by JetBrains.
the class MissingEventsOnModalDialogTest method main.
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource = DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame, DnDConstants.ACTION_COPY_OR_MOVE, new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE, new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
use of java.awt.dnd.DragSource in project n2a by frothga.
the class FixedParameterSpacePanel method buildDomainTab.
private void buildDomainTab(ParameterDomain domain) {
TNode nRoot = new TNode(new NodeSimpleLabel(domain.getName()));
populate(nRoot, domain);
FilterableParameterTreePanel pnlFilterableTree = new FilterableParameterTreePanel(nRoot);
final ParameterTree treParams = pnlFilterableTree.getTree();
treParams.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
TreePath path = treParams.getPathForLocation(e.getX(), e.getY());
if (path == null || ((TNode) path.getLastPathComponent()).getUserObject() instanceof NodeSubdomain) {
treParams.setCursor(Cursor.getDefaultCursor());
} else {
treParams.setCursor(DragCursors.getOpenhandcursor());
}
}
});
treParams.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
updateDetailsPanelFromTree(treParams);
}
});
treParams.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1 && treParams.getSelectionCount() != 0) {
List<ParameterBundle> bundles = getSelectedParamsAsBundles(treParams.getSelectionPaths());
if (bundles != null) {
dropIntoGroups(bundles, dropEmphasis);
}
}
}
});
DragSource ds = new DragSource();
ds.createDefaultDragGestureRecognizer(treParams, DnDConstants.ACTION_LINK, this);
int existingIndex = tabParamDomains.indexOfTabByKey(domain.getName());
if (existingIndex != -1) {
tabParamDomains.remove(existingIndex);
}
tabParamDomains.add(domain.getName(), pnlFilterableTree);
int index = tabParamDomains.getTabCount();
tabParamDomains.setIconAt(index - 1, domain.getIcon());
tabParamDomains.setUseBorderAt(index - 1, true);
}
use of java.awt.dnd.DragSource in project airavata by apache.
the class ComponentSelector method initGUI.
private void initGUI() {
this.treeModel = new ComponentTreeModel(new ComponentTreeNode("Components"));
this.tree = new JTree(this.treeModel);
// Add a tool tip.
ToolTipManager.sharedInstance().registerComponent(this.tree);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
@Override
public java.awt.Component getTreeCellRendererComponent(JTree t, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean focus) {
super.getTreeCellRendererComponent(t, value, sel, expanded, leaf, row, focus);
ComponentTreeNode node = (ComponentTreeNode) value;
if (node.getComponentReference() == null) {
setToolTipText(null);
} else {
setToolTipText("Drag a component to the composer to add");
}
return this;
}
};
// Change icons
try {
renderer.setOpenIcon(SwingUtil.createImageIcon("opened.gif"));
renderer.setClosedIcon(SwingUtil.createImageIcon("closed.gif"));
renderer.setLeafIcon(SwingUtil.createImageIcon("leaf.gif"));
} catch (RuntimeException e) {
logger.warn("Failed to load image icons. " + "It will use the default icons instead.", e);
}
this.tree.setCellRenderer(renderer);
this.tree.setShowsRootHandles(true);
this.tree.setEditable(false);
this.tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
// update.
if (event.isAddedPath()) {
TreePath path = event.getPath();
select(path);
}
}
});
// Drag and dtop
DragGestureListener dragGestureListener = new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent event) {
ComponentSelector.this.dragGestureRecognized(event);
}
};
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this.tree, DnDConstants.ACTION_COPY_OR_MOVE, dragGestureListener);
this.dragSourceListener = new DragSourceAdapter() {
};
// Popup
this.tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
if (event.isPopupTrigger()) {
showPopupIfNecessary(event);
}
}
});
createNodePopupMenu();
}
Aggregations