Example usage for javafx.scene.control MenuItem setOnAction

List of usage examples for javafx.scene.control MenuItem setOnAction

Introduction

In this page you can find the example usage for javafx.scene.control MenuItem setOnAction.

Prototype

public final void setOnAction(EventHandler<ActionEvent> value) 

Source Link

Usage

From source file:org.beryx.viewreka.fxapp.codearea.ViewrekaCodeArea.java

protected void showContextMenu(ContextMenu cm) {
    if (codeTemplates.isEmpty())
        return;/*from   w ww.j a  v a  2  s . co  m*/

    cm.getItems().clear();
    for (CodeTemplate template : codeTemplates) {
        // TODO retrieve context and create MenuItem only if template.isAllowedInContext() returns true
        MenuItem item = new MenuItem(template.getName());
        item.setOnAction(ev -> {
            if (classLoaderManager != null) {
                classLoaderManager.setProjectClassLoader();
            }
            try {
                insertCodeFragment(template);
            } finally {
                classLoaderManager.resetClassLoader();
            }
        });
        cm.getItems().add(item);
    }
    cm.getItems()
            .sort((item1, item2) -> item1.getText().toLowerCase().compareTo(item2.getText().toLowerCase()));
    cm.show(getScene().getWindow());
}

From source file:org.pdfsam.ui.selection.single.SingleSelectionPane.java

private void initContextMenu() {
    MenuItem infoItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Document properties"),
            AwesomeIcon.INFO);//w  w  w  . j a  va  2  s  .c  om
    infoItem.setOnAction(e -> Platform.runLater(() -> {
        eventStudio().broadcast(new ShowPdfDescriptorRequest(descriptor));
    }));

    MenuItem setDestinationItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Set output"),
            AwesomeIcon.PENCIL_SQUARE_ALT);
    setDestinationItem.setOnAction(e -> {
        eventStudio().broadcast(new SetDestinationRequest(descriptor.getFile().getParentFile()),
                getOwnerModule());
    });

    MenuItem openFileItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Open"), AwesomeIcon.FILE_ALT);
    openFileItem.setOnAction(e -> {
        eventStudio().broadcast(new OpenFileRequest(descriptor.getFile()));
    });

    MenuItem openFolderItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Open Folder"),
            AwesomeIcon.FOLDER_OPEN);
    openFolderItem.setOnAction(e -> {
        eventStudio().broadcast(new OpenFileRequest(descriptor.getFile().getParentFile()));
    });

    field.getTextField().setContextMenu(new ContextMenu(setDestinationItem, new SeparatorMenuItem(), infoItem,
            openFileItem, openFolderItem));
}

From source file:com.heliosdecompiler.helios.gui.view.editors.DisassemblerView.java

