Example usage for javafx.scene.control ContextMenu setAutoHide

List of usage examples for javafx.scene.control ContextMenu setAutoHide

Introduction

In this page you can find the example usage for javafx.scene.control ContextMenu setAutoHide.

Prototype

public final void setAutoHide(boolean value) 

Source Link

Usage

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);
    MenuItem inserirLinha = new MenuItem("Inserir linha");
    inserirLinha.setOnAction(new EventHandler<ActionEvent>() {
        @Override/*w ww  . j a va2s . com*/
        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:org.sleuthkit.autopsy.imageanalyzer.gui.GroupPane.java

/**
 * called automatically during constructor by FXMLConstructor.
 *
 * checks that FXML loading went ok and performs additional setup
 *///from www . j  a  v  a 2  s . com
@FXML
void initialize() {
    assert gridView != null : "fx:id=\"tilePane\" was not injected: check your FXML file 'GroupPane.fxml'.";
    assert grpCatSplitMenu != null : "fx:id=\"grpCatSplitMenu\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert grpTagSplitMenu != null : "fx:id=\"grpTagSplitMenu\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert headerToolBar != null : "fx:id=\"headerToolBar\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert segButton != null : "fx:id=\"previewList\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert slideShowToggle != null : "fx:id=\"segButton\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert tileToggle != null : "fx:id=\"tileToggle\" was not injected: check your FXML file 'GroupHeader.fxml'.";

    grouping.addListener(new InvalidationListener() {
        private void updateFiles() {
            final ObservableList<Long> fileIds = grouping.get().fileIds();
            Platform.runLater(() -> {
                gridView.setItems(FXCollections.observableArrayList(fileIds));
            });
            resetHeaderString();
        }

        @Override
        public void invalidated(Observable o) {
            getScrollBar().ifPresent((scrollBar) -> {
                scrollBar.setValue(0);
            });

            //set the embeded header
            resetHeaderString();
            //and assign fileIDs to gridView
            if (grouping.get() == null) {
                Platform.runLater(gridView.getItems()::clear);

            } else {
                grouping.get().fileIds().addListener((Observable observable) -> {
                    updateFiles();
                });

                updateFiles();
            }
        }
    });

    //configure flashing glow animation on next unseen group button
    flashAnimation.setCycleCount(Timeline.INDEFINITE);
    flashAnimation.setAutoReverse(true);

    //configure gridView cell properties
    gridView.cellHeightProperty().bind(Toolbar.getDefault().sizeSliderValue().add(75));
    gridView.cellWidthProperty().bind(Toolbar.getDefault().sizeSliderValue().add(75));
    gridView.setCellFactory((GridView<Long> param) -> new DrawableCell());

    //configure toolbar properties
    HBox.setHgrow(spacer, Priority.ALWAYS);
    spacer.setMinWidth(Region.USE_PREF_SIZE);

    ArrayList<MenuItem> grpTagMenues = new ArrayList<>();
    for (final TagName tn : TagUtils.getNonCategoryTagNames()) {
        MenuItem menuItem = createGrpTagMenuItem(tn);
        grpTagMenues.add(menuItem);
    }
    try {
        grpTagSplitMenu.setText(TagUtils.getFollowUpTagName().getDisplayName());
        grpTagSplitMenu.setOnAction(createGrpTagMenuItem(TagUtils.getFollowUpTagName()).getOnAction());
    } catch (TskCoreException tskCoreException) {
        LOGGER.log(Level.WARNING, "failed to load FollowUpTagName", tskCoreException);
    }
    grpTagSplitMenu.setGraphic(new ImageView(DrawableAttribute.TAGS.getIcon()));
    grpTagSplitMenu.getItems().setAll(grpTagMenues);

    ArrayList<MenuItem> grpCategoryMenues = new ArrayList<>();
    for (final Category cat : Category.values()) {
        MenuItem menuItem = createGrpCatMenuItem(cat);
        grpCategoryMenues.add(menuItem);
    }
    grpCatSplitMenu.setText(Category.FIVE.getDisplayName());
    grpCatSplitMenu.setGraphic(new ImageView(DrawableAttribute.CATEGORY.getIcon()));
    grpCatSplitMenu.getItems().setAll(grpCategoryMenues);
    grpCatSplitMenu.setOnAction(createGrpCatMenuItem(Category.FIVE).getOnAction());

    Runnable syncMode = () -> {
        switch (groupViewMode.get()) {
        case SLIDE_SHOW:
            slideShowToggle.setSelected(true);
            break;
        case TILE:
            tileToggle.setSelected(true);
            break;
        }
    };
    syncMode.run();
    //make togle states match view state
    groupViewMode.addListener((o) -> {
        syncMode.run();
    });

    slideShowToggle.toggleGroupProperty().addListener((o) -> {
        slideShowToggle.getToggleGroup().selectedToggleProperty()
                .addListener((observable, oldToggle, newToggle) -> {
                    if (newToggle == null) {
                        oldToggle.setSelected(true);
                    }
                });
    });

    //listen to toggles and update view state
    slideShowToggle.setOnAction((ActionEvent t) -> {
        activateSlideShowViewer(globalSelectionModel.lastSelectedProperty().get());
    });

    tileToggle.setOnAction((ActionEvent t) -> {
        activateTileViewer();
    });

    controller.viewState().addListener((ObservableValue<? extends GroupViewState> observable,
            GroupViewState oldValue, GroupViewState newValue) -> {
        setViewState(newValue);
    });

    addEventFilter(KeyEvent.KEY_PRESSED, tileKeyboardNavigationHandler);
    gridView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        private ContextMenu buildContextMenu() {
            ArrayList<MenuItem> menuItems = new ArrayList<>();

            menuItems.add(CategorizeAction.getPopupMenu());

            menuItems.add(AddDrawableTagAction.getInstance().getPopupMenu());

            Collection<? extends ContextMenuActionsProvider> menuProviders = Lookup.getDefault()
                    .lookupAll(ContextMenuActionsProvider.class);

            for (ContextMenuActionsProvider provider : menuProviders) {

                for (final Action act : provider.getActions()) {

                    if (act instanceof Presenter.Popup) {
                        Presenter.Popup aact = (Presenter.Popup) act;

                        menuItems.add(SwingMenuItemAdapter.create(aact.getPopupPresenter()));
                    }
                }
            }
            final MenuItem extractMenuItem = new MenuItem("Extract File(s)");
            extractMenuItem.setOnAction((ActionEvent t) -> {
                SwingUtilities.invokeLater(() -> {
                    TopComponent etc = WindowManager.getDefault()
                            .findTopComponent(ImageAnalyzerTopComponent.PREFERRED_ID);
                    ExtractAction.getInstance().actionPerformed(new java.awt.event.ActionEvent(etc, 0, null));
                });
            });
            menuItems.add(extractMenuItem);

            ContextMenu contextMenu = new ContextMenu(menuItems.toArray(new MenuItem[] {}));
            contextMenu.setAutoHide(true);
            return contextMenu;
        }

        @Override
        public void handle(MouseEvent t) {
            switch (t.getButton()) {
            case PRIMARY:
                if (t.getClickCount() == 1) {
                    globalSelectionModel.clearSelection();
                    if (contextMenu != null) {
                        contextMenu.hide();
                    }
                }
                t.consume();
                break;
            case SECONDARY:
                if (t.getClickCount() == 1) {
                    selectAllFiles();
                }
                if (contextMenu == null) {
                    contextMenu = buildContextMenu();
                }

                contextMenu.hide();
                contextMenu.show(GroupPane.this, t.getScreenX(), t.getScreenY());
                t.consume();
                break;
            }
        }
    });

    //        Platform.runLater(() -> {
    ActionUtils.configureButton(nextGroupAction, nextButton);
    final EventHandler<ActionEvent> onAction = nextButton.getOnAction();
    nextButton.setOnAction((ActionEvent event) -> {
        flashAnimation.stop();
        nextButton.setEffect(null);
        onAction.handle(event);
    });

    ActionUtils.configureButton(forwardAction, forwardButton);
    ActionUtils.configureButton(backAction, backButton);
    //        });

    nextGroupAction.disabledProperty().addListener(
            (ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                nextButton.setEffect(newValue ? null : DROP_SHADOW);
                if (newValue == false) {
                    flashAnimation.play();
                } else {
                    flashAnimation.stop();
                }
            });

    //listen to tile selection and make sure it is visible in scroll area
    //TODO: make sure we are testing complete visability not just bounds intersection
    globalSelectionModel.lastSelectedProperty().addListener((observable, oldFileID, newFileId) -> {
        if (groupViewMode.get() == GroupViewMode.SLIDE_SHOW) {
            slideShowPane.setFile(newFileId);
        } else {

            scrollToFileID(newFileId);
        }
    });

    setViewState(controller.viewState().get());
}

