1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.desy.acop.chart.customizer;
24
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.lang.reflect.Method;
28 import java.text.Format;
29 import javax.swing.JFormattedTextField;
30 import de.desy.acop.chart.Acop;
31
32 public class AcopJFormattedTextField extends JFormattedTextField
33 {
34 Acop acopBean;
35 String proper;
36 int dataType;
37 boolean checkRange;
38 double lowCheck, highCheck;
39 Class c;
40 Object oldValue, newValue;
41 public AcopJFormattedTextField()
42 {
43 super();
44
45 }
46 public AcopJFormattedTextField(Object value)
47 {
48 super(value);
49
50 }
51 public AcopJFormattedTextField(Format format)
52 {
53 super(format);
54
55 }
56 public AcopJFormattedTextField(AbstractFormatter formatter)
57 {
58 super(formatter);
59
60 }
61 public AcopJFormattedTextField(AbstractFormatterFactory factory)
62 {
63 super(factory);
64
65 }
66 public AcopJFormattedTextField(AbstractFormatterFactory factory, Object currentValue)
67 {
68 super(factory, currentValue);
69
70 }
71 public void setAcopBean(Acop Bean)
72 {
73 acopBean = Bean;
74 try
75 {
76 Method m = acopBean.getClass().getMethod("get" + proper, null);
77 oldValue = m.invoke(acopBean, null);
78
79
80
81 setValue(oldValue);
82 setText(oldValue.toString());
83 }
84 catch (Exception ex)
85 {
86 ex.printStackTrace();
87 }
88 }
89 public AcopJFormattedTextField(String text, String property, Object value)
90 {
91 super(value);
92 initAcopJFormattedTextField(text, property, value, false, 0, 0);
93 }
94 public AcopJFormattedTextField(String text, String property, Object value, boolean check, double low,
95 double high)
96 {
97 super(value);
98 initAcopJFormattedTextField(text, property, value, check, low, high);
99 }
100 public void initAcopJFormattedTextField(String text, String property, Object value, boolean check,
101 double low, double high)
102 {
103 proper = property;
104 oldValue = value;
105 checkRange = check;
106 this.lowCheck = low;
107 this.highCheck = high;
108 setName(proper);
109 if (value.equals(int.class) || value.equals(short.class))
110 {
111 c = int.class;
112 dataType = 0;
113 }
114 else if (value.equals(float.class) || value.equals(double.class))
115 {
116 c = double.class;
117 dataType = 1;
118 }
119 else
120 {
121 c = String.class;
122 dataType = 2;
123 }
124
125 this.addPropertyChangeListener(new PropertyChangeListener()
126 {
127 public void propertyChange(PropertyChangeEvent evt)
128 {
129 String name = evt.getPropertyName();
130 if ( name.equals(proper)) {
131 return;
132 }
133 Object source = evt.getSource();
134 newValue = ((JFormattedTextField) source).getValue();
135
136
137
138
139
140
141
142 if (checkRange)
143 {
144 double conValue = Double.valueOf(newValue.toString());
145 if (conValue < lowCheck || conValue > highCheck)
146 {
147 setValue(oldValue);
148 setText(oldValue.toString());
149 }
150 }
151 if (acopBean != null && !newValue.equals(oldValue))
152 {
153 try
154 {
155 Method m = acopBean.getClass().getMethod("set" + proper, c);
156 m.invoke(acopBean, newValue);
157
158
159
160 firePropertyChange(proper, oldValue, newValue);
161
162 oldValue = newValue;
163 }
164 catch (Exception ex)
165 {
166 ex.printStackTrace();
167 }
168 }
169
170 }
171 });
172 }
173 }