Remove static access to BitsquareUI#getPrimaryStage

- Bind BitsquareUI#start's primaryStage for DI
 - Statically inject primaryStage directly into Help and Popups
This commit is contained in:
Chris Beams 2014-11-04 18:55:17 +01:00
parent f6368754b9
commit 6f27c5ce29
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
9 changed files with 54 additions and 43 deletions

View file

@ -48,15 +48,8 @@ import lighthouse.files.AppDirectory;
public class BitsquareUI extends Application { public class BitsquareUI extends Application {
private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class); private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class);
private static Stage primaryStage; private BitsquareModule bitsquareModule;
private Injector injector;
private final BitsquareModule bitsquareModule;
private final Injector injector;
public BitsquareUI() {
this.bitsquareModule = new BitsquareModule();
this.injector = Guice.createInjector(bitsquareModule);
}
public static void main(String[] args) { public static void main(String[] args) {
Application.launch(BitsquareUI.class, args); Application.launch(BitsquareUI.class, args);
@ -64,7 +57,8 @@ public class BitsquareUI extends Application {
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
BitsquareUI.primaryStage = primaryStage; bitsquareModule = new BitsquareModule(primaryStage);
injector = Guice.createInjector(bitsquareModule);
// route uncaught exceptions to a user-facing dialog // route uncaught exceptions to a user-facing dialog
@ -137,8 +131,4 @@ public class BitsquareUI extends Application {
bitsquareModule.close(injector); bitsquareModule.close(injector);
System.exit(0); System.exit(0);
} }
public static Stage getPrimaryStage() {
return primaryStage;
}
} }

View file

@ -34,6 +34,8 @@ import com.google.inject.name.Names;
import java.util.Properties; import java.util.Properties;
import javafx.stage.Stage;
import net.tomp2p.connection.Ports; import net.tomp2p.connection.Ports;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -45,13 +47,15 @@ import scala.concurrent.duration.Duration;
public class BitsquareModule extends AbstractBitsquareModule { public class BitsquareModule extends AbstractBitsquareModule {
private static final Logger log = LoggerFactory.getLogger(BitsquareModule.class); private static final Logger log = LoggerFactory.getLogger(BitsquareModule.class);
private final Stage primaryStage;
public BitsquareModule() { public BitsquareModule(Stage primaryStage) {
this(ConfigLoader.loadConfig()); this(primaryStage, ConfigLoader.loadConfig());
} }
public BitsquareModule(Properties properties) { public BitsquareModule(Stage primaryStage, Properties properties) {
super(properties); super(properties);
this.primaryStage = primaryStage;
} }
@Override @Override
@ -89,7 +93,7 @@ public class BitsquareModule extends AbstractBitsquareModule {
} }
protected GuiModule guiModule() { protected GuiModule guiModule() {
return new GuiModule(properties); return new GuiModule(properties, primaryStage);
} }
@Override @Override

View file

@ -18,6 +18,8 @@
package io.bitsquare.gui; package io.bitsquare.gui;
import io.bitsquare.di.AbstractBitsquareModule; import io.bitsquare.di.AbstractBitsquareModule;
import io.bitsquare.gui.components.Popups;
import io.bitsquare.gui.main.help.Help;
import io.bitsquare.gui.main.trade.offerbook.OfferBook; import io.bitsquare.gui.main.trade.offerbook.OfferBook;
import io.bitsquare.gui.util.BSFormatter; import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.gui.util.validation.BankAccountNumberValidator; import io.bitsquare.gui.util.validation.BankAccountNumberValidator;
@ -28,10 +30,15 @@ import io.bitsquare.gui.util.validation.PasswordValidator;
import java.util.Properties; import java.util.Properties;
import javafx.stage.Stage;
public class GuiModule extends AbstractBitsquareModule { public class GuiModule extends AbstractBitsquareModule {
public GuiModule(Properties properties) { private final Stage primaryStage;
public GuiModule(Properties properties, Stage primaryStage) {
super(properties); super(properties);
this.primaryStage = primaryStage;
} }
@Override @Override
@ -46,5 +53,9 @@ public class GuiModule extends AbstractBitsquareModule {
bind(FiatValidator.class).asEagerSingleton(); bind(FiatValidator.class).asEagerSingleton();
bind(InputValidator.class).asEagerSingleton(); bind(InputValidator.class).asEagerSingleton();
bind(PasswordValidator.class).asEagerSingleton(); bind(PasswordValidator.class).asEagerSingleton();
bind(Stage.class).toInstance(primaryStage);
Popups.primaryStage = primaryStage;
Help.primaryStage = primaryStage;
} }
} }