From source file:org.sleuthkit.autopsy.imagegallery.gui.GroupPane.java

/**
 * called automatically during constructor by FXMLConstructor.
 *
 * checks that FXML loading went ok and performs additional setup
 *//*from  w  ww. j a v a  2s  .  c  o  m*/
@FXML
void initialize() {
    assert gridView != null : "fx:id=\"tilePane\" was not injected: check your FXML file 'GroupPane.fxml'.";
    assert grpCatSplitMenu != null : "fx:id=\"grpCatSplitMenu\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert grpTagSplitMenu != null : "fx:id=\"grpTagSplitMenu\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert headerToolBar != null : "fx:id=\"headerToolBar\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert segButton != null : "fx:id=\"previewList\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert slideShowToggle != null : "fx:id=\"segButton\" was not injected: check your FXML file 'GroupHeader.fxml'.";
    assert tileToggle != null : "fx:id=\"tileToggle\" was not injected: check your FXML file 'GroupHeader.fxml'.";

    //configure flashing glow animation on next unseen group button
    flashAnimation.setCycleCount(Timeline.INDEFINITE);
    flashAnimation.setAutoReverse(true);

    //configure gridView cell properties
    gridView.cellHeightProperty().bind(Toolbar.getDefault().sizeSliderValue().add(75));
    gridView.cellWidthProperty().bind(Toolbar.getDefault().sizeSliderValue().add(75));
    gridView.setCellFactory((GridView<Long> param) -> new DrawableCell());

    //configure toolbar properties
    HBox.setHgrow(spacer, Priority.ALWAYS);
    spacer.setMinWidth(Region.USE_PREF_SIZE);

    try {
        grpTagSplitMenu.setText(TagUtils.getFollowUpTagName().getDisplayName());
        grpTagSplitMenu.setOnAction(createGrpTagMenuItem(TagUtils.getFollowUpTagName()).getOnAction());
    } catch (TskCoreException tskCoreException) {
        LOGGER.log(Level.WARNING, "failed to load FollowUpTagName", tskCoreException);
    }
    grpTagSplitMenu.setGraphic(new ImageView(DrawableAttribute.TAGS.getIcon()));
    grpTagSplitMenu.showingProperty()
            .addListener((ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) -> {
                if (t1) {
                    ArrayList<MenuItem> selTagMenues = new ArrayList<>();
                    for (final TagName tn : TagUtils.getNonCategoryTagNames()) {
                        MenuItem menuItem = TagUtils.createSelTagMenuItem(tn, grpTagSplitMenu);
                        selTagMenues.add(menuItem);
                    }
                    grpTagSplitMenu.getItems().setAll(selTagMenues);
                }
            });

    ArrayList<MenuItem> grpCategoryMenues = new ArrayList<>();
    for (final Category cat : Category.values()) {
        MenuItem menuItem = createGrpCatMenuItem(cat);
        grpCategoryMenues.add(menuItem);
    }
    grpCatSplitMenu.setText(Category.FIVE.getDisplayName());
    grpCatSplitMenu.setGraphic(new ImageView(DrawableAttribute.CATEGORY.getIcon()));
    grpCatSplitMenu.getItems().setAll(grpCategoryMenues);
    grpCatSplitMenu.setOnAction(createGrpCatMenuItem(Category.FIVE).getOnAction());

    Runnable syncMode = () -> {
        switch (groupViewMode.get()) {
        case SLIDE_SHOW:
            slideShowToggle.setSelected(true);
            break;
        case TILE:
            tileToggle.setSelected(true);
            break;
        }
    };
    syncMode.run();
    //make togle states match view state
    groupViewMode.addListener((o) -> {
        syncMode.run();
    });

    slideShowToggle.toggleGroupProperty().addListener((o) -> {
        slideShowToggle.getToggleGroup().selectedToggleProperty()
                .addListener((observable, oldToggle, newToggle) -> {
                    if (newToggle == null) {
                        oldToggle.setSelected(true);
                    }
                });
    });

    //listen to toggles and update view state
    slideShowToggle.setOnAction((ActionEvent t) -> {
        activateSlideShowViewer(globalSelectionModel.lastSelectedProperty().get());
    });

    tileToggle.setOnAction((ActionEvent t) -> {
        activateTileViewer();
    });

    controller.viewState().addListener((ObservableValue<? extends GroupViewState> observable,
            GroupViewState oldValue, GroupViewState newValue) -> {
        setViewState(newValue);
    });

    addEventFilter(KeyEvent.KEY_PRESSED, tileKeyboardNavigationHandler);
    gridView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

        private ContextMenu buildContextMenu() {
            ArrayList<MenuItem> menuItems = new ArrayList<>();

            menuItems.add(CategorizeAction.getPopupMenu());

            menuItems.add(AddDrawableTagAction.getInstance().getPopupMenu());

            Collection<? extends ContextMenuActionsProvider> menuProviders = Lookup.getDefault()
                    .lookupAll(ContextMenuActionsProvider.class);

            for (ContextMenuActionsProvider provider : menuProviders) {

                for (final Action act : provider.getActions()) {

                    if (act instanceof Presenter.Popup) {
                        Presenter.Popup aact = (Presenter.Popup) act;

                        menuItems.add(SwingMenuItemAdapter.create(aact.getPopupPresenter()));
                    }
                }
            }
            final MenuItem extractMenuItem = new MenuItem("Extract File(s)");
            extractMenuItem.setOnAction((ActionEvent t) -> {
                SwingUtilities.invokeLater(() -> {
                    TopComponent etc = WindowManager.getDefault()
                            .findTopComponent(ImageGalleryTopComponent.PREFERRED_ID);
                    ExtractAction.getInstance().actionPerformed(new java.awt.event.ActionEvent(etc, 0, null));
                });
            });
            menuItems.add(extractMenuItem);

            ContextMenu contextMenu = new ContextMenu(menuItems.toArray(new MenuItem[] {}));
            contextMenu.setAutoHide(true);
            return contextMenu;
        }

        @Override
        public void handle(MouseEvent t) {
            switch (t.getButton()) {
            case PRIMARY:
                if (t.getClickCount() == 1) {
                    globalSelectionModel.clearSelection();
                    if (contextMenu != null) {
                        contextMenu.hide();
                    }
                }
                t.consume();
                break;
            case SECONDARY:
                if (t.getClickCount() == 1) {
                    selectAllFiles();
                }
                if (globalSelectionModel.getSelected().isEmpty() == false) {
                    if (contextMenu == null) {
                        contextMenu = buildContextMenu();
                    }

                    contextMenu.hide();
                    contextMenu.show(GroupPane.this, t.getScreenX(), t.getScreenY());
                }
                t.consume();
                break;
            }
        }
    });

    ActionUtils.configureButton(nextGroupAction, nextButton);
    final EventHandler<ActionEvent> onAction = nextButton.getOnAction();
    nextButton.setOnAction((ActionEvent event) -> {
        flashAnimation.stop();
        nextButton.setEffect(null);
        onAction.handle(event);
    });

    ActionUtils.configureButton(forwardAction, forwardButton);
    ActionUtils.configureButton(backAction, backButton);

    nextGroupAction.disabledProperty().addListener(
            (ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                nextButton.setEffect(newValue ? null : DROP_SHADOW);
                if (newValue == false) {
                    flashAnimation.play();
                } else {
                    flashAnimation.stop();
                }
            });

    //listen to tile selection and make sure it is visible in scroll area
    //TODO: make sure we are testing complete visability not just bounds intersection
    globalSelectionModel.lastSelectedProperty().addListener((observable, oldFileID, newFileId) -> {
        if (groupViewMode.get() == GroupViewMode.SLIDE_SHOW) {
            slideShowPane.setFile(newFileId);
        } else {

            scrollToFileID(newFileId);
        }
    });

    setViewState(controller.viewState().get());
}

From source file:org.sleuthkit.autopsy.timeline.ui.detailview.EventNodeBase.java

public ContextMenu getContextMenu(MouseEvent mouseEvent) {
    ContextMenu chartContextMenu = chartLane.getContextMenu(mouseEvent);

    ContextMenu contextMenu = ActionUtils.createContextMenu(Lists.newArrayList(getActions()));
    contextMenu.getItems().add(new SeparatorMenuItem());
    contextMenu.getItems().addAll(chartContextMenu.getItems());
    contextMenu.setAutoHide(true);
    return contextMenu;
}