Example usage for javafx.beans.binding Bindings isNotNull

List of usage examples for javafx.beans.binding Bindings isNotNull

Introduction

In this page you can find the example usage for javafx.beans.binding Bindings isNotNull.

Prototype

public static BooleanBinding isNotNull(final ObservableObjectValue<?> op) 

Source Link

Document

Creates a new javafx.beans.binding.BooleanBinding that holds true if the value of an javafx.beans.value.ObservableObjectValue is not null .

Usage

From source file:dtv.controller.FXMLMainController.java

public void init(ObservableList<DVBChannel> serviceData, TableView<DVBChannel> table,
        TableColumn<DVBChannel, Integer> idx, TableColumn<DVBChannel, String> name,
        TableColumn<DVBChannel, String> type, TableColumn<DVBChannel, String> ppr) {

    table.setEditable(true);/*ww  w .jav a  2 s.  c o  m*/
    idx.setCellValueFactory(cellData -> cellData.getValue().idxProperty().asObject());
    name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
    name.setEditable(true);
    type.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
    // nid.setCellValueFactory(cellData -> cellData.getValue().nidProperty().asObject());
    ppr.setCellValueFactory(cellData -> cellData.getValue().pprProperty());
    // newCol.setCellValueFactory(cellData -> cellData.getValue().neewProperty());

    // Context menu
    table.setRowFactory(tableView -> {
        final TableRow<DVBChannel> row = new TableRow<>();
        final ContextMenu rowMenu = new ContextMenu();

        final MenuItem removeItem = new MenuItem("Delete");
        removeItem.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                final DVBChannel service = row.getItem();
                serviceData.removeAll(service);
            }
        });

        rowMenu.getItems().addAll(removeItem);
        row.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(row.itemProperty())).then(rowMenu)
                .otherwise((ContextMenu) null));

        return row;
    });

    ppr.setCellFactory(col -> {
        final TableCell<DVBChannel, String> cell = new TableCell<>();

        cell.textProperty().bind(cell.itemProperty());
        cell.itemProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue != null) {
                final ContextMenu cellMenu = new ContextMenu();
                for (String pref : Utils.prefTab) {
                    final CheckMenuItem prefMenuItem = new CheckMenuItem(pref);

                    if (Utils.isPreferenceOn(cell.getText(), pref)) {
                        prefMenuItem.setSelected(true);
                    }

                    prefMenuItem.selectedProperty().addListener((obs1, old_val, new_val) -> {
                        final String new_ppr;
                        final DVBChannel service = (DVBChannel) cell.getTableRow().getItem();

                        if (new_val) {
                            new_ppr = Utils.add_ppr(cell.getText(), pref);
                        } else {
                            new_ppr = Utils.remove_ppr(cell.getText(), pref);
                        }

                        service.setPpr(new_ppr);
                        service.setModified(true);
                    });

                    cellMenu.getItems().add(prefMenuItem);
                    cell.setContextMenu(cellMenu);
                }
            } else {
                cell.setContextMenu(null);
            }
        });
        return cell;
    });

    // Editable service name
    name.setCellFactory(p -> new EditingCell());

    name.setOnEditCommit(t -> {

        final DVBChannel service = t.getTableView().getItems().get(t.getTablePosition().getRow());
        service.setName(t.getNewValue());
        service.setModified(true);
    });
}