Search in sources :

Example 26 with MouseMotionListener

use of java.awt.event.MouseMotionListener in project Terasology by MovingBlocks.

the class AwtMouseDevice method registerToAwtGlCanvas.

public void registerToAwtGlCanvas(AWTGLCanvas canvas) {
    canvas.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int button = e.getButton() - 1;
            buttonStates.add(button);
            MouseInput mouseInput = MouseInput.find(InputType.MOUSE_BUTTON, button);
            queue.offer(new MouseAction(mouseInput, ButtonState.DOWN, getPosition()));
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            int button = e.getButton() - 1;
            buttonStates.remove(button);
            MouseInput mouseInput = MouseInput.find(InputType.MOUSE_BUTTON, button);
            queue.offer(new MouseAction(mouseInput, ButtonState.UP, getPosition()));
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
    canvas.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
            updateMouse(e.getX(), e.getY());
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            updateMouse(e.getX(), e.getY());
        }
    });
    canvas.addMouseWheelListener(e -> {
        int yOffset = e.getUnitsToScroll();
        if (yOffset != 0.0) {
            int id = (yOffset > 0) ? 1 : -1;
            queue.offer(new MouseAction(InputType.MOUSE_WHEEL.getInput(id), 1, getPosition()));
        }
    });
}
Also used : MouseAction(org.terasology.input.device.MouseAction) MouseListener(java.awt.event.MouseListener) MouseEvent(java.awt.event.MouseEvent) MouseMotionListener(java.awt.event.MouseMotionListener) MouseInput(org.terasology.input.MouseInput)

Example 27 with MouseMotionListener

use of java.awt.event.MouseMotionListener in project airavata by apache.

the class GraphCanvas method initGUI.

/**
 * Initializes the GUI.
 */
private void initGUI() {
    this.panel = new JPanel() {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            GraphCanvas.this.paintComponent((Graphics2D) g);
        }
    };
    this.panel.setLayout(null);
    // To make the background color visible.
    this.panel.setOpaque(true);
    this.panel.setBackground(new Color(255, 255, 255));
    this.panel.setDoubleBuffered(true);
    this.panel.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent event) {
            GraphCanvas.this.mouseClicked(event);
        }

        @Override
        public void mousePressed(MouseEvent event) {
            GraphCanvas.this.mousePressed(event);
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            GraphCanvas.this.mouseReleased(event);
            notifyListeners(new GraphCanvasEvent(GraphCanvasEvent.GraphCanvasEventType.WORKFLOW_CHANGED, GraphCanvas.this, GraphCanvas.this.workflow));
        }
    });
    this.panel.addMouseMotionListener(new MouseMotionListener() {

        public void mouseDragged(MouseEvent event) {
            GraphCanvas.this.mouseDragged(event);
        }

        public void mouseMoved(MouseEvent event) {
            GraphCanvas.this.mouseMoved(event);
        }
    });
    this.panel.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent event) {
            GraphCanvas.this.keyPressed(event);
        }

        @Override
        public void keyReleased(KeyEvent event) {
            GraphCanvas.this.keyReleased(event);
        }
    });
    this.scrollPane = new JScrollPane(this.panel);
    // Set up drag and drop
    DropTargetListener dropTargetListener = new DropTargetAdapter() {

        public void drop(DropTargetDropEvent event) {
            GraphCanvas.this.drop(event);
        }
    };
    new DropTarget(this.panel, DnDConstants.ACTION_COPY_OR_MOVE, dropTargetListener);
    createPopupMenu();
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) MouseEvent(java.awt.event.MouseEvent) Color(java.awt.Color) KeyAdapter(java.awt.event.KeyAdapter) MouseAdapter(java.awt.event.MouseAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) KeyEvent(java.awt.event.KeyEvent) DropTargetAdapter(java.awt.dnd.DropTargetAdapter) DropTargetListener(java.awt.dnd.DropTargetListener) DropTarget(java.awt.dnd.DropTarget) DropTargetDropEvent(java.awt.dnd.DropTargetDropEvent)

