/
TextFormatter.scala
276 lines (247 loc) · 11 KB
/
TextFormatter.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
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
/*
* Copyright (c) 2011-2014, ScalaFX Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the ScalaFX Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package scalafx.scene.control
import java.util.function.UnaryOperator
import javafx.scene.{control => jfxsc}
import javafx.{util => jfxu}
import scala.language.implicitConversions
import scalafx.Includes._
import scalafx.beans.property.ObjectProperty
import scalafx.delegate.SFXDelegate
import scalafx.util.StringConverter
object TextFormatter {
/**
* Converts a ScalaFX TextFormatter to its JavaFX counterpart.
*
* @param v ScalaFX TextFormatter
* @return JavaFX TextFormatter
*/
implicit def sfxTextFormatter2jfx[V](v: TextFormatter[V]): jfxsc.TextFormatter[V] =
if (v != null) v.delegate else null
/**
* This string converter converts the text to the same String value. This might be useful for cases where you
* want to manipulate with the text through the value or you need to provide a default text value.
*/
val IdentityStringConverter: StringConverter[String] = jfxsc.TextFormatter.IDENTITY_STRING_CONVERTER
object Change {
implicit def sfxTextFormatterChange2jfx[V](v: Change): jfxsc.TextFormatter.Change =
if (v != null) v.delegate else null
}
/**
* Contains the state representing a change in the content or selection for a
* `TextInputControl`.
*
* Wraps a JavaFX [[https://docs.oracle.com/javase/8/javafx/api/javafx/scalafx/scene/control/TextFormatter.Change.html]]
*/
class Change(override val delegate: jfxsc.TextFormatter.Change)
extends SFXDelegate[jfxsc.TextFormatter.Change] {
/**
* Gets the control associated with this change.
* @return The control associated with this change. This will never be null.
*/
def control: Control = delegate.getControl
/**
* Gets the start index into the `TextInputControl.text`
* for the modification. This will always be a value > 0 and
* <= `TextInputControl.length`.
*
* @return The start index
*/
def rangeStart: Int = delegate.getRangeStart
/**
* Gets the end index into the `TextInputControl.text`
* for the modification. This will always be a value > `angeStart` and
* <= `TextInputControl.length`.
*
* @return The end index
*/
def rangeEnd: Int = delegate.getRangeEnd
/**
* Gets the new caret position. This value will always be > 0 and
* <= `controlNewText.Length`
*
* @return The new caret position
*/
def caretPosition: Int = delegate.getCaretPosition
/**
* Sets the caret position. The caret position value must be > 0 and
* <= `controlNewText.Length`. Note that there
* is an order dependence here, in that the position should be
* specified after the new text has been specified.
*
* @param newCaretPosition The new caret position
*/
def caretPosition_=(newCaretPosition: Int): Unit = { delegate.setCaretPosition(newCaretPosition) }
/**
* Gets the new anchor. This value will always be > 0 and
* <= `controlNewText.Length`
*
* @return The new anchor position
*/
def anchor: Int = delegate.getAnchor
/**
* Sets the anchor. The anchor value must be > 0 and
* <= `controlNewText.Length`. Note that there
* is an order dependence here, in that the position should be
* specified after the new text has been specified.
*
* @param newAnchor The new anchor position
*/
def anchor_=(newAnchor: Int): Unit = { delegate.setAnchor(newAnchor) }
/**
* Gets the current caret position of the control.
* @return The previous caret position
*/
def controlCaretPosition: Int = delegate.getControlCaretPosition
/**
* Gets the current anchor position of the control.
* @return The previous anchor
*/
def controlAnchor: Int = delegate.getControlAnchor
/**
* Gets the selection of this change. Note that the selection range refers to `controlNewText`, not
* the current control text.
* @return The selected range of this change.
*/
def selection: IndexRange = delegate.getSelection
/**
* Gets the text used in this change. For example, this may be new
* text being added, or text which is replacing all the control's text
* within the range of start and end. Typically it is an empty string
* only for cases where the range is being deleted.
*
* @return The text involved in this change. This will never be null.
*/
def text: String = delegate.getText
/**
* Sets the text to use in this change. This is used to replace the
* range from start to end, if such a range exists, or to insert text
* at the position represented by start == end.
*
* @param value The text. This cannot be null.
*/
def text_=(value: String): Unit = { delegate.setText(value) }
/**
* This is the full text that control has before the change. To get the text
* after this change, use `controlNewText`.
* @return the previous text of control
*/
def controlText: String = delegate.getControlText
/**
* Gets the complete new text which will be used on the control after
* this change. Note that some controls (such as TextField) may do further
* filtering after the change is made (such as stripping out newlines)
* such that you cannot assume that the newText will be exactly the same
* as what is finally set as the content on the control, however it is
* correct to assume that this is the case for the purpose of computing
* the new caret position and new anchor position (as those values supplied
* will be modified as necessary after the control has stripped any
* additional characters that the control might strip).
*
* @return The controls proposed new text at the time of this call, according
* to the state set for start, end, and text properties on this Change object.
*/
def controlNewText: String = delegate.getControlNewText
}
private def toUnaryOperator(op: (TextFormatter.Change) => TextFormatter.Change): UnaryOperator[jfxsc.TextFormatter.Change] =
new UnaryOperator[jfxsc.TextFormatter.Change] {
override def apply(a: jfxsc.TextFormatter.Change): jfxsc.TextFormatter.Change = op(new TextFormatter.Change(a))
}
}
/**
* A Formatter describes a format of a `TextInputControl` text.
*
* Wraps a $JFX $URL0 $TC]].
*
* @define TC TextFormatter
* @define URL0 [[https://docs.oracle.com/javase/8/javafx/api/javafx/scalafx/scene/control/TextFormatter.html
* @define JFX JavaFX
* @define ORIGINALDOC Original Documentation]].
*/
class TextFormatter[V](override val delegate: jfxsc.TextFormatter[V])
extends SFXDelegate[jfxsc.TextFormatter[V]] {
/**
* Creates a new Formatter with the provided filter.
* @param filter The filter to use in this formatter or null
*/
def this(filter: UnaryOperator[jfxsc.TextFormatter.Change]) =
this(delegate = new jfxsc.TextFormatter(filter))
/**
* Creates a new Formatter with the provided filter.
* @param filter The filter that can modify the change.
*/
def this(filter: (TextFormatter.Change) => TextFormatter.Change) =
this(delegate = new jfxsc.TextFormatter(TextFormatter.toUnaryOperator(filter)))
/**
* Creates a new Formatter with the provided filter, value converter and default value.
* @param valueConverter The value converter to use in this formatter or null.
* @param defaultValue the default value.
* @param filter The filter to use in this formatter or null
*/
def this(valueConverter: jfxu.StringConverter[V],
defaultValue: V,
filter: UnaryOperator[jfxsc.TextFormatter.Change]) =
this(new jfxsc.TextFormatter(valueConverter, defaultValue, filter))
/**
* Creates a new Formatter with the provided filter, value converter and default value.
* @param valueConverter The value converter to use in this formatter or null.
* @param defaultValue the default value.
* @param filter The filter that can modify the change.
*/
def this(valueConverter: jfxu.StringConverter[V],
defaultValue: V,
filter: (TextFormatter.Change) => TextFormatter.Change) =
this(new jfxsc.TextFormatter(valueConverter, defaultValue, TextFormatter.toUnaryOperator(filter)))
/**
* Creates a new Formatter with the provided value converter and default value.
* @param valueConverter The value converter to use in this formatter. This must not be null.
* @param defaultValue the default value
*/
def this(valueConverter: jfxu.StringConverter[V],
defaultValue: V) =
this(new jfxsc.TextFormatter(valueConverter, defaultValue))
/**
* Creates a new Formatter with the provided value converter. The default value will be null.
* @param valueConverter The value converter to use in this formatter. This must not be null.
*/
def this(valueConverter: StringConverter[V]) =
this(new jfxsc.TextFormatter(valueConverter))
/**
* The converter between the values and text.
*/
def valueConverter: StringConverter[V] = delegate.getValueConverter
/**
* Filter allows user to intercept and modify any change done to the text content.
*/
def filter: UnaryOperator[jfxsc.TextFormatter.Change] = delegate.getFilter
/**
* The current value for this formatter. When the formatter is set on a `TextInputControl` and has a
* `valueConverter`, the value is set by the control, when the text is committed.
*/
def value: ObjectProperty[V] = delegate.valueProperty
def value_=(v: V): Unit = { delegate.valueProperty.setValue(v) }
}