From 54bcedbef1c108737e01fd6d8b7803a5b817c25e Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 10 Sep 2019 15:15:22 +0200 Subject: [PATCH] Shortcuts: close tab and close window. --- .../kauron/jstudy/controller/Controller.java | 34 +++++++++++++++---- .../resources/es/kauron/jstudy/view/main.fxml | 13 ++++++- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index e53ac28..989a183 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -13,22 +13,24 @@ import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; -import javafx.scene.control.*; import javafx.scene.control.MenuItem; +import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.DragEvent; import javafx.scene.input.TransferMode; import javafx.scene.layout.BorderPane; import javafx.stage.FileChooser; +import javafx.stage.Window; +import javafx.stage.WindowEvent; import java.awt.*; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URL; -import java.util.*; import java.util.List; +import java.util.*; public class Controller implements Initializable { @FXML @@ -37,12 +39,16 @@ public class Controller implements Initializable { @FXML private BorderPane root; + @FXML + private MenuItem menuCloseTab; + private BooleanProperty table = new SimpleBooleanProperty(false); private Map tabMap = new HashMap<>(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> table.set(tabMap.get(n) != null)); + tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> menuCloseTab.setDisable(!n.isClosable())); Platform.runLater(() -> root.getScene().getWindow().setOnCloseRequest(event -> { for (Tab tab : tabPane.getTabs()) { @@ -105,14 +111,13 @@ public class Controller implements Initializable { if (result.isPresent() && result.get().equals(mergeBT)) { List aux = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - aux.addAll(TestItem.loadFrom(list.get(i), TestItem.COLONS)); + for (File file : list) { + aux.addAll(TestItem.loadFrom(file, TestItem.COLONS)); } tabPane.getTabs().add(createTableTab("Merge table", aux, null)); tabPane.getSelectionModel().selectLast(); } else if (result.isPresent() && result.get().equals(openBT)){ - for (int i = 0; i < list.size(); i++) { - File file = list.get(i); + for (File file : list) { List aux = TestItem.loadFrom(file, TestItem.COLONS); if (aux != null) { tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); @@ -234,7 +239,7 @@ public class Controller implements Initializable { if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(URI.create("https://kauron.github.io/jstudy")); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -257,4 +262,19 @@ public class Controller implements Initializable { event.acceptTransferModes(TransferMode.ANY); event.consume(); } + + @FXML + protected void onQuit(ActionEvent event) { + Window w = root.getScene().getWindow(); + w.fireEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSE_REQUEST)); + } + + @FXML + protected void onCloseTab(ActionEvent event) { + Tab tab = tabPane.getSelectionModel().getSelectedItem(); + if (!tab.isClosable()) return; + EventHandler handler = tab.getOnCloseRequest(); + if (handler != null) handler.handle(event); + if (!event.isConsumed()) tabPane.getTabs().remove(tab); + } } diff --git a/src/main/resources/es/kauron/jstudy/view/main.fxml b/src/main/resources/es/kauron/jstudy/view/main.fxml index 9271918..60b4eaa 100644 --- a/src/main/resources/es/kauron/jstudy/view/main.fxml +++ b/src/main/resources/es/kauron/jstudy/view/main.fxml @@ -5,7 +5,7 @@ - +
@@ -123,6 +123,17 @@ + + + + + + + + + + +