Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added utility class for loading fxml views #363

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/ui/Displayable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.zerotask.voices.ui;

import javafx.stage.Stage;

/**
* Marker interface for views that need a reference to their stage.
*
* @author SirWindfield
*
*/
public interface Displayable {

/**
* Sets the stage that has been used to show the view content.
*
* @param stage
* The stage.
*/
void setStage(Stage stage);
}
97 changes: 97 additions & 0 deletions mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/ui/UILoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package de.zerotask.voices.ui;

import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.ViewModel;
import de.saxsys.mvvmfx.ViewTuple;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
* A small utility class that is capable of loading views. TODO: generic
* parameter VM could eventually be dropped but the whole thing would be then
* not type-safe...
*
* @author SirWindfield
*
*/
public class UILoader {

public static <V extends FxmlView<VM>, VM extends ViewModel> void load(Class<V> viewClass, Class<VM> viewModelClass,
Stage parentStage) {
load(viewClass, viewModelClass, parentStage, "");
}

public static <V extends FxmlView<VM>, VM extends ViewModel> void load(Class<V> viewClass, Class<VM> viewModelClass,
Stage parentStage, String title) {
load(viewClass, viewModelClass, parentStage, title, true);
}

public static <V extends FxmlView<VM>, VM extends ViewModel> void load(Class<V> viewClass, Class<VM> viewModelClass,
Stage parentStage, String title, String... css) {
load(viewClass, viewModelClass, parentStage, title, true, (String[]) null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug in line 34. (String[])null needs to be css.

}

public static <V extends FxmlView<VM>, VM extends ViewModel> void load(Class<V> viewClass, Class<VM> viewModelClass,
Stage parentStage, String title, boolean resizable, String... css) {
// load our UI
ViewTuple<V, VM> ui = FluentViewLoader.fxmlView(viewClass).load();
// retrieve view element
Parent view = ui.getView();
// create a stage for displaying
Stage stage = createStage(view, parentStage, title, resizable, css);
// if view is instance of Displayable, set stage so it will
// automatically be available as soon as the view gets loaded
if (Displayable.class.isAssignableFrom(viewClass)) {
Displayable displayable = (Displayable) ui.getCodeBehind();
displayable.setStage(stage);
}
}

private static Stage createStage(Parent view, Stage parentStage, String title, boolean resizable, String... css) {
// create a new stage
Stage stage = new Stage(); // the stage style could possibly be set by a
// system property. Normally, if you want to
// display additional windows the default
// style should be used
// this stage should be displayed over the parent stage
stage.initOwner(parentStage);
// this will prevent events from being passed to the parent stage
stage.initModality(Modality.NONE);
// customize the stage...
stage.setTitle(title);
stage.setResizable(resizable);

// to further make the window feel more natural, pass all icons from the
// parent to this stage
stage.getIcons().addAll(parentStage.getIcons());

// copied from dialog helper:
if (stage.getScene() == null) {
Scene scene = new Scene(view);
// just use the css property if it is non null
if (css != null && css.length != 0) {
// convention: if the first element is null, the var args will be
// assumed to be empty...
if (css[0] != null) {
scene.getStylesheets().addAll(css);
}
}
stage.setScene(scene);

// finally show the stage
stage.show();
// return the stage so it can be used for the code behind part
return stage;
}
// I have no idea if that actually will ever happen!
return null;
}

public static <V extends FxmlView<VM>, VM extends ViewModel> void load(Class<V> viewClass, Class<VM> viewModelClass,
Stage parentStage, String title, boolean resizable) {
load(viewClass, viewModelClass, parentStage, title, resizable, (String[]) null);
}
}