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

Fix Bug #156. FxmlViews located in the default package can now be loaded... #157

Merged
merged 1 commit into from
Dec 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>