Skip to content

Commit

Permalink
PoC of copy paste in table view
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Chwedczuk committed Dec 25, 2021
1 parent b3dcc0c commit e0fcffe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TableView fx:id="tableView" style="-fx-font-family: monospaced;" GridPane.rowIndex="1" />
<TableView fx:id="tableView" onKeyPressed="#tableViewKeyPressed" style="-fx-font-family: monospaced;" GridPane.rowIndex="1" />
<HBox alignment="CENTER" GridPane.halignment="CENTER">
<children>
<ImageView fitHeight="32.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true" HBox.hgrow="NEVER">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package pl.marcinchwedczuk.elfviewer.gui.mainwindow;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
Expand Down Expand Up @@ -103,6 +104,9 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
}
});

// Allow selecting cells
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.getSelectionModel().setCellSelectionEnabled(true);
tableView.setPlaceholder(new Label("Select data to display"));

recentlyOpenFiles = new RecentlyOpenFiles(recentlyOpen, this::loadElfFile);
Expand Down Expand Up @@ -135,6 +139,29 @@ private void recreateTreeView() {
rootItem.setExpanded(true);
}

@FXML
private void tableViewKeyPressed(KeyEvent event) {
// Copy to clipboard
KeyCodeCombination copyShortcut = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN);
if (copyShortcut.match(event)) {
event.consume();

StringBuilder clipboardText = new StringBuilder();

// Cells are in by row order, here we depend on this JavaFX behaviour
ObservableList<TablePosition> cells = tableView.getSelectionModel().getSelectedCells();
int lastRow = -1;
for (int i = 0; i < cells.size(); i++) {
TablePosition position = cells.get(i);

Object data = position.getTableColumn().getCellData(position.getRow());

System.out.println(String.format(
"row %d, col %d - %s", position.getRow(), position.getColumn(), data));
}
}
}

@FXML
private void guiOpen() {
File f = openFileChooser.showOpenDialog(window);
Expand Down

0 comments on commit e0fcffe

Please sign in to comment.