This repository was archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathMainController.java
More file actions
375 lines (301 loc) · 12.1 KB
/
MainController.java
File metadata and controls
375 lines (301 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package wallettemplate;
import javafx.animation.TranslateTransition;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TabPane;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import javafx.util.StringConverter;
import network.thunder.core.communication.layer.high.Channel;
import network.thunder.core.communication.layer.middle.broadcasting.types.PubkeyIPObject;
import network.thunder.core.database.objects.PaymentWrapper;
import network.thunder.core.etc.Constants;
import network.thunder.core.etc.Tools;
import network.thunder.core.helper.callback.SyncListener;
import network.thunder.core.helper.callback.results.NullResultCommand;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.listeners.DownloadProgressTracker;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.utils.MonetaryFormat;
import org.fxmisc.easybind.EasyBind;
import wallettemplate.controls.NotificationBarPane;
import wallettemplate.utils.BitcoinUIModel;
import wallettemplate.utils.GuiUtils;
import java.util.List;
import java.util.ResourceBundle;
/**
* Gets created auto-magically by FXMLLoader via reflection. The widget fields are set to the GUI controls they're named
* after. This class handles all the updates and event handling for the main UI.
*/
public class MainController {
public HBox controlsBox;
@FXML
private ResourceBundle resources;
@FXML
private TabPane tabPane;
@FXML
private Font x1;
@FXML
private Button receiveMoneyBtn;
@FXML
private Button sendMoneyOutBtn;
@FXML
private ListView blockchainTxList;
@FXML
private Label balance;
@FXML
private Color x2;
@FXML
private Label balance1;
@FXML
private Color x22;
@FXML
private Font x11;
@FXML
private Button openChannel;
@FXML
private Button thunderReceiveMoneyBtn;
@FXML
private Button thunderSendMoneyOutBtn;
@FXML
private Button thunderRefreshBtn;
@FXML
private Button startListenButton;
@FXML
private Button fetchIPButton;
@FXML
private Button syncButton;
@FXML
private ListView nodesList;
@FXML
private ListView channelNetworkList;
@FXML
private ListView channelList;
@FXML
private ListView thunderTxListIncluded;
@FXML
private ListView thunderTxListSettled;
@FXML
private ListView thunderTxListOpen;
@FXML
private ListView thunderTxListRefunded;
@FXML
private Label thunderBalance;
@FXML
private Label blockchainBalance;
@FXML
private Color x21;
@FXML
private Color x211;
@FXML
void fetchIPs (ActionEvent event) {
Main.thunderContext.fetchNetworkIPs(new NullResultCommand());
}
@FXML
void startListen (ActionEvent event) {
Main.thunderContext.startListening(new NullResultCommand());
}
@FXML
void syncThunder (ActionEvent event) {
if (selectedNode != null) {
Main.thunderContext.openChannel(selectedNode.pubkey, new NullResultCommand());
}
}
@FXML
void onReloadButton (ActionEvent event) {
Main.thunderContext.getSyncData(new SyncListener() {
@Override
public void onSegmentSynced (int segment) {
System.out.println("Segment synced: " + segment);
}
@Override
public void onSyncFinished () {
System.out.println("MainController.onSyncFinished");
}
@Override
public void onSyncFailed () {
System.out.println("MainController.onSyncFailed");
}
});
}
@FXML
void refresh (ActionEvent event) {
model.update();
}
private PubkeyIPObject selectedNode = null;
private BitcoinUIModel model = new BitcoinUIModel();
private NotificationBarPane.Item syncItem;
// Called by FXMLLoader.
public void initialize () {
}
public void onBitcoinSetup () {
model.init();
balance.textProperty().bind(EasyBind.map(model.balanceProperty(), Coin::toFriendlyString));
// Don't let the user click send money when the wallet is empty.
sendMoneyOutBtn.disableProperty().bind(model.balanceProperty().isEqualTo(Coin.ZERO));
Bindings.bindContent(blockchainTxList.getItems(), model.transactions);
Bindings.bindContent(nodesList.getItems(), model.ipList);
Bindings.bindContent(channelNetworkList.getItems(), model.channelNetworkList);
Bindings.bindContent(channelList.getItems(), model.channelList);
Bindings.bindContent(thunderTxListIncluded.getItems(), model.transactionsThunderIncluded);
Bindings.bindContent(thunderTxListOpen.getItems(), model.transactionsThunderOpen);
Bindings.bindContent(thunderTxListRefunded.getItems(), model.transactionsThunderRefunded);
Bindings.bindContent(thunderTxListSettled.getItems(), model.transactionsThunderSettled);
thunderBalance.textProperty().bind(EasyBind.map(model.balanceThunder, Coin::toFriendlyString));
blockchainBalance.textProperty().bind(EasyBind.map(model.balance, Coin::toFriendlyString));
openChannel.textProperty().bind(model.openChannelButtonText);
thunderReceiveMoneyBtn.disableProperty().bind(model.sendReceiveButtonEnabled);
thunderSendMoneyOutBtn.disableProperty().bind(model.sendReceiveButtonEnabled);
nodesList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<PubkeyIPObject>() {
@Override
public void changed (ObservableValue<? extends PubkeyIPObject> observable, PubkeyIPObject oldValue, PubkeyIPObject newValue) {
if (newValue != null) {
selectedNode = newValue;
}
}
});
nodesList.setCellFactory(param1 -> new TextFieldListCell(new StringConverter<PubkeyIPObject>() {
@Override
public String toString (PubkeyIPObject tx) {
return tx.hostname;
}
@Override
public PubkeyIPObject fromString (String string) {
return null;
}
}));
thunderTxListIncluded.setCellFactory(param1 -> new TextFieldListCell(new StringConverter<PaymentWrapper>() {
@Override
public String toString (PaymentWrapper channel) {
String text = "";
String status = "";
if (channel.paymentData.sending) {
text += "[ -> ]";
status += channel.statusSender;
} else {
text += "[ <- ]";
status += channel.statusReceiver;
}
text += " " + Coin.valueOf(channel.paymentData.amount).toFriendlyString() + " ";
text += "<" + Tools.bytesToHex(channel.paymentData.secret.hash).substring(0, 8) + "..." + ">";
text += " [" + status + "]";
return text;
}
@Override
public PaymentWrapper fromString (String string) {
return null;
}
}));
channelList.setCellFactory(param1 -> new TextFieldListCell(new StringConverter<Channel>() {
@Override
public String toString (Channel channel) {
return Coin.valueOf(channel.channelStatus.amountServer).toFriendlyString() +
" with " + model.getHostname(Main.dbHandler.getIPObjects(), channel.nodeKeyClient.getPubKey());
}
@Override
public Channel fromString (String string) {
return null;
}
}));
blockchainTxList.setCellFactory(param1 -> new TextFieldListCell(new StringConverter<Transaction>() {
@Override
public String toString (Transaction tx) {
Coin value = tx.getValue(Main.wallet);
if (value.isPositive()) {
return tx.getConfidence().getDepthInBlocks() + " Incoming payment of " + MonetaryFormat.BTC.format(value);
} else if (value.isNegative()) {
Address address = tx.getOutput(0).getAddressFromP2PKHScript(Constants.getNetwork());
if (address == null) {
return tx.getConfidence().getDepthInBlocks() + " Outbound payment to ThunderChannel of " + value.toFriendlyString().substring(1);
}
return tx.getConfidence().getDepthInBlocks() + " Outbound payment to " + address;
}
return "Payment with id " + tx.getHash();
}
@Override
public Transaction fromString (String string) {
return null;
}
}));
blockchainTxList.setCellFactory(param -> new TextFieldListCell(new StringConverter<Transaction>() {
@Override
public String toString (Transaction tx) {
Coin value = tx.getValue(Main.wallet);
if (value.isPositive()) {
return tx.getConfidence().getDepthInBlocks() + " Incoming payment of " + MonetaryFormat.BTC.format(value);
} else if (value.isNegative()) {
Address address = tx.getOutput(0).getAddressFromP2PKHScript(Constants.getNetwork());
if (address == null) {
return tx.getConfidence().getDepthInBlocks() + " Outbound payment to ThunderChannel of " + value.toFriendlyString().substring
(1);
}
return tx.getConfidence().getDepthInBlocks() + " Outbound payment to " + address;
}
return "Payment with id " + tx.getHash();
}
@Override
public Transaction fromString (String string) {
return null;
}
}));
}
private void showBitcoinSyncMessage () {
}
public void sendMoneyOut (ActionEvent event) {
// Hide this UI and show the send money UI. This UI won't be clickable until the user dismisses send_money.
Main.instance.overlayUI("send_money_blockchain.fxml");
}
public void settingsClicked (ActionEvent event) {
Main.OverlayUI<WalletSettingsController> screen = Main.instance.overlayUI("wallet_settings.fxml");
screen.controller.initialize(null);
}
public void restoreFromSeedAnimation () {
// Buttons slide out ...
TranslateTransition leave = new TranslateTransition(Duration.millis(1200), controlsBox);
leave.setByY(80.0);
leave.play();
}
public void readyToGoAnimation () {
// Buttons slide in and clickable address appears simultaneously.
}
public DownloadProgressTracker progressBarUpdater () {
return model.getDownloadProgressTracker();
}
public void receiveMoney (ActionEvent actionEvent) {
Main.instance.overlayUI("receive_money_blockchain.fxml");
}
@FXML
void thunderReceiveMoney (ActionEvent event) {
Main.instance.overlayUI("receive_money_request.fxml");
}
@FXML
void thunderSendMoneyOut (ActionEvent event) {
Main.instance.overlayUI("send_money.fxml");
}
@FXML
void openChannel (ActionEvent event) {
if (selectedNode != null) {
List<Channel> openChannel = Main.dbHandler.getOpenChannel();
if (openChannel.size() == 0) {
Main.thunderContext.openChannel(selectedNode.pubkey, new NullResultCommand());
} else {
Main.thunderContext.closeChannel(openChannel.get(0), new NullResultCommand());
}
} else {
GuiUtils.informationalAlert("ThunderNetwork Wallet", "No node to open channel with selected..");
}
}
@FXML
void thunderRefresh (ActionEvent event) {
}
}