Skip to content

Commit

Permalink
Merge pull request #104 from sialcasa/#82_fluent_api
Browse files Browse the repository at this point in the history
#82 fluent api
  • Loading branch information
manuel-mauky committed Aug 4, 2014
2 parents 686b40e + 7225fc7 commit 09509d6
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.saxsys.jfx.exampleapplication.view.maincontainer.MainContainerView;
import de.saxsys.jfx.exampleapplication.viewmodel.maincontainer.MainContainerViewModel;
import de.saxsys.jfx.mvvm.cdi.MvvmfxCdiApplication;
import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import de.saxsys.jfx.mvvm.viewloader.ViewLoader;
import de.saxsys.jfx.mvvm.viewloader.ViewTuple;

Expand All @@ -23,14 +24,10 @@ public static void main(String... args) {
launch(args);
}

@Inject
private ViewLoader viewLoader;

@Override
public void start(Stage stage) {
ViewTuple<MainContainerView, MainContainerViewModel> tuple =
viewLoader
.loadViewTuple(MainContainerView.class);
FluentViewLoader.fxmlView(MainContainerView.class).load();

Parent view = tuple.getView();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
Expand Down Expand Up @@ -46,10 +48,6 @@ public class MainContainerView implements FxmlView<MainContainerViewModel>, Init
// Inject the Code behind instance of the ListView
private ListView<Integer> personWelcomeListView;

@Inject
// ViewLoder
private ViewLoader viewLoader;

@Inject
// Notification Center
private NotificationCenter notificationCenter;
Expand Down Expand Up @@ -93,8 +91,7 @@ public void changed(ObservableValue<? extends Number> arg0,
public ViewTuple<? extends View, ? extends ViewModel> map(Integer element) {
if (!viewMap.containsKey(element)) {
ViewTuple<PersonWelcomeView, PersonWelcomeViewModel> loadedViewTuple
= viewLoader
.loadViewTuple(PersonWelcomeView.class);
= FluentViewLoader.fxmlView(PersonWelcomeView.class).load();

PersonWelcomeView codeBehind = loadedViewTuple.getCodeBehind();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.saxsys.jfx.mvvmfx.fx_root_example;

import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
Expand All @@ -25,12 +26,7 @@ public class LabeledTextField extends HBox implements FxmlView<LabeledTextFieldV
private LabeledTextFieldViewModel viewModel;

public LabeledTextField() {
ViewLoader viewLoader = new ViewLoader();

viewLoader.setCodeBehind(this);
viewLoader.setRoot(this);

viewLoader.loadViewTuple(this.getClass());
FluentViewLoader.fxmlView(this.getClass()).root(this).codeBehind(this).load();
}

public void initialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.saxsys.jfx.exampleapplication.view.maincontainer.MainContainerView;
import de.saxsys.jfx.exampleapplication.viewmodel.maincontainer.MainContainerViewModel;
import de.saxsys.jfx.mvvm.guice.MvvmfxGuiceApplication;
import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import de.saxsys.jfx.mvvm.viewloader.ViewLoader;
import de.saxsys.jfx.mvvm.viewloader.ViewTuple;

Expand All @@ -21,19 +22,15 @@
*/
public class Starter extends MvvmfxGuiceApplication {

// Get the MVVM View Loader
@Inject
private ViewLoader viewLoader;

public static void main(final String[] args) {
launch(args);
}

@Override
public void startMvvmfx(final Stage stage) throws Exception {
ViewTuple<MainContainerView, MainContainerViewModel> tuple =
viewLoader
.loadViewTuple(MainContainerView.class);
FluentViewLoader.fxmlView(MainContainerView.class).load();

// Locate View for loaded FXML file
final Parent view = tuple.getView();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.saxsys.jfx.mvvmfx.helloworld;


import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -21,10 +22,7 @@ public static void main(String... args) {
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World Application");

ViewLoader viewLoader = new ViewLoader();

ViewTuple<HelloWorldView, HelloWorldViewModel> viewTuple = viewLoader
.loadViewTuple(HelloWorldView.class);
ViewTuple<HelloWorldView, HelloWorldViewModel> viewTuple = FluentViewLoader.javaView(HelloWorldView.class).load();

Parent root = viewTuple.getView();
stage.setScene(new Scene(root));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.saxsys.jfx.mvvmfx.helloworld;

import de.saxsys.jfx.mvvm.viewloader.FluentViewLoader;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -20,11 +21,8 @@ public static void main(String... args) {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World Application");

ViewLoader viewLoader = new ViewLoader();

ViewTuple<HelloWorldView, HelloWorldViewModel> viewTuple = viewLoader
.loadViewTuple(HelloWorldView.class);

ViewTuple<HelloWorldView, HelloWorldViewModel> viewTuple = FluentViewLoader.fxmlView(HelloWorldView.class).load();

Parent root = viewTuple.getView();
stage.setScene(new Scene(root));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package de.saxsys.jfx.mvvm.viewloader;

import de.saxsys.jfx.mvvm.api.FxmlView;
import de.saxsys.jfx.mvvm.api.JavaView;
import de.saxsys.jfx.mvvm.api.ViewModel;
import de.saxsys.jfx.mvvm.base.view.View;
import net.jodah.typetools.TypeResolver;

import java.util.ResourceBundle;

/**
* Fluent API for loading Views. This is basically a wrapper around the {@link de.saxsys.jfx.mvvm.viewloader.ViewLoader}
* to get a better usable API.
*
* @author manuel.mauky
*/
public class FluentViewLoader {

/**
* This class is the builder step to load a java based view.
* @param <ViewType>
* @param <ViewModelType>
*/
public static class JavaViewStep<ViewType extends JavaView<? extends ViewModelType>, ViewModelType extends ViewModel> {

private Class<? extends ViewType> viewType;
private ResourceBundle resourceBundle;

JavaViewStep(Class<? extends ViewType> viewType){
this.viewType = viewType;
}

/**
* @param resourceBundle the resource bundle that is used while loading the view.
* @return this instance of the builder step.
*/
public JavaViewStep<ViewType, ViewModelType> resourceBundle(ResourceBundle resourceBundle){
this.resourceBundle = resourceBundle;
return this;
}

/**
* The final step of the Fluent API. This method loads the view based on the given params.
*
* @return a view tuple containing the loaded view.
*/
public ViewTuple<ViewType, ViewModelType> load(){
ViewLoader viewLoader = new ViewLoader();

return viewLoader.loadViewTuple(viewType, resourceBundle);
}
}

/**
* This class is the builder step to load a fxml based view.
* @param <ViewType>
* @param <ViewModelType>
*/
public static class FxmlViewStep<ViewType extends FxmlView<? extends ViewModelType>, ViewModelType extends ViewModel> {

private Class<? extends ViewType> viewType;
private ResourceBundle resourceBundle;
private Object root;
private ViewType codeBehind;

FxmlViewStep(Class<? extends ViewType> viewType){
this.viewType = viewType;
}

/**
* @param resourceBundle the resource bundle that is used while loading the view.
* @return this instance of the builder step.
*/
public FxmlViewStep<ViewType, ViewModelType> resourceBundle(ResourceBundle resourceBundle){
this.resourceBundle = resourceBundle;
return this;
}

/**
* This param is used to define a JavaFX node that is used as the root element
* when loading the fxml file.
* <br />
*
* This can be useful when creating custom controls with the fx:root element.
*
* @param root the root element that is used to load the fxml file.
* @return this instance of the builder step.
*/
public FxmlViewStep<ViewType, ViewModelType> root(Object root){
this.root = root;
return this;
}

/**
* This param is used to define an existing instance of the codeBehind class that
* is used instead of creating a new one while loading.
* <br />
*
* This can be useful when creating custom controls with the fx:root element.
*
* @param codeBehind the codeBehind instance that is used to load the fxml file.
* @return this instance of the builder step.
*/
public FxmlViewStep<ViewType, ViewModelType> codeBehind(ViewType codeBehind){
this.codeBehind = codeBehind;
return this;
}

/**
* The final step of the Fluent API. This method loads the view based on the given params.
*
* @return a view tuple containing the loaded view.
*/
public ViewTuple<ViewType, ViewModelType> load(){
ViewLoader viewLoader = new ViewLoader();

return viewLoader.loadViewTuple(viewType, resourceBundle, codeBehind, root);
}
}


/**
* This method is the entry point of the Fluent API to load a java based view.
*/
public static <ViewType extends JavaView<? extends ViewModelType>, ViewModelType extends ViewModel>
JavaViewStep<ViewType, ViewModelType> javaView(Class<? extends ViewType> viewType){
return new JavaViewStep<>(viewType);
}

/**
* This method is the entry point of the Fluent API to load a fxml based View.
*/
public static <ViewType extends FxmlView<? extends ViewModelType>, ViewModelType extends ViewModel>
FxmlViewStep<ViewType, ViewModelType> fxmlView(Class<? extends ViewType> viewType){
return new FxmlViewStep<>(viewType);
}

}
Loading

0 comments on commit 09509d6

Please sign in to comment.