Java Code Examples for javafx.scene.input.MouseEvent#MOUSE_PRESSED

The following examples show how to use javafx.scene.input.MouseEvent#MOUSE_PRESSED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RadialMenu.java    From Enzo with Apache License 2.0 6 votes vote down vote up
private void initHandler() {
    mouseHandler          = mouseEvent -> {
        final Object SOURCE = mouseEvent.getSource();
        if (MouseEvent.MOUSE_PRESSED == mouseEvent.getEventType()) {
            MenuItem menuItem = items.get(SOURCE);
            if (menuItem.isSelectable()) {
                menuItem.setSelected(!menuItem.isSelected());
                if (menuItem.isSelected()) {
                    fireItemEvent(new ItemEvent(menuItem, this, null, ItemEvent.ITEM_SELECTED));
                } else {
                    fireItemEvent(new ItemEvent(menuItem, this, null, ItemEvent.ITEM_DESELECTED));
                }
            } else {
                click(menuItem);
                fireItemEvent(new ItemEvent(menuItem, this, null, ItemEvent.ITEM_CLICKED));
            }
        }
    };
}
 
Example 2
Source File: Switch.java    From tilesfx with Apache License 2.0 6 votes vote down vote up
public Switch() {
    getStylesheets().add(Switch.class.getResource("switch.css").toExternalForm());
    aspectRatio      = PREFERRED_HEIGHT / PREFERRED_WIDTH;
    _active          = false;
    _activeColor     = Tile.BLUE;
    _foregroundColor = Tile.FOREGROUND;
    _backgroundColor = Tile.BACKGROUND;

    mouseEventHandler = e -> {
        final EventType TYPE = e.getEventType();
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            setActive(!isActive());
            fireEvent(SWITCH_PRESSED);
        } else if(MouseEvent.MOUSE_RELEASED == TYPE) {
            fireEvent(SWITCH_RELEASED);
        }
    };
    selectedListener = o -> moveThumb();

    timeline = new Timeline();

    initGraphics();
    registerListeners();
}
 
Example 3
Source File: HeatControlSkin.java    From Enzo with Apache License 2.0 6 votes vote down vote up
private void handleMouseEvent(final MouseEvent MOUSE_EVENT) {
    final Object    SRC  = MOUSE_EVENT.getSource();
    final EventType TYPE = MOUSE_EVENT.getEventType();
    if (SRC.equals(targetIndicator)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {    
            userAction = true;
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getTarget()));
            resizeText();                
        } else if (MouseEvent.MOUSE_DRAGGED == TYPE) {
            touchRotate(MOUSE_EVENT.getSceneX() - getSkinnable().getLayoutX(), MOUSE_EVENT.getSceneY() - getSkinnable().getLayoutY(), targetIndicatorRotate);                
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {                
            getSkinnable().setTarget(Double.parseDouble(newTarget));
            fadeBack();
        }
    }
}
 
Example 4
Source File: OverviewPanel.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(final MouseEvent t) {
    if (t.getEventType() == MouseEvent.MOUSE_MOVED) {
        handleResizing(t);
    } else if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
        origin = t.getX(); // Set the origin on all mouse presses.

        handleResizing(t);
    } else if (t.getEventType() == MouseEvent.MOUSE_RELEASED) {
        handleRelease(t);
    } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        handleDragging(t);
    }
}
 
Example 5
Source File: JavaFxRecorderHook.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public MouseEvent getContextMenuMouseEvent(Node source) {
    Bounds boundsInParent = source.getBoundsInParent();
    double x = boundsInParent.getWidth() / 2;
    double y = boundsInParent.getHeight() / 2;
    Point2D screenXY = source.localToScreen(x, y);
    MouseEvent e = new MouseEvent(source, source, MouseEvent.MOUSE_PRESSED, x, y, screenXY.getX(), screenXY.getY(),
            MouseButton.SECONDARY, 1, false, true, false, false, false, false, true, true, true, false, null);
    return e;
}
 
Example 6
Source File: RFXComponent.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void handleRawRecording(IJSONRecorder recorder, Event event) {
    if (event instanceof MouseEvent && event.getEventType() == MouseEvent.MOUSE_PRESSED) {
        recorder.recordRawMouseEvent(this, (MouseEvent) event);
    }
    if (event instanceof KeyEvent && event.getEventType() != KeyEvent.KEY_RELEASED) {
        recorder.recordRawKeyEvent(this, (KeyEvent) event);
    }
}
 
