From c8699dd66b2b50771e34f75e4648924e2a0949f9 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sat, 30 Aug 2014 00:54:59 +0200 Subject: [PATCH] fix bugs with bindings --- .../java/io/bitsquare/gui/CodeBehind.java | 9 +++-- .../bitsquare/gui/trade/TradeController.java | 31 +++++++++++++---- .../gui/trade/createoffer/CreateOfferCB.java | 12 +++---- .../gui/trade/createoffer/CreateOfferPM.java | 33 ++++++++++++++----- .../trade/orderbook/OrderBookController.java | 2 -- 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/bitsquare/gui/CodeBehind.java b/src/main/java/io/bitsquare/gui/CodeBehind.java index a689ad906c..c8563ff728 100644 --- a/src/main/java/io/bitsquare/gui/CodeBehind.java +++ b/src/main/java/io/bitsquare/gui/CodeBehind.java @@ -43,10 +43,13 @@ public class CodeBehind implements Initializable { root.sceneProperty().addListener((ov, oldValue, newValue) -> { // we got removed from the scene // lets terminate - if (oldValue != null && newValue == null) terminate(); + if (oldValue != null && newValue == null) + terminate(); + }); presentationModel.initialized(); + presentationModel.activate(); } /** @@ -55,8 +58,10 @@ public class CodeBehind implements Initializable { */ public void terminate() { log.trace("Lifecycle: terminate " + this.getClass().getSimpleName()); - if (childController != null) childController.terminate(); + if (childController != null) + childController.terminate(); + presentationModel.deactivate(); presentationModel.terminate(); } diff --git a/src/main/java/io/bitsquare/gui/trade/TradeController.java b/src/main/java/io/bitsquare/gui/trade/TradeController.java index e1eba34810..462f615c7e 100644 --- a/src/main/java/io/bitsquare/gui/trade/TradeController.java +++ b/src/main/java/io/bitsquare/gui/trade/TradeController.java @@ -30,9 +30,10 @@ import java.io.IOException; import java.net.URL; +import java.util.List; import java.util.ResourceBundle; -import javafx.application.Platform; +import javafx.collections.ListChangeListener; import javafx.fxml.Initializable; import javafx.scene.*; import javafx.scene.control.*; @@ -49,6 +50,7 @@ public class TradeController extends CachedViewController { protected CreateOfferCB createOfferCodeBehind; protected TakeOfferController takeOfferController; protected GuiceFXMLLoader orderBookLoader; + private Node createOfferView; /////////////////////////////////////////////////////////////////////////////////////////// @@ -77,8 +79,24 @@ public class TradeController extends CachedViewController { // Textfield focus out triggers validation, use runLater as quick fix... //TODO update to new verison - ((TabPane) root).getSelectionModel().selectedIndexProperty().addListener((observableValue) -> - Platform.runLater(InputTextField::hideErrorMessageDisplay)); + ((TabPane) root).getSelectionModel().selectedIndexProperty().addListener((observableValue, oldValue, + newValue) -> + { + InputTextField.hideErrorMessageDisplay(); + } + ); + + // We want to get informed when a tab get closed + ((TabPane) root).getTabs().addListener((ListChangeListener) change -> { + change.next(); + List removedTabs = change.getRemoved(); + if (removedTabs.size() == 1 && createOfferView.equals(removedTabs.get(0).getContent())) { + if (createOfferCodeBehind != null) { + createOfferCodeBehind.terminate(); + createOfferCodeBehind = null; + } + } + }); } @@ -114,11 +132,12 @@ public class TradeController extends CachedViewController { // in different graphs GuiceFXMLLoader loader = new GuiceFXMLLoader(getClass().getResource(navigationItem.getFxmlUrl()), false); try { - final Parent view = loader.load(); + createOfferView = loader.load(); createOfferCodeBehind = loader.getController(); + log.debug("####### loader.getController() " + createOfferCodeBehind); createOfferCodeBehind.setParentController(this); final Tab tab = new Tab("Create offer"); - tab.setContent(view); + tab.setContent(createOfferView); tabPane.getTabs().add(tab); tabPane.getSelectionModel().select(tabPane.getTabs().size() - 1); return createOfferCodeBehind; @@ -159,8 +178,6 @@ public class TradeController extends CachedViewController { /////////////////////////////////////////////////////////////////////////////////////////// public void onCreateOfferViewRemoved() { - createOfferCodeBehind = null; - orderBookController.onCreateOfferViewRemoved(); } diff --git a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCB.java b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCB.java index 7f9b7671de..a6d20e5a71 100644 --- a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCB.java +++ b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCB.java @@ -78,22 +78,20 @@ public class CreateOfferCB extends CachedCodeBehind { balanceTextField.setup(presentationModel.getWalletFacade(), presentationModel.address.get()); } - @Override public void activate() { super.activate(); } - @Override public void deactivate() { super.deactivate(); - - //TODO check that again - if (parentController != null) ((TradeController) parentController).onCreateOfferViewRemoved(); } @Override public void terminate() { super.terminate(); + + // Used to re-enable createOfferButton in OrderBookController + if (parentController != null) ((TradeController) parentController).onCreateOfferViewRemoved(); } @@ -174,13 +172,15 @@ public class CreateOfferCB extends CachedCodeBehind { } }); + presentationModel.requestPlaceOfferFailed.addListener((o, oldValue, newValue) -> { if (newValue) { Popups.openErrorPopup("Error", "An error occurred when placing the offer.\n" + - presentationModel.requestPlaceOfferErrorMessage); + presentationModel.requestPlaceOfferErrorMessage.get()); presentationModel.requestPlaceOfferFailed.set(false); } }); + } private void setupBindings() { diff --git a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPM.java b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPM.java index c3d5b463e2..bd85b699d9 100644 --- a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPM.java +++ b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPM.java @@ -74,7 +74,7 @@ class CreateOfferPM extends PresentationModel { final BooleanProperty isCloseButtonVisible = new SimpleBooleanProperty(); final BooleanProperty isPlaceOfferButtonVisible = new SimpleBooleanProperty(true); - final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty(); + final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty(true); final BooleanProperty showWarningAdjustedVolume = new SimpleBooleanProperty(); final BooleanProperty showWarningInvalidFiatDecimalPlaces = new SimpleBooleanProperty(); final BooleanProperty showWarningInvalidBtcDecimalPlaces = new SimpleBooleanProperty(); @@ -206,7 +206,6 @@ class CreateOfferPM extends PresentationModel { } void onFocusOutMinAmountTextField(Boolean oldValue, Boolean newValue, String userInput) { - if (oldValue && !newValue) { InputValidator.ValidationResult result = isBtcInputValid(minAmount.get()); minAmountValidationResult.set(result); @@ -287,6 +286,12 @@ class CreateOfferPM extends PresentationModel { model.calculateTotalToPay(); model.calculateCollateral(); } + validateInput(); + }); + + minAmount.addListener((ov, oldValue, newValue) -> { + setMinAmountToModel(); + validateInput(); }); price.addListener((ov, oldValue, newValue) -> { @@ -296,6 +301,7 @@ class CreateOfferPM extends PresentationModel { model.calculateTotalToPay(); model.calculateCollateral(); } + validateInput(); }); volume.addListener((ov, oldValue, newValue) -> { @@ -306,6 +312,7 @@ class CreateOfferPM extends PresentationModel { model.calculateTotalToPay(); model.calculateCollateral(); } + validateInput(); }); // Binding with Bindings.createObjectBinding does not work because of bi-directional binding @@ -314,6 +321,13 @@ class CreateOfferPM extends PresentationModel { model.priceAsFiat.addListener((ov, oldValue, newValue) -> price.set(formatFiat(newValue))); model.volumeAsFiat.addListener((ov, oldValue, newValue) -> volume.set(formatFiat(newValue))); + model.requestPlaceOfferFailed.addListener((ov, oldValue, newValue) -> { + isPlaceOfferButtonDisabled.set(!newValue); + requestPlaceOfferFailed.set(newValue); + }); + model.requestPlaceOfferSuccess.addListener((ov, oldValue, newValue) -> isPlaceOfferButtonVisible.set + (!newValue)); + // ObservableLists model.acceptedCountries.addListener((Observable o) -> acceptedCountries.set(BSFormatter .countryLocalesToString(model.acceptedCountries))); @@ -340,14 +354,7 @@ class CreateOfferPM extends PresentationModel { isCloseButtonVisible.bind(model.requestPlaceOfferSuccess); requestPlaceOfferErrorMessage.bind(model.requestPlaceOfferErrorMessage); - requestPlaceOfferFailed.bind(model.requestPlaceOfferFailed); showTransactionPublishedScreen.bind(model.requestPlaceOfferSuccess); - - isPlaceOfferButtonDisabled.bind(Bindings.createBooleanBinding(() -> !model.requestPlaceOfferFailed.get(), - model.requestPlaceOfferFailed)); - - isPlaceOfferButtonVisible.bind(Bindings.createBooleanBinding(() -> !model.requestPlaceOfferSuccess.get(), - model.requestPlaceOfferSuccess)); } private void calculateVolume() { @@ -390,6 +397,14 @@ class CreateOfferPM extends PresentationModel { model.volumeAsFiat.set(parseToFiatWith2Decimals(volume.get())); } + private void validateInput() { + isPlaceOfferButtonDisabled.set(!(isBtcInputValid(amount.get()).isValid && + isBtcInputValid(minAmount.get()).isValid && + isBtcInputValid(price.get()).isValid && + isBtcInputValid(volume.get()).isValid && + model.isMinAmountLessOrEqualAmount()) + ); + } private InputValidator.ValidationResult isBtcInputValid(String input) { diff --git a/src/main/java/io/bitsquare/gui/trade/orderbook/OrderBookController.java b/src/main/java/io/bitsquare/gui/trade/orderbook/OrderBookController.java index 16fe47604a..8215e78887 100644 --- a/src/main/java/io/bitsquare/gui/trade/orderbook/OrderBookController.java +++ b/src/main/java/io/bitsquare/gui/trade/orderbook/OrderBookController.java @@ -552,11 +552,9 @@ public class OrderBookController extends CachedViewController { volume.setText(BSFormatter.formatPrice(a * p)); } - public void onCreateOfferViewRemoved() { createOfferButton.setDisable(false); } - }