Example 28 with MouseMotionListener

use of java.awt.event.MouseMotionListener in project gate-core by GateNLP.

the class AnnotationEditor method initListeners.

protected void initListeners() {
    // resize the window when the table changes.
    featuresEditor.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            // the table has changed size -> resize the window too!
            popupWindow.pack();
        }
    });
    KeyAdapter keyAdapter = new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            hideTimer.stop();
        }
    };
    typeCombo.getEditor().getEditorComponent().addKeyListener(keyAdapter);
    MouseListener windowMouseListener = new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent evt) {
            hideTimer.stop();
        }

        // allow a JWindow to be dragged with a mouse
        @Override
        public void mousePressed(MouseEvent me) {
            pressed = me;
        }
    };
    MouseMotionListener windowMouseMotionListener = new MouseMotionAdapter() {

        Point location;

        // allow a JWindow to be dragged with a mouse
        @Override
        public void mouseDragged(MouseEvent me) {
            location = popupWindow.getLocation(location);
            int x = location.x - pressed.getX() + me.getX();
            int y = location.y - pressed.getY() + me.getY();
            popupWindow.setLocation(x, y);
            pinnedButton.setSelected(true);
        }
    };
    popupWindow.getRootPane().addMouseListener(windowMouseListener);
    popupWindow.getRootPane().addMouseMotionListener(windowMouseMotionListener);
    InputMap inputMap = ((JComponent) popupWindow.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    actionMap = ((JComponent) popupWindow.getContentPane()).getActionMap();
    // add the key-action bindings of this Component to the parent window
    solAction = new StartOffsetLeftAction("", MainFrame.getIcon("extend-left"), SOL_DESC, KeyEvent.VK_LEFT);
    solButton.setAction(solAction);
    setShortCuts(inputMap, SOL_KEY_STROKES, "solAction");
    actionMap.put("solAction", solAction);
    sorAction = new StartOffsetRightAction("", MainFrame.getIcon("extend-right"), SOR_DESC, KeyEvent.VK_RIGHT);
    sorButton.setAction(sorAction);
    setShortCuts(inputMap, SOR_KEY_STROKES, "sorAction");
    actionMap.put("sorAction", sorAction);
    delAction = new DeleteAnnotationAction("", MainFrame.getIcon("remove-annotation"), "Delete the annotation", KeyEvent.VK_DELETE);
    delButton.setAction(delAction);
    inputMap.put(KeyStroke.getKeyStroke("alt DELETE"), "delAction");
    actionMap.put("delAction", delAction);
    eolAction = new EndOffsetLeftAction("", MainFrame.getIcon("extend-left"), EOL_DESC, KeyEvent.VK_LEFT);
    eolButton.setAction(eolAction);
    setShortCuts(inputMap, EOL_KEY_STROKES, "eolAction");
    actionMap.put("eolAction", eolAction);
    eorAction = new EndOffsetRightAction("", MainFrame.getIcon("extend-right"), EOR_DESC, KeyEvent.VK_RIGHT);
    eorButton.setAction(eorAction);
    setShortCuts(inputMap, EOR_KEY_STROKES, "eorAction");
    actionMap.put("eorAction", eorAction);
    pinnedButton.setToolTipText("<html>Press to pin window in place" + "&nbsp;&nbsp;<font color=#667799><small>Ctrl-P" + "&nbsp;&nbsp;</small></font></html>");
    inputMap.put(KeyStroke.getKeyStroke("control P"), "toggle pin");
    actionMap.put("toggle pin", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            pinnedButton.doClick();
        }
    });
    DismissAction dismissAction = new DismissAction("", null, "Close the window", KeyEvent.VK_ESCAPE);
    dismissButton.setAction(dismissAction);
    inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "dismissAction");
    inputMap.put(KeyStroke.getKeyStroke("alt ESCAPE"), "dismissAction");
    actionMap.put("dismissAction", dismissAction);
    ApplyAction applyAction = new ApplyAction("Apply", null, "", KeyEvent.VK_ENTER);
    inputMap.put(KeyStroke.getKeyStroke("alt ENTER"), "applyAction");
    actionMap.put("applyAction", applyAction);
    typeCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            String newType = typeCombo.getSelectedItem().toString();
            if (ann == null || ann.getType().equals(newType))
                return;
            // annotation editing
            Integer oldId = ann.getId();
            Annotation oldAnn = ann;
            set.remove(ann);
            try {
                set.add(oldId, oldAnn.getStartNode().getOffset(), oldAnn.getEndNode().getOffset(), newType, oldAnn.getFeatures());
                Annotation newAnn = set.get(oldId);
                // update the selection to the new annotation
                getOwner().selectAnnotation(new AnnotationDataImpl(set, newAnn));
                editAnnotation(newAnn, set);
                owner.annotationChanged(newAnn, set, oldAnn.getType());
            } catch (InvalidOffsetException ioe) {
                throw new GateRuntimeException(ioe);
            }
        }
    });
    hideTimer = new Timer(HIDE_DELAY, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            annotationEditorInstance.setVisible(false);
        }
    });
    hideTimer.setRepeats(false);
    AncestorListener textAncestorListener = new AncestorListener() {

        @Override
        public void ancestorAdded(AncestorEvent event) {
            if (wasShowing) {
                annotationEditorInstance.setVisible(true);
            }
            wasShowing = false;
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            if (isShowing()) {
                wasShowing = true;
                popupWindow.dispose();
            }
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
        }

        private boolean wasShowing = false;
    };
    owner.getTextComponent().addAncestorListener(textAncestorListener);
}
Also used : ActionEvent(java.awt.event.ActionEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) AncestorEvent(javax.swing.event.AncestorEvent) KeyEvent(java.awt.event.KeyEvent) MouseListener(java.awt.event.MouseListener) GateRuntimeException(gate.util.GateRuntimeException) AnnotationDataImpl(gate.gui.annedit.AnnotationDataImpl) AncestorListener(javax.swing.event.AncestorListener) AbstractAction(javax.swing.AbstractAction) ComponentAdapter(java.awt.event.ComponentAdapter) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JComponent(javax.swing.JComponent) InvalidOffsetException(gate.util.InvalidOffsetException) Point(java.awt.Point) Point(java.awt.Point) Annotation(gate.Annotation) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) ActionListener(java.awt.event.ActionListener) Timer(javax.swing.Timer) InputMap(javax.swing.InputMap) ComponentEvent(java.awt.event.ComponentEvent)

