From 2b48b4cd6e1d8af3085d355913a951457bafdbe8 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 21 Aug 2020 10:48:41 +0200 Subject: [PATCH] better handling of server connection editing --- .../sparrowwallet/sparrow/AppController.java | 18 +++++ .../sparrow/event/RequestConnectEvent.java | 5 ++ .../sparrow/event/RequestDisconnectEvent.java | 5 ++ .../preferences/PreferencesDialog.java | 12 ++++ .../ServerPreferencesController.java | 65 ++++++++++++------- .../sparrow/preferences/server.fxml | 5 ++ 6 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/RequestConnectEvent.java create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/RequestDisconnectEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 7ad4dc7f..db9c28a1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -532,6 +532,14 @@ public class AppController implements Initializable { return onlineProperty.get(); } + public void connect() { + serverToggle.selectedProperty().set(true); + } + + public void disconnect() { + serverToggle.selectedProperty().set(false); + } + public static BooleanProperty onlineProperty() { return onlineProperty; } public static Integer getCurrentBlockHeight() { @@ -1214,4 +1222,14 @@ public class AppController implements Initializable { public void requestQRScan(RequestQRScanEvent event) { openTransactionFromQR(null); } + + @Subscribe + public void requestConnect(RequestConnectEvent event) { + connect(); + } + + @Subscribe + public void requestDisconnect(RequestDisconnectEvent event) { + disconnect(); + } } \ No newline at end of file diff --git a/src/main/java/com/sparrowwallet/sparrow/event/RequestConnectEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/RequestConnectEvent.java new file mode 100644 index 00000000..cf1d3d45 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/RequestConnectEvent.java @@ -0,0 +1,5 @@ +package com.sparrowwallet.sparrow.event; + +public class RequestConnectEvent { + //Empty event class used to request programmatic connection to the server +} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/RequestDisconnectEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/RequestDisconnectEvent.java new file mode 100644 index 00000000..2153aaae --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/RequestDisconnectEvent.java @@ -0,0 +1,5 @@ +package com.sparrowwallet.sparrow.event; + +public class RequestDisconnectEvent { + //Empty event class used to request programmatic disconnection from the server +} diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java b/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java index dc5d5969..82e96ca7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/PreferencesDialog.java @@ -1,7 +1,10 @@ package com.sparrowwallet.sparrow.preferences; import com.sparrowwallet.sparrow.AppController; +import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.event.RequestConnectEvent; import com.sparrowwallet.sparrow.io.Config; +import com.sparrowwallet.sparrow.net.ElectrumServer; import javafx.fxml.FXMLLoader; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonType; @@ -12,6 +15,8 @@ import org.controlsfx.tools.Borders; import java.io.IOException; public class PreferencesDialog extends Dialog { + private final boolean existingConnection; + public PreferencesDialog() { this(null); } @@ -45,6 +50,13 @@ public class PreferencesDialog extends Dialog { dialogPane.setPrefWidth(650); dialogPane.setPrefHeight(500); + existingConnection = ElectrumServer.isConnected(); + setOnCloseRequest(event -> { + if(existingConnection && !ElectrumServer.isConnected()) { + EventManager.get().post(new RequestConnectEvent()); + } + }); + setResultConverter(dialogButton -> dialogButton == newWalletButtonType ? Boolean.TRUE : null); } catch(IOException e) { throw new RuntimeException(e); diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java index 11481206..1fc1af52 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java @@ -1,9 +1,11 @@ package com.sparrowwallet.sparrow.preferences; import com.google.common.net.HostAndPort; +import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.TextFieldValidator; import com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch; import com.sparrowwallet.sparrow.event.ConnectionEvent; +import com.sparrowwallet.sparrow.event.RequestDisconnectEvent; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.net.ElectrumServer; @@ -61,6 +63,9 @@ public class ServerPreferencesController extends PreferencesDetailController { @FXML private Button testConnection; + @FXML + private Button editConnection; + @FXML private TextArea testResults; @@ -125,33 +130,36 @@ public class ServerPreferencesController extends PreferencesDetailController { } }); - testConnection.setDisable(ElectrumServer.isConnected()); + boolean isConnected = ElectrumServer.isConnected(); + setFieldsEditable(!isConnected); + + testConnection.managedProperty().bind(testConnection.visibleProperty()); + testConnection.setVisible(!isConnected); testConnection.setOnAction(event -> { testResults.setText("Connecting to " + config.getElectrumServer() + "..."); testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.ELLIPSIS_H, null)); - boolean existingConnection = ElectrumServer.isConnected(); - if(existingConnection) { - ElectrumServer.ServerBannerService serverBannerService = new ElectrumServer.ServerBannerService(); - serverBannerService.setOnSucceeded(successEvent -> { - showConnectionSuccess(null, serverBannerService.getValue()); - }); - serverBannerService.setOnFailed(this::showConnectionFailure); - serverBannerService.start(); - } else { - ElectrumServer.ConnectionService connectionService = new ElectrumServer.ConnectionService(false); - connectionService.setPeriod(Duration.minutes(1)); - connectionService.setOnSucceeded(successEvent -> { - ConnectionEvent connectionEvent = (ConnectionEvent)connectionService.getValue(); - showConnectionSuccess(connectionEvent.getServerVersion(), connectionEvent.getServerBanner()); - connectionService.cancel(); - }); - connectionService.setOnFailed(workerStateEvent -> { - showConnectionFailure(workerStateEvent); - connectionService.cancel(); - }); - connectionService.start(); - } + ElectrumServer.ConnectionService connectionService = new ElectrumServer.ConnectionService(false); + connectionService.setPeriod(Duration.minutes(1)); + connectionService.setOnSucceeded(successEvent -> { + ConnectionEvent connectionEvent = (ConnectionEvent)connectionService.getValue(); + showConnectionSuccess(connectionEvent.getServerVersion(), connectionEvent.getServerBanner()); + connectionService.cancel(); + }); + connectionService.setOnFailed(workerStateEvent -> { + showConnectionFailure(workerStateEvent); + connectionService.cancel(); + }); + connectionService.start(); + }); + + editConnection.managedProperty().bind(editConnection.visibleProperty()); + editConnection.setVisible(isConnected); + editConnection.setOnAction(event -> { + EventManager.get().post(new RequestDisconnectEvent()); + setFieldsEditable(true); + editConnection.setVisible(false); + testConnection.setVisible(true); }); String electrumServer = config.getElectrumServer(); @@ -196,6 +204,17 @@ public class ServerPreferencesController extends PreferencesDetailController { } } + private void setFieldsEditable(boolean editable) { + host.setEditable(editable); + port.setEditable(editable); + useSsl.setDisable(!editable); + certificate.setEditable(editable); + certificateSelect.setDisable(!editable); + useProxy.setDisable(!editable); + proxyHost.setEditable(editable); + proxyPort.setEditable(editable); + } + private void showConnectionSuccess(List serverVersion, String serverBanner) { testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.CHECK_CIRCLE, Color.rgb(80, 161, 79))); if(serverVersion != null) { diff --git a/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml b/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml index 3d05f50e..25d3e6b2 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/preferences/server.fxml @@ -59,6 +59,11 @@ +