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 {
private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class);
private static Stage primaryStage;
private final BitsquareModule bitsquareModule;
private final Injector injector;
public BitsquareUI() {
this.bitsquareModule = new BitsquareModule();
this.injector = Guice.createInjector(bitsquareModule);
}
private BitsquareModule bitsquareModule;
private Injector injector;
public static void main(String[] args) {
Application.launch(BitsquareUI.class, args);
@ -64,7 +57,8 @@ public class BitsquareUI extends Application {
@Override
public void start(Stage primaryStage) {
BitsquareUI.primaryStage = primaryStage;
bitsquareModule = new BitsquareModule(primaryStage);
injector = Guice.createInjector(bitsquareModule);
// route uncaught exceptions to a user-facing dialog
@ -137,8 +131,4 @@ public class BitsquareUI extends Application {
bitsquareModule.close(injector);
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 javafx.stage.Stage;
import net.tomp2p.connection.Ports;
import org.slf4j.Logger;
@ -45,13 +47,15 @@ import scala.concurrent.duration.Duration;
public class BitsquareModule extends AbstractBitsquareModule {
private static final Logger log = LoggerFactory.getLogger(BitsquareModule.class);
private final Stage primaryStage;
public BitsquareModule() {
this(ConfigLoader.loadConfig());
public BitsquareModule(Stage primaryStage) {
this(primaryStage, ConfigLoader.loadConfig());
}
public BitsquareModule(Properties properties) {
public BitsquareModule(Stage primaryStage, Properties properties) {
super(properties);
this.primaryStage = primaryStage;
}
@Override
@ -89,7 +93,7 @@ public class BitsquareModule extends AbstractBitsquareModule {
}
protected GuiModule guiModule() {
return new GuiModule(properties);
return new GuiModule(properties, primaryStage);
}
@Override

View File

@ -18,6 +18,8 @@
package io.bitsquare.gui;
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.util.BSFormatter;
import io.bitsquare.gui.util.validation.BankAccountNumberValidator;
@ -28,10 +30,15 @@ import io.bitsquare.gui.util.validation.PasswordValidator;
import java.util.Properties;
import javafx.stage.Stage;
public class GuiModule extends AbstractBitsquareModule {
public GuiModule(Properties properties) {
private final Stage primaryStage;
public GuiModule(Properties properties, Stage primaryStage) {
super(properties);
this.primaryStage = primaryStage;
}
@Override
@ -46,5 +53,9 @@ public class GuiModule extends AbstractBitsquareModule {
bind(FiatValidator.class).asEagerSingleton();
bind(InputValidator.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 paymentLabel = new SimpleStringProperty();
private final ObjectProperty<Coin> amountAsCoin = new SimpleObjectProperty<>();
private OverlayManager overlayManager;

View File

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

View File

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

View File

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

View File

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

View File

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