1 package fi.jyu.mit.fxgui;
3 import java.io.IOException;
6 import javafx.fxml.FXML;
7 import javafx.fxml.FXMLLoader;
8 import javafx.scene.Node;
9 import javafx.scene.Parent;
10 import javafx.scene.Scene;
11 import javafx.stage.Modality;
12 import javafx.stage.Stage;
21 public class ModalController<
TYPE>
implements ModalControllerInterface<TYPE> {
31 public interface ModalInitializeInterface<
TYPE, CONTROLLERTYPE
extends ModalControllerInterface<TYPE>> {
39 class MyGenericClass<T> {
96 return ((Stage)n.getScene().getWindow());
118 return showModal(url, title, modalityStage, oletus, null);
131 public static <TYPE, CONTROLLERTYPE extends ModalControllerInterface<TYPE>>
132 TYPE showModal(URL url, String title, Stage modalityStage,
TYPE oletus,
133 ModalInitializeInterface<TYPE, CONTROLLERTYPE> initializeCallback) {
135 FXMLLoader ldr =
new FXMLLoader(url);
136 Parent root = ldr.load();
137 @SuppressWarnings(
"unchecked")
138 final CONTROLLERTYPE dialogCtrl = (CONTROLLERTYPE)ldr.getController();
139 Stage stage =
new Stage();
140 if ( dialogCtrl instanceof ModalController ) {
144 stage.setScene(
new Scene(root));
145 stage.setTitle(title);
146 if ( modalityStage != null ) {
147 stage.initModality(Modality.WINDOW_MODAL);
148 stage.initOwner(modalityStage);
150 stage.initModality(Modality.APPLICATION_MODAL);
152 if ( dialogCtrl != null ) {
153 dialogCtrl.setDefault(oletus);
154 stage.setOnShown((e)-> {
157 dialogCtrl.handleShown();
159 if ( initializeCallback != null ) initializeCallback.initialize(dialogCtrl);
161 return dialogCtrl.getResult();
166 }
catch (IOException e) {
167 System.err.println(e.toString());
180 public static <TYPE, CONTROLLERTYPE extends ModalControllerInterface<TYPE>> CONTROLLERTYPE
showModeless(URL url, String title,
TYPE oletus) {
181 Stage stage =
new Stage();
183 FXMLLoader ldr =
new FXMLLoader(url);
184 Parent root = ldr.load();
185 @SuppressWarnings(
"unchecked")
186 final CONTROLLERTYPE dialogCtrl = (CONTROLLERTYPE)ldr.getController();
187 stage.setScene(
new Scene(root));
188 stage.setTitle(title);
189 if ( dialogCtrl != null ) {
190 dialogCtrl.setDefault(oletus);
191 stage.setOnShown((e)-> {
194 dialogCtrl.handleShown();
202 }
catch (IOException e) {
203 System.err.println(e.toString());