javafx.scene.control.TableView.TableViewSelectionModel Java Examples

The following examples show how to use javafx.scene.control.TableView.TableViewSelectionModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings("unchecked")
public void selectCells(TableView<?> tableView, String value) {
    @SuppressWarnings("rawtypes")
    TableViewSelectionModel selectionModel = tableView.getSelectionModel();
    selectionModel.clearSelection();
    JSONObject cells = new JSONObject(value);
    JSONArray object = (JSONArray) cells.get("cells");
    for (int i = 0; i < object.length(); i++) {
        JSONArray jsonArray = object.getJSONArray(i);
        int rowIndex = Integer.parseInt(jsonArray.getString(0));
        int columnIndex = getColumnIndex(jsonArray.getString(1));
        @SuppressWarnings("rawtypes")
        TableColumn column = tableView.getColumns().get(columnIndex);
        if (getVisibleCellAt(tableView, rowIndex, columnIndex) == null) {
            tableView.scrollTo(rowIndex);
            tableView.scrollToColumn(column);
        }
        selectionModel.select(rowIndex, column);
    }
}
 
Example #2
Source File: StringTable.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Set selection
 *  @param sel_row_col Flattened list of row, col, row, col, .. cell indices. May be <code>null</code>
 */
public void setSelection(final List<Integer> sel_row_col)
{
    final TableViewSelectionModel<List<ObservableCellValue>> selection = table.getSelectionModel();
    selection.clearSelection();

    if (sel_row_col == null)
        return;

    final ObservableList<TableColumn<List<ObservableCellValue>, ?>> columns = table.getColumns();
    int i = 0;
    while (i < sel_row_col.size())
    {
        final int row = sel_row_col.get(i++);
        final int col = sel_row_col.get(i++);
        if (row < data.size()  &&  col < columns.size())
            selection.select(row, columns.get(col));
    }
}
 
Example #3
Source File: JavaFXTableViewElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean marathon_select(String value) {
    TableView<?> tableView = (TableView<?>) node;
    TableViewSelectionModel<?> selectionModel = tableView.getSelectionModel();
    if ("".equals(value)) {
        selectionModel.clearSelection();
        return true;
    } else if (value.equals("all")) {
        int rowSize = tableView.getItems().size();
        for (int i = 0; i < rowSize; i++) {
            selectionModel.select(i);
        }
        return true;
    } else if (selectionModel.isCellSelectionEnabled()) {
        selectCells(tableView, value);
        return true;
    } else {
        int[] selectedRows = getSelectedRows(value);
        selectionModel.clearSelection();
        for (int rowIndex : selectedRows) {
            if (getVisibleCellAt(tableView, rowIndex, tableView.getColumns().size() - 1) == null) {
                tableView.scrollTo(rowIndex);
            }
            selectionModel.select(rowIndex);
        }
        return true;
    }
}
 
Example #4
Source File: MediaPane.java    From OpenLabeler with Apache License 2.0 4 votes vote down vote up
public TableViewSelectionModel<MediaFile> getSelectionModel() {
    return tvMedia.getSelectionModel();
}
 
Example #5
Source File: ImagePanel.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private static TextArea createTextArea(TableCell<Annotation, String> cell) {
    TextArea textArea = new TextArea(cell.getItem() == null ? "" : cell.getItem());
    textArea.setPrefRowCount(1);
    textArea.setWrapText(true);
    textArea.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
            if (!textArea.isFocused() && cell.getItem() != null && cell.isEditing()) {
                cell.commitEdit(textArea.getText());
            }
            cell.getTableView().getItems().get(cell.getIndex()).setText(textArea.getText());
        }
    });
    textArea.addEventFilter(MouseEvent.MOUSE_CLICKED, (event) -> {
        if (event.getClickCount() > 1) {
            cell.getTableView().edit(cell.getTableRow().getIndex(), cell.getTableColumn());
        } else {
            TableViewSelectionModel<Annotation> selectionModel = cell.getTableView().getSelectionModel();
            if (event.isControlDown()) {
                if (selectionModel.isSelected(cell.getIndex())) {
                    selectionModel.clearSelection(cell.getIndex());
                } else {
                    selectionModel.select(cell.getIndex());
                }
            } else {
                selectionModel.clearAndSelect(cell.getIndex());
            }
        }
    });
    textArea.addEventFilter(KeyEvent.KEY_PRESSED, (event) -> {
        if (event.getCode() == KeyCode.ENTER && event.isShiftDown() && cell.isEditing()) {
            cell.commitEdit(textArea.getText());
            cell.getTableView().getItems().get(cell.getIndex()).setText(textArea.getText());
            event.consume();
        }
        if (event.getCode() == KeyCode.F2) {
            cell.getTableView().edit(cell.getTableRow().getIndex(), cell.getTableColumn());
        }
    });
    return textArea;
}
 
