Search in sources :

Example 1 with N5DatasetTreeCellRenderer

use of org.janelia.saalfeldlab.n5.ui.N5DatasetTreeCellRenderer in project mars-fx by duderstadt-lab.

the class BdvViewTable method buildBdvTableIndex.

protected BorderPane buildBdvTableIndex() {
    TableView<MarsBdvSource> bdvTable = new TableView<MarsBdvSource>();
    addBdvSourceNameField = new CustomTextField();
    TableColumn<MarsBdvSource, String> typeColumn = new TableColumn<>();
    typeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    typeColumn.setCellValueFactory(bdvSource -> new ReadOnlyObjectWrapper<>((bdvSource.getValue().isN5()) ? "N5" : "HD5"));
    typeColumn.setSortable(false);
    typeColumn.setEditable(false);
    typeColumn.setPrefWidth(40);
    typeColumn.setMinWidth(40);
    typeColumn.setStyle("-fx-alignment: CENTER-LEFT;");
    bdvTable.getColumns().add(typeColumn);
    TableColumn<MarsBdvSource, MarsBdvSource> deleteColumn = new TableColumn<>();
    deleteColumn.setPrefWidth(30);
    deleteColumn.setMinWidth(30);
    deleteColumn.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()));
    deleteColumn.setCellFactory(param -> new TableCell<MarsBdvSource, MarsBdvSource>() {

        private final Button removeButton = new Button();

        @Override
        protected void updateItem(MarsBdvSource pRow, boolean empty) {
            super.updateItem(pRow, empty);
            if (pRow == null) {
                setGraphic(null);
                return;
            }
            removeButton.setGraphic(FontAwesomeIconFactory.get().createIcon(de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.MINUS, "1.0em"));
            removeButton.setCenterShape(true);
            removeButton.setStyle("-fx-background-radius: 5em; " + "-fx-min-width: 18px; " + "-fx-min-height: 18px; " + "-fx-max-width: 18px; " + "-fx-max-height: 18px;");
            setGraphic(removeButton);
            removeButton.setOnAction(e -> {
                marsImageMetadata.removeBdvSource(pRow.getName());
                loadBdvSources();
            });
        }
    });
    deleteColumn.setStyle("-fx-alignment: CENTER;");
    deleteColumn.setSortable(false);
    bdvTable.getColumns().add(deleteColumn);
    TableColumn<MarsBdvSource, String> nameColumn = new TableColumn<>("Name");
    nameColumn.setCellFactory(column -> EditCell.createStringEditCell());
    nameColumn.setOnEditCommit(event -> {
        String newBdvName = event.getNewValue();
        if (!marsImageMetadata.hasBdvSource(newBdvName)) {
            MarsBdvSource bdvSource = event.getRowValue();
            String oldName = bdvSource.getName();
            bdvSource.setName(newBdvName);
            // We need to rebuild the map to maintain the order in the table
            List<MarsBdvSource> bdvSourceList = new ArrayList<MarsBdvSource>();
            for (String key : marsImageMetadata.getBdvSourceNames()) {
                if (key.equals(oldName))
                    bdvSourceList.add(bdvSource);
                else
                    bdvSourceList.add(marsImageMetadata.getBdvSource(key));
            }
            marsImageMetadata.removeAllBdvSources();
            for (MarsBdvSource item : bdvSourceList) marsImageMetadata.putBdvSource(item);
        } else {
            ((MarsBdvSource) event.getTableView().getItems().get(event.getTablePosition().getRow())).setName(event.getOldValue());
            bdvTable.refresh();
        }
    });
    nameColumn.setCellValueFactory(bdvSource -> new ReadOnlyObjectWrapper<>(bdvSource.getValue().getName()));
    nameColumn.setSortable(false);
    nameColumn.setPrefWidth(100);
    nameColumn.setMinWidth(100);
    nameColumn.setStyle("-fx-alignment: CENTER-LEFT;");
    bdvTable.getColumns().add(nameColumn);
    bdvTable.setItems(bdvRowList);
    bdvTable.setEditable(true);
    bdvIndexTableListener = new ChangeListener<MarsBdvSource>() {

        public void changed(ObservableValue<? extends MarsBdvSource> observable, MarsBdvSource oldMarsBdvSource, MarsBdvSource newMarsBdvSource) {
            if (newMarsBdvSource != null)
                bdvSourceOptionsPane.setMarsBdvSource(newMarsBdvSource);
            else
                bdvSourceOptionsPane.setMarsBdvSource(null);
        }
    };
    bdvTable.getSelectionModel().selectedItemProperty().addListener(bdvIndexTableListener);
    Button addButton = new Button();
    addButton.setGraphic(FontAwesomeIconFactory.get().createIcon(de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.PLUS, "1.0em"));
    addButton.setCenterShape(true);
    addButton.setCursor(Cursor.DEFAULT);
    addButton.setStyle("-fx-background-radius: 5em; " + "-fx-min-width: 18px; " + "-fx-min-height: 18px; " + "-fx-max-width: 18px; " + "-fx-max-height: 18px;");
    addButton.setOnAction(e -> {
        if (!addBdvSourceNameField.getText().equals("") && !marsImageMetadata.hasBdvSource(addBdvSourceNameField.getText())) {
            MarsBdvSource bdvSource = new MarsBdvSource(addBdvSourceNameField.getText());
            switch(this.buttonType) {
                case 0:
                    bdvSource.setN5(true);
                    break;
                case 1:
                    bdvSource.setN5(false);
                    break;
            }
            if (bdvSource.isN5()) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        DatasetSelectorDialog selectionDialog = new DatasetSelectorDialog(new N5ViewerReaderFun(), new N5BasePathFun(), System.getProperty(// no
                        "user.home"), // no
                        new N5MetadataParser[] {}, // parsers
                        new N5MetadataParser[] { new ImagePlusLegacyMetadataParser(), new N5CosemMetadataParser(), new N5SingleScaleMetadataParser(), new CanonicalMetadataParser(), new N5GenericSingleScaleMetadataParser() });
                        selectionDialog.setVirtualOption(false);
                        selectionDialog.setCropOption(false);
                        selectionDialog.setTreeRenderer(new N5DatasetTreeCellRenderer(true));
                        // Prevents NullPointerException
                        selectionDialog.setContainerPathUpdateCallback(x -> {
                        });
                        final Consumer<DataSelection> callback = (DataSelection dataSelection) -> {
                            Platform.runLater(new Runnable() {

                                @Override
                                public void run() {
                                    bdvSource.setPath(selectionDialog.getN5RootPath());
                                    bdvSource.setN5Dataset(dataSelection.metadata.get(0).getPath());
                                    bdvSource.setProperty("info", getDatasetInfo(((N5DatasetMetadata) dataSelection.metadata.get(0)).getAttributes()));
                                    marsImageMetadata.putBdvSource(bdvSource);
                                    loadBdvSources();
                                }
                            });
                        };
                        selectionDialog.run(callback);
                    }
                });
            } else {
                FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("Select xml");
                fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
                fileChooser.getExtensionFilters().add(new ExtensionFilter("xml file", "*.xml"));
                File path = fileChooser.showOpenDialog(getNode().getScene().getWindow());
                if (path != null) {
                    bdvSource.setPath(path.getAbsolutePath());
                    marsImageMetadata.putBdvSource(bdvSource);
                    loadBdvSources();
                }
            }
        }
    });
    addBdvSourceNameField.textProperty().addListener((observable, oldValue, newValue) -> {
        if (addBdvSourceNameField.getText().isEmpty()) {
            addBdvSourceNameField.setRight(new Label(""));
        } else {
            addBdvSourceNameField.setRight(addButton);
        }
    });
    addBdvSourceNameField.setStyle("-fx-background-radius: 2em; ");
    typeButton = new Button();
    typeButton.setText("N5");
    typeButton.setCenterShape(true);
    typeButton.setStyle("-fx-background-radius: 2em; " + "-fx-min-width: 60px; " + "-fx-min-height: 30px; " + "-fx-max-width: 60px; " + "-fx-max-height: 30px;");
    typeButton.setOnAction(e -> {
        buttonType++;
        if (buttonType > 1)
            buttonType = 0;
        switch(buttonType) {
            case 0:
                typeButton.setText("N5");
                typeButton.setGraphic(null);
                break;
            case 1:
                typeButton.setText("HD5");
                typeButton.setGraphic(null);
                break;
        }
    });
    BorderPane bomttomPane = new BorderPane();
    bomttomPane.setCenter(addBdvSourceNameField);
    bomttomPane.setLeft(typeButton);
    BorderPane bdvTableIndex = new BorderPane();
    bdvTableIndex.setCenter(bdvTable);
    bdvTableIndex.setBottom(bomttomPane);
    BorderPane.setMargin(addBdvSourceNameField, new Insets(5));
    BorderPane.setMargin(typeButton, new Insets(5));
    return bdvTableIndex;
}
Also used : Button(javafx.scene.control.Button) Arrays(java.util.Arrays) N5MetadataParser(org.janelia.saalfeldlab.n5.metadata.N5MetadataParser) CanonicalMetadataParser(org.janelia.saalfeldlab.n5.metadata.canonical.CanonicalMetadataParser) FXCollections(javafx.collections.FXCollections) N5BasePathFun(org.janelia.saalfeldlab.n5.ij.N5Importer.N5BasePathFun) EditCell(de.mpg.biochem.mars.fx.util.EditCell) TextFieldTableCell(javafx.scene.control.cell.TextFieldTableCell) MarsMetadata(de.mpg.biochem.mars.metadata.MarsMetadata) DatasetSelectorDialog(org.janelia.saalfeldlab.n5.ui.DatasetSelectorDialog) ArrayList(java.util.ArrayList) TableColumn(javafx.scene.control.TableColumn) DataSelection(org.janelia.saalfeldlab.n5.ui.DataSelection) SwingUtilities(javax.swing.SwingUtilities) N5GenericSingleScaleMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5GenericSingleScaleMetadataParser) TableCell(javafx.scene.control.TableCell) Insets(javafx.geometry.Insets) ReadOnlyObjectWrapper(javafx.beans.property.ReadOnlyObjectWrapper) N5CosemMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser) MetadataEvent(de.mpg.biochem.mars.fx.event.MetadataEvent) TableView(javafx.scene.control.TableView) N5DatasetMetadata(org.janelia.saalfeldlab.n5.metadata.N5DatasetMetadata) DatasetAttributes(org.janelia.saalfeldlab.n5.DatasetAttributes) FontAwesomeIconFactory(de.jensd.fx.glyphs.fontawesome.utils.FontAwesomeIconFactory) SplitPane(javafx.scene.control.SplitPane) N5SingleScaleMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5SingleScaleMetadataParser) Label(javafx.scene.control.Label) N5ViewerReaderFun(org.janelia.saalfeldlab.n5.ij.N5Importer.N5ViewerReaderFun) Node(javafx.scene.Node) Event(javafx.event.Event) ImagePlusLegacyMetadataParser(org.janelia.saalfeldlab.n5.metadata.imagej.ImagePlusLegacyMetadataParser) N5DatasetTreeCellRenderer(org.janelia.saalfeldlab.n5.ui.N5DatasetTreeCellRenderer) Collectors(java.util.stream.Collectors) File(java.io.File) MetadataEventHandler(de.mpg.biochem.mars.fx.event.MetadataEventHandler) Consumer(java.util.function.Consumer) Platform(javafx.application.Platform) Cursor(javafx.scene.Cursor) FileChooser(javafx.stage.FileChooser) List(java.util.List) ObservableValue(javafx.beans.value.ObservableValue) ObservableList(javafx.collections.ObservableList) BorderPane(javafx.scene.layout.BorderPane) ChangeListener(javafx.beans.value.ChangeListener) ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) CustomTextField(org.controlsfx.control.textfield.CustomTextField) MarsBdvSource(de.mpg.biochem.mars.metadata.MarsBdvSource) ImagePlusLegacyMetadataParser(org.janelia.saalfeldlab.n5.metadata.imagej.ImagePlusLegacyMetadataParser) BorderPane(javafx.scene.layout.BorderPane) Insets(javafx.geometry.Insets) MarsBdvSource(de.mpg.biochem.mars.metadata.MarsBdvSource) ArrayList(java.util.ArrayList) Label(javafx.scene.control.Label) N5SingleScaleMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5SingleScaleMetadataParser) Consumer(java.util.function.Consumer) Button(javafx.scene.control.Button) ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) FileChooser(javafx.stage.FileChooser) N5ViewerReaderFun(org.janelia.saalfeldlab.n5.ij.N5Importer.N5ViewerReaderFun) DataSelection(org.janelia.saalfeldlab.n5.ui.DataSelection) TableView(javafx.scene.control.TableView) N5MetadataParser(org.janelia.saalfeldlab.n5.metadata.N5MetadataParser) N5CosemMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser) TableColumn(javafx.scene.control.TableColumn) DatasetSelectorDialog(org.janelia.saalfeldlab.n5.ui.DatasetSelectorDialog) N5DatasetTreeCellRenderer(org.janelia.saalfeldlab.n5.ui.N5DatasetTreeCellRenderer) CustomTextField(org.controlsfx.control.textfield.CustomTextField) N5GenericSingleScaleMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5GenericSingleScaleMetadataParser) CanonicalMetadataParser(org.janelia.saalfeldlab.n5.metadata.canonical.CanonicalMetadataParser) File(java.io.File) N5BasePathFun(org.janelia.saalfeldlab.n5.ij.N5Importer.N5BasePathFun)

Aggregations

FontAwesomeIconFactory (de.jensd.fx.glyphs.fontawesome.utils.FontAwesomeIconFactory)1 MetadataEvent (de.mpg.biochem.mars.fx.event.MetadataEvent)1 MetadataEventHandler (de.mpg.biochem.mars.fx.event.MetadataEventHandler)1 EditCell (de.mpg.biochem.mars.fx.util.EditCell)1 MarsBdvSource (de.mpg.biochem.mars.metadata.MarsBdvSource)1 MarsMetadata (de.mpg.biochem.mars.metadata.MarsMetadata)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Platform (javafx.application.Platform)1 ReadOnlyObjectWrapper (javafx.beans.property.ReadOnlyObjectWrapper)1 ChangeListener (javafx.beans.value.ChangeListener)1 ObservableValue (javafx.beans.value.ObservableValue)1 FXCollections (javafx.collections.FXCollections)1 ObservableList (javafx.collections.ObservableList)1 Event (javafx.event.Event)1 Insets (javafx.geometry.Insets)1