/*
* Copyright 2015 Sudipto Chandra.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sandsoft.cymric.startup;
import org.sandsoft.cymric.model.CymricCore;
import org.sandsoft.cymric.util.Commons;
import org.sandsoft.cymric.util.Logs;
import org.sandsoft.cymric.util.Resources;
import org.sandsoft.cymric.view.RootLayout;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.web.WebView;
import javafx.stage.DirectoryChooser;
/**
* StarupWizard Controller class. This wizard opens at startup and prompt for
* general settings. If settings are already done, this wizard should not be
* shown.
*
* @author Sudipto Chandra
*/
public class StartupWizard extends javafx.scene.layout.BorderPane {
private final String WORKDIR_CHOOSER_TITLE = "Choose Cymric Working Directory";
private final String WORKDIR_SUCCESS_MSG = "Click Next to continue...";
private final String WORKDIR_FAILED_MSG = "Invalid directory. Click Browse to select a valid one.";
/**
* Creates a new instance of StartupWizard.
*
* @param core Object used for interactivity among the other process.
* @return A new instance of StartupWizard.
*/
public static StartupWizard createNew(CymricCore core) {
try {
//get layout
StartupWizard startup = (StartupWizard) Commons.loadPaneFromFXML(StartupWizard.class);
startup.setPrefWidth(600);
startup.setPrefHeight(400);
//post load work
startup.setCymricCore(core);
startup.initialize();
return startup;
} catch (Exception ex) {
Logs.showStackTrace(ex);
}
return null;
}
//cymric core
private CymricCore mCore;
//
// FXML Objects
//
@FXML
private TabPane tabPane;
@FXML
private Tab welcomeTab;
@FXML
private Tab tutorialTab;
@FXML
private Tab setupTab;
@FXML
private Tab startTab;
@FXML
private Button nextButton;
@FXML
private Button previousButton;
@FXML
private WebView tutorialViewer;
@FXML
private TitledPane workdirAccordion;
@FXML
private Label workdirMsgLabel;
@FXML
private Label workdirInfoLabel;
@FXML
private TextField workdirTextField;
@FXML
private Button workdirButton;
@FXML
private GridPane bottomBar;
/**
* Initializes controls after FXML is loaded.
*/
public void initialize() {
//laod tutorial
tutorialViewer.getEngine().loadContent(
Resources.getTutorial(Resources.TUTORIAL_INTRO));
//setup info
workdirInfoLabel.setText(
Resources.getString("WorkdirInfo"));
//catch tab selection change property
tabPane.getSelectionModel().selectedIndexProperty().addListener(
(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
selectedTabIndexChange(oldValue.intValue(), newValue.intValue());
}
);
}
/**
* Sets the Cymric core to this object. It should be called after all
* initialization has been completed.
*
* @param core Reference to original Cymric core object.
*/
public void setCymricCore(CymricCore core) {
mCore = core;
}
/**
* Checks if the all setup is done and okay.
*
* @return True if okay; false otherwise.
*/
private boolean isSetupOkay() {
return mCore.getAppSettings().getWorkingPath() != null;
}
/**
* Displays the root layout.
*/
private void showRootLayout() {
mCore.attachRootNode(RootLayout.createNew(mCore), true);
}
/**
* Called when a new index has been selected.
*
* @param prev Previously selected index
* @param now Currently selected index
*/
private void selectedTabIndexChange(int prev, int now) {
Tab current = tabPane.getTabs().get(now);
//enable disable next button
if (current == setupTab) {
nextButton.setDisable(!isSetupOkay());
} else {
nextButton.setDisable(false);
}
//show-hide bottom bar
if (current == welcomeTab || current == startTab) {
bottomBar.setMinHeight(0.0);
bottomBar.setPrefHeight(0.0);
} else {
bottomBar.setMinHeight(40.0);
bottomBar.setPrefHeight(40.0);
}
}
//
// Listener methods
//
@FXML
private void previousButtonClicked() {
tabPane.getSelectionModel().selectPrevious();
}
@FXML
private void nextButtonClicked() {
tabPane.getSelectionModel().selectNext();
}
@FXML
private void startButtonClicked() {
showRootLayout();
}
@FXML
private void browseWorkingDir() {
//select working directory
String file = null;
try {
javafx.stage.DirectoryChooser dirChooser = new DirectoryChooser();
dirChooser.setTitle(WORKDIR_CHOOSER_TITLE);
String sel = dirChooser.showDialog(mCore.getPrimaryStage()).toString();
mCore.getAppSettings().setWorkingPath(sel);
file = mCore.getAppSettings().getWorkingPath();
} catch (Exception ex) {
}
//change view
if (file != null) {
nextButton.setDisable(false);
workdirTextField.setText(file);
workdirMsgLabel.setText(WORKDIR_SUCCESS_MSG);
} else {
nextButton.setDisable(true);
workdirTextField.setText(null);
workdirMsgLabel.setText(WORKDIR_FAILED_MSG);
}
}
}