From 942326f6f759dc8eec9e9e1bcd53cfc52a60eee4 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 31 Jul 2017 22:53:42 -0700 Subject: [PATCH] Implement a cell that copies input when clicked, and add it to table. --- .../org/nwapw/abacus/fx/AbacusController.java | 13 +++++++--- .../org/nwapw/abacus/fx/CopyableCell.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/nwapw/abacus/fx/CopyableCell.java diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 06cbae5..7c9d539 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -3,15 +3,14 @@ package org.nwapw.abacus.fx; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.TableColumn; -import javafx.scene.control.TableView; -import javafx.scene.control.TextField; +import javafx.scene.control.*; import javafx.scene.text.Text; +import javafx.util.Callback; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.tree.TreeNode; + public class AbacusController { private static final String ERR_SYNTAX = "Syntax Error"; @@ -38,12 +37,18 @@ public class AbacusController { @FXML public void initialize(){ + Callback, TableCell> cellFactory = + param -> new CopyableCell<>(); + abacus = new Abacus(); historyData = FXCollections.observableArrayList(); historyTable.setItems(historyData); historyTable.getSelectionModel().setCellSelectionEnabled(true); + inputColumn.setCellFactory(cellFactory); inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty()); + parsedColumn.setCellFactory(cellFactory); parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty()); + outputColumn.setCellFactory(cellFactory); outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty()); } diff --git a/src/main/java/org/nwapw/abacus/fx/CopyableCell.java b/src/main/java/org/nwapw/abacus/fx/CopyableCell.java new file mode 100644 index 0000000..7755704 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/fx/CopyableCell.java @@ -0,0 +1,26 @@ +package org.nwapw.abacus.fx; + +import javafx.scene.control.TableCell; +import javafx.scene.input.MouseEvent; + +import java.awt.*; +import java.awt.datatransfer.StringSelection; + +public class CopyableCell extends TableCell { + + public CopyableCell(){ + addEventFilter(MouseEvent.MOUSE_CLICKED, event -> { + if(event.getClickCount() == 2){ + Toolkit.getDefaultToolkit().getSystemClipboard() + .setContents(new StringSelection(getText()), null); + } + }); + } + + @Override + protected void updateItem(T item, boolean empty) { + super.updateItem(item, empty); + setText((empty || item == null) ? null : item.toString()); + setGraphic(null); + } +}