From 444948f3ca9c50451b3c807f965bc4edb34d2a11 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 24 Nov 2014 08:57:22 +0100 Subject: [PATCH] Introduce viewfx.view.support.CachingViewLoader ... and move view.fxml package to view.support.fxml --- .../io/bitsquare/app/gui/BitsquareApp.java | 7 +-- src/main/java/io/bitsquare/gui/GuiModule.java | 4 +- .../arbitrator/ArbitratorSettingsView.java | 9 ++-- .../restrictions/RestrictionsView.java | 9 ++-- .../io/bitsquare/gui/main/trade/BuyView.java | 4 +- .../io/bitsquare/gui/main/trade/SellView.java | 4 +- .../bitsquare/gui/main/trade/TradeView.java | 13 +++--- src/main/java/viewfx/view/ViewLoader.java | 4 +- .../view/support/CachingViewLoader.java | 46 +++++++++++++++++++ .../{ => support}/fxml/FxmlViewLoader.java | 22 +++------ .../io/bitsquare/app/gui/ViewLoaderTests.java | 6 +-- 11 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 src/main/java/viewfx/view/support/CachingViewLoader.java rename src/main/java/viewfx/view/{ => support}/fxml/FxmlViewLoader.java (74%) diff --git a/src/main/java/io/bitsquare/app/gui/BitsquareApp.java b/src/main/java/io/bitsquare/app/gui/BitsquareApp.java index 8335f05bc0..7937babade 100644 --- a/src/main/java/io/bitsquare/app/gui/BitsquareApp.java +++ b/src/main/java/io/bitsquare/app/gui/BitsquareApp.java @@ -39,7 +39,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import viewfx.view.View; -import viewfx.view.fxml.FxmlViewLoader; +import viewfx.view.ViewLoader; +import viewfx.view.support.CachingViewLoader; import viewfx.view.support.guice.GuiceViewFactory; import javafx.application.Application; @@ -95,8 +96,8 @@ public class BitsquareApp extends Application { // load the main view and create the main scene - FxmlViewLoader viewLoader = injector.getInstance(FxmlViewLoader.class); - View view = viewLoader.load(Navigation.Item.MAIN.getFxmlUrl(), false); + ViewLoader viewLoader = injector.getInstance(CachingViewLoader.class); + View view = viewLoader.load(Navigation.Item.MAIN.getFxmlUrl()); Scene scene = new Scene((Parent) view.getRoot(), 1000, 600); scene.getStylesheets().setAll( diff --git a/src/main/java/io/bitsquare/gui/GuiModule.java b/src/main/java/io/bitsquare/gui/GuiModule.java index 3cc9d9d4b8..033d81a3bd 100644 --- a/src/main/java/io/bitsquare/gui/GuiModule.java +++ b/src/main/java/io/bitsquare/gui/GuiModule.java @@ -37,7 +37,8 @@ import java.util.ResourceBundle; import viewfx.view.ViewFactory; import viewfx.view.ViewLoader; -import viewfx.view.fxml.FxmlViewLoader; +import viewfx.view.support.CachingViewLoader; +import viewfx.view.support.fxml.FxmlViewLoader; import viewfx.view.support.guice.GuiceViewFactory; import javafx.stage.Stage; @@ -60,6 +61,7 @@ public class GuiModule extends BitsquareModule { bind(ResourceBundle.class).toInstance(BSResources.getResourceBundle()); bind(ViewLoader.class).to(FxmlViewLoader.class).asEagerSingleton(); + bind(CachingViewLoader.class).asEagerSingleton(); bind(OfferBook.class).asEagerSingleton(); bind(Navigation.class).asEagerSingleton(); diff --git a/src/main/java/io/bitsquare/gui/main/account/arbitrator/ArbitratorSettingsView.java b/src/main/java/io/bitsquare/gui/main/account/arbitrator/ArbitratorSettingsView.java index c4302d252b..d994f54781 100644 --- a/src/main/java/io/bitsquare/gui/main/account/arbitrator/ArbitratorSettingsView.java +++ b/src/main/java/io/bitsquare/gui/main/account/arbitrator/ArbitratorSettingsView.java @@ -23,7 +23,8 @@ import io.bitsquare.gui.main.account.arbitrator.registration.ArbitratorRegistrat import javax.inject.Inject; import viewfx.view.View; -import viewfx.view.fxml.FxmlViewLoader; +import viewfx.view.ViewLoader; +import viewfx.view.support.CachingViewLoader; import viewfx.view.support.AbstractView; import javafx.fxml.FXML; @@ -36,19 +37,19 @@ class ArbitratorSettingsView extends AbstractView { private ArbitratorRegistrationView arbitratorRegistrationView; - private final FxmlViewLoader viewLoader; + private final ViewLoader viewLoader; private final Navigation navigation; private final Stage primaryStage; @Inject - private ArbitratorSettingsView(FxmlViewLoader viewLoader, Navigation navigation, Stage primaryStage) { + private ArbitratorSettingsView(CachingViewLoader viewLoader, Navigation navigation, Stage primaryStage) { this.viewLoader = viewLoader; this.navigation = navigation; this.primaryStage = primaryStage; } private void loadView(Navigation.Item navigationItem) { - View view = viewLoader.load(navigationItem.getFxmlUrl(), false); + View view = viewLoader.load(navigationItem.getFxmlUrl()); arbitratorRegistrationView = (ArbitratorRegistrationView) view; final Stage stage = new Stage(); diff --git a/src/main/java/io/bitsquare/gui/main/account/content/restrictions/RestrictionsView.java b/src/main/java/io/bitsquare/gui/main/account/content/restrictions/RestrictionsView.java index 0640727bb9..5ea9ee350b 100644 --- a/src/main/java/io/bitsquare/gui/main/account/content/restrictions/RestrictionsView.java +++ b/src/main/java/io/bitsquare/gui/main/account/content/restrictions/RestrictionsView.java @@ -30,8 +30,9 @@ import java.util.Locale; import javax.inject.Inject; import viewfx.view.View; -import viewfx.view.fxml.FxmlViewLoader; +import viewfx.view.ViewLoader; import viewfx.view.Wizard; +import viewfx.view.support.CachingViewLoader; import viewfx.view.support.ActivatableViewAndModel; import javafx.fxml.FXML; @@ -56,11 +57,11 @@ public class RestrictionsView extends ActivatableViewAndModel { private Fiat price; private Offer offer; - private final FxmlViewLoader viewLoader; + private final ViewLoader viewLoader; private final Navigation navigation; - protected TradeView(FxmlViewLoader viewLoader, Navigation navigation) { + protected TradeView(CachingViewLoader viewLoader, Navigation navigation) { this.viewLoader = viewLoader; this.navigation = navigation; } @@ -122,7 +123,7 @@ public class TradeView extends ActivatableView { TabPane tabPane = root; if (navigationItem == Navigation.Item.OFFER_BOOK && offerBookView == null) { // Offerbook must not be cached by ViewLoader as we use 2 instances for sell and buy screens. - View view = viewLoader.load(navigationItem.getFxmlUrl(), false); + View view = viewLoader.load(navigationItem.getFxmlUrl()); final Tab tab = new Tab(direction == Direction.BUY ? "Buy Bitcoin" : "Sell Bitcoin"); tab.setClosable(false); tab.setContent(view.getRoot()); @@ -137,7 +138,7 @@ public class TradeView extends ActivatableView { else if (navigationItem == Navigation.Item.CREATE_OFFER && createOfferView == null) { // CreateOffer and TakeOffer must not be cached by ViewLoader as we cannot use a view multiple times // in different graphs - View view = viewLoader.load(navigationItem.getFxmlUrl(), false); + View view = viewLoader.load(navigationItem.getFxmlUrl()); createOfferView = (CreateOfferView) view; createOfferView.initWithData(direction, amount, price); createOfferRoot = view.getRoot(); @@ -152,7 +153,7 @@ public class TradeView extends ActivatableView { offer != null) { // CreateOffer and TakeOffer must not be cached by ViewLoader as we cannot use a view multiple times // in different graphs - View view = viewLoader.load(Navigation.Item.TAKE_OFFER.getFxmlUrl(), false); + View view = viewLoader.load(Navigation.Item.TAKE_OFFER.getFxmlUrl()); takeOfferView = (TakeOfferView) view; takeOfferView.initWithData(direction, amount, offer); takeOfferRoot = view.getRoot(); diff --git a/src/main/java/viewfx/view/ViewLoader.java b/src/main/java/viewfx/view/ViewLoader.java index d148af7a2e..3ca72b4359 100644 --- a/src/main/java/viewfx/view/ViewLoader.java +++ b/src/main/java/viewfx/view/ViewLoader.java @@ -17,6 +17,6 @@ package viewfx.view; -public interface ViewLoader { - View load(Location location); +public interface ViewLoader { + View load(Object location); } diff --git a/src/main/java/viewfx/view/support/CachingViewLoader.java b/src/main/java/viewfx/view/support/CachingViewLoader.java new file mode 100644 index 0000000000..72450e316c --- /dev/null +++ b/src/main/java/viewfx/view/support/CachingViewLoader.java @@ -0,0 +1,46 @@ +/* + * 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 viewfx.view.support; + +import java.util.HashMap; + +import javax.inject.Inject; + +import viewfx.view.View; +import viewfx.view.ViewLoader; + +public class CachingViewLoader implements ViewLoader { + + private final HashMap cache = new HashMap<>(); + private final ViewLoader delegate; + + @Inject + public CachingViewLoader(ViewLoader delegate) { + this.delegate = delegate; + } + + @Override + public View load(Object location) { + if (cache.containsKey(location)) + return cache.get(location); + + View view = delegate.load(location); + cache.put(location, view); + return view; + } +} diff --git a/src/main/java/viewfx/view/fxml/FxmlViewLoader.java b/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java similarity index 74% rename from src/main/java/viewfx/view/fxml/FxmlViewLoader.java rename to src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java index aab219ce6c..f5231532a3 100644 --- a/src/main/java/viewfx/view/fxml/FxmlViewLoader.java +++ b/src/main/java/viewfx/view/support/fxml/FxmlViewLoader.java @@ -15,14 +15,12 @@ * along with Bitsquare. If not, see . */ -package viewfx.view.fxml; +package viewfx.view.support.fxml; import java.io.IOException; import java.net.URL; -import java.util.HashMap; -import java.util.Map; import java.util.ResourceBundle; import javax.inject.Inject; @@ -33,9 +31,8 @@ import viewfx.view.ViewLoader; import javafx.fxml.FXMLLoader; -public class FxmlViewLoader implements ViewLoader { +public class FxmlViewLoader implements ViewLoader { - private final Map cache = new HashMap<>(); private final ViewFactory viewFactory; private final ResourceBundle resourceBundle; @@ -45,22 +42,17 @@ public class FxmlViewLoader implements ViewLoader { this.resourceBundle = resourceBundle; } - @Override - public View load(URL url) { - return load(url, true); - } + public View load(Object location) { + if (!(location instanceof URL)) + throw new IllegalArgumentException("FXML view locations must be of type URL"); - public View load(URL url, boolean useCaching) { - if (useCaching && cache.containsKey(url)) - return cache.get(url); + URL url = (URL) location; try { FXMLLoader loader = new FXMLLoader(url, resourceBundle); loader.setControllerFactory(viewFactory); loader.load(); - View view = loader.getController(); - cache.put(url, view); - return view; + return loader.getController(); } catch (IOException ex) { throw new RuntimeException("Failed to load View at location " + url, ex); } diff --git a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java b/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java index 94a3fa9084..bbeb525d0b 100644 --- a/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java +++ b/src/test/java/io/bitsquare/app/gui/ViewLoaderTests.java @@ -29,7 +29,7 @@ import java.net.MalformedURLException; import java.util.ResourceBundle; -import viewfx.view.fxml.FxmlViewLoader; +import viewfx.view.support.fxml.FxmlViewLoader; import viewfx.view.support.guice.GuiceViewFactory; import javafx.application.Application; @@ -81,11 +81,11 @@ public class ViewLoaderTests { @Test(expected = BitsquareException.class) public void loadingBogusFxmlResourceShouldThrow() throws MalformedURLException { - new FxmlViewLoader(viewFactory, resourceBundle).load(Navigation.Item.BOGUS.getFxmlUrl(), false); + new FxmlViewLoader(viewFactory, resourceBundle).load(Navigation.Item.BOGUS.getFxmlUrl()); } @Test public void loadingValidFxmlResourceShouldNotThrow() { - new FxmlViewLoader(viewFactory, resourceBundle).load(Navigation.Item.ACCOUNT.getFxmlUrl(), false); + new FxmlViewLoader(viewFactory, resourceBundle).load(Navigation.Item.ACCOUNT.getFxmlUrl()); } } \ No newline at end of file