/
CreateNumericEventDialog.scala
197 lines (159 loc) · 5.74 KB
/
CreateNumericEventDialog.scala
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
package com.krystal.bull.gui.dialog
import com.krystal.bull.gui.GUIUtil.numberFormatter
import com.krystal.bull.gui.home.InitEventParams
import com.krystal.bull.gui.{GUIUtil, GlobalData, KrystalBullUtil}
import org.bitcoins.core.number._
import org.bitcoins.core.protocol.tlv.DigitDecompositionEventDescriptorV0TLV
import org.bitcoins.core.util.TimeUtil
import scalafx.Includes._
import scalafx.geometry.{Insets, Pos}
import scalafx.scene.control._
import scalafx.scene.layout.{GridPane, HBox}
import scalafx.stage.Window
import scalafx.util.StringConverter
import java.util.Date
object CreateNumericEventDialog {
def showAndWait(
parentWindow: Window,
initParamsOpt: Option[InitEventParams]): Option[InitEventParams] = {
val descriptorOpt =
initParamsOpt.map(
_.descriptorTLV.asInstanceOf[DigitDecompositionEventDescriptorV0TLV])
val titleStr = initParamsOpt match {
case Some(value) => s"Clone of ${value.sanitizedEventName}"
case None => "Create New Numeric Event"
}
val dialog = new Dialog[Option[InitEventParams]]() {
initOwner(parentWindow)
title = titleStr
}
dialog.dialogPane().buttonTypes = Seq(ButtonType.OK, ButtonType.Cancel)
dialog.dialogPane().stylesheets = GlobalData.currentStyleSheets
dialog.resizable = true
val eventNameTF = new TextField() {
text = initParamsOpt.map(_.sanitizedEventName).getOrElse("")
minWidth = 300
}
val appendDateCheckBox = new CheckBox() {
alignmentInParent = Pos.CenterRight
selected = initParamsOpt.exists(_.hasAppendedDate)
}
val datePicker: DatePicker = new DatePicker() {
minWidth = 300
}
val hourPicker = new ComboBox[Int](1.to(12)) {
value = initParamsOpt.map(_.hour).getOrElse(12)
}
val minutePicker = new ComboBox[Int](0.to(59)) {
value = initParamsOpt.map(_.minute).getOrElse(0)
converter = new StringConverter[Int] {
override def fromString(string: String): Int = string.toInt
override def toString(t: Int): String = {
if (t < 10) s"0$t"
else t.toString
}
}
}
val amOrPmPicker = new ComboBox[String](Vector("AM", "PM")) {
value = initParamsOpt.map(_.isAM) match {
case Some(true) | None => "AM"
case Some(false) => "PM"
}
}
val maxTF = new TextField() {
text = descriptorOpt.map(_.maxNum.toString()).getOrElse("")
}
GUIUtil.setNumericInput(maxTF)
val isSignedCheckBox = new CheckBox() {
alignmentInParent = Pos.Center
selected = descriptorOpt.exists(_.minNum != 0)
}
val unitTF = new TextField() {
text = descriptorOpt.map(_.unit.normStr).getOrElse("")
}
val precisionTF = new TextField() {
text = descriptorOpt.map(_.precision.toLong.toString).getOrElse("0")
}
GUIUtil.setNumericInput(precisionTF)
dialog.dialogPane().content = new GridPane {
padding = Insets(top = 10, right = 10, bottom = 10, left = 10)
hgap = 10
vgap = 10
var row = 0
add(new Label("Event Name"), 0, row)
add(eventNameTF, 1, row)
if (GlobalData.advancedMode) {
row += 1
add(new Label("Append date to name"), 0, row)
add(appendDateCheckBox, 1, row)
}
row += 1
add(new Label("Maturity Date"), 0, row)
add(datePicker, 1, row)
if (GlobalData.advancedMode) {
row += 1
add(new Label("Maturity Time"), 0, row)
val hbox = new HBox() {
alignment = Pos.Center
spacing = 10
children =
Vector(hourPicker, minutePicker, amOrPmPicker, new Label("UTC"))
}
add(hbox, 1, row)
}
row += 1
add(new Label("Max Outcome"), 0, row)
add(maxTF, 1, row)
row += 1
add(new Label("Include Negatives"), 0, row)
add(isSignedCheckBox, 1, row)
row += 1
add(new Label("Units"), 0, row)
add(unitTF, 1, row)
row += 1
add(new Label("Precision"), 0, row)
add(precisionTF, 1, row)
}
// Enable/Disable OK button depending on whether all data was entered.
val okButton = dialog.dialogPane().lookupButton(ButtonType.OK)
// Simple validation that sufficient data was entered
okButton.disable <== eventNameTF.text.isEmpty
// When the OK button is clicked, convert the result to a T.
dialog.resultConverter = dialogButton =>
if (dialogButton == ButtonType.OK) {
val maturityDate = KrystalBullUtil.toInstant(
datePicker = datePicker,
hourPicker = hourPicker,
minutePicker = minutePicker,
amOrPmPicker = amOrPmPicker)
val eventName = {
val name = eventNameTF.text.value
if (appendDateCheckBox.selected.value) {
val timeStr = TimeUtil.iso8601ToString(Date.from(maturityDate))
s"$name $timeStr"
} else name
}
val maxNumber = numberFormatter.parse(maxTF.text.value).longValue()
// log 2 of maxNumber gives us how many base 2 digits are needed
val digitsNeeded =
Math.ceil(Math.log(maxNumber) / Math.log(2)).toInt
val isSigned = isSignedCheckBox.selected.value
val unit = unitTF.text.value
val precision =
numberFormatter.parse(precisionTF.text.value).longValue()
val descriptor = DigitDecompositionEventDescriptorV0TLV(
base = UInt16(2),
isSigned = isSigned,
numDigits = digitsNeeded,
unit = unit,
precision = Int32(precision))
val params = InitEventParams(eventName, maturityDate, descriptor)
Some(params)
} else None
dialog.showAndWait() match {
case Some(Some(params: InitEventParams)) =>
Some(params)
case Some(_) | None => None
}
}
}