1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-12-22 08:23:33 +01:00

Shortcuts: close tab and close window.

This commit is contained in:
Carlos Galindo 2019-09-10 15:15:22 +02:00
parent 674ed9d7cf
commit 54bcedbef1
Signed by: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 39 additions and 8 deletions

View file

@ -13,22 +13,24 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; 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.MenuItem; import javafx.scene.control.MenuItem;
import javafx.scene.control.*;
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;
import javafx.scene.input.TransferMode; import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.*;
import java.util.List; import java.util.List;
import java.util.*;
public class Controller implements Initializable { public class Controller implements Initializable {
@FXML @FXML
@ -37,12 +39,16 @@ public class Controller implements Initializable {
@FXML @FXML
private BorderPane root; private BorderPane root;
@FXML
private MenuItem menuCloseTab;
private BooleanProperty table = new SimpleBooleanProperty(false); private BooleanProperty table = new SimpleBooleanProperty(false);
private Map<Tab, TableController> tabMap = new HashMap<>(); private Map<Tab, TableController> tabMap = new HashMap<>();
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { 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) -> table.set(tabMap.get(n) != null));
tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> menuCloseTab.setDisable(!n.isClosable()));
Platform.runLater(() -> Platform.runLater(() ->
root.getScene().getWindow().setOnCloseRequest(event -> { root.getScene().getWindow().setOnCloseRequest(event -> {
for (Tab tab : tabPane.getTabs()) { for (Tab tab : tabPane.getTabs()) {
@ -105,14 +111,13 @@ public class Controller implements Initializable {
if (result.isPresent() && result.get().equals(mergeBT)) { if (result.isPresent() && result.get().equals(mergeBT)) {
List<TestItem> aux = new ArrayList<>(); List<TestItem> aux = new ArrayList<>();
for (int i = 0; i < list.size(); i++) { for (File file : list) {
aux.addAll(TestItem.loadFrom(list.get(i), TestItem.COLONS)); aux.addAll(TestItem.loadFrom(file, TestItem.COLONS));
} }
tabPane.getTabs().add(createTableTab("Merge table", aux, null)); tabPane.getTabs().add(createTableTab("Merge table", aux, null));
tabPane.getSelectionModel().selectLast(); tabPane.getSelectionModel().selectLast();
} else if (result.isPresent() && result.get().equals(openBT)){ } else if (result.isPresent() && result.get().equals(openBT)){
for (int i = 0; i < list.size(); i++) { for (File file : list) {
File file = list.get(i);
List<TestItem> aux = TestItem.loadFrom(file, TestItem.COLONS); List<TestItem> aux = TestItem.loadFrom(file, TestItem.COLONS);
if (aux != null) { if (aux != null) {
tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); 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()) { if (Desktop.isDesktopSupported()) {
try { try {
Desktop.getDesktop().browse(URI.create("https://kauron.github.io/jstudy")); Desktop.getDesktop().browse(URI.create("https://kauron.github.io/jstudy"));
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -257,4 +262,19 @@ public class Controller implements Initializable {
event.acceptTransferModes(TransferMode.ANY); event.acceptTransferModes(TransferMode.ANY);
event.consume(); 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<Event> handler = tab.getOnCloseRequest();
if (handler != null) handler.handle(event);
if (!event.isConsumed()) tabPane.getTabs().remove(tab);
}
} }

View file

@ -5,7 +5,7 @@
<?import javafx.scene.image.*?> <?import javafx.scene.image.*?>
<?import javafx.scene.input.KeyCodeCombination?> <?import javafx.scene.input.KeyCodeCombination?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<BorderPane fx:id="root" prefHeight="450.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="es.kauron.jstudy.controller.Controller"> <BorderPane fx:id="root" prefHeight="450.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="es.kauron.jstudy.controller.Controller">
<center> <center>
<TabPane fx:id="tabPane" focusTraversable="false" minHeight="400.0" minWidth="600.0" tabMinWidth="100.0" BorderPane.alignment="CENTER"> <TabPane fx:id="tabPane" focusTraversable="false" minHeight="400.0" minWidth="600.0" tabMinWidth="100.0" BorderPane.alignment="CENTER">
<tabs> <tabs>
@ -123,6 +123,17 @@
</image> </image>
</ImageView> </ImageView>
</graphic></MenuItem> </graphic></MenuItem>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="menuCloseTab" onAction="#onCloseTab" text="_Close tab" disable="true">
<accelerator>
<KeyCodeCombination alt="UP" code="W" control="DOWN" meta="UP" shift="UP" shortcut="UP"/>
</accelerator>
</MenuItem>
<MenuItem onAction="#onQuit" text="_Quit">
<accelerator>
<KeyCodeCombination alt="UP" code="Q" control="DOWN" meta="UP" shift="UP" shortcut="UP"/>
</accelerator>
</MenuItem>
</items> </items>
</Menu> </Menu>
</menus> </menus>