Example #6
Source File: JavaFXElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public String getSelection(TableView<?> tableView) {
    TableViewSelectionModel<?> selectionModel = tableView.getSelectionModel();
    if (!selectionModel.isCellSelectionEnabled()) {
        ObservableList<Integer> selectedIndices = selectionModel.getSelectedIndices();
        if (tableView.getItems().size() == selectedIndices.size()) {
            return "all";
        }
        if (selectedIndices.size() == 0) {
            return "";
        }
        return getRowSelectionText(selectedIndices);
    }

    @SuppressWarnings("rawtypes")
    ObservableList<TablePosition> selectedCells = selectionModel.getSelectedCells();
    int[] rows = new int[selectedCells.size()];
    int[] columns = new int[selectedCells.size()];
    int rowCount = tableView.getItems().size();
    int columnCount = tableView.getColumns().size();

    if (selectedCells.size() == rowCount * columnCount) {
        return "all";
    }

    if (selectedCells.size() == 0) {
        return "";
    }
    JSONObject cells = new JSONObject();
    JSONArray value = new JSONArray();
    for (int i = 0; i < selectedCells.size(); i++) {
        TablePosition<?, ?> cell = selectedCells.get(i);
        rows[i] = cell.getRow();
        columns[i] = cell.getColumn();
        List<String> cellValue = new ArrayList<>();
        cellValue.add(cell.getRow() + "");
        cellValue.add(getColumnName(tableView, cell.getColumn()));
        value.put(cellValue);
    }
    cells.put("cells", value);
    return cells.toString();
}
 
Example #7
Source File: PVTable.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
public PVTable(final PVTableModel model)
{
    this.model = model;
    table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
    model.setUpdateSuppressor(() -> table.getEditingCell() != null);

    // Initial sort: No columns, just new item at bottom
    sorted.setComparator(SORT_NEW_ITEM_LAST);

    // When user clicks on column headers, table.comparatorProperty updates
    // to sort by those columns.
    // SortedList should fundamentally use that order, i.e.
    //   sorted.comparatorProperty().bind(table.comparatorProperty());
    // but in addition the NEW_ITEM should remain last.
    final InvalidationListener sort_changed = p ->
    {
        final Comparator<? super TableItemProxy> column_comparator = table.getComparator();
        // System.out.println("Table column sort: " + column_comparator);
        if (column_comparator == null)
            sorted.setComparator(SORT_NEW_ITEM_LAST);
        else
            sorted.setComparator(SORT_NEW_ITEM_LAST.thenComparing(column_comparator));
    };

    // The InvalidationListener is called when sort order is set up, down or null.
    // Iffy: A ChangeListener was only called when sort order is set up or null,
    // no change when sorting up vs. down, so failed to set the combined
    // SORT_NEW_ITEM_LAST.thenComparing(column_comparator)
    table.comparatorProperty().addListener(sort_changed);

    // TableView.DEFAULT_SORT_POLICY will check if table items are a SortedList.
    // If so, it warns unless  sortedList.comparator == table.comparator,
    // which is not the case since we wrap it in SORT_NEW_ITEM_LAST
    table.setSortPolicy(table ->
    {
        // Underlying 'rows' are kept in their original order
        // System.out.println("Data:");
        // for (TableItemProxy proxy : rows)
        //    System.out.println(proxy.name.get() + (proxy == TableItemProxy.NEW_ITEM ? " (NEW)" : ""));
        // The 'sorted' list uses the changing comparator
        // System.out.println("Sorted:");
        // for (TableItemProxy proxy : sorted)
        //    System.out.println(proxy.name.get() + (proxy == TableItemProxy.NEW_ITEM ? " (NEW)" : ""));

        // Nothing to do, sorted list already handles everything
        return true;
    });

    // Select complete rows
    final TableViewSelectionModel<TableItemProxy> table_sel = table.getSelectionModel();
    table_sel.setCellSelectionEnabled(false);
    table_sel.setSelectionMode(SelectionMode.MULTIPLE);

    // Publish selected PV
    final InvalidationListener sel_changed = change ->
    {
        final List<ProcessVariable> pvs = getSelectedItems()
             .map(proxy -> new ProcessVariable(proxy.getItem().getName()))
             .collect(Collectors.toList());
        SelectionService.getInstance().setSelection("PV Table", pvs);
    };
    table_sel.getSelectedItems().addListener(sel_changed);

    createTableColumns();

    table.setEditable(true);

    toolbar = createToolbar();

    // Have table use the available space
    setMargin(table, new Insets(5));
    VBox.setVgrow(table, Priority.ALWAYS);

    getChildren().setAll(toolbar, table);

    setItemsFromModel();

    createContextMenu();

    model.addListener(model_listener);

    hookDragAndDrop();
}
 
Example #8
Source File: FxTable.java    From FxDock with Apache License 2.0 4 votes vote down vote up
public TableViewSelectionModel<T> getSelectionModel()
{
	return table.getSelectionModel();
}