Example 7
Source File: GaugeSkin.java    From Medusa with Apache License 2.0 5 votes vote down vote up
public void handleMouseEvent(final MouseEvent EVENT) {
    if (gauge.isDisabled()) return;
    final EventType TYPE = EVENT.getEventType();
    if (MouseEvent.MOUSE_PRESSED == TYPE) {
        gauge.fireEvent(gauge.BTN_PRESSED_EVENT);
        drawKnob(true);
    } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
        gauge.fireEvent(gauge.BTN_RELEASED_EVENT);
        drawKnob(false);
    }
}
 
Example 8
Source File: VSkin.java    From Medusa with Apache License 2.0 5 votes vote down vote up
public void handleMouseEvent(final MouseEvent EVENT) {
    if (gauge.isDisabled()) return;
    final EventType TYPE = EVENT.getEventType();
    if (MouseEvent.MOUSE_PRESSED == TYPE) {
        gauge.fireEvent(gauge.BTN_PRESSED_EVENT);
        drawKnob(true);
    } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
        gauge.fireEvent(gauge.BTN_RELEASED_EVENT);
        drawKnob(false);
    }
}
 
Example 9
Source File: InteractiveGaugeSkin.java    From medusademo with Apache License 2.0 5 votes vote down vote up
public void handleMouseEvent(final MouseEvent EVENT) {
    if (getSkinnable().isDisabled()) return;
    final EventType TYPE = EVENT.getEventType();
    if (MouseEvent.MOUSE_PRESSED == TYPE) {
        getSkinnable().fireEvent(getSkinnable().BTN_PRESSED_EVENT);
        drawKnob(true);
    } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
        getSkinnable().fireEvent(getSkinnable().BTN_RELEASED_EVENT);
        drawKnob(false);
    }
}
 
Example 10
Source File: HSkin.java    From Medusa with Apache License 2.0 5 votes vote down vote up
public void handleMouseEvent(final MouseEvent EVENT) {
    if (gauge.isDisabled()) return;
    final EventType TYPE = EVENT.getEventType();
    if (MouseEvent.MOUSE_PRESSED == TYPE) {
        gauge.fireEvent(gauge.BTN_PRESSED_EVENT);
        drawKnob(true);
    } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
        gauge.fireEvent(gauge.BTN_RELEASED_EVENT);
        drawKnob(false);
    }
}
 
Example 11
Source File: QuarterSkin.java    From Medusa with Apache License 2.0 5 votes vote down vote up
public void handleMouseEvent(final MouseEvent EVENT) {
    if (gauge.isDisabled()) return;
    final EventType TYPE = EVENT.getEventType();
    if (MouseEvent.MOUSE_PRESSED == TYPE) {
        gauge.fireEvent(gauge.BTN_PRESSED_EVENT);
        drawKnob(true);
    } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
        gauge.fireEvent(gauge.BTN_RELEASED_EVENT);
        drawKnob(false);
    }
}
 
Example 12
Source File: InfluenceAnalysisUI.java    From SONDY with GNU General Public License v3.0 4 votes vote down vote up
public final VBox createNetworkVisualization(){
    System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
    Viewer viewer = new Viewer(AppParameters.authorNetwork, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
    view = viewer.addDefaultView(false); 
    viewer.enableAutoLayout();
    view.resizeFrame(Main.columnWidthLEFT, 290);
    swingNode = new CustomSwingNode();
    swingNode.setContent(view);
    swingNode.resize(Main.columnWidthLEFT, 290);
    EventHandler<MouseEvent> mouseHandlerGraphClick = new EventHandler<MouseEvent>() { 
        @Override 
        public void handle(MouseEvent mouseEvent) { 
            if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
                if(mouseEvent.getButton() == MouseButton.SECONDARY){
                    Node node = (Node) view.findNodeOrSpriteAt(mouseEvent.getX(), mouseEvent.getY());
                    if(node != null){
                        userMessages(node.getId());
                    }
                }
                if(mouseEvent.getButton() == MouseButton.PRIMARY){
                    double translateCoeff = view.getCamera().getViewPercent();
                    Point3 center = view.getCamera().getViewCenter();
                    if(mouseEvent.getY()>175){
                        view.getCamera().setViewCenter(center.x, center.y-5*translateCoeff, center.z);
                    }else{
                        view.getCamera().setViewCenter(center.x, center.y+5*translateCoeff, center.z);
                    }
                    if(mouseEvent.getX()>275){
                        view.getCamera().setViewCenter(center.x+5*translateCoeff, center.y, center.z);
                    }else{
                        view.getCamera().setViewCenter(center.x-5*translateCoeff, center.y, center.z);
                    }
                }
                if(mouseEvent.getButton() == MouseButton.MIDDLE){
                    view.getCamera().setViewPercent(view.getCamera().getViewPercent()/2);
                }
            }
        }
    };   
    EventHandler<ScrollEvent> mouseHandlerGraphScroll = new EventHandler<ScrollEvent>() { 
        @Override
        public void handle(ScrollEvent event) {
            if(event.getDeltaY() < 0){
                view.getCamera().setViewPercent(view.getCamera().getViewPercent()*2);
            }else{
                view.getCamera().setViewPercent(view.getCamera().getViewPercent()/2);
            }
        }
    };   
    swingNode.setOnMousePressed(mouseHandlerGraphClick);
    swingNode.setOnScroll(mouseHandlerGraphScroll);
    VBox graphBox = new VBox();
    graphBox.getChildren().addAll(new Rectangle(Main.columnWidthLEFT,0),swingNode);
    initializeNetworkVisualizationStyle();
    return graphBox;
}
 
