mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-26 08:25:23 -04:00
Change signature of ViewLoader#load to accept Class
This commit is contained in:
parent
4ebc0c0e41
commit
9041ff3d82
5 changed files with 14 additions and 143 deletions
|
@ -18,5 +18,5 @@
|
||||||
package viewfx.view;
|
package viewfx.view;
|
||||||
|
|
||||||
public interface ViewLoader {
|
public interface ViewLoader {
|
||||||
View load(Object location);
|
View load(Class<? extends View> viewClass);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,12 +35,12 @@ public class CachingViewLoader implements ViewLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View load(Object location) {
|
public View load(Class<? extends View> viewClass) {
|
||||||
if (cache.containsKey(location))
|
if (cache.containsKey(viewClass))
|
||||||
return cache.get(location);
|
return cache.get(viewClass);
|
||||||
|
|
||||||
View view = delegate.load(location);
|
View view = delegate.load(viewClass);
|
||||||
cache.put(location, view);
|
cache.put(viewClass, view);
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,10 +33,7 @@ import viewfx.view.ViewLoader;
|
||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import org.springframework.cglib.core.ReflectUtils;
|
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue;
|
import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue;
|
||||||
|
@ -53,12 +50,7 @@ public class FxmlViewLoader implements ViewLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public View load(Class<?> clazz) {
|
public View load(Class<? extends View> viewClass) {
|
||||||
if (!View.class.isAssignableFrom(clazz))
|
|
||||||
throw new IllegalArgumentException("Class must be of generic type Class<? extends View>: " + clazz);
|
|
||||||
|
|
||||||
Class<? extends View> viewClass = (Class<? extends View>) clazz;
|
|
||||||
|
|
||||||
FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class);
|
FxmlView fxmlView = AnnotationUtils.getAnnotation(viewClass, FxmlView.class);
|
||||||
|
|
||||||
final Class<? extends FxmlView.PathConvention> convention;
|
final Class<? extends FxmlView.PathConvention> 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 " +
|
"Failed to load view class [%s] because FXML file at [%s] could not be loaded " +
|
||||||
"as a classpath resource. Does it exist?", viewClass, specifiedLocation);
|
"as a classpath resource. Does it exist?", viewClass, specifiedLocation);
|
||||||
|
|
||||||
return load(fxmlUrl);
|
return loadFromFxml(fxmlUrl);
|
||||||
} catch (InstantiationException | IllegalAccessException ex) {
|
} catch (InstantiationException | IllegalAccessException ex) {
|
||||||
throw new ViewfxException(ex, "Failed to load view from class %s", viewClass);
|
throw new ViewfxException(ex, "Failed to load view from class %s", viewClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public View load(URL url) {
|
public View loadFromFxml(URL fxmlUrl) {
|
||||||
checkNotNull(url, "FXML URL must not be null");
|
checkNotNull(fxmlUrl, "FXML URL must not be null");
|
||||||
try {
|
try {
|
||||||
FXMLLoader loader = new FXMLLoader(url, resourceBundle);
|
FXMLLoader loader = new FXMLLoader(fxmlUrl, resourceBundle);
|
||||||
loader.setControllerFactory(viewFactory);
|
loader.setControllerFactory(viewFactory);
|
||||||
loader.load();
|
loader.load();
|
||||||
Object controller = loader.getController();
|
Object controller = loader.getController();
|
||||||
if (controller == null)
|
if (controller == null)
|
||||||
throw new ViewfxException("Failed to load view from FXML file at [%s]. " +
|
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))
|
if (!(controller instanceof View))
|
||||||
throw new ViewfxException("Controller of type [%s] loaded from FXML file at [%s] " +
|
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;
|
return (View) controller;
|
||||||
} catch (IOException ex) {
|
} 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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -36,7 +36,6 @@ import org.junit.rules.ExpectedException;
|
||||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.Matchers.contains;
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
public class FxmlViewLoaderTests {
|
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
|
@FxmlView
|
||||||
static class Malformed extends AbstractView {
|
static class Malformed extends AbstractView {
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue