Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broke out Wallet and Nodes into their own components #8

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ of the License (see COPYING and COPYING.addendum).
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Optional;
import java.util.Date;
import java.text.SimpleDateFormat;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
Expand All @@ -34,35 +32,20 @@ of the License (see COPYING and COPYING.addendum).
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.CornerRadii;
import javafx.stage.Stage;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.application.Platform;
import javafx.util.Callback;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javafx.beans.value.ObservableValue;
// import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import org.unigrid.janus.model.service.DebugService;
import org.unigrid.janus.model.service.RPCService;
import org.unigrid.janus.model.service.WindowService;
// import org.unigrid.janus.model.rpc.entity.NewAddress;
import org.unigrid.janus.model.Transaction;
import org.unigrid.janus.model.Passphrase;
import org.unigrid.janus.model.rpc.entity.ListTransactions;
import org.unigrid.janus.model.Wallet;

public class MainWindowController implements Initializable, PropertyChangeListener {
Expand All @@ -83,17 +66,8 @@ public class MainWindowController implements Initializable, PropertyChangeListen
private static final int TAB_SETTINGS_DEBUG = 5;

/* Injected fx:id from FXML */
@FXML private Label lblBalance;
@FXML private Label lblBlockCount;
@FXML private Label lblConnection;
@FXML private FlowPane pnlBalance;
@FXML private FlowPane pnlLocked;
// wallet table
@FXML private TableView tblWalletTrans;
@FXML private TableColumn colWalletTransDate;
@FXML private TableColumn colWalletTransType;
@FXML private TableColumn colWalletTransAddress;
@FXML private TableColumn colWalletTransAmount;
// main navigation
@FXML private ToggleButton btnWallet;
@FXML private ToggleButton btnTransactions;
Expand All @@ -118,42 +92,6 @@ public class MainWindowController implements Initializable, PropertyChangeListen
public void initialize(URL url, ResourceBundle rb) {
/* Empty on purpose */
wallet.addPropertyChangeListener(this);
setupWalletTransactions();
}

private void setupWalletTransactions() {
try {
colWalletTransDate.setCellValueFactory(
new Callback<CellDataFeatures<Transaction, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Transaction, String> t) {
long time = t.getValue().getTime();
Date date = new Date(time * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return new ReadOnlyStringWrapper(sdf.format(date));
// return new ReadOnlyStringWrapper("n/a");
}
});
colWalletTransType.setCellValueFactory(
new PropertyValueFactory<Transaction, String>("category"));
colWalletTransAddress.setCellValueFactory(
new PropertyValueFactory<Transaction, String>("account"));
colWalletTransAmount.setCellValueFactory(
new PropertyValueFactory<Transaction, Double>("amount"));
} catch (Exception e) {
debug.log(String.format("ERROR: (setup wallet table) %s", e.getMessage()));
}
}

private void loadWalletPreviewTrans() {
ListTransactions transactions = rpc.call(new ListTransactions.Request(0, 10),
ListTransactions.class);
ObservableList<Transaction> walletTransactions = FXCollections.observableArrayList();

for (Transaction t : transactions.getResult()) {
walletTransactions.add(t);
}

tblWalletTrans.setItems(walletTransactions);
}