Example 29 with MouseMotionListener

use of java.awt.event.MouseMotionListener in project GDSC-SMLM by aherbert.

the class ImageJ3DResultsViewer method createImage3DUniverse.

/**
 * Creates the image 3D universe with a unique name.
 *
 * @param title the title
 * @param titleList the title list (of titles to ignore)
 * @return the image 3D universe
 */
private Image3DUniverse createImage3DUniverse(String title, List<String> titleList) {
    // Get a unique name by appending numbers to the end
    String title2 = title;
    int counter = 2;
    while (titleList.contains(title2)) {
        title2 = title + " " + (counter++);
    }
    final Image3DUniverse universe = new Image3DUniverse();
    universe.addUniverseListener(new LocalUniverseListener());
    universe.setShowBoundingBoxUponSelection(false);
    universe.showAttribute(DefaultUniverse.ATTRIBUTE_SCALEBAR, false);
    // Capture a canvas mouse click/region and identify the coordinates.
    final ImageCanvas3D canvas = (ImageCanvas3D) universe.getCanvas();
    final BranchGroup scene = universe.getScene();
    final MouseListener mouseListener = new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent event) {
            if (!consumeEvent(event)) {
                return;
            }
            // Consume the event
            event.consume();
            // This finds the vertex indices of the rendered object.
            final Pair<Content, IntersectionInfo> pair = getPickedContent(canvas, scene, event.getX(), event.getY());
            if (pair == null) {
                // Do the same as the mouseClicked in Image3DUniverse
                universe.select(null);
                return;
            }
            // Only process content added from localisations
            final Content c = pair.getKey();
            if (!(c.getUserData() instanceof ResultsMetaData)) {
                // Do the same as the mouseClicked in Image3DUniverse
                universe.select(c);
                return;
            }
            final ResultsMetaData data = (ResultsMetaData) c.getUserData();
            final MemoryPeakResults results = data.results;
            // Look up the localisation from the clicked vertex
            final ContentInstant content = c.getCurrent();
            int index = -1;
            if (content.getContent() instanceof CustomMeshNode) {
                final CustomMeshNode node = (CustomMeshNode) content.getContent();
                final CustomMesh mesh = node.getMesh();
                int vertexCount;
                final GeometryArray ga = (GeometryArray) mesh.getGeometry();
                // Default to the number of vertices
                vertexCount = ga.getValidVertexCount();
                final int countPerLocalisation = vertexCount / results.size();
                // Determine the localisation
                final int vertexIndex = pair.getValue().getVertexIndices()[0];
                index = vertexIndex / countPerLocalisation;
            } else if (content.getContent() instanceof ItemGroupNode) {
                // All shapes have the index as the user data
                final Object o = pair.getValue().getGeometry().getUserData();
                if (o instanceof Integer) {
                    index = (Integer) pair.getValue().getGeometry().getUserData();
                }
            }
            if (index == -1) {
                return;
            }
            final PeakResult result = results.get(index);
            if (event.getClickCount() > 1) {
                // Centre on the localisation
                final Point3d coordinate = new Point3d();
                coordinate.set(data.points.get(index));
                // Handle the local transform of the content ...
                final Transform3D vWorldToLocal = getVworldToLocal(content);
                vWorldToLocal.transform(coordinate);
                universe.centerAt(coordinate);
            } else if (event.isShiftDown()) {
                // Ctrl+Shift held down to remove selected
                data.removeFromSelectionModel(result);
            } else {
                // Ctrl held down to set selection
                data.addToSelectionModel(result);
            }
        }

        private boolean consumeEvent(final MouseEvent event) {
            if (event.isConsumed() || event.getButton() != MouseEvent.BUTTON1 || !(event.isControlDown())) {
                return false;
            }
            if (event.getClickCount() == 1) {
                return true;
            }
            return (event.getClickCount() == 2);
        }
    };
    // 0 = ImageCanvas3D
    // 1 = DefaultUniverse
    // 2 = Image3DUniverse
    final MouseListener[] l = canvas.getMouseListeners();
    for (int i = 0; i < l.length; i++) {
        if (l[i].getClass().getName().contains("Image3DUniverse")) {
            // We want to be before the Image3DUniverse to allow consuming the click event.
            // Only allow the click event.
            // This disables the right-click pop-up menu.
            // It doesn't have anything of use for localisations anyway.
            canvas.removeMouseListener(l[i]);
            canvas.addMouseListener(mouseListener);
            canvas.addMouseListener(new MouseListenerWrapper(l[i], MouseListenerWrapper.MOUSE_CLICKED));
        }
    }
    // 0 = ImageCanvas3D
    // 1 = DefaultUniverse
    // 2 = Image3DUniverse
    // 3 = EventCatcher (from scijava)
    final MouseMotionListener[] ml = canvas.getMouseMotionListeners();
    for (int i = 0; i < ml.length; i++) {
        if (ml[i].getClass().getName().contains("Image3DUniverse")) {
            // Ignore this as it just shows the name in the IJ status bar
            canvas.removeMouseMotionListener(ml[i]);
        }
    }
    // Finally display the window
    universe.show();
    final ImageWindow3D window = universe.getWindow();
    GUI.center(window);
    window.setTitle(title2);
    WindowManager.addWindow(window);
    window.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent event) {
            WindowManager.removeWindow(window);
        }
    });
    // Add a new menu for SMLM functionality
    createSmlmMenuBar(universe);
    return universe;
}
Also used : CustomMeshNode(customnode.CustomMeshNode) Transform3D(org.scijava.java3d.Transform3D) ItemGroupNode(uk.ac.sussex.gdsc.smlm.ij.ij3d.ItemGroupNode) IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray) WindowAdapter(java.awt.event.WindowAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MouseListener(java.awt.event.MouseListener) Point3d(org.scijava.vecmath.Point3d) CustomMesh(customnode.CustomMesh) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) IntersectionInfo(org.scijava.java3d.PickInfo.IntersectionInfo) MouseEvent(java.awt.event.MouseEvent) Image3DUniverse(ij3d.Image3DUniverse) ImageCanvas3D(ij3d.ImageCanvas3D) BranchGroup(org.scijava.java3d.BranchGroup) MouseAdapter(java.awt.event.MouseAdapter) ContentInstant(ij3d.ContentInstant) CustomContentInstant(uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContentInstant) ImageWindow3D(ij3d.ImageWindow3D) Content(ij3d.Content) CustomContent(uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContent) WindowEvent(java.awt.event.WindowEvent) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject)

