-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFT.b4j
110 lines (86 loc) · 3.1 KB
/
TFT.b4j
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
Version=4.7
AppType=JavaFX
NumberOfModules=2
Module1=TextFormatter
Module2=TFChange
Build1=Default,b4j.example
NumberOfFiles=1
File1=1.bjl
NumberOfLibraries=3
Library1=jcore
Library2=jfx
Library3=javaobject
@EndOfDesignText@
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region
#LibraryAuthor: Steve Laming
#LibraryName: jTextFormatter
#LibraryVersion: 0.1
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private tfOneLine As TextField
Private tfLowerCase As TextField
Private tfNumeric As TextField
Private DecimalSeparator As String = ","
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
'Need a new textformatter object for each field, you can use the same callback(Eventname) for multiple fields.
Dim tf As TextFormatter
tf.Initialize(Me,"OneLine",tfOneLine)
Dim tf As TextFormatter
tf.Initialize(Me,"Lowercase",tfLowerCase)
Dim tf As TextFormatter
tf.Initialize(Me,"Numeric",tfNumeric)
MainForm.Show
End Sub
'from https://uwesander.de/?p=203
Sub LowerCase_TextValidator(Change As TFChange) As TFChange
Log("LowerCase")
Dim Text As String = Change.Text
'Only return this input if it is a lowercase character
If Text = "" Or Regex.IsMatch("[a-z]*",Text) Then Return Change
'Invalidates this change, it will not be applied to the input field.
Return Null
End Sub
Sub Numeric_TextValidator(Change As TFChange) As TFChange
Log("Numeric")
'Get the character that has been changed
Dim ThisChar As String = Change.Text
'Get the full text as it would be applied to the field
Dim Text As String = Change.ControlNewText
'Duplicate the full text so we can manipulate it
Dim FullText As String = Text
'Check we are using the correct decimal separator
If DecimalSeparator <> "." And ThisChar = "." Then Return Null
'Test there is only one decimal Separator
If FullText.Replace(DecimalSeparator,"").Length < FullText.Length - 1 Then Return Null
'Allow '-' and '.' at the beginning
If FullText.StartsWith("-") Or FullText.StartsWith(DecimalSeparator) Then FullText = FullText.SubString(1)
'Allow '.' at the end
If FullText.EndsWith(DecimalSeparator) Then FullText = FullText.SubString2(0,FullText.Length-1)
'IsNumber allows f and d at the end so override this
If Regex.IsMatch("[df]",ThisChar) Then Return Null
'Isnumber requires decimal separator to be ".", so change it for the next test.
FullText = FullText.Replace(DecimalSeparator,".")
'What is left should be an empty string(we have removed other characters we want to allow) or a valid number
If FullText = "" Or IsNumber(FullText) Then Return Change
'Invalidates this change, it will not be applied to the input field.
Return Null
End Sub
'
Sub OneLine_TextValidator(Change As TFChange) As TFChange
Log("OneLine")
Dim Text As String = Change.Text
'If there is more than one line, set the text to only the first one.
'Useful for managing pasted text.
If Text.Contains(CRLF) Then
Dim Pos As Int = Text.IndexOf(CRLF)
If Pos > -1 Then Change.Text = Text.SubString2(0,Pos)
End If
Return Change
End Sub