Example 13
Source File: CameraController.java    From FXyzLib with GNU General Public License v3.0 4 votes vote down vote up
private void handleMouseEvent(MouseEvent t) {

        if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
            switch (t.getButton()) {
                case PRIMARY:
                    handlePrimaryMousePress(t);
                    break;
                case MIDDLE:
                    handleMiddleMousePress(t);
                    break;
                case SECONDARY:
                    handleSecondaryMousePress(t);
                    break;
                default:
                    throw new AssertionError();
            }
            handleMousePress(t);
        } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            Point2D d = getMouseDelta(t);

            switch (t.getButton()) {
                case PRIMARY:
                    handlePrimaryMouseDrag(t, d, speed);
                    break;
                case MIDDLE:
                    handleMiddleMouseDrag(t, d, speed);
                    break;
                case SECONDARY:
                    handleSecondaryMouseDrag(t, d, speed);
                    break;
                default:
                    throw new AssertionError();
            }
        } else if (t.getEventType() == MouseEvent.MOUSE_MOVED) {
            handleMouseMoved(t, getMouseDelta(t), speed);
        } else if (t.getEventType() == MouseEvent.MOUSE_CLICKED) {
            switch (t.getButton()) {
                case PRIMARY:
                    handlePrimaryMouseClick(t);
                    break;
                case MIDDLE:
                    handleMiddleMouseClick(t);
                    break;
                case SECONDARY:
                    handleSecondaryMouseClick(t);
                    break;
                default:
                    throw new AssertionError();
            }
        }
    }
 
Example 14
Source File: RadialBargraphSkin.java    From Enzo with Apache License 2.0 4 votes vote down vote up
private void handleMouseEvent(final MouseEvent MOUSE_EVENT) {
    final Object    SRC  = MOUSE_EVENT.getSource();
    final EventType TYPE = MOUSE_EVENT.getEventType();
    if (getSkinnable().isInteractive() && SRC.equals(threshold)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Threshold");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getThreshold()));
            resizeText();
        } else if (MouseEvent.MOUSE_DRAGGED == TYPE) {
            touchRotate(MOUSE_EVENT.getSceneX() - getSkinnable().getLayoutX(), MOUSE_EVENT.getSceneY() - getSkinnable().getLayoutY(), thresholdRotate);
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            getSkinnable().setThreshold(Double.parseDouble(value.getText()));
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC instanceof Marker) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText(((Marker) SRC).getText());
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", ((Marker) SRC).getValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_DRAGGED == TYPE) {
            touchRotate(MOUSE_EVENT.getSceneX() - getSkinnable().getLayoutX(), MOUSE_EVENT.getSceneY() - getSkinnable().getLayoutY(), getSkinnable().getMarkers().get(SRC));
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            ((Marker) SRC).setValue(Double.parseDouble(value.getText()));
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC.equals(minMeasuredValue)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Min");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getMinMeasuredValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC.equals(maxMeasuredValue)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Max");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getMaxMeasuredValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            fadeBackToInteractive();
        }
    }
}
 
