Search in sources :

Example 1 with TextFieldValidator

use of com.sparrowwallet.sparrow.control.TextFieldValidator in project sparrow by sparrowwallet.

the class ServerPreferencesController method initializeView.

@Override
public void initializeView(Config config) {
    EventManager.get().register(this);
    getMasterController().closingProperty().addListener((observable, oldValue, newValue) -> {
        EventManager.get().unregister(this);
    });
    Platform.runLater(this::setupValidation);
    publicElectrumForm.managedProperty().bind(publicElectrumForm.visibleProperty());
    coreForm.managedProperty().bind(coreForm.visibleProperty());
    electrumForm.managedProperty().bind(electrumForm.visibleProperty());
    serverTypeToggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
        if (serverTypeToggleGroup.getSelectedToggle() != null) {
            ServerType existingType = config.getServerType();
            ServerType serverType = (ServerType) newValue.getUserData();
            publicElectrumForm.setVisible(serverType == ServerType.PUBLIC_ELECTRUM_SERVER);
            coreForm.setVisible(serverType == ServerType.BITCOIN_CORE);
            electrumForm.setVisible(serverType == ServerType.ELECTRUM_SERVER);
            config.setServerType(serverType);
            testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.QUESTION_CIRCLE, ""));
            testResults.clear();
            if (existingType != serverType) {
                EventManager.get().post(new ServerTypeChangedEvent(serverType));
            }
        } else if (oldValue != null) {
            oldValue.setSelected(true);
        }
    });
    ServerType serverType = config.getServerType() != null ? (config.getServerType() == ServerType.PUBLIC_ELECTRUM_SERVER && !PublicElectrumServer.supportedNetwork() ? ServerType.BITCOIN_CORE : config.getServerType()) : (config.getCoreServer() == null && config.getElectrumServer() != null ? ServerType.ELECTRUM_SERVER : (config.getCoreServer() != null || !PublicElectrumServer.supportedNetwork() ? ServerType.BITCOIN_CORE : ServerType.PUBLIC_ELECTRUM_SERVER));
    if (!PublicElectrumServer.supportedNetwork()) {
        serverTypeSegmentedButton.getButtons().remove(publicElectrumToggle);
        serverTypeToggleGroup.getToggles().remove(publicElectrumToggle);
    }
    serverTypeToggleGroup.selectToggle(serverTypeToggleGroup.getToggles().stream().filter(toggle -> toggle.getUserData() == serverType).findFirst().orElse(null));
    publicElectrumServer.setItems(FXCollections.observableList(PublicElectrumServer.getServers()));
    publicElectrumServer.getSelectionModel().selectedItemProperty().addListener(getPublicElectrumServerListener(config));
    publicUseProxy.selectedProperty().bindBidirectional(useProxy.selectedProperty());
    publicProxyHost.textProperty().bindBidirectional(proxyHost.textProperty());
    publicProxyPort.textProperty().bindBidirectional(proxyPort.textProperty());
    corePort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
    electrumPort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
    proxyPort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
    coreProxyPort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
    publicProxyPort.setTextFormatter(new TextFieldValidator(TextFieldValidator.ValidationModus.MAX_INTEGERS, 5).getFormatter());
    coreHost.textProperty().addListener(getBitcoinCoreListener(config));
    corePort.textProperty().addListener(getBitcoinCoreListener(config));
    coreUser.textProperty().addListener(getBitcoinAuthListener(config));
    corePass.textProperty().addListener(getBitcoinAuthListener(config));
    coreUseProxy.selectedProperty().bindBidirectional(useProxy.selectedProperty());
    coreProxyHost.textProperty().bindBidirectional(proxyHost.textProperty());
    coreProxyPort.textProperty().bindBidirectional(proxyPort.textProperty());
    electrumHost.textProperty().addListener(getElectrumServerListener(config));
    electrumPort.textProperty().addListener(getElectrumServerListener(config));
    proxyHost.textProperty().addListener(getProxyListener(config));
    proxyPort.textProperty().addListener(getProxyListener(config));
    coreDataDirField.managedProperty().bind(coreDataDirField.visibleProperty());
    coreUserPassField.managedProperty().bind(coreUserPassField.visibleProperty());
    coreUserPassField.visibleProperty().bind(coreDataDirField.visibleProperty().not());
    coreAuthToggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
        if (coreAuthToggleGroup.getSelectedToggle() != null) {
            CoreAuthType coreAuthType = (CoreAuthType) newValue.getUserData();
            coreDataDirField.setVisible(coreAuthType == CoreAuthType.COOKIE);
            config.setCoreAuthType(coreAuthType);
        } else if (oldValue != null) {
            oldValue.setSelected(true);
        }
    });
    CoreAuthType coreAuthType = config.getCoreAuthType() != null ? config.getCoreAuthType() : CoreAuthType.COOKIE;
    coreAuthToggleGroup.selectToggle(coreAuthToggleGroup.getToggles().stream().filter(toggle -> toggle.getUserData() == coreAuthType).findFirst().orElse(null));
    coreDataDir.textProperty().addListener((observable, oldValue, newValue) -> {
        File dataDir = getDirectory(newValue);
        config.setCoreDataDir(dataDir);
    });
    coreDataDirSelect.setOnAction(event -> {
        Stage window = new Stage();
        DirectoryChooser directorChooser = new DirectoryChooser();
        directorChooser.setTitle("Select Bitcoin Core Data Directory");
        directorChooser.setInitialDirectory(config.getCoreDataDir() != null ? config.getCoreDataDir() : new File(System.getProperty("user.home")));
        File dataDir = directorChooser.showDialog(window);
        if (dataDir != null) {
            coreDataDir.setText(dataDir.getAbsolutePath());
        }
    });
    recentCoreServers.setConverter(new UrlHostConverter());
    recentCoreServers.setItems(FXCollections.observableList(Config.get().getRecentCoreServers() == null ? new ArrayList<>() : Config.get().getRecentCoreServers()));
    recentCoreServers.prefWidthProperty().bind(coreHost.widthProperty());
    recentCoreServers.valueProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null && Protocol.getProtocol(newValue) != null) {
            HostAndPort hostAndPort = Protocol.getProtocol(newValue).getServerHostAndPort(newValue);
            coreHost.setText(hostAndPort.getHost());
            corePort.setText(Integer.toString(hostAndPort.getPort()));
        }
    });
    recentElectrumServers.setConverter(new UrlHostConverter());
    recentElectrumServers.setItems(FXCollections.observableList(Config.get().getRecentElectrumServers() == null ? new ArrayList<>() : Config.get().getRecentElectrumServers()));
    recentElectrumServers.prefWidthProperty().bind(electrumHost.widthProperty());
    recentElectrumServers.valueProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null && Protocol.getProtocol(newValue) != null) {
            HostAndPort hostAndPort = Protocol.getProtocol(newValue).getServerHostAndPort(newValue);
            electrumHost.setText(hostAndPort.getHost());
            electrumPort.setText(Integer.toString(hostAndPort.getPort()));
            electrumUseSsl.setSelected(Protocol.getProtocol(newValue) == Protocol.SSL);
        }
    });
    electrumUseSsl.selectedProperty().addListener((observable, oldValue, newValue) -> {
        setElectrumServerInConfig(config);
        electrumCertificate.setDisable(!newValue);
        electrumCertificateSelect.setDisable(!newValue);
    });
    electrumCertificate.textProperty().addListener((observable, oldValue, newValue) -> {
        File crtFile = getCertificate(newValue);
        config.setElectrumServerCert(crtFile);
    });
    electrumCertificateSelect.setOnAction(event -> {
        Stage window = new Stage();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Select Electrum Server certificate");
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Files", org.controlsfx.tools.Platform.getCurrent().equals(org.controlsfx.tools.Platform.UNIX) ? "*" : "*.*"), new FileChooser.ExtensionFilter("CRT", "*.crt"));
        AppServices.moveToActiveWindowScreen(window, 800, 450);
        File file = fileChooser.showOpenDialog(window);
        if (file != null) {
            electrumCertificate.setText(file.getAbsolutePath());
        }
    });
    useProxy.selectedProperty().addListener((observable, oldValue, newValue) -> {
        config.setUseProxy(newValue);
        proxyHost.setText(proxyHost.getText() + " ");
        proxyHost.setText(proxyHost.getText().trim());
        proxyHost.setDisable(!newValue);
        proxyPort.setDisable(!newValue);
        publicProxyHost.setDisable(!newValue);
        publicProxyPort.setDisable(!newValue);
    });
    boolean isConnected = AppServices.isConnecting() || AppServices.isConnected();
    if (AppServices.isConnecting()) {
        testResults.appendText("Connecting to server, please wait...");
    }
    testConnection.managedProperty().bind(testConnection.visibleProperty());
    testConnection.setVisible(!isConnected);
    setTestResultsFont();
    testConnection.setOnAction(event -> {
        testConnection.setGraphic(getGlyph(FontAwesome5.Glyph.ELLIPSIS_H, null));
        testResults.setText("Connecting to " + config.getServerAddress() + "...");
        if (Config.get().requiresInternalTor() && Tor.getDefault() == null) {
            startTor();
        } else {
            startElectrumConnection();
        }
    });
    editConnection.managedProperty().bind(editConnection.visibleProperty());
    editConnection.setVisible(isConnected);
    editConnection.setDisable(AppServices.isConnecting());
    editConnection.setOnAction(event -> {
        EventManager.get().post(new RequestDisconnectEvent());
        setFieldsEditable(true);
        editConnection.setVisible(false);
        testConnection.setVisible(true);
    });
    PublicElectrumServer configPublicElectrumServer = PublicElectrumServer.fromUrl(config.getPublicElectrumServer());
    if (configPublicElectrumServer == null && PublicElectrumServer.supportedNetwork()) {
        List<PublicElectrumServer> servers = PublicElectrumServer.getServers();
        if (!servers.isEmpty()) {
            publicElectrumServer.setValue(servers.get(new Random().nextInt(servers.size())));
        }
    } else {
        publicElectrumServer.setValue(configPublicElectrumServer);
    }
    String coreServer = config.getCoreServer();
    if (coreServer != null) {
        Protocol protocol = Protocol.getProtocol(coreServer);
        if (protocol != null) {
            HostAndPort server = protocol.getServerHostAndPort(coreServer);
            coreHost.setText(server.getHost());
            if (server.hasPort()) {
                corePort.setText(Integer.toString(server.getPort()));
            }
        }
    } else {
        coreHost.setText("127.0.0.1");
        corePort.setText(String.valueOf(Network.get().getDefaultPort()));
    }
    coreDataDir.setText(config.getCoreDataDir() != null ? config.getCoreDataDir().getAbsolutePath() : getDefaultCoreDataDir().getAbsolutePath());
    if (config.getCoreAuth() != null) {
        String[] userPass = config.getCoreAuth().split(":");
        if (userPass.length > 0) {
            coreUser.setText(userPass[0]);
        }
        if (userPass.length > 1) {
            corePass.setText(userPass[1]);
        }
    }
    String electrumServer = config.getElectrumServer();
    if (electrumServer != null) {
        Protocol protocol = Protocol.getProtocol(electrumServer);
        if (protocol != null) {
            boolean ssl = protocol.equals(Protocol.SSL);
            electrumUseSsl.setSelected(ssl);
            electrumCertificate.setDisable(!ssl);
            electrumCertificateSelect.setDisable(!ssl);
            HostAndPort server = protocol.getServerHostAndPort(electrumServer);
            electrumHost.setText(server.getHost());
            if (server.hasPort()) {
                electrumPort.setText(Integer.toString(server.getPort()));
            }
        }
    }
    File certificateFile = config.getElectrumServerCert();
    if (certificateFile != null) {
        electrumCertificate.setText(certificateFile.getAbsolutePath());
    }
    useProxy.setSelected(config.isUseProxy());
    proxyHost.setDisable(!config.isUseProxy());
    proxyPort.setDisable(!config.isUseProxy());
    publicProxyHost.setDisable(!config.isUseProxy());
    publicProxyPort.setDisable(!config.isUseProxy());
    String proxyServer = config.getProxyServer();
    if (proxyServer != null) {
        HostAndPort server = HostAndPort.fromString(proxyServer);
        proxyHost.setText(server.getHost());
        if (server.hasPort()) {
            proxyPort.setText(Integer.toString(server.getPort()));
        }
    }
    setFieldsEditable(!isConnected);
}
Also used : Form(tornadofx.control.Form) CertificateFactory(java.security.cert.CertificateFactory) javafx.scene.control(javafx.scene.control) LoggerFactory(org.slf4j.LoggerFactory) FXCollections(javafx.collections.FXCollections) SimpleDateFormat(java.text.SimpleDateFormat) ValidationSupport(org.controlsfx.validation.ValidationSupport) Random(java.util.Random) Config(com.sparrowwallet.sparrow.io.Config) ArrayList(java.util.ArrayList) Glyph(org.controlsfx.glyphfont.Glyph) StyleClassValidationDecoration(org.controlsfx.validation.decoration.StyleClassValidationDecoration) FontAwesome5(com.sparrowwallet.sparrow.glyphfont.FontAwesome5) ValidationResult(org.controlsfx.validation.ValidationResult) Subscribe(com.google.common.eventbus.Subscribe) TextFieldValidator(com.sparrowwallet.sparrow.control.TextFieldValidator) DateFormat(java.text.DateFormat) DirectoryChooser(javafx.stage.DirectoryChooser) Storage(com.sparrowwallet.sparrow.io.Storage) com.sparrowwallet.sparrow.net(com.sparrowwallet.sparrow.net) Logger(org.slf4j.Logger) UnlabeledToggleSwitch(com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch) ComboBoxTextField(com.sparrowwallet.sparrow.control.ComboBoxTextField) Field(tornadofx.control.Field) com.sparrowwallet.sparrow.event(com.sparrowwallet.sparrow.event) Font(javafx.scene.text.Font) StringConverter(javafx.util.StringConverter) FileInputStream(java.io.FileInputStream) HostAndPort(com.google.common.net.HostAndPort) Tor(org.berndpruenster.netlayer.tor.Tor) File(java.io.File) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) FileChooser(javafx.stage.FileChooser) Duration(javafx.util.Duration) List(java.util.List) AppServices(com.sparrowwallet.sparrow.AppServices) Network(com.sparrowwallet.drongo.Network) Stage(javafx.stage.Stage) Mode(com.sparrowwallet.sparrow.Mode) EventManager(com.sparrowwallet.sparrow.EventManager) SegmentedButton(org.controlsfx.control.SegmentedButton) Optional(java.util.Optional) Validator(org.controlsfx.validation.Validator) ChangeListener(javafx.beans.value.ChangeListener) NotNull(org.jetbrains.annotations.NotNull) HostAndPort(com.google.common.net.HostAndPort) Random(java.util.Random) FileChooser(javafx.stage.FileChooser) Stage(javafx.stage.Stage) TextFieldValidator(com.sparrowwallet.sparrow.control.TextFieldValidator) File(java.io.File) DirectoryChooser(javafx.stage.DirectoryChooser)

Aggregations

Subscribe (com.google.common.eventbus.Subscribe)1 HostAndPort (com.google.common.net.HostAndPort)1 Network (com.sparrowwallet.drongo.Network)1 AppServices (com.sparrowwallet.sparrow.AppServices)1 EventManager (com.sparrowwallet.sparrow.EventManager)1 Mode (com.sparrowwallet.sparrow.Mode)1 ComboBoxTextField (com.sparrowwallet.sparrow.control.ComboBoxTextField)1 TextFieldValidator (com.sparrowwallet.sparrow.control.TextFieldValidator)1 UnlabeledToggleSwitch (com.sparrowwallet.sparrow.control.UnlabeledToggleSwitch)1 com.sparrowwallet.sparrow.event (com.sparrowwallet.sparrow.event)1 FontAwesome5 (com.sparrowwallet.sparrow.glyphfont.FontAwesome5)1 Config (com.sparrowwallet.sparrow.io.Config)1 Storage (com.sparrowwallet.sparrow.io.Storage)1 com.sparrowwallet.sparrow.net (com.sparrowwallet.sparrow.net)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 CertificateFactory (java.security.cert.CertificateFactory)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1