package xmltool;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import org.fxmisc.richtext.CodeArea;
import xmltool.utils.UIHelperUtil;
public class Main extends Application {
private Scene scene;
@Override
public void start(final Stage stage) throws Exception {
setUserAgentStylesheet(STYLESHEET_CASPIAN);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Main.fxml"));
Parent root = fxmlLoader.load();
scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setScene(scene);
stage.setTitle("XML Tool");
stage.show();
final TabPane tabPane = (TabPane) scene.lookup("#tabPane");
Tab currTab = tabPane.getSelectionModel().getSelectedItem();
UIBean.setCurCodeArea((CodeArea) currTab.getContent().lookup("#textArea"));
UIBean.getCurCodeArea().requestFocus();
//prevent default on close action of tabs
UIHelperUtil.setOnCloseRequest(tabPane, stage);
//add changeListener for the current selected tab
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
@Override
public void changed(ObservableValue<? extends Tab> ov, Tab oldTab, Tab newTab) {
UIBean.setCurCodeArea((CodeArea) newTab.getContent().lookup("#textArea"));
//can't request focus here as it should happen few ms lates.
}
});
tabPane.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number t, final Number t1) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (tabPane.getTabs().size() > 0) {
final Tab tab = tabPane.getTabs().get(t1.intValue());
final Timeline animation = new Timeline(
new KeyFrame(Duration.millis(25),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Platform.runLater(new Runnable() {
@Override
public void run() {
tab.getContent().lookup("#textArea").requestFocus();
}
});
}
}));
animation.setCycleCount(1);
animation.play();
} else { //all tabs are closed exit the program
UIHelperUtil.exitProgram();
}
}
});
}
});
initialization(fxmlLoader.getController());
}
/**
* Restores the program to some saved state.
*/
private void initialization(MainController mainController) {
//restores the serelized file to the previous state
UIHelperUtil.restoreState(mainController);
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}