Search in sources :

Example 6 with DragSource

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);
}
Also used : JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) TransferHandler(javax.swing.TransferHandler) DragSource(java.awt.dnd.DragSource)

Example 7 with DragSource

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());
}
Also used : MouseEvent(java.awt.event.MouseEvent) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragSource(java.awt.dnd.DragSource) Point(java.awt.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DragGestureRecognizer(java.awt.dnd.DragGestureRecognizer) DragSourceContext(java.awt.dnd.DragSourceContext) Cursor(java.awt.Cursor) ObjectOutputStream(java.io.ObjectOutputStream) DragSourceListener(java.awt.dnd.DragSourceListener) StringSelection(java.awt.datatransfer.StringSelection) TooManyListenersException(java.util.TooManyListenersException) Button(java.awt.Button) ByteArrayInputStream(java.io.ByteArrayInputStream) DragSourceMotionListener(java.awt.dnd.DragSourceMotionListener) Component(java.awt.Component) ObjectInputStream(java.io.ObjectInputStream)

Example 8 with DragSource

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();
}
Also used : Frame(java.awt.Frame) DragSource(java.awt.dnd.DragSource) DropTarget(java.awt.dnd.DropTarget) Point(java.awt.Point) Robot(java.awt.Robot)

Example 9 with DragSource

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);
}
Also used : MouseEvent(java.awt.event.MouseEvent) ParameterTree(gov.sandia.umf.platform.ui.ensemble.tree.ParameterTree) MouseAdapter(java.awt.event.MouseAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DragSource(java.awt.dnd.DragSource) NodeSimpleLabel(replete.gui.controls.simpletree.NodeSimpleLabel) NodeSubdomain(gov.sandia.umf.platform.ui.ensemble.tree.NodeSubdomain) FilterableParameterTreePanel(gov.sandia.umf.platform.ui.ensemble.tree.FilterableParameterTreePanel) TNode(replete.gui.controls.simpletree.TNode) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) TreePath(javax.swing.tree.TreePath) List(java.util.List) ParameterSetList(gov.sandia.umf.platform.ensemble.params.ParameterSetList) ArrayList(java.util.ArrayList) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 10 with DragSource

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();
}
Also used : DragSourceAdapter(java.awt.dnd.DragSourceAdapter) MouseEvent(java.awt.event.MouseEvent) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragGestureListener(java.awt.dnd.DragGestureListener) MouseAdapter(java.awt.event.MouseAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) DragSource(java.awt.dnd.DragSource) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) JTree(javax.swing.JTree) WorkflowRuntimeException(org.apache.airavata.workflow.model.exceptions.WorkflowRuntimeException) TreePath(javax.swing.tree.TreePath) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

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