Skip to content

Commit

Permalink
Merge pull request #157 from sialcasa/#156_bugfix_default_package
Browse files Browse the repository at this point in the history
Fix Bug #156. FxmlViews located in the default package can now be loaded...
  • Loading branch information
manuel-mauky committed Dec 4, 2014
2 parents 68c7ca2 + 7c052f1 commit 11350f9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,38 @@ public class FxmlViewLoader {
public <ViewType extends View<? extends ViewModelType>, ViewModelType extends ViewModel> ViewTuple<ViewType, ViewModelType> loadFxmlViewTuple(
Class<? extends ViewType> viewType, ResourceBundle resourceBundle, Object controller, Object root,
ViewModelType viewModel) {
final String pathToFXML = "/" + viewType.getPackage().getName().replaceAll("\\.", "/") + "/"
+ viewType.getSimpleName() + ".fxml";

final String pathToFXML = createFxmlPath(viewType);
return loadFxmlViewTuple(pathToFXML, resourceBundle, controller, root, viewModel);
}

/**
* This method is used to create a String with the path to the FXML file for a given View class.
*
* This is done by taking the package of the view class (if any) and replace "." with "/".
* After that the Name of the class and the file ending ".fxml" is appended.
*
* Example: de.saxsys.myapp.ui.MainView as view class will be transformed to "/de/saxsys/myapp/ui/MainView.fxml"
*
* Example 2: MainView (located in the default package) will be transformed to "/MainView.fxml"
*
* @param viewType the view class type.
* @return the path to the fxml file as string.
*/
private String createFxmlPath(Class<?> viewType){
final StringBuilder pathBuilder = new StringBuilder();

pathBuilder.append("/");

if(viewType.getPackage() != null){
pathBuilder.append(viewType.getPackage().getName().replaceAll("\\.","/"));
pathBuilder.append("/");
}

pathBuilder.append(viewType.getSimpleName());
pathBuilder.append(".fxml");

return pathBuilder.toString();
}

/**
* Load the viewTuple by the path of the fxml file.
Expand Down
11 changes: 11 additions & 0 deletions mvvmfx/src/test/java/FxmlViewInDefaultPackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.internal.viewloader.example.TestViewModel;

/**
* The purpose of this class is to reproduce Bug no. 154 (https://github.com/sialcasa/mvvmFX/issues/156).
*
* The problem is that FxmlView's that are located in the default package can't be loaded properly. Instead a
* NullPointerException was thrown.
*/
public class FxmlViewInDefaultPackage implements FxmlView<TestViewModel> {
}
29 changes: 29 additions & 0 deletions mvvmfx/src/test/java/FxmlViewinDefaultPackageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import de.saxsys.javafx.test.JfxRunner;
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.ViewTuple;
import de.saxsys.mvvmfx.internal.viewloader.example.TestViewModel;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

/**
* This test reproduces the bug no 154 (https://github.com/sialcasa/mvvmFX/issues/156).
*
* A FxmlView located in the default package couldn't be loaded because a NullPointerException was thrown.
*/
@RunWith(JfxRunner.class)
public class FxmlViewinDefaultPackageTest {


@Test
public void test(){

ViewTuple<FxmlViewInDefaultPackage, TestViewModel> viewTuple = FluentViewLoader
.fxmlView(FxmlViewInDefaultPackage.class).load();

assertThat(viewTuple).isNotNull();
assertThat(viewTuple.getView()).isNotNull();
}

}
6 changes: 6 additions & 0 deletions mvvmfx/src/test/resources/FxmlViewInDefaultPackage.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="FxmlViewInDefaultPackage">

</VBox>

0 comments on commit 11350f9

Please sign in to comment.