Skip to content

Commit

Permalink
Merge pull request #351 from sialcasa/350_javaview_codebehind
Browse files Browse the repository at this point in the history
#350 JavaViews can now be loaded with existing codeBehind
  • Loading branch information
manuel-mauky committed Jan 16, 2016
2 parents 5428dd5 + cebe9a6 commit d1f303f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
29 changes: 23 additions & 6 deletions mvvmfx/src/main/java/de/saxsys/mvvmfx/FluentViewLoader.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.saxsys.mvvmfx;

import java.util.ResourceBundle;

import de.saxsys.mvvmfx.internal.viewloader.FxmlViewLoader;
import de.saxsys.mvvmfx.internal.viewloader.JavaViewLoader;
import de.saxsys.mvvmfx.internal.viewloader.ResourceBundleManager;

import java.util.ResourceBundle;

/**
* Fluent API for loading Views. <br>
*
Expand Down Expand Up @@ -57,6 +57,7 @@ public static class JavaViewStep<ViewType extends JavaView<? extends ViewModelTy
private ResourceBundle resourceBundle;

private ViewModelType viewModel;
private ViewType codeBehind;

JavaViewStep(Class<? extends ViewType> viewType) {
this.viewType = viewType;
Expand Down Expand Up @@ -92,7 +93,21 @@ public JavaViewStep<ViewType, ViewModelType> viewModel(ViewModelType viewModel)
this.viewModel = viewModel;
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.
*
* @param codeBehind
* the codeBehind instance that is used to load this java view.
* @return this instance of the builder step.
*/
public JavaViewStep<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.
Expand All @@ -102,9 +117,11 @@ public JavaViewStep<ViewType, ViewModelType> viewModel(ViewModelType viewModel)
public ViewTuple<ViewType, ViewModelType> load() {
JavaViewLoader javaViewLoader = new JavaViewLoader();

return javaViewLoader.loadJavaViewTuple(viewType, ResourceBundleManager.getInstance().mergeWithGlobal(resourceBundle), viewModel);
return javaViewLoader.loadJavaViewTuple(viewType, ResourceBundleManager.getInstance().mergeWithGlobal(resourceBundle), viewModel, codeBehind);
}
}


}

/**
* This class is the builder step to load a fxml based view. It is accessed from the {@link FluentViewLoader} with
Expand All @@ -123,7 +140,7 @@ public static class FxmlViewStep<ViewType extends FxmlView<? extends ViewModelTy
private Object root;
private ViewType codeBehind;
private ViewModelType viewModel;

FxmlViewStep(Class<? extends ViewType> viewType) {
this.viewType = viewType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@
import java.util.List;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;
import javafx.scene.Parent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.saxsys.mvvmfx.ViewModel;
import de.saxsys.mvvmfx.ViewTuple;

/**
* This viewLoader is used to load views that are implementing {@link de.saxsys.mvvmfx.JavaView}.
*
Expand Down Expand Up @@ -78,10 +69,10 @@ public class JavaViewLoader {
*/
public <ViewType extends View<? extends ViewModelType>, ViewModelType extends ViewModel> ViewTuple<ViewType, ViewModelType> loadJavaViewTuple(
Class<? extends ViewType>
viewType, ResourceBundle resourceBundle, final ViewModelType existingViewModel) {
viewType, ResourceBundle resourceBundle, final ViewModelType existingViewModel, ViewType codeBehind) {
DependencyInjector injectionFacade = DependencyInjector.getInstance();
final ViewType view = injectionFacade.getInstanceOf(viewType);

final ViewType view = codeBehind == null ? injectionFacade.getInstanceOf(viewType) : codeBehind;

if (!(view instanceof Parent)) {
throw new IllegalArgumentException("Can not load java view! The view class has to extend from "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.URL;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;

import static de.saxsys.mvvmfx.internal.viewloader.ResourceBundleAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -543,5 +544,26 @@ public void initialize(URL location, ResourceBundle resources) {

assertThat(loadedView.resources).isNull();
}

}


@Test
public void testExistingCodeBehindIsUsed() {
AtomicInteger counter = new AtomicInteger(0);
class TestView extends VBox implements JavaView<TestViewModel> {

TestView() {
counter.incrementAndGet();
}
}

TestView view = new TestView();

final ViewTuple<TestView, TestViewModel> viewTuple = FluentViewLoader.javaView(TestView.class).codeBehind(view).load();

assertThat(viewTuple.getView()).isEqualTo(view);
assertThat(viewTuple.getCodeBehind()).isEqualTo(view);

assertThat(counter.get()).isEqualTo(1);
}

}

0 comments on commit d1f303f

Please sign in to comment.