@Override
protected Node createView0(OpenedFile file, String path) {
    CodeArea codeArea = new CodeArea();

    if (controller instanceof KrakatauDisassemblerController) {
        ContextMenu contextMenu = new ContextMenu();

        MenuItem save = new MenuItem("Assemble");
        save.setOnAction(e -> {
            save(codeArea).whenComplete((res, err) -> {
                if (err != null) {
                    if (err instanceof KrakatauException) {
                        StringBuilder message = new StringBuilder();
                        message.append("stdout:\r\n").append(((KrakatauException) err).getStdout())
                                .append("\r\n\r\nstderr:\r\n").append(((KrakatauException) err).getStderr());

                        messageHandler.handleLongMessage(Message.ERROR_FAILED_TO_ASSEMBLE_KRAKATAU,
                                message.toString());
                    } else {
                        messageHandler.handleException(Message.ERROR_UNKNOWN_ERROR.format(), err);
                    }/*from  w w  w  .  j a  v a2  s .c  o  m*/
                } else {
                    file.putContent(path, res);
                    messageHandler.handleMessage(Message.GENERIC_ASSEMBLED.format());
                }
            });
        });

        contextMenu.getItems().add(save);
        codeArea.setContextMenu(contextMenu);
    }

    codeArea.setStyle("-fx-font-size: 1em");
    codeArea.getProperties().put("fontSize", 1);

    codeArea.setParagraphGraphicFactory(line -> {
        Node label = LineNumberFactory.get(codeArea, (digits) -> "%1$" + digits + "d").apply(line);
        label.styleProperty().bind(codeArea.styleProperty());
        return label;
    });
    codeArea.replaceText("Disassembling... this may take a while");
    codeArea.getUndoManager().forgetHistory();

    codeArea.richChanges().filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX
            .successionEnds(Duration.ofMillis(500)).supplyTask(() -> computeHighlightingAsync(codeArea))
            .awaitLatest(codeArea.richChanges()).filterMap(t -> {
                if (t.isSuccess()) {
                    return Optional.of(t.get());
                } else {
                    t.getFailure().printStackTrace();
                    return Optional.empty();
                }
            }).subscribe(f -> applyHighlighting(codeArea, f));
    codeArea.getStylesheets().add(getClass().getResource("/java-keywords.css").toExternalForm());

    codeArea.addEventFilter(ScrollEvent.SCROLL, e -> {
        if (e.isShortcutDown()) {
            if (e.getDeltaY() > 0) {
                int size = (int) codeArea.getProperties().get("fontSize") + 1;
                codeArea.setStyle("-fx-font-size: " + size + "em");
                codeArea.getProperties().put("fontSize", size);
            } else {
                int size = (int) codeArea.getProperties().get("fontSize") - 1;
                if (size > 0) {
                    codeArea.setStyle("-fx-font-size: " + size + "em");
                    codeArea.getProperties().put("fontSize", size);
                }
            }
            e.consume();
        }
    });

    controller.disassemble(file, path, (success, text) -> {
        Platform.runLater(() -> {
            codeArea.replaceText(text);
            codeArea.getUndoManager().forgetHistory();
        });
    });

    return new VirtualizedScrollPane<>(codeArea);
}

From source file:de.pixida.logtest.designer.automaton.Graph.java

Graph(final IAutomatonEditor aAutomatonEditor) {
    Validate.notNull(aAutomatonEditor);/*  ww w.  j  a  v  a2 s.  co  m*/
    this.automatonEditor = aAutomatonEditor;

    this.pane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
    final double minSizeX = 800.0;
    final double minSizeY = 600.0;
    this.pane.setMinSize(minSizeX, minSizeY);
    final double paddingRightBottom = 8.0;
    this.pane.setPadding(new Insets(0.0, paddingRightBottom, paddingRightBottom, 0.0));
    this.pane.setOnMouseMoved(event -> this.mouseMovedOnPane(event));
    this.pane.setOnMouseClicked(event -> this.mouseClickedOnPane(event));
    final MenuItem mi = new MenuItem("Create new state");
    mi.setGraphic(Icons.getIconGraphics("pencil_add"));
    mi.setOnAction(event -> {
        // The following code does not really belong here as the class is designed only to handle business belongings... it should be
        // moved somewhere else!?
        final AutomatonNode newNode = new AutomatonNode(Graph.this);
        newNode.setPosition(new Point2D(this.lastMousePositionOnPaneX, this.lastMousePositionOnPaneY));
        newNode.setName("New state");
        Graph.this.addObject(newNode);
        Graph.this.handleChange();
        Graph.this.showConfigFrameOfObject(newNode);
    });
    this.contextMenu.getItems().add(mi);
}

From source file:com.github.douglasjunior.simpleCSVEditor.FXMLController.java

