-
Notifications
You must be signed in to change notification settings - Fork 105
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/ui/Displayable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
97
mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/ui/UILoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 becss
.