diff --git a/wallettemplate/src/main/java/org/bitcoinj/walletfx/overlay/OverlayWindowController.java b/wallettemplate/src/main/java/org/bitcoinj/walletfx/overlay/OverlayWindowController.java new file mode 100644 index 000000000..0a0a0f28b --- /dev/null +++ b/wallettemplate/src/main/java/org/bitcoinj/walletfx/overlay/OverlayWindowController.java @@ -0,0 +1,28 @@ +/* + * Copyright by the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.bitcoinj.walletfx.overlay; + +import wallettemplate.MainController; + +/** + * Interface for controllers displayed via OverlayWindow.OverlayUI + */ +public interface OverlayWindowController { + /** + * @param ui The overlay UI (node, controller pair) + */ + void setOverlayUI(MainController.OverlayUI> ui); +} diff --git a/wallettemplate/src/main/java/wallettemplate/MainController.java b/wallettemplate/src/main/java/wallettemplate/MainController.java index 4c1295a83..4a4a36981 100644 --- a/wallettemplate/src/main/java/wallettemplate/MainController.java +++ b/wallettemplate/src/main/java/wallettemplate/MainController.java @@ -36,12 +36,12 @@ import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.util.Duration; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import org.bitcoinj.walletfx.utils.GuiUtils; import org.bitcoinj.walletfx.utils.TextFieldValidator; import wallettemplate.controls.ClickableBitcoinAddress; import wallettemplate.controls.NotificationBarPane; import org.bitcoinj.walletfx.utils.BitcoinUIModel; -import org.bitcoinj.walletfx.utils.GuiUtils; import org.bitcoinj.walletfx.utils.easing.EasingMode; import org.bitcoinj.walletfx.utils.easing.ElasticInterpolator; @@ -170,7 +170,7 @@ public class MainController { return model.getDownloadProgressTracker(); } - public class OverlayUI { + public class OverlayUI> { public Node ui; public T controller; @@ -219,22 +219,18 @@ public class MainController { } @Nullable - private OverlayUI currentOverlay; + private OverlayUI> currentOverlay; - public OverlayUI overlayUI(Node node, T controller) { + public > OverlayUI overlayUI(Node node, T controller) { checkGuiThread(); OverlayUI pair = new OverlayUI<>(node, controller); - // Auto-magically set the overlayUI member, if it's there. - try { - controller.getClass().getField("overlayUI").set(controller, pair); - } catch (IllegalAccessException | NoSuchFieldException ignored) { - } + controller.setOverlayUI(pair); pair.show(); return pair; } /** Loads the FXML file with the given name, blurs out the main UI and puts this one on top. */ - public OverlayUI overlayUI(String name) { + public > OverlayUI overlayUI(String name) { try { checkGuiThread(); // Load the UI from disk. @@ -243,13 +239,7 @@ public class MainController { Pane ui = loader.load(); T controller = loader.getController(); OverlayUI pair = new OverlayUI<>(ui, controller); - // Auto-magically set the overlayUI member, if it's there. - try { - if (controller != null) - controller.getClass().getField("overlayUI").set(controller, pair); - } catch (IllegalAccessException | NoSuchFieldException ignored) { - ignored.printStackTrace(); - } + controller.setOverlayUI(pair); pair.show(); return pair; } catch (IOException e) { diff --git a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java index bb82fa284..88d311a59 100644 --- a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java +++ b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java @@ -29,6 +29,7 @@ import javafx.event.ActionEvent; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import org.bouncycastle.crypto.params.KeyParameter; import wallettemplate.controls.BitcoinAddressValidator; import org.bitcoinj.walletfx.utils.TextFieldValidator; @@ -39,7 +40,7 @@ import static org.bitcoinj.walletfx.utils.GuiUtils.*; import javax.annotation.Nullable; -public class SendMoneyController { +public class SendMoneyController implements OverlayWindowController { public Button sendBtn; public Button cancelBtn; public TextField address; @@ -47,11 +48,16 @@ public class SendMoneyController { public TextField amountEdit; public Label btcLabel; - public MainController.OverlayUI overlayUI; + private MainController.OverlayUI> overlayUI; private Wallet.SendResult sendResult; private KeyParameter aesKey; + @Override + public void setOverlayUI(MainController.OverlayUI> ui) { + overlayUI = ui; + } + // Called by FXMLLoader public void initialize() { Coin balance = Main.bitcoin.wallet().getBalance(); diff --git a/wallettemplate/src/main/java/wallettemplate/WalletPasswordController.java b/wallettemplate/src/main/java/wallettemplate/WalletPasswordController.java index e5a6e5b6c..cc370a80e 100644 --- a/wallettemplate/src/main/java/wallettemplate/WalletPasswordController.java +++ b/wallettemplate/src/main/java/wallettemplate/WalletPasswordController.java @@ -30,6 +30,7 @@ import javafx.scene.control.ProgressIndicator; import javafx.scene.image.ImageView; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.bouncycastle.crypto.params.KeyParameter; @@ -44,7 +45,7 @@ import static org.bitcoinj.walletfx.utils.GuiUtils.*; * User interface for entering a password on demand, e.g. to send money. Also used when encrypting a wallet. Shows a * progress meter as we scrypt the password. */ -public class WalletPasswordController { +public class WalletPasswordController implements OverlayWindowController { private static final Logger log = LoggerFactory.getLogger(WalletPasswordController.class); @FXML HBox buttonsBox; @@ -54,10 +55,15 @@ public class WalletPasswordController { @FXML GridPane widgetGrid; @FXML Label explanationLabel; - public MainController.OverlayUI overlayUI; + private MainController.OverlayUI> overlayUI; private SimpleObjectProperty aesKey = new SimpleObjectProperty<>(); + @Override + public void setOverlayUI(MainController.OverlayUI> ui) { + overlayUI = ui; + } + public void initialize() { progressMeter.setOpacity(0); Platform.runLater(pass1::requestFocus); diff --git a/wallettemplate/src/main/java/wallettemplate/WalletSetPasswordController.java b/wallettemplate/src/main/java/wallettemplate/WalletSetPasswordController.java index 9b73f064e..9c558587f 100644 --- a/wallettemplate/src/main/java/wallettemplate/WalletSetPasswordController.java +++ b/wallettemplate/src/main/java/wallettemplate/WalletSetPasswordController.java @@ -23,6 +23,7 @@ import javafx.scene.control.*; import javafx.scene.layout.*; import org.bitcoinj.crypto.*; import org.bitcoinj.wallet.*; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import org.slf4j.*; import org.bouncycastle.crypto.params.*; @@ -34,7 +35,7 @@ import java.util.concurrent.*; import org.bitcoinj.walletfx.utils.KeyDerivationTasks; import static org.bitcoinj.walletfx.utils.GuiUtils.*; -public class WalletSetPasswordController { +public class WalletSetPasswordController implements OverlayWindowController { private static final Logger log = LoggerFactory.getLogger(WalletSetPasswordController.class); public PasswordField pass1, pass2; @@ -43,7 +44,7 @@ public class WalletSetPasswordController { public Button closeButton; public Label explanationLabel; - public MainController.OverlayUI overlayUI; + private MainController.OverlayUI> overlayUI; // These params were determined empirically on a top-range (as of 2014) MacBook Pro with native scrypt support, // using the scryptenc command line tool from the original scrypt distribution, given a memory limit of 40mb. public static final Protos.ScryptParameters SCRYPT_PARAMETERS = Protos.ScryptParameters.newBuilder() @@ -53,6 +54,11 @@ public class WalletSetPasswordController { .setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt())) .build(); + @Override + public void setOverlayUI(MainController.OverlayUI> ui) { + overlayUI = ui; + } + public void initialize() { progressMeter.setOpacity(0); } diff --git a/wallettemplate/src/main/java/wallettemplate/WalletSettingsController.java b/wallettemplate/src/main/java/wallettemplate/WalletSettingsController.java index 8ed1887c8..cc195477c 100644 --- a/wallettemplate/src/main/java/wallettemplate/WalletSettingsController.java +++ b/wallettemplate/src/main/java/wallettemplate/WalletSettingsController.java @@ -28,6 +28,7 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.DatePicker; import javafx.scene.control.TextArea; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.bouncycastle.crypto.params.KeyParameter; @@ -47,7 +48,7 @@ import static org.bitcoinj.walletfx.utils.GuiUtils.informationalAlert; import static org.bitcoinj.walletfx.utils.WTUtils.didThrow; import static org.bitcoinj.walletfx.utils.WTUtils.unchecked; -public class WalletSettingsController { +public class WalletSettingsController implements OverlayWindowController { private static final Logger log = LoggerFactory.getLogger(WalletSettingsController.class); @FXML Button passwordButton; @@ -55,10 +56,15 @@ public class WalletSettingsController { @FXML TextArea wordsArea; @FXML Button restoreButton; - public MainController.OverlayUI overlayUI; + private MainController.OverlayUI> overlayUI; private KeyParameter aesKey; + @Override + public void setOverlayUI(MainController.OverlayUI> ui) { + overlayUI = ui; + } + // Note: NOT called by FXMLLoader! public void initialize(@Nullable KeyParameter aesKey) { DeterministicSeed seed = Main.bitcoin.wallet().getKeyChainSeed(); diff --git a/wallettemplate/src/main/java/wallettemplate/controls/ClickableBitcoinAddress.java b/wallettemplate/src/main/java/wallettemplate/controls/ClickableBitcoinAddress.java index 0d584685d..e070f8ea4 100644 --- a/wallettemplate/src/main/java/wallettemplate/controls/ClickableBitcoinAddress.java +++ b/wallettemplate/src/main/java/wallettemplate/controls/ClickableBitcoinAddress.java @@ -42,6 +42,7 @@ import javafx.scene.layout.Pane; import org.bitcoinj.core.Address; import org.bitcoinj.uri.BitcoinURI; +import org.bitcoinj.walletfx.overlay.OverlayWindowController; import wallettemplate.Main; import org.bitcoinj.walletfx.utils.GuiUtils; import org.bitcoinj.walletfx.utils.QRCodeImages; @@ -57,7 +58,7 @@ import static javafx.beans.binding.Bindings.convert; * address looks like a blue hyperlink. Next to it there are two icons, one that copies to the clipboard and another * that shows a QRcode. */ -public class ClickableBitcoinAddress extends AnchorPane { +public class ClickableBitcoinAddress extends AnchorPane implements OverlayWindowController { @FXML protected Label addressLabel; @FXML protected ContextMenu addressMenu; @FXML protected Label copyWidget; @@ -66,6 +67,10 @@ public class ClickableBitcoinAddress extends AnchorPane { protected SimpleObjectProperty
address = new SimpleObjectProperty<>(); private final StringExpression addressStr; + @Override + public void setOverlayUI(MainController.OverlayUI> ui) { + } + public ClickableBitcoinAddress() { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("bitcoin_address.fxml"));