Example usage for javafx.scene.input KeyCode getKeyCode

List of usage examples for javafx.scene.input KeyCode getKeyCode

Introduction

In this page you can find the example usage for javafx.scene.input KeyCode getKeyCode.

Prototype

public static KeyCode getKeyCode(String name) 

Source Link

Document

Parses textual representation of a key.

Usage

From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java

private void createDialog() {
    dialog = new Stage();
    dialog.initOwner(qupath.getStage());
    dialog.setTitle("Training sample selector");

    pointCreator = new RandomPointCreator();

    for (PathClass pathClass : pathClassListModel) {
        if (pathClass != null && pathClass.getName() != null)
            pointCreator.addPathClass(pathClass,
                    KeyCode.getKeyCode(pathClass.getName().toUpperCase().substring(0, 1)));
        //            pointCreator.addPathClass(pathClass, KeyStroke.getKeyStroke(new pathClass.getName().toLowerCase().charAt(0), 0).getKeyCode());
    }//  ww w .j  a v a 2 s .c  o m
    //      PathClass tumourClass = PathClassFactory.getDefaultPathClass(PathClasses.TUMOR);
    //      PathClass stromaClass = PathClassFactory.getDefaultPathClass(PathClasses.STROMA);
    //      pointCreator.addPathClass(tumourClass, KeyCode.T);
    //      pointCreator.addPathClass(stromaClass, KeyCode.S);
    QuPathViewer viewer = qupath.getViewer();
    pointCreator.registerViewer(viewer);

    // Adapt to changing active viewers
    ImageDataChangeListener<BufferedImage> listener = new ImageDataChangeListener<BufferedImage>() {

        @Override
        public void imageDataChanged(ImageDataWrapper<BufferedImage> source,
                ImageData<BufferedImage> imageDataOld, ImageData<BufferedImage> imageDataNew) {
            if (pointCreator != null) {
                QuPathViewer viewer = qupath.getViewer();
                pointCreator.registerViewer(viewer);
                updateObjectCache(viewer);
            }
            refreshList();
            updateLabel();
        }

    };
    qupath.addImageDataChangeListener(listener);

    // Remove listeners for cleanup
    dialog.setOnCloseRequest(e -> {
        pointCreator.deregisterViewer();
        qupath.removeImageDataChangeListener(listener);
        dialog.setOnCloseRequest(null);
        dialog = null;
        // Re-enable mode switching
        qupath.setModeSwitchingEnabled(true);
    });

    ParameterPanelFX paramPanel = new ParameterPanelFX(params);
    paramPanel.getPane().setPadding(new Insets(2, 5, 5, 5));

    list = new ListView<PathClass>(pathClassListModel);
    list.setPrefSize(400, 200);

    // TODO: ADD A SENSIBLE RENDERER!
    // For now, this is simply duplicated from PathAnnotationPanel
    list.setCellFactory(new Callback<ListView<PathClass>, ListCell<PathClass>>() {

        @Override
        public ListCell<PathClass> call(ListView<PathClass> p) {

            ListCell<PathClass> cell = new ListCell<PathClass>() {

                @Override
                protected void updateItem(PathClass value, boolean bln) {
                    super.updateItem(value, bln);
                    int size = 10;
                    if (value == null) {
                        setText(null);
                        setGraphic(null);
                    } else if (value.getName() == null) {
                        setText("None");
                        setGraphic(new Rectangle(size, size, ColorToolsFX.getCachedColor(0, 0, 0, 0)));
                    } else {
                        setText(value.getName());
                        setGraphic(new Rectangle(size, size, ColorToolsFX.getPathClassColor(value)));
                    }
                }

            };

            return cell;
        }
    });

    //      list.setCellRenderer(new PathClassListCellRendererPoints());

    list.setTooltip(new Tooltip("Available classes"));

    labelCount = new Label();
    labelCount.setTextAlignment(TextAlignment.CENTER);
    labelCount.setPadding(new Insets(5, 0, 5, 0));
    BorderPane panelTop = new BorderPane();
    panelTop.setTop(paramPanel.getPane());
    panelTop.setCenter(list);
    panelTop.setBottom(labelCount);
    labelCount.prefWidthProperty().bind(panelTop.widthProperty());
    updateLabel();

    //      panelButtons.add(new JButton(new UndoAction("Undo")));
    Action actionAdd = new Action("Add to class", e -> {
        if (list == null || pointCreator == null)
            return;
        PathClass pathClass = list.getSelectionModel().getSelectedItem();
        pointCreator.addPoint(pathClass);
    });
    Action actionSkip = new Action("Skip", e -> {
        if (pointCreator != null)
            pointCreator.addPoint(null);
    });

    GridPane panelButtons = PanelToolsFX.createColumnGridControls(ActionUtils.createButton(actionAdd),
            ActionUtils.createButton(actionSkip));

    BorderPane pane = new BorderPane();
    pane.setCenter(panelTop);
    pane.setBottom(panelButtons);

    pane.setPadding(new Insets(10, 10, 10, 10));
    Scene scene = new Scene(pane);
    dialog.setScene(scene);
}