@Override
public void initialize(URL url, ResourceBundle rb) {
    file = new File("");
    csvFormat = CSVFormat.DEFAULT.withIgnoreEmptyLines(false);

    ContextMenu contextMenu = new ContextMenu();
    contextMenu.setAutoHide(true);/*from w w  w  .ja  v a2s. c  o m*/
    MenuItem inserirLinha = new MenuItem("Inserir linha");
    inserirLinha.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            addNewRow();
            setNotSaved();
        }
    });

    contextMenu.getItems().add(inserirLinha);
    MenuItem removerLinha = new MenuItem("Remover linha");
    removerLinha.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            deleteRow();
            setNotSaved();
        }
    });
    contextMenu.getItems().add(removerLinha);

    contextMenu.getItems().add(new SeparatorMenuItem());

    MenuItem inserirColuna = new MenuItem("Inserir coluna");
    inserirColuna.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            addNewColumn();
            setNotSaved();
        }
    });
    contextMenu.getItems().add(inserirColuna);

    MenuItem removerColuna = new MenuItem("Remover coluna");
    removerColuna.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            deleteColumn();
            setNotSaved();
        }
    });
    contextMenu.getItems().add(removerColuna);

    tableView.setContextMenu(contextMenu);
}

From source file:Main.java

@Override
public void start(Stage stage) {
    stage.setTitle("Menu Sample");
    Scene scene = new Scene(new VBox(), 400, 350);
    scene.setFill(Color.OLDLACE);

    name.setFont(new Font("Verdana Bold", 22));
    binName.setFont(new Font("Arial Italic", 10));
    pic.setFitHeight(150);//w w  w.j a  v a 2s  .  c  om
    pic.setPreserveRatio(true);
    description.setWrapText(true);
    description.setTextAlignment(TextAlignment.JUSTIFY);

    shuffle();

    MenuBar menuBar = new MenuBar();

    // --- Graphical elements
    final VBox vbox = new VBox();
    vbox.setAlignment(Pos.CENTER);
    vbox.setSpacing(10);
    vbox.setPadding(new Insets(0, 10, 0, 10));
    vbox.getChildren().addAll(name, binName, pic, description);

    // --- Menu File
    Menu menuFile = new Menu("File");
    MenuItem add = new MenuItem("Shuffle", new ImageView(new Image("src/menusample/new.png")));
    add.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            shuffle();
            vbox.setVisible(true);
        }
    });

    MenuItem clear = new MenuItem("Clear");
    clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
    clear.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            vbox.setVisible(false);
        }
    });

    MenuItem exit = new MenuItem("Exit");
    exit.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            System.exit(0);
        }
    });

    menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);

    // --- Menu Edit
    Menu menuEdit = new Menu("Edit");
    Menu menuEffect = new Menu("Picture Effect");

    final ToggleGroup groupEffect = new ToggleGroup();
    for (Entry effect : effects) {
        RadioMenuItem itemEffect = new RadioMenuItem((String) effect.getKey());
        itemEffect.setUserData(effect.getValue());
        itemEffect.setToggleGroup(groupEffect);
        menuEffect.getItems().add(itemEffect);
    }

    final MenuItem noEffects = new MenuItem("No Effects");
    noEffects.setDisable(true);
    noEffects.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            pic.setEffect(null);
            groupEffect.getSelectedToggle().setSelected(false);
            noEffects.setDisable(true);
        }
    });

    groupEffect.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
        public void changed(ObservableValue ov, Toggle old_toggle, Toggle new_toggle) {
            if (groupEffect.getSelectedToggle() != null) {
                Effect effect = (Effect) groupEffect.getSelectedToggle().getUserData();
                pic.setEffect(effect);
                noEffects.setDisable(false);
            } else {
                noEffects.setDisable(true);
            }
        }
    });

    menuEdit.getItems().addAll(menuEffect, noEffects);

    // --- Menu View
    Menu menuView = new Menu("View");
    CheckMenuItem titleView = createMenuItem("Title", name);
    CheckMenuItem binNameView = createMenuItem("Binomial name", binName);
    CheckMenuItem picView = createMenuItem("Picture", pic);
    CheckMenuItem descriptionView = createMenuItem("Decsription", description);

    menuView.getItems().addAll(titleView, binNameView, picView, descriptionView);
    menuBar.getMenus().addAll(menuFile, menuEdit, menuView);

    // --- Context Menu
    final ContextMenu cm = new ContextMenu();
    MenuItem cmItem1 = new MenuItem("Copy Image");
    cmItem1.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            Clipboard clipboard = Clipboard.getSystemClipboard();
            ClipboardContent content = new ClipboardContent();
            content.putImage(pic.getImage());
            clipboard.setContent(content);
        }
    });

    cm.getItems().add(cmItem1);
    pic.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            if (e.getButton() == MouseButton.SECONDARY)
                cm.show(pic, e.getScreenX(), e.getScreenY());
        }
    });

    ((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);

    stage.setScene(scene);
    stage.show();
}