@FXML
Expand All @@ -162,7 +100,7 @@ private void onShown(WindowEvent event) {
Platform.runLater(() -> {
try {
debug.log("Shown event executing.");
loadWalletPreviewTrans();
// loadWalletPreviewTrans();
} catch (Exception e) {
debug.log(String.format("ERROR: (onShown) %s", e.getMessage()));
}
Expand Down Expand Up @@ -208,7 +146,7 @@ private void tabSelect(int tab) {
private void onWalletTap(MouseEvent event) {
try {
tabSelect(TAB_WALLET);
loadWalletPreviewTrans();
// loadWalletPreviewTrans();
debug.log("Wallet clicked!");
} catch (Exception e) {
debug.log(String.format("ERROR: (wallet click) %s", e.getMessage()));
Expand Down Expand Up @@ -294,67 +232,6 @@ private void onSetDebugTap(MouseEvent event) {
settingSelected(TAB_SETTINGS_DEBUG);
}

@FXML
private void onUnlock(MouseEvent event) {
debug.log("Unlock clicked!");
try {
Dialog<Passphrase> dialog = new Dialog<>();
dialog.setTitle("Unlock Wallet");
dialog.setHeaderText("Enter your pass phrase to unlock the wallet.\n"
+ "This will be the same as you used to lock it.");
Label label1 = new Label("Passphrase:");
Label label2 = new Label("Repeat Passphrase:");
TextArea phrase = new TextArea();
TextArea repeat = new TextArea();
phrase.setPrefHeight(40);
repeat.setPrefHeight(40);

GridPane grid = new GridPane();
grid.add(label1, 1, 1);
grid.add(phrase, 1, 2);
grid.add(label2, 1, 3);
grid.add(repeat, 1, 4);
dialog.getDialogPane().setContent(grid);

ButtonType buttonTypeOk = new ButtonType("Unlock", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(buttonTypeOk);
dialog.getDialogPane().lookupButton(buttonTypeOk).setDisable(true);

repeat.setOnKeyTyped((KeyEvent evt) -> {
String sPhrase = phrase.getText();
String sRepeat = repeat.getText();
if (sPhrase.equals(sRepeat)) {
dialog.getDialogPane().lookupButton(buttonTypeOk).setDisable(false);
} else {
dialog.getDialogPane().lookupButton(buttonTypeOk).setDisable(true);
}
});

dialog.setResultConverter(new Callback<ButtonType, Passphrase>() {
@Override
public Passphrase call(ButtonType b) {
if (b == buttonTypeOk) {
return new Passphrase(phrase.getText(), repeat.getText());
}

return null;
}
});

dialog.getDialogPane().getStylesheets().add("/org/unigrid/janus/view/main.css");

Optional<Passphrase> result = dialog.showAndWait();

if (result.isPresent()) {
debug.log(String.format("Unlock dialog result: %s", result.get()));
pnlLocked.setVisible(false);
pnlBalance.setVisible(true);
}
} catch (Exception e) {
debug.log(String.format("ERROR: (passphrase unlock) %s", e.getMessage()));
}
}

@FXML
private void onLock(MouseEvent event) {
debug.log("Update passphrase clicked!");
Expand All @@ -380,8 +257,7 @@ private void onLock(MouseEvent event) {
BorderStrokeStyle.SOLID,
new CornerRadii(3),
new BorderWidths(1))));
pnlBalance.setVisible(false);
pnlLocked.setVisible(true);
wallet.setLocked(true);
tabSelect(TAB_WALLET);
}
}
Expand Down Expand Up @@ -415,13 +291,8 @@ private void onRepeatPassphraseChange(KeyEvent event) {
}

public void propertyChange(PropertyChangeEvent event) {
Stage stage = window.getStage();
debug.log("Main Window change fired!");
debug.log(event.getPropertyName());
if (event.getPropertyName().equals(wallet.BALANCE_PROPERTY)) {
debug.log(String.format("Value: %.8f", (double) event.getNewValue()));
lblBalance.setText(String.format("%.8f", (double) event.getNewValue()));
}
if (event.getPropertyName().equals(wallet.BLOCKS_PROPERTY)) {
debug.log(String.format("blocks: %d", (int) event.getNewValue()));
int blocks = (int) event.getNewValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
The Janus Wallet
Copyright © 2021 The Unigrid Foundation

This program is free software: you can redistribute it and/or modify it under the terms of the
addended GNU Affero General Public License as published by the Free Software Foundation, version 3
of the License (see COPYING and COPYING.addendum).

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received an addended copy of the GNU Affero General Public License with this program.
If not, see <http://www.gnu.org/licenses/> and <https://github.com/unigrid-project/janus-java>.
*/

package org.unigrid.janus.controller.view;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import org.unigrid.janus.model.service.DebugService;
import org.unigrid.janus.model.service.RPCService;
import org.unigrid.janus.model.service.WindowService;

public class NodesController implements Initializable {
private static DebugService debug = new DebugService();
private static RPCService rpc = new RPCService();
private static WindowService window = new WindowService();

@Override
public void initialize(URL url, ResourceBundle rb) {
/* Empty on purpose */
}
}
Loading