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.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<Tab, TableController> 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<TestItem> 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<TestItem> 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<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.input.KeyCodeCombination?>
<?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>
<TabPane fx:id="tabPane" focusTraversable="false" minHeight="400.0" minWidth="600.0" tabMinWidth="100.0" BorderPane.alignment="CENTER">
<tabs>
@ -123,6 +123,17 @@
</image>
</ImageView>
</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>
</Menu>
</menus>