Search in sources :

Example 1 with SelectionMode

use of javafx.scene.control.SelectionMode in project Gargoyle by callakrsos.

the class FxTableViewUtil method installCopyHandler.

@SuppressWarnings("rawtypes")
public static void installCopyHandler(TableView<?> table) {
    table.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
        if (e.isConsumed())
            return;
        int type = -1;
        if (e.isControlDown() && e.getCode() == KeyCode.C) {
            if (e.isShiftDown()) {
                type = 2;
            } else {
                type = 1;
            }
        }
        if (type == -1)
            return;
        TableViewSelectionModel<?> selectionModel = table.getSelectionModel();
        SelectionMode selectionMode = selectionModel.getSelectionMode();
        boolean cellSelectionEnabled = selectionModel.isCellSelectionEnabled();
        if (!cellSelectionEnabled) {
            Object selectedItem = table.getSelectionModel().getSelectedItem();
            ObservableList<?> columns = table.getColumns();
            Optional<String> reduce = columns.stream().filter(ob -> ob instanceof TableColumn).map(obj -> (TableColumn) obj).map(tc -> tc.getCellData(selectedItem)).filter(v -> v != null).map(v -> v.toString()).reduce((o1, o2) -> o1.toString().concat("\t").concat(o2.toString()));
            reduce.ifPresent(str -> {
                FxClipboardUtil.putString(str);
                e.consume();
            });
        } else if (cellSelectionEnabled) {
            ObservableList<TablePosition> selectedCells = selectionModel.getSelectedCells();
            TablePosition tablePosition = selectedCells.get(0);
            TableColumn tableColumn = tablePosition.getTableColumn();
            int row = tablePosition.getRow();
            int col = table.getColumns().indexOf(tableColumn);
            switch(type) {
                case 1:
                    StringBuilder sb = new StringBuilder();
                    for (TablePosition cell : selectedCells) {
                        if (row != cell.getRow()) {
                            sb.append("\n");
                            row++;
                        } else if (col != table.getColumns().indexOf(cell.getTableColumn())) {
                            sb.append("\t");
                        }
                        Object cellData = cell.getTableColumn().getCellData(cell.getRow());
                        sb.append(ValueUtil.decode(cellData, cellData, "").toString());
                    }
                    FxClipboardUtil.putString(sb.toString());
                    e.consume();
                    break;
                case 2:
                    Object cellData = tableColumn.getCellData(row);
                    FxClipboardUtil.putString(ValueUtil.decode(cellData, cellData, "").toString());
                    e.consume();
                    break;
            }
        }
    });
}
Also used : KeyCode(javafx.scene.input.KeyCode) ObjectProperty(javafx.beans.property.ObjectProperty) TablePosition(javafx.scene.control.TablePosition) TextFieldTableCell(javafx.scene.control.cell.TextFieldTableCell) KeyEvent(javafx.scene.input.KeyEvent) StringConverter(javafx.util.StringConverter) Field(java.lang.reflect.Field) TableColumn(javafx.scene.control.TableColumn) TableCell(javafx.scene.control.TableCell) SelectionMode(javafx.scene.control.SelectionMode) ComboBoxTableCell(javafx.scene.control.cell.ComboBoxTableCell) Clipboard(javafx.scene.input.Clipboard) Optional(java.util.Optional) ObservableValue(javafx.beans.value.ObservableValue) ObservableList(javafx.collections.ObservableList) TableView(javafx.scene.control.TableView) Callback(javafx.util.Callback) Method(java.lang.reflect.Method) TableViewSelectionModel(javafx.scene.control.TableView.TableViewSelectionModel) TableColumn(javafx.scene.control.TableColumn) ObservableList(javafx.collections.ObservableList) TablePosition(javafx.scene.control.TablePosition) SelectionMode(javafx.scene.control.SelectionMode)

Aggregations

Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 ObjectProperty (javafx.beans.property.ObjectProperty)1 ObservableValue (javafx.beans.value.ObservableValue)1 ObservableList (javafx.collections.ObservableList)1 SelectionMode (javafx.scene.control.SelectionMode)1 TableCell (javafx.scene.control.TableCell)1 TableColumn (javafx.scene.control.TableColumn)1 TablePosition (javafx.scene.control.TablePosition)1 TableView (javafx.scene.control.TableView)1 TableViewSelectionModel (javafx.scene.control.TableView.TableViewSelectionModel)1 ComboBoxTableCell (javafx.scene.control.cell.ComboBoxTableCell)1 TextFieldTableCell (javafx.scene.control.cell.TextFieldTableCell)1 Clipboard (javafx.scene.input.Clipboard)1 KeyCode (javafx.scene.input.KeyCode)1 KeyEvent (javafx.scene.input.KeyEvent)1 Callback (javafx.util.Callback)1 StringConverter (javafx.util.StringConverter)1