Example 15
Source File: GaugeSkin.java    From Enzo with Apache License 2.0 4 votes vote down vote up
private void handleMouseEvent(final MouseEvent MOUSE_EVENT) {
    final Object    SRC  = MOUSE_EVENT.getSource();
    final EventType TYPE = MOUSE_EVENT.getEventType();
    if (getSkinnable().isInteractive() && SRC.equals(threshold)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Threshold");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getThreshold()));
            resizeText();
        } else if (MouseEvent.MOUSE_DRAGGED == TYPE) {
            touchRotate(MOUSE_EVENT.getSceneX() - getSkinnable().getLayoutX(), MOUSE_EVENT.getSceneY() - getSkinnable().getLayoutY(), thresholdRotate);
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            getSkinnable().setThreshold(Double.parseDouble(value.getText()));
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC instanceof Marker) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText(((Marker) SRC).getText());
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", ((Marker) SRC).getValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_DRAGGED == TYPE) {
            touchRotate(MOUSE_EVENT.getSceneX() - getSkinnable().getLayoutX(), MOUSE_EVENT.getSceneY() - getSkinnable().getLayoutY(), getSkinnable().getMarkers().get(SRC));
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            ((Marker) SRC).setValue(Double.parseDouble(value.getText()));
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC.equals(minMeasuredValue)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Min");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getMinMeasuredValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            fadeBackToInteractive();
        }
    } else if (getSkinnable().isInteractive() && SRC.equals(maxMeasuredValue)) {
        if (MouseEvent.MOUSE_PRESSED == TYPE) {
            unit.setText("Max");
            value.setText(String.format(Locale.US, "%." + getSkinnable().getDecimals() + "f", getSkinnable().getMaxMeasuredValue()));
            resizeText();
        } else if (MouseEvent.MOUSE_RELEASED == TYPE) {
            fadeBackToInteractive();
        }
    }
}
 
Example 16
Source File: GUIConsoleInput.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new GUI console input.
 *
 * @param area GUI text area
 */
public GUIConsoleInput(InlineCssTextArea area) {
  this.area = area;
  initialPos = -1;
  inputText = null;
  area.setEditable(false);
  queue = new ArrayBlockingQueue<>(1);
  listener = new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent e) {
      switch (e.getCode()) {
        case UP:
        case KP_UP:
        case PAGE_UP:
          area.moveTo(initialPos);
          e.consume();
          break;
        case DOWN:
        case KP_DOWN:
        case PAGE_DOWN:
          area.moveTo(area.getLength());
          e.consume();
          break;
        // ensure always that caret position is >= initialPos
        case LEFT:
        case KP_LEFT:
        case BACK_SPACE:
          if (area.getCaretPosition() == initialPos)
            e.consume();
          break;
        // return user response
        case ENTER:
          // after user release enter key return response
          if (e.getEventType() == KeyEvent.KEY_RELEASED)
            returnResponse();
          break;
        // nothing
        default:
          // change area input color
          if (initialPos < area.getLength()) {
            area.setStyle(initialPos, area.getLength(), "-fx-fill: #4a148c;");
          }
          break;
      }
    }
  };
  mouseListener = new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent e) {
      EventType<? extends MouseEvent> type = e.getEventType();
      // disable mouse selection
      if (type == MouseEvent.DRAG_DETECTED || type == MouseEvent.MOUSE_DRAGGED)
        e.consume();
      // control caret position
      if (type == MouseEvent.MOUSE_CLICKED || type == MouseEvent.MOUSE_RELEASED || type == MouseEvent.MOUSE_PRESSED) {
        if (e.getButton() == MouseButton.PRIMARY) {
          if (area.getCaretPosition() < initialPos) {
            area.setDisable(true);
            area.moveTo(initialPos);
            area.setDisable(false);
          }
        } else {
          e.consume();
        }
      }
    }
  };
  stopListener = new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue obs, Boolean oldValue, Boolean newValue) {
      if (!newValue) {
        returnResponse();
      }
    }
  };
}
 
Example 17
Source File: JavaFxRecorderHook.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private boolean isContextMenuMouseEvent(MouseEvent event) {
    if (event.getEventType() != MouseEvent.MOUSE_PRESSED)
        return false;
    return mouseModifiers.equals(mouseEventGetModifiersExText(event));
}
 