From source file:net.sf.mzmine.chartbasics.gui.javafx.EChartViewer.java

protected void addMenuItem(Menu parent, String title, EventHandler<ActionEvent> al) {
    MenuItem pngItem = new MenuItem(title);
    pngItem.setOnAction(al);
    parent.getItems().add(pngItem);//  ww  w . ja  va  2s .  c o m
}

From source file:net.sf.mzmine.chartbasics.gui.javafx.EChartViewer.java

protected void addMenuItem(ContextMenu parent, String title, EventHandler<ActionEvent> al) {
    MenuItem pngItem = new MenuItem(title);
    pngItem.setOnAction(al);
    parent.getItems().add(pngItem);//from   w w  w .ja  v  a 2 s .  co  m
}

From source file:net.sourceforge.pmd.util.fxdesigner.XPathPanelController.java

private void initGenerateXPathFromStackTrace() {

    ContextMenu menu = new ContextMenu();

    MenuItem item = new MenuItem("Generate from stack trace...");
    item.setOnAction(e -> {
        try {/*  w  ww  .j  a va2 s.c o m*/
            Stage popup = new Stage();
            FXMLLoader loader = new FXMLLoader(DesignerUtil.getFxml("generate-xpath-from-stack-trace.fxml"));
            Parent root = loader.load();
            Button button = (Button) loader.getNamespace().get("generateButton");
            TextArea area = (TextArea) loader.getNamespace().get("stackTraceArea");

            ValidationSupport validation = new ValidationSupport();

            validation.registerValidator(area,
                    Validator.createEmptyValidator("The stack trace may not be empty"));
            button.disableProperty().bind(validation.invalidProperty());

            button.setOnAction(f -> {
                DesignerUtil.stackTraceToXPath(area.getText()).ifPresent(xpathExpressionArea::replaceText);
                popup.close();
            });

            popup.setScene(new Scene(root));
            popup.initStyle(StageStyle.UTILITY);
            popup.initModality(Modality.WINDOW_MODAL);
            popup.initOwner(designerRoot.getMainStage());
            popup.show();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    });

    menu.getItems().add(item);

    xpathExpressionArea.addEventHandler(MouseEvent.MOUSE_CLICKED, t -> {
        if (t.getButton() == MouseButton.SECONDARY) {
            menu.show(xpathExpressionArea, t.getScreenX(), t.getScreenY());
        }
    });
}

From source file:org.pdfsam.ui.selection.multiple.SelectionTable.java

private void initContextMenu() {
    MenuItem infoItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Document properties"),
            AwesomeIcon.INFO);//from w  ww . j  a va2 s .  com
    infoItem.setOnAction(e -> Platform.runLater(() -> {
        eventStudio().broadcast(
                new ShowPdfDescriptorRequest(getSelectionModel().getSelectedItem().getPdfDocumentDescriptor()));
    }));
    MenuItem setDestinationItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Set output"),
            AwesomeIcon.PENCIL_SQUARE_ALT);

    setDestinationItem.setOnAction(e -> {
        File outFile = new File(
                getSelectionModel().getSelectedItem().getPdfDocumentDescriptor().getFile().getParent(),
                "out.pdf");
        eventStudio().broadcast(new SetDestinationRequest(outFile), getOwnerModule());
    });

    MenuItem removeSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Remove"),
            AwesomeIcon.MINUS_SQUARE_ALT);

    removeSelected.setOnAction(e -> eventStudio().broadcast(new RemoveSelectedEvent(), getOwnerModule()));

    MenuItem moveTopSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Top"),
            AwesomeIcon.ANGLE_DOUBLE_UP);
    moveTopSelected
            .setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.TOP), getOwnerModule()));

    MenuItem moveUpSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Up"),
            AwesomeIcon.ANGLE_UP);
    moveUpSelected
            .setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.UP), getOwnerModule()));

    MenuItem moveDownSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move Down"),
            AwesomeIcon.ANGLE_DOWN);
    moveDownSelected
            .setOnAction(e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.DOWN), getOwnerModule()));

    MenuItem moveBottomSelected = createMenuItem(DefaultI18nContext.getInstance().i18n("Move to Bottom"),
            AwesomeIcon.ANGLE_DOUBLE_DOWN);
    moveBottomSelected.setOnAction(
            e -> eventStudio().broadcast(new MoveSelectedEvent(MoveType.BOTTOM), getOwnerModule()));

    MenuItem openFileItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Open"), AwesomeIcon.FILE_ALT);
    openFileItem.setOnAction(e -> {
        eventStudio().broadcast(new OpenFileRequest(
                getSelectionModel().getSelectedItem().getPdfDocumentDescriptor().getFile()));
    });

    MenuItem openFolderItem = createMenuItem(DefaultI18nContext.getInstance().i18n("Open Folder"),
            AwesomeIcon.FOLDER_OPEN);
    openFolderItem.setOnAction(e -> {
        eventStudio().broadcast(new OpenFileRequest(
                getSelectionModel().getSelectedItem().getPdfDocumentDescriptor().getFile().getParentFile()));
    });
    // https://javafx-jira.kenai.com/browse/RT-28136
    // infoItem.setAccelerator(new KeyCodeCombination(KeyCode.P, KeyCombination.ALT_DOWN));
    // setDestinationItem.setAccelerator(new KeyCodeCombination(KeyCode.O, KeyCombination.ALT_DOWN));
    // removeSelected.setAccelerator(new KeyCodeCombination(KeyCode.CANCEL));
    // moveBottomSelected.setAccelerator(new KeyCodeCombination(KeyCode.END, KeyCombination.ALT_DOWN));
    // moveDownSelected.setAccelerator(new KeyCodeCombination(KeyCode.DOWN, KeyCombination.ALT_DOWN));
    // moveUpSelected.setAccelerator(new KeyCodeCombination(KeyCode.UP, KeyCombination.ALT_DOWN));
    // moveTopSelected.setAccelerator(new KeyCodeCombination(KeyCode.HOME, KeyCombination.ALT_DOWN));

    eventStudio().add(SelectionChangedEvent.class, (SelectionChangedEvent e) -> {
        setDestinationItem.setDisable(!e.isSingleSelection());
        infoItem.setDisable(!e.isSingleSelection());
        openFileItem.setDisable(!e.isSingleSelection());
        openFolderItem.setDisable(!e.isSingleSelection());
        removeSelected.setDisable(e.isClearSelection());
        moveTopSelected.setDisable(!e.canMove(MoveType.TOP));
        moveUpSelected.setDisable(!e.canMove(MoveType.UP));
        moveDownSelected.setDisable(!e.canMove(MoveType.DOWN));
        moveBottomSelected.setDisable(!e.canMove(MoveType.BOTTOM));

    }, getOwnerModule());
    setContextMenu(new ContextMenu(setDestinationItem, new SeparatorMenuItem(), removeSelected, moveTopSelected,
            moveUpSelected, moveDownSelected, moveBottomSelected, new SeparatorMenuItem(), infoItem,
            openFileItem, openFolderItem));
}