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;
}
}
});
}
Aggregations