From 3e41a528a5a6c94f4199be9da31fb4f0bda9c18f Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Fri, 9 Dec 2016 17:38:21 +0100 Subject: [PATCH] Added tab duplication --- .../java/es/kauron/jstudy/controller/Controller.java | 12 +++++++++++- .../es/kauron/jstudy/controller/TableController.java | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index d09b230..aec4ea0 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -14,6 +14,7 @@ import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.control.*; +import javafx.scene.control.MenuItem; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.DragEvent; @@ -152,7 +153,7 @@ public class Controller implements Initializable { private Tab createTableTab(String name, List list, File file) { try { - Tab tab = new Tab(name); + final Tab tab = new Tab(name); FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/table.fxml")); Parent tableRoot = loader.load(); @@ -160,7 +161,16 @@ public class Controller implements Initializable { ((TableController) loader.getController()).setData(name, list, this, file); tabMap.put(tab, loader.getController()); + MenuItem duplicate = new MenuItem("Duplicate table"); + duplicate.setOnAction(event -> { + TableController controller = loader.getController(); + Tab newTab = createTableTab(controller.getName() + " (copy)", controller.getData(), null); + tabPane.getTabs().add(tabPane.getTabs().indexOf(tab) + 1, newTab); + tabPane.getSelectionModel().selectNext(); + }); + tab.setContent(tableRoot); + tab.setContextMenu(new ContextMenu(duplicate)); tab.setOnCloseRequest(event -> { if (!((TableController) loader.getController()).saved.get()) { Alert dialog = new Alert(Alert.AlertType.WARNING); diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index 2d77a1c..bd44a6f 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -25,6 +25,7 @@ import javafx.stage.Stage; import java.io.File; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; @@ -40,7 +41,7 @@ public class TableController implements Initializable { private Controller parent; private File file; private String name; - protected BooleanProperty saved = new SimpleBooleanProperty(); + BooleanProperty saved = new SimpleBooleanProperty(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -66,6 +67,9 @@ public class TableController implements Initializable { saved.set(file != null); } + List getData() {return new ArrayList<>(data);} + String getName() {return file == null ? name : file.getName().substring(0, file.getName().lastIndexOf('.'));} + @FXML protected void onSaveAction(ActionEvent event) { if (file == null) {