/
FactEditor.scala
181 lines (151 loc) · 6.44 KB
/
FactEditor.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
package org.narrativeandplay.hypedyn.dialogs
import javafx.scene.{control => jfxsc}
import scala.util.Try
import scalafx.Includes._
import scalafx.collections.ObservableBuffer
import scalafx.scene.control._
import scalafx.stage.{Modality, Window}
import scalafx.util.StringConverter
import scalafx.util.StringConverter.sfxStringConverter2jfx
import org.tbee.javafx.scene.layout.MigPane
import org.narrativeandplay.hypedyn.story.rules._
/**
* Dialog for editing facts
*
* @param dialogTitle The title of the dialog
* @param availableFactTypes The fact types available for creation
* @param factToEdit An option containing the fact to edit, or None if a new fact is to be created
* @param ownerWindow The parent window of the dialog, for inheriting icons
*/
class FactEditor private (dialogTitle: String,
availableFactTypes: List[String],
factToEdit: Option[Fact],
ownerWindow: Window) extends Dialog[Fact] {
/**
* Creates a new dialog for creating a fact
*
* @param dialogTitle The title of the dialog
* @param availableFactTypes The fact types available for creation
* @param ownerWindow The parent window of the dialog, for inheriting icons
* @return A new fact dialog
*/
def this(dialogTitle: String, availableFactTypes: List[String], ownerWindow: Window) =
this(dialogTitle, availableFactTypes, None, ownerWindow)
/**
* Creates a new dialog for editing a fact
*
* @param dialogTitle The title of the dialog
* @param availableFactTypes The fact types available for creation
* @param factToEdit The fact to edit
* @param ownerWindow The parent window of the dialog, for inheriting icons
* @return A new fact dialog
*/
def this(dialogTitle: String, availableFactTypes: List[String], factToEdit: Fact, ownerWindow: Window) =
this(dialogTitle, availableFactTypes, Some(factToEdit), ownerWindow)
title = dialogTitle
headerText = None
initOwner(ownerWindow)
// HACK: make fact editor always on top
// Possibly due to some touch screen issues
dialogPane().scene().window().asInstanceOf[javafx.stage.Stage].alwaysOnTop = true
dialogPane().buttonTypes.addAll(ButtonType.OK, ButtonType.Cancel)
val okButton = dialogPane().lookupButton(ButtonType.OK)
private val factNameField = new TextField()
private val stringFactInitValueField = new TextField()
private val integerFactInitValueField = new Spinner[BigInt]() {
editable = true
valueFactory = new jfxsc.SpinnerValueFactory[BigInt]() {
setValue(0)
setConverter(new StringConverter[BigInt] {
override def fromString(string: String): BigInt = Try(BigInt(string)) getOrElse BigInt(0)
override def toString(t: BigInt): String = t.toString()
})
override def increment(steps: Int): Unit = setValue(getValue + steps)
override def decrement(steps: Int): Unit = setValue(getValue - steps)
}
editor().text onChange { (_, _, _) =>
valueFactory().value = valueFactory().converter().fromString(editor().text())
}
}
private val booleanFactInitValueField = new ComboBox[Boolean]() {
items = ObservableBuffer(true, false)
value = false
}
private val factTypesField = new ComboBox[String]() {
promptText = "Choose a fact type"
items = ObservableBuffer(availableFactTypes)
factToEdit foreach {
case _: StringFact => value = Fact.StringFact
case _: IntegerFact => value = Fact.IntegerFact
case _: BooleanFact => value = Fact.BooleanFact
case f => throw new IllegalArgumentException(s"Unknown or not enabled fact type: $f")
}
}
private val initValLabel = new Label("Initial value: ")
private val contentPane: MigPane = new MigPane("fill") {
add(new Label("Fact Type: "))
add(factTypesField, "wrap")
add(new Label("Name: "))
add(factNameField, "wrap")
}
dialogPane().content = contentPane
factTypesField.onAction = { _ =>
if (!contentPane.getChildren.contains(initValLabel)) {
contentPane.add(initValLabel)
}
contentPane.remove(integerFactInitValueField)
contentPane.remove(booleanFactInitValueField)
contentPane.remove(stringFactInitValueField)
factTypesField.value() match {
case Fact.StringFact => contentPane.add(stringFactInitValueField)
case Fact.IntegerFact => contentPane.add(integerFactInitValueField)
case Fact.BooleanFact => contentPane.add(booleanFactInitValueField)
case _ => println("error")
}
dialogPane().getScene.getWindow.sizeToScene()
}
factToEdit foreach { f =>
contentPane.add(initValLabel)
factNameField.text = f.name
f match {
case s: StringFact =>
contentPane.add(stringFactInitValueField)
stringFactInitValueField.text = s.initialValue
case i: IntegerFact =>
contentPane.add(integerFactInitValueField)
integerFactInitValueField.valueFactory().value = i.initalValue
case b: BooleanFact =>
contentPane.add(booleanFactInitValueField)
booleanFactInitValueField.value = b.initialValue
case unknown => throw new IllegalArgumentException(s"Unenabled fact type: $unknown")
}
}
okButton.disable <== factTypesField.selectionModel().selectedItem.isNull || factNameField.text.isEmpty
resultConverter = {
case ButtonType.OK =>
factTypesField.value() match {
case Fact.StringFact => StringFact(factToEdit map (_.id) getOrElse FactId(-1),
factNameField.text(),
stringFactInitValueField.text())
case Fact.IntegerFact => IntegerFact(factToEdit map (_.id) getOrElse FactId(-1),
factNameField.text(),
integerFactInitValueField.value())
case Fact.BooleanFact => BooleanFact(factToEdit map (_.id) getOrElse FactId(-1),
factNameField.text(),
booleanFactInitValueField.value())
case _ => throw new IllegalArgumentException("Invalid fact type")
}
case _ => null
}
/**
* Shows a blocking fact editor dialog
*
* @return An option containing the result of the dialog, or None if the dialog was not closed using
* the OK button
*/
def showAndWait(): Option[Fact] = {
initModality(Modality.ApplicationModal)
val result = delegate.showAndWait()
if (result.isPresent) Some(result.get()) else None
}
}