diff --git a/src/main/java/viewfx/view/ViewLoader.java b/src/main/java/viewfx/view/ViewLoader.java index 3ca72b4359..c55bdc01b3 100644 --- a/src/main/java/viewfx/view/ViewLoader.java +++ b/src/main/java/viewfx/view/ViewLoader.java @@ -18,5 +18,5 @@ package viewfx.view; public interface ViewLoader { - View load(Object location); + View load(Class viewClass); } diff --git a/src/main/java/viewfx/view/support/CachingViewLoader.java b/src/main/java/viewfx/view/support/CachingViewLoader.java index 72450e316c..0020f4fb24 100644 --- a/src/main/java/viewfx/view/support/CachingViewLoader.java +++ b/src/main/java/viewfx/view/support/CachingViewLoader.java @@ -35,12 +35,12 @@ public class CachingViewLoader implements ViewLoader { } @Override - public View load(Object location) { - if (cache.containsKey(location)) - return cache.get(location); + public View load(Class viewClass) { + if (cache.containsKey(viewClass)) + return cache.get(viewClass); - View view = delegate.load(location); - cache.put(location, view); + View view = delegate.load(viewClass); + cache.put(viewClass, view); return view; } } diff --git a/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java b/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java index cad8c8bb68..1bd52f5fa6 100644 --- a/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java +++ b/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java @@ -33,10 +33,7 @@ import viewfx.view.ViewLoader; import javafx.fxml.FXMLLoader; -import java.lang.reflect.Constructor; -import org.springframework.cglib.core.ReflectUtils; import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.util.ReflectionUtils; import static com.google.common.base.Preconditions.checkNotNull; import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue; @@ -53,12 +50,7 @@ public class FxmlViewLoader implements ViewLoader { } @SuppressWarnings("unchecked") - public View load(Class clazz) { - if (!View.class.isAssignableFrom(clazz)) - throw new IllegalArgumentException("Class must be of generic type Class: " + clazz); - - Class viewClass = (Class) clazz; - + public View load(Class viewClass) { FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class); final Class convention; @@ -94,39 +86,30 @@ public class FxmlViewLoader implements ViewLoader { "Failed to load view class [%s] because FXML file at [%s] could not be loaded " + "as a classpath resource. Does it exist?", viewClass, specifiedLocation); - return load(fxmlUrl); + return loadFromFxml(fxmlUrl); } catch (InstantiationException | IllegalAccessException ex) { throw new ViewfxException(ex, "Failed to load view from class %s", viewClass); } } - public View load(URL url) { - checkNotNull(url, "FXML URL must not be null"); + public View loadFromFxml(URL fxmlUrl) { + checkNotNull(fxmlUrl, "FXML URL must not be null"); try { - FXMLLoader loader = new FXMLLoader(url, resourceBundle); + FXMLLoader loader = new FXMLLoader(fxmlUrl, resourceBundle); loader.setControllerFactory(viewFactory); loader.load(); Object controller = loader.getController(); if (controller == null) throw new ViewfxException("Failed to load view from FXML file at [%s]. " + - "Does it declare an fx:controller attribute?", url); + "Does it declare an fx:controller attribute?", fxmlUrl); if (!(controller instanceof View)) throw new ViewfxException("Controller of type [%s] loaded from FXML file at [%s] " + - "does not implement [%s] as expected.", controller.getClass(), url, View.class); + "does not implement [%s] as expected.", controller.getClass(), fxmlUrl, View.class); return (View) controller; } catch (IOException ex) { - throw new ViewfxException(ex, "Failed to load view from FXML file at [%s]", url); + throw new ViewfxException(ex, "Failed to load view from FXML file at [%s]", fxmlUrl); } } - public View load(Object location) { - if (location instanceof URL) - return load((URL) location); - if (location instanceof Class) - return load((Class) location); - - throw new IllegalArgumentException("Argument is not of type URL or Class: " + location); - - } } diff --git a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java b/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java deleted file mode 100644 index 0e36f241ed..0000000000 --- a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * This file is part of Bitsquare. - * - * Bitsquare is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bitsquare is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bitsquare. If not, see . - */ - -package io.bitsquare.app.gui; - -import io.bitsquare.app.BitsquareEnvironment; -import io.bitsquare.gui.main.funds.FundsView; -import io.bitsquare.locale.BSResources; - -import com.google.inject.Guice; -import com.google.inject.Injector; - -import java.io.File; - -import java.net.MalformedURLException; -import java.net.URL; - -import java.util.ResourceBundle; - -import viewfx.ViewfxException; -import viewfx.view.support.fxml.FxmlViewLoader; -import viewfx.view.support.guice.GuiceViewFactory; - -import javafx.application.Application; -import javafx.stage.Stage; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import joptsimple.OptionParser; - -public class ViewLoaderTests { - - public static class TestApp extends Application { - static Stage primaryStage; - - @Override - public void start(Stage primaryStage) throws Exception { - TestApp.primaryStage = primaryStage; - } - } - - @BeforeClass - public static void initJavaFX() throws InterruptedException { - Thread t = new Thread("JavaFX Init Thread") { - public void run() { - Application.launch(TestApp.class); - } - }; - t.setDaemon(true); - t.start(); - while (TestApp.primaryStage == null) - Thread.sleep(10); - } - - private GuiceViewFactory viewFactory; - private ResourceBundle resourceBundle; - - @Before - public void setUp() { - OptionParser parser = new OptionParser(); - BitsquareEnvironment env = new BitsquareEnvironment(parser.parse(new String[]{})); - Injector injector = Guice.createInjector(new BitsquareAppModule(env, TestApp.primaryStage)); - viewFactory = injector.getInstance(GuiceViewFactory.class); - viewFactory.setInjector(injector); - - resourceBundle = BSResources.getResourceBundle(); - } - - @Test(expected = ViewfxException.class) - public void loadingBogusFxmlResourceShouldThrow() throws MalformedURLException { - URL uri = new File("/tmp/bogus1234").toURI().toURL(); - new FxmlViewLoader(viewFactory, resourceBundle).load(uri); - } - - @Test - public void loadingFromValidFxmlViewClassShouldNotThrow() { - new FxmlViewLoader(viewFactory, resourceBundle).load(FundsView.class); - } - - @Test(expected = IllegalArgumentException.class) - public void loadingFromNonViewClassShouldThrow() { - new FxmlViewLoader(viewFactory, resourceBundle).load(File.class); - } -} \ No newline at end of file diff --git a/src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java b/src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java index bbd482e221..620d7ba842 100644 --- a/src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java +++ b/src/test/java/viewfx/view/support/fxml/FxmlViewLoaderTests.java @@ -36,7 +36,6 @@ import org.junit.rules.ExpectedException; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.*; import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.contains; import static org.mockito.Mockito.mock; public class FxmlViewLoaderTests { @@ -90,17 +89,6 @@ public class FxmlViewLoaderTests { } - static class NonView { - } - - @Test - public void nonViewClassShouldThrow() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Class must be of generic type"); - viewLoader.load(NonView.class); - } - - @FxmlView static class Malformed extends AbstractView { }