1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-11-13 15:43:44 +01:00

Added tab duplication

This commit is contained in:
Carlos Galindo 2016-12-09 17:38:21 +01:00
parent 67df24c9dc
commit 3e41a528a5
Signed by: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 16 additions and 2 deletions

View file

@ -14,6 +14,7 @@ import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent; import javafx.scene.input.DragEvent;
@ -152,7 +153,7 @@ public class Controller implements Initializable {
private Tab createTableTab(String name, List<TestItem> list, File file) { private Tab createTableTab(String name, List<TestItem> list, File file) {
try { try {
Tab tab = new Tab(name); final Tab tab = new Tab(name);
FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/table.fxml")); FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/table.fxml"));
Parent tableRoot = loader.load(); Parent tableRoot = loader.load();
@ -160,7 +161,16 @@ public class Controller implements Initializable {
((TableController) loader.getController()).setData(name, list, this, file); ((TableController) loader.getController()).setData(name, list, this, file);
tabMap.put(tab, loader.getController()); 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.setContent(tableRoot);
tab.setContextMenu(new ContextMenu(duplicate));
tab.setOnCloseRequest(event -> { tab.setOnCloseRequest(event -> {
if (!((TableController) loader.getController()).saved.get()) { if (!((TableController) loader.getController()).saved.get()) {
Alert dialog = new Alert(Alert.AlertType.WARNING); Alert dialog = new Alert(Alert.AlertType.WARNING);

View file

@ -25,6 +25,7 @@ import javafx.stage.Stage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -40,7 +41,7 @@ public class TableController implements Initializable {
private Controller parent; private Controller parent;
private File file; private File file;
private String name; private String name;
protected BooleanProperty saved = new SimpleBooleanProperty(); BooleanProperty saved = new SimpleBooleanProperty();
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
@ -66,6 +67,9 @@ public class TableController implements Initializable {
saved.set(file != null); saved.set(file != null);
} }
List<TestItem> getData() {return new ArrayList<>(data);}
String getName() {return file == null ? name : file.getName().substring(0, file.getName().lastIndexOf('.'));}
@FXML @FXML
protected void onSaveAction(ActionEvent event) { protected void onSaveAction(ActionEvent event) {
if (file == null) { if (file == null) {