Example 30 with MouseMotionListener

use of java.awt.event.MouseMotionListener in project archi by archimatetool.

the class SimpleSwingExample method createMainPanel.

private void createMainPanel() {
    // see below for class definition
    mainPanel = new MainPanel();
    mainPanel.setPreferredSize(new Dimension(INITIAL_PANEL_WIDTH, INITIAL_PANEL_HEIGHT));
    mainPanel.setBackground(Color.WHITE);
    mainPanel.setLayout(null);
    mainFrame.getContentPane().add(new JScrollPane(mainPanel), BorderLayout.CENTER);
    mainPanel.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            selectedEntity = null;
            for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity == null; ) {
                SimpleNode entity = (SimpleNode) iter.next();
                double x = entity.getX();
                double y = entity.getY();
                double w = entity.getWidth();
                double h = entity.getHeight();
                Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h);
                if (rect.contains(e.getX(), e.getY())) {
                    selectedEntity = entity;
                }
            }
            if (selectedEntity != null) {
                mouseDownPoint = e.getPoint();
                selectedEntityPositionAtMouseDown = new Point((int) selectedEntity.getX(), (int) selectedEntity.getY());
            } else {
                mouseDownPoint = null;
                selectedEntityPositionAtMouseDown = null;
            }
            updateGUI();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            selectedEntity = null;
            mouseDownPoint = null;
            selectedEntityPositionAtMouseDown = null;
            updateGUI();
        }
    });
    mainPanel.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseDragged(MouseEvent e) {
        // if (selectedEntity != null) {
        // //TODO: Add mouse moving
        // //selectedEntity.setLocationInLayout(selectedEntityPositionAtMouseDown.x + dx, selectedEntityPositionAtMouseDown.y + dy);
        // updateGUI();
        // }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }
    });
}
Also used : JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) Point(java.awt.Point) LayoutBendPoint(org.eclipse.zest.layouts.LayoutBendPoint) MouseMotionListener(java.awt.event.MouseMotionListener) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode) Iterator(java.util.Iterator)

Aggregations

MouseMotionListener (java.awt.event.MouseMotionListener)30 MouseEvent (java.awt.event.MouseEvent)18 MouseListener (java.awt.event.MouseListener)11 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 MouseAdapter (java.awt.event.MouseAdapter)8 JPanel (javax.swing.JPanel)6 Point (java.awt.Point)4 BoxLayout (javax.swing.BoxLayout)4 JComponent (javax.swing.JComponent)4 JLabel (javax.swing.JLabel)4 Dimension (java.awt.Dimension)3 KeyAdapter (java.awt.event.KeyAdapter)3 KeyEvent (java.awt.event.KeyEvent)3 MouseMotionAdapter (java.awt.event.MouseMotionAdapter)3 JScrollPane (javax.swing.JScrollPane)3 Component (java.awt.Component)2 DropTarget (java.awt.dnd.DropTarget)2 MouseWheelEvent (java.awt.event.MouseWheelEvent)2 MouseWheelListener (java.awt.event.MouseWheelListener)2