View file

@ -59,6 +59,7 @@ public class AddressTextField extends AnchorPane {
private final StringProperty address = new SimpleStringProperty(); private final StringProperty address = new SimpleStringProperty();
private final StringProperty paymentLabel = new SimpleStringProperty(); private final StringProperty paymentLabel = new SimpleStringProperty();
private final ObjectProperty<Coin> amountAsCoin = new SimpleObjectProperty<>(); private final ObjectProperty<Coin> amountAsCoin = new SimpleObjectProperty<>();
private OverlayManager overlayManager; private OverlayManager overlayManager;

View file

@ -17,7 +17,6 @@
package io.bitsquare.gui.components; package io.bitsquare.gui.components;
import io.bitsquare.BitsquareUI;
import io.bitsquare.gui.OverlayManager; import io.bitsquare.gui.OverlayManager;
import io.bitsquare.locale.BSResources; import io.bitsquare.locale.BSResources;
@ -30,6 +29,7 @@ import java.util.List;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.stage.Stage;
import org.controlsfx.control.action.AbstractAction; import org.controlsfx.control.action.AbstractAction;
import org.controlsfx.control.action.Action; import org.controlsfx.control.action.Action;
@ -42,6 +42,8 @@ import org.slf4j.LoggerFactory;
public class Popups { public class Popups {
private static final Logger log = LoggerFactory.getLogger(Popups.class); private static final Logger log = LoggerFactory.getLogger(Popups.class);
public static Stage primaryStage;
// TODO just temporary, class will be removed completely // TODO just temporary, class will be removed completely
public static void setOverlayManager(OverlayManager overlayManager) { public static void setOverlayManager(OverlayManager overlayManager) {
Popups.overlayManager = overlayManager; Popups.overlayManager = overlayManager;
@ -70,7 +72,7 @@ public class Popups {
public static void openInfoPopup(String masthead, String message, List<Action> actions) { public static void openInfoPopup(String masthead, String message, List<Action> actions) {
Dialogs.create() Dialogs.create()
.owner(BitsquareUI.getPrimaryStage()) .owner(primaryStage)
.message(message) .message(message)
.masthead(masthead) .masthead(masthead)
.actions(actions) .actions(actions)
@ -107,7 +109,7 @@ public class Popups {
public static Action openConfirmPopup(String title, String masthead, String message, List<Action> actions) { public static Action openConfirmPopup(String title, String masthead, String message, List<Action> actions) {
return Dialogs.create() return Dialogs.create()
.owner(BitsquareUI.getPrimaryStage()) .owner(primaryStage)
.title(title) .title(title)
.message(message) .message(message)
.masthead(masthead) .masthead(masthead)
@ -140,7 +142,7 @@ public class Popups {
private static void openWarningPopup(String title, String masthead, String message, List<Action> actions) { private static void openWarningPopup(String title, String masthead, String message, List<Action> actions) {
Dialogs.create() Dialogs.create()
.owner(BitsquareUI.getPrimaryStage()) .owner(primaryStage)
.title(title) .title(title)
.message(message) .message(message)
.masthead(masthead) .masthead(masthead)
@ -173,7 +175,7 @@ public class Popups {
private static Action openErrorPopup(String title, String masthead, String message, List<Action> actions) { private static Action openErrorPopup(String title, String masthead, String message, List<Action> actions) {
return Dialogs.create() return Dialogs.create()
.owner(BitsquareUI.getPrimaryStage()) .owner(primaryStage)
.title(title) .title(title)
.message(message) .message(message)
.masthead(masthead) .masthead(masthead)
@ -202,7 +204,7 @@ public class Popups {
} }
}); });
return Dialogs.create() return Dialogs.create()
.owner(BitsquareUI.getPrimaryStage()) .owner(primaryStage)
.title(title) .title(title)
.message(message) .message(message)
.masthead(masthead) .masthead(masthead)

View file

@ -17,7 +17,6 @@
package io.bitsquare.gui.main.account.arbitrator; package io.bitsquare.gui.main.account.arbitrator;
import io.bitsquare.BitsquareUI;
import io.bitsquare.gui.CachedViewCB; import io.bitsquare.gui.CachedViewCB;
import io.bitsquare.gui.Navigation; import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.main.account.arbitrator.registration.ArbitratorRegistrationViewCB; import io.bitsquare.gui.main.account.arbitrator.registration.ArbitratorRegistrationViewCB;
@ -43,7 +42,8 @@ public class ArbitratorSettingsViewCB extends CachedViewCB {
private static final Logger log = LoggerFactory.getLogger(ArbitratorSettingsViewCB.class); private static final Logger log = LoggerFactory.getLogger(ArbitratorSettingsViewCB.class);
private Navigation navigation; private final Navigation navigation;
private final Stage primaryStage;
private ArbitratorRegistrationViewCB arbitratorRegistrationViewCB; private ArbitratorRegistrationViewCB arbitratorRegistrationViewCB;
@ -53,9 +53,10 @@ public class ArbitratorSettingsViewCB extends CachedViewCB {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
private ArbitratorSettingsViewCB(Navigation navigation) { private ArbitratorSettingsViewCB(Navigation navigation, Stage primaryStage) {
super(); super();
this.navigation = navigation; this.navigation = navigation;
this.primaryStage = primaryStage;
} }
@ -100,17 +101,16 @@ public class ArbitratorSettingsViewCB extends CachedViewCB {
final Parent view = loader.load(); final Parent view = loader.load();
arbitratorRegistrationViewCB = loader.getController(); arbitratorRegistrationViewCB = loader.getController();
final Stage rootStage = BitsquareUI.getPrimaryStage();
final Stage stage = new Stage(); final Stage stage = new Stage();
stage.setTitle("Arbitrator"); stage.setTitle("Arbitrator");
stage.setMinWidth(800); stage.setMinWidth(800);
stage.setMinHeight(400); stage.setMinHeight(400);
stage.setWidth(800); stage.setWidth(800);
stage.setHeight(600); stage.setHeight(600);
stage.setX(rootStage.getX() + 50); stage.setX(primaryStage.getX() + 50);
stage.setY(rootStage.getY() + 50); stage.setY(primaryStage.getY() + 50);
stage.initModality(Modality.WINDOW_MODAL); stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(rootStage); stage.initOwner(primaryStage);
Scene scene = new Scene(view, 800, 600); Scene scene = new Scene(view, 800, 600);
stage.setScene(scene); stage.setScene(scene);
stage.show(); stage.show();

View file

@ -17,7 +17,6 @@
package io.bitsquare.gui.main.account.content.restrictions; package io.bitsquare.gui.main.account.content.restrictions;
import io.bitsquare.BitsquareUI;
import io.bitsquare.arbitrator.Arbitrator; import io.bitsquare.arbitrator.Arbitrator;
import io.bitsquare.gui.CachedViewCB; import io.bitsquare.gui.CachedViewCB;
import io.bitsquare.gui.Navigation; import io.bitsquare.gui.Navigation;
@ -54,6 +53,7 @@ import org.slf4j.LoggerFactory;
public class RestrictionsViewCB extends CachedViewCB<RestrictionsPM> implements ContextAware { public class RestrictionsViewCB extends CachedViewCB<RestrictionsPM> implements ContextAware {
private static final Logger log = LoggerFactory.getLogger(RestrictionsViewCB.class); private static final Logger log = LoggerFactory.getLogger(RestrictionsViewCB.class);
private final Stage primaryStage;
@FXML ListView<Locale> languagesListView; @FXML ListView<Locale> languagesListView;
@FXML ListView<Country> countriesListView; @FXML ListView<Country> countriesListView;
@ -69,8 +69,9 @@ public class RestrictionsViewCB extends CachedViewCB<RestrictionsPM> implements
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
private RestrictionsViewCB(RestrictionsPM presentationModel) { private RestrictionsViewCB(RestrictionsPM presentationModel, Stage primaryStage) {
super(presentationModel); super(presentationModel);
this.primaryStage = primaryStage;
} }
@ -193,17 +194,16 @@ public class RestrictionsViewCB extends CachedViewCB<RestrictionsPM> implements
Initializable childController = loader.getController(); Initializable childController = loader.getController();
//childController.setParentController(this); //childController.setParentController(this);
final Stage rootStage = BitsquareUI.getPrimaryStage();
final Stage stage = new Stage(); final Stage stage = new Stage();
stage.setTitle("Arbitrator selection"); stage.setTitle("Arbitrator selection");
stage.setMinWidth(800); stage.setMinWidth(800);
stage.setMinHeight(500); stage.setMinHeight(500);
stage.setWidth(800); stage.setWidth(800);
stage.setHeight(600); stage.setHeight(600);
stage.setX(rootStage.getX() + 50); stage.setX(primaryStage.getX() + 50);
stage.setY(rootStage.getY() + 50); stage.setY(primaryStage.getY() + 50);
stage.initModality(Modality.WINDOW_MODAL); stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(rootStage); stage.initOwner(primaryStage);
Scene scene = new Scene((Parent) view, 800, 600); Scene scene = new Scene((Parent) view, 800, 600);
stage.setScene(scene); stage.setScene(scene);
stage.setOnHidden(windowEvent -> { stage.setOnHidden(windowEvent -> {

View file

@ -17,8 +17,6 @@
package io.bitsquare.gui.main.help; package io.bitsquare.gui.main.help;
import io.bitsquare.BitsquareUI;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -35,6 +33,7 @@ public class Help {
private static final Logger log = LoggerFactory.getLogger(Help.class); private static final Logger log = LoggerFactory.getLogger(Help.class);
private static Stage helpWindow; private static Stage helpWindow;
public static Stage primaryStage;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -49,7 +48,7 @@ public class Help {
if (helpWindow == null) { if (helpWindow == null) {
helpWindow = new Stage(); helpWindow = new Stage();
helpWindow.initModality(Modality.NONE); helpWindow.initModality(Modality.NONE);
helpWindow.initOwner(BitsquareUI.getPrimaryStage()); helpWindow.initOwner(primaryStage);
webView = new WebView(); webView = new WebView();
helpWindow.setScene(new Scene(webView, 800, 600)); helpWindow.setScene(new Scene(webView, 800, 600));
} }

View file

@ -35,13 +35,15 @@ import org.junit.Test;
public class ViewLoaderTest { public class ViewLoaderTest {
public static class TestApp extends Application { public static class TestApp extends Application {
static Stage primaryStage;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
TestApp.primaryStage = primaryStage;
} }
} }
@BeforeClass @BeforeClass
public static void initJavaFX() { public static void initJavaFX() throws InterruptedException {
Thread t = new Thread("JavaFX Init Thread") { Thread t = new Thread("JavaFX Init Thread") {
public void run() { public void run() {
Application.launch(TestApp.class); Application.launch(TestApp.class);
@ -49,12 +51,14 @@ public class ViewLoaderTest {
}; };
t.setDaemon(true); t.setDaemon(true);
t.start(); t.start();
while (TestApp.primaryStage == null)
Thread.sleep(10);
} }
@Before @Before
public void setUp() { public void setUp() {
Injector injector = Guice.createInjector(new BitsquareModule()); Injector injector = Guice.createInjector(new BitsquareModule(TestApp.primaryStage));
ViewLoader.setInjector(injector); ViewLoader.setInjector(injector);
} }