Example usage for javafx.scene.control Tab setOnCloseRequest

List of usage examples for javafx.scene.control Tab setOnCloseRequest

Introduction

In this page you can find the example usage for javafx.scene.control Tab setOnCloseRequest.

Prototype

public void setOnCloseRequest(EventHandler<Event> value) 

Source Link

Usage

From source file:de.pixida.logtest.designer.MainWindow.java

private void addEditorAsNewTab(final Editor editor) {
    final Tab newTab = new Tab();
    newTab.setContent(editor);/*www. j  av a2 s.c o m*/
    newTab.setGraphic(Icons.getIconGraphics(editor.getIconName()));
    newTab.textProperty().bind(editor.getTitleShort());
    newTab.setOnCloseRequest(event -> {
        if (!this.handleCloseTab()) {
            event.consume(); // Do not continue / leave tab open
        }
    });
    this.tabPane.getTabs().add(newTab);
    this.tabPane.getSelectionModel().select(newTab);
}

From source file:com.esri.geoevent.test.performance.ui.OrchestratorController.java

private void addFixtureTab(final Fixture fixture, boolean isDefault) {
    try {//from ww w .  j a  v a 2s .  c om
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Fixture.fxml"));
        Parent fixtureTab = (Parent) loader.load();

        FixtureController controller = loader.getController();
        controller.setFixture(fixture);
        controller.setIsDefault(isDefault);

        Tab newTab = new Tab(fixture.getName());
        newTab.setContent(fixtureTab);
        newTab.closableProperty().setValue(!isDefault);
        newTab.setOnCloseRequest(event -> {
            boolean isOkClicked = showConfirmationDialog(
                    UIMessages.getMessage("UI_CLOSE_TAB_LABEL", fixture.getName()));
            if (!isOkClicked) {
                event.consume();
            } else {
                fixtures.getFixtures().remove(fixture);
            }
        });
        fixtureTabPane.getTabs().add(newTab);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.beryx.viewreka.fxapp.Viewreka.java

private void updateProjectTabs(ProjectSettings projectSettings) {

    if (selectedIndexListener != null) {
        projectTabPane.getSelectionModel().selectedIndexProperty().removeListener(selectedIndexListener);
    }/*from  ww  w  .  j av a2 s .  c o  m*/

    menuTabBindingSourceCode.setSettings(projectSettings, true);
    menuTabBindingHelp.setSettings(projectSettings, false);

    if (projectSettings != null) {
        selectedIndexListener = (obs, oldValue, newValue) -> projectSettings
                .setProperty(PROP_SELECTED_TAB_INDEX, newValue);
        projectTabPane.getSelectionModel().selectedIndexProperty().addListener(selectedIndexListener);

        selectedItemListener = (obs, oldTab, newTab) -> {
            butSaveFile.disableProperty().unbind();
            mnuSaveFile.disableProperty().unbind();
            butSaveFile.setDisable(true);
            mnuSaveFile.setDisable(true);
            if (newTab != null) {
                CodeTabData tabData = getData(newTab);
                Platform.runLater(() -> {
                    BooleanBinding scriptChangedBinding = Bindings.createBooleanBinding(
                            () -> !tabData.isDirty(), tabData.getTextProperty(),
                            tabData.getInitialTextProperty());
                    butSaveFile.disableProperty().bind(scriptChangedBinding);
                    mnuSaveFile.disableProperty().bind(scriptChangedBinding);

                    StringBinding tabTextBinding = Bindings.createStringBinding(() -> {
                        String text = tabData.getTabText();
                        if (tabData.isDirty()) {
                            text = "*" + text;
                        }
                        return text;
                    }, tabData.getTextProperty(), tabData.getInitialTextProperty());
                    newTab.textProperty().bind(tabTextBinding);
                });
            }

            Platform.runLater(() -> {
                menuTabBindingSourceCode.setSettings(projectSettings, true);
                menuTabBindingHelp.setSettings(projectSettings, false);

                butSaveAll.disableProperty().unbind();
                mnuSaveAll.disableProperty().unbind();
                butSaveAll.setDisable(true);
                mnuSaveAll.setDisable(true);

                List<BooleanBinding> saveBindings = new ArrayList<>();
                projectTabPane.getTabs().forEach(tab -> {
                    CodeTabData tabData = getData(tab);
                    saveBindings.add(Bindings.createBooleanBinding(() -> !tabData.isDirty(),
                            tabData.getTextProperty(), tabData.getInitialTextProperty()));
                });
                if (!saveBindings.isEmpty()) {
                    BooleanBinding binding = saveBindings.get(0);
                    for (int i = 1; i < saveBindings.size(); i++) {
                        binding = Bindings.and(binding, saveBindings.get(i));
                    }
                    mnuSaveAll.disableProperty().bind(binding);
                    butSaveAll.disableProperty().bind(binding);
                }
            });
        };
        projectTabPane.getSelectionModel().selectedItemProperty().addListener(selectedItemListener);
    }

    int selectedTabIndex = (projectSettings == null) ? 0
            : projectSettings.getProperty(PROP_SELECTED_TAB_INDEX, 0, false);

    ObservableList<Tab> tabs = projectTabPane.getTabs();
    tabs.clear();

    Tab selectedTab = null;
    List<String> openFiles = getOpenFiles(projectSettings);
    for (int i = 0; i < openFiles.size(); i++) {
        String filePath = openFiles.get(i);
        Tab addedTab = null;
        if (filePath.equals(FILE_ALIAS_SOURCE_CODE)) {
            addedTab = tabSourceCode;
            String projectPath = projectPathProperty.get();
            String tabText = (projectPath == null) ? "Code" : new File(projectPath).getName();
            tabSourceCode.setUserData(new CodeTabData(tabText));
        } else if (filePath.equals(FILE_ALIAS_HELP)) {
            addedTab = tabHelp;
        } else {
            try {
                File file = new File(filePath);
                addedTab = CodeAreaTab.fromFile(file);
                final Tab tab = addedTab;
                tab.setOnCloseRequest(ev -> {
                    if (!tryClose(tab))
                        ev.consume();
                });
            } catch (Exception e) {
                addedTab = null;
            }
        }
        if (addedTab != null) {
            tabs.add(addedTab);
            getData(addedTab).setFilePath(filePath);
            if (i == selectedTabIndex) {
                selectedTab = addedTab;
            }
        }
    }
    if (!tabs.isEmpty()) {
        if (selectedTab == null) {
            selectedTab = tabs.get(0);
        }
        projectTabPane.getSelectionModel().select(null);
        projectTabPane.getSelectionModel().select(selectedTab);
    }
}