-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNumberView.java
126 lines (105 loc) · 3.11 KB
/
NumberView.java
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
/*
Written 1999 by Douglas Greiman.
This software may be used and distributed according to the terms
of the GNU Public License, incorporated herein by reference.
*/
package duggelz.jape;
import java.awt.*;
import java.awt.event.*;
public class NumberView extends TextField implements FieldView, TextListener
{
private boolean suppressEvents = false;
private String fieldName;
private boolean modified = false;
private String oldValue;
private Structure struct;
public NumberView(String fieldName, int length)
{
super(length);
this.fieldName = fieldName;
this.addTextListener(this);
}
public String getFieldName() { return this.fieldName; }
public String getValue() { return this.getText(); }
public boolean isModified() { return this.modified; }
public void setStruct(Structure struct) { this.struct = struct; }
public void textValueChanged(TextEvent event)
{
// Ignore programmatic changes
if( this.suppressEvents == true ) {
return;
}
// Get new value
String newValue = getText();
// Validate new value
try {
if( newValue.equals("") ) {
newValue = "0";
}
// Attempt to save value
this.commit(newValue);
// Success. Save new value and notify listeners
this.oldValue = newValue;
} catch( NumberFormatException xcptn) {
// Failure
this.setText(this.oldValue);
}
}
public void commit(String newValue)
{
// Look for the all structs with named field
if( this.struct != null )
{
String oldValue = struct.get(this.fieldName);
if( (oldValue == null && (newValue != null)) ||
(oldValue != null && (! oldValue.equals(newValue))) )
{
this.modified = true;
struct.set(this.fieldName, newValue);
this.fireDataChangeEvent(new DataChangeEvent(this, this.oldValue, newValue));
}
}
}
public synchronized void refresh()
{
try {
this.suppressEvents = true;
// Look for the first struct with named field
String value = null;
if( this.struct != null )
{
value = struct.get(this.fieldName);
}
// Update gui
if( value != null ) {
this.setText(value);
} else {
this.setText("");
}
this.oldValue = value;
// Update state machine
this.modified = false;
} finally {
this.suppressEvents = false;
}
}
public String toString() {
String s = "NumberView[suppressEvents = " + suppressEvents + "," +
"fieldName = " + fieldName + "," +
"modified = " + modified + "," +
"oldValue = " + oldValue + "," +
"struct = " + struct + "]";
return s;
}
// Data Change event processing
private DataChangeMixin mixin = new DataChangeMixin();
public void addDataChangeListener(DataChangeListener l) {
this.mixin.addDataChangeListener(l);
}
public void removeDataChangeListener(DataChangeListener l) {
this.mixin.removeDataChangeListener(l);
}
public void fireDataChangeEvent(DataChangeEvent e) {
this.mixin.fireDataChangeEvent(e);
}
}