-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathChoiceView.java
111 lines (92 loc) · 2.77 KB
/
ChoiceView.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
/*
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.*;
import java.util.Vector;
public class ChoiceView extends Choice implements FieldView, ItemListener
{
private boolean suppressEvents = false;
private String fieldName;
private boolean modified = false;
private String oldValue;
private Structure struct;
public ChoiceView(String fieldName, Vector choices)
{
super();
this.fieldName = fieldName;
for( int idx = 0; idx < choices.size(); ++idx ) {
this.add((String) choices.elementAt(idx));
}
this.addItemListener(this);
}
public String getFieldName() { return this.fieldName; }
public boolean isModified() { return this.modified; }
public String getValue() { return this.getSelectedItem(); }
public void setStruct(Structure struct) { this.struct = struct; }
public void itemStateChanged(ItemEvent event)
{
// Ignore programmatic changes
if( this.suppressEvents == true ) {
return;
}
// Get new value
String newValue = this.getSelectedItem();
// Attempt to save value
this.commit(newValue);
// Success. Save new value and notify listeners
this.oldValue = newValue;
}
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.select(value);
} else {
this.select(0);
}
this.oldValue = value;
// Update state machine
this.modified = false;
} finally {
this.suppressEvents = false;
}
}
// 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);
}
}