Example 18
Source File: ImagePanel.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(MouseEvent e) {
    if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
        requestFocus();
        if (new_rectangle_is_being_drawn == false) {
            starting_point_x = e.getX();
            starting_point_y = e.getY();
            newRect = new Annotation();
            newRect.setFill(SELECTED_ANNOTATION_COLOR);
            new_rectangle_is_being_drawn = true;
            anchorPane.getChildren().add(newRect);
        }
    }
    if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        if (!edit) {
            return;
        }
        if (new_rectangle_is_being_drawn == true) {
            double current_ending_point_x = e.getX();
            double current_ending_point_y = e.getY();
            if (current_ending_point_x < 0) {
                current_ending_point_x = 0;
            }
            if (current_ending_point_y < 0) {
                current_ending_point_y = 0;
            }
            if (current_ending_point_x > canvas.getWidth()) {
                current_ending_point_x = canvas.getWidth();
            }
            if (current_ending_point_y > canvas.getHeight()) {
                current_ending_point_y = canvas.getHeight();
            }

            adjust_rectangle_properties(starting_point_x, starting_point_y, current_ending_point_x, current_ending_point_y,
                    newRect);
            ensureVisible(scrollPane, newRect);
        }
    }
    if (e.getEventType() == MouseEvent.MOUSE_RELEASED) {
        anchorPane.getChildren().remove(newRect);
        newRect.setFill(ANNOTATION_COLOR);
        newRect.setText("Annotation");
        if (newRect.getWidth() > 10 && newRect.getHeight() > 10) {
            System.out.println("ImagePanel.ImagePanelMouseListener.handle(" + newRect + ")");
            annotations.add(newRect);
        }
        drawGraphics();
        newRect = null;
        new_rectangle_is_being_drawn = false;
    }
}
 
Example 19
Source File: SwitchTileSkin.java    From OEE-Designer with MIT License 4 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override protected void initGraphics() {
       super.initGraphics();

       mouseEventHandler = e -> {
           final EventType TYPE = e.getEventType();
           if (MouseEvent.MOUSE_PRESSED == TYPE) {
               tile.setActive(!tile.isActive());
               tile.fireEvent(SWITCH_PRESSED);
           } else if(MouseEvent.MOUSE_RELEASED == TYPE) {
               tile.fireEvent(SWITCH_RELEASED);
           }
       };
       selectedListener = o -> moveThumb();

       timeline = new Timeline();

       titleText = new Text();
       titleText.setFill(tile.getTitleColor());
       Helper.enableNode(titleText, !tile.getTitle().isEmpty());

       text = new Text(tile.getText());
       text.setFill(tile.getUnitColor());
       Helper.enableNode(text, tile.isTextVisible());

       description = new Label(tile.getDescription());
       description.setAlignment(tile.getDescriptionAlignment());
       description.setWrapText(true);
       description.setTextFill(tile.getTextColor());
       Helper.enableNode(description, !tile.getDescription().isEmpty());

       switchBorder = new Rectangle();

       switchBackground = new Rectangle();
       switchBackground.setMouseTransparent(true);
       switchBackground.setFill(tile.isActive() ? tile.getActiveColor() : tile.getBackgroundColor());

       thumb = new Circle();
       thumb.setMouseTransparent(true);
       thumb.setEffect(shadow);

       getPane().getChildren().addAll(titleText, text, description, switchBorder, switchBackground, thumb);
   }
 
Example 20
Source File: DragSupport.java    From scenic-view with GNU General Public License v3.0 4 votes vote down vote up
public DragSupport(Node target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property<Number> property, final double factor) {
    this.target = target;
    mouseEventHandler = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            if (t.getEventType() != MouseEvent.MOUSE_ENTERED_TARGET
                    && t.getEventType() != MouseEvent.MOUSE_EXITED_TARGET) {
                lastMouseEvent = t;
            }
            if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    anchor = property.getValue();
                    dragAnchor = getCoord(t, orientation);
                    t.consume();
                }
            } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    property.setValue(anchor.doubleValue()
                            + (getCoord(t, orientation) - dragAnchor) * factor);
                    t.consume();
                }
            }
        }
    };
    keyboardEventHandler = (KeyEvent t) -> {
        if (t.getEventType() == KeyEvent.KEY_PRESSED) {
            if (t.getCode() == modifier) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        } else if (t.getEventType() == KeyEvent.KEY_RELEASED) {
            if (t.getCode() != modifier && isModifierCorrect(t, modifier)) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        }
    };
    target.addEventHandler(MouseEvent.ANY, mouseEventHandler);
    target.addEventHandler(KeyEvent.ANY, keyboardEventHandler);
}