Commit e97b40e899a11f372c465bea23e061c655ea86c5
1 parent
68ae1315
Exists in
master
Make ParamField class compatible with Java7.
Showing
1 changed file
with
48 additions
and
70 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java
@@ -4,7 +4,6 @@ import java.awt.Color; | @@ -4,7 +4,6 @@ import java.awt.Color; | ||
4 | import java.awt.Dimension; | 4 | import java.awt.Dimension; |
5 | import java.awt.event.ActionEvent; | 5 | import java.awt.event.ActionEvent; |
6 | import java.awt.event.ActionListener; | 6 | import java.awt.event.ActionListener; |
7 | -import java.beans.PropertyChangeEvent; | ||
8 | import java.text.DateFormat; | 7 | import java.text.DateFormat; |
9 | import java.text.ParseException; | 8 | import java.text.ParseException; |
10 | import java.text.SimpleDateFormat; | 9 | import java.text.SimpleDateFormat; |
@@ -12,7 +11,6 @@ import java.util.ArrayList; | @@ -12,7 +11,6 @@ import java.util.ArrayList; | ||
12 | import java.util.HashMap; | 11 | import java.util.HashMap; |
13 | import java.util.List; | 12 | import java.util.List; |
14 | import java.util.Locale; | 13 | import java.util.Locale; |
15 | -import java.util.Objects; | ||
16 | 14 | ||
17 | import javax.swing.BoxLayout; | 15 | import javax.swing.BoxLayout; |
18 | import javax.swing.JComboBox; | 16 | import javax.swing.JComboBox; |
@@ -20,12 +18,8 @@ import javax.swing.JLabel; | @@ -20,12 +18,8 @@ import javax.swing.JLabel; | ||
20 | import javax.swing.JPanel; | 18 | import javax.swing.JPanel; |
21 | import javax.swing.JTextField; | 19 | import javax.swing.JTextField; |
22 | import javax.swing.SwingUtilities; | 20 | import javax.swing.SwingUtilities; |
23 | -import javax.swing.event.ChangeEvent; | ||
24 | -import javax.swing.event.ChangeListener; | ||
25 | import javax.swing.event.DocumentEvent; | 21 | import javax.swing.event.DocumentEvent; |
26 | import javax.swing.event.DocumentListener; | 22 | import javax.swing.event.DocumentListener; |
27 | -import javax.swing.text.Document; | ||
28 | -import javax.swing.text.JTextComponent; | ||
29 | 23 | ||
30 | import org.apache.logging.log4j.LogManager; | 24 | import org.apache.logging.log4j.LogManager; |
31 | import org.apache.logging.log4j.Logger; | 25 | import org.apache.logging.log4j.Logger; |
@@ -70,17 +64,17 @@ public abstract class ParamField extends JPanel { | @@ -70,17 +64,17 @@ public abstract class ParamField extends JPanel { | ||
70 | this.paramName = paramName; | 64 | this.paramName = paramName; |
71 | } | 65 | } |
72 | 66 | ||
73 | - public static class StringField extends ParamField { | 67 | + public static class StringField extends ParamField implements TextFieldListener { |
74 | JTextField field; | 68 | JTextField field; |
75 | 69 | ||
76 | StringField(RequestView requestView, String paramName) { | 70 | StringField(RequestView requestView, String paramName) { |
77 | super(requestView, paramName); | 71 | super(requestView, paramName); |
78 | field = new JTextField(); | 72 | field = new JTextField(); |
79 | - addChangeListener(field, e -> onUpdate()); | 73 | + addChangeListener(this, field); |
80 | this.add(field); | 74 | this.add(field); |
81 | } | 75 | } |
82 | 76 | ||
83 | - private void onUpdate() { | 77 | + public void update(JTextField field) { |
84 | if ("".equals(field.getText())) { | 78 | if ("".equals(field.getText())) { |
85 | requestView.updateParam(paramName, null); | 79 | requestView.updateParam(paramName, null); |
86 | } else { | 80 | } else { |
@@ -89,17 +83,17 @@ public abstract class ParamField extends JPanel { | @@ -89,17 +83,17 @@ public abstract class ParamField extends JPanel { | ||
89 | } | 83 | } |
90 | } | 84 | } |
91 | 85 | ||
92 | - public static class FloatField extends ParamField { | 86 | + public static class FloatField extends ParamField implements TextFieldListener { |
93 | JTextField field; | 87 | JTextField field; |
94 | 88 | ||
95 | FloatField(RequestView requestView, String paramName) { | 89 | FloatField(RequestView requestView, String paramName) { |
96 | super(requestView, paramName); | 90 | super(requestView, paramName); |
97 | field = new JTextField(); | 91 | field = new JTextField(); |
98 | - addChangeListener(field, e -> onUpdate()); | 92 | + addChangeListener(this, field); |
99 | this.add(field); | 93 | this.add(field); |
100 | } | 94 | } |
101 | 95 | ||
102 | - private void onUpdate() { | 96 | + public void update(JTextField field) { |
103 | if ("".equals(field.getText())) { | 97 | if ("".equals(field.getText())) { |
104 | field.setBackground(Color.WHITE); | 98 | field.setBackground(Color.WHITE); |
105 | requestView.updateParam(paramName, null); | 99 | requestView.updateParam(paramName, null); |
@@ -114,7 +108,7 @@ public abstract class ParamField extends JPanel { | @@ -114,7 +108,7 @@ public abstract class ParamField extends JPanel { | ||
114 | } | 108 | } |
115 | } | 109 | } |
116 | 110 | ||
117 | - public static class DateRangeField extends ParamField { | 111 | + public static class DateRangeField extends ParamField implements TextFieldListener { |
118 | JTextField fieldMin; | 112 | JTextField fieldMin; |
119 | JTextField fieldMax; | 113 | JTextField fieldMax; |
120 | 114 | ||
@@ -122,27 +116,29 @@ public abstract class ParamField extends JPanel { | @@ -122,27 +116,29 @@ public abstract class ParamField extends JPanel { | ||
122 | super(requestView, paramName); | 116 | super(requestView, paramName); |
123 | this.add(new JLabel("min ")); | 117 | this.add(new JLabel("min ")); |
124 | fieldMin = new JTextField(); | 118 | fieldMin = new JTextField(); |
119 | + fieldMin.setName(MIN_SUFFIX); | ||
125 | fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); | 120 | fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); |
126 | - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); | 121 | + addChangeListener(this, fieldMin); |
127 | this.add(fieldMin); | 122 | this.add(fieldMin); |
128 | 123 | ||
129 | this.add(new JLabel("max ")); | 124 | this.add(new JLabel("max ")); |
130 | fieldMax = new JTextField(); | 125 | fieldMax = new JTextField(); |
126 | + fieldMax.setName(MAX_SUFFIX); | ||
131 | fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); | 127 | fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT)); |
132 | - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); | 128 | + addChangeListener(this, fieldMin); |
133 | this.add(fieldMax); | 129 | this.add(fieldMax); |
134 | } | 130 | } |
135 | 131 | ||
136 | - private void onUpdate(JTextField field, String suffix) { | 132 | + public void update(JTextField field) { |
137 | DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH); | 133 | DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH); |
138 | if ("".equals(field.getText())) { | 134 | if ("".equals(field.getText())) { |
139 | field.setBackground(Color.WHITE); | 135 | field.setBackground(Color.WHITE); |
140 | - requestView.updateParam(paramName + suffix, null); | 136 | + requestView.updateParam(paramName + field.getName(), null); |
141 | } else if (field.getText().matches(DATE_REGEX)) { | 137 | } else if (field.getText().matches(DATE_REGEX)) { |
142 | try { | 138 | try { |
143 | long date = df.parse(field.getText()).getTime(); | 139 | long date = df.parse(field.getText()).getTime(); |
144 | date = (Math.round((date / 86400000.0) + 2440587.5)); // to JD | 140 | date = (Math.round((date / 86400000.0) + 2440587.5)); // to JD |
145 | - requestView.updateParam(paramName + suffix, date); | 141 | + requestView.updateParam(paramName + field.getName(), date); |
146 | field.setBackground(Color.WHITE); | 142 | field.setBackground(Color.WHITE); |
147 | } catch (ParseException e) { | 143 | } catch (ParseException e) { |
148 | field.setBackground(Color.PINK); | 144 | field.setBackground(Color.PINK); |
@@ -154,28 +150,31 @@ public abstract class ParamField extends JPanel { | @@ -154,28 +150,31 @@ public abstract class ParamField extends JPanel { | ||
154 | } | 150 | } |
155 | } | 151 | } |
156 | 152 | ||
157 | - public static class FloatRangeField extends ParamField { | 153 | + public static class FloatRangeField extends ParamField implements TextFieldListener { |
158 | JTextField fieldMin; | 154 | JTextField fieldMin; |
159 | JTextField fieldMax; | 155 | JTextField fieldMax; |
160 | 156 | ||
161 | FloatRangeField(RequestView requestView, String paramName) { | 157 | FloatRangeField(RequestView requestView, String paramName) { |
162 | super(requestView, paramName); | 158 | super(requestView, paramName); |
163 | fieldMin = new JTextField(); | 159 | fieldMin = new JTextField(); |
164 | - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); | 160 | + fieldMin.setName(MIN_SUFFIX); |
161 | + addChangeListener(this, fieldMin); | ||
165 | this.add(fieldMin); | 162 | this.add(fieldMin); |
166 | 163 | ||
167 | fieldMax = new JTextField(); | 164 | fieldMax = new JTextField(); |
168 | - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); | 165 | + fieldMax.setName(MAX_SUFFIX); |
166 | + addChangeListener(this, fieldMax); | ||
169 | this.add(fieldMax); | 167 | this.add(fieldMax); |
170 | } | 168 | } |
171 | 169 | ||
172 | - private void onUpdate(JTextField field, String suffix) { | 170 | + public void update(JTextField field) { |
173 | if ("".equals(field.getText())) { | 171 | if ("".equals(field.getText())) { |
174 | field.setBackground(Color.WHITE); | 172 | field.setBackground(Color.WHITE); |
175 | - requestView.updateParam(paramName + suffix, null); | 173 | + requestView.updateParam(paramName + field.getName(), null); |
176 | } else { | 174 | } else { |
177 | try { | 175 | try { |
178 | - requestView.updateParam(paramName + suffix, Float.parseFloat(field.getText())); | 176 | + requestView.updateParam(paramName + field.getName(), |
177 | + Float.parseFloat(field.getText())); | ||
179 | field.setBackground(Color.WHITE); | 178 | field.setBackground(Color.WHITE); |
180 | } catch (NumberFormatException e) { | 179 | } catch (NumberFormatException e) { |
181 | field.setBackground(Color.PINK); | 180 | field.setBackground(Color.PINK); |
@@ -184,7 +183,7 @@ public abstract class ParamField extends JPanel { | @@ -184,7 +183,7 @@ public abstract class ParamField extends JPanel { | ||
184 | } | 183 | } |
185 | } | 184 | } |
186 | 185 | ||
187 | - public static class TargetNameField extends ParamField { | 186 | + public static class TargetNameField extends ParamField implements TextFieldListener { |
188 | JComboBox<String> comboBox; | 187 | JComboBox<String> comboBox; |
189 | JTextField field; | 188 | JTextField field; |
190 | String lastContent; | 189 | String lastContent; |
@@ -196,7 +195,7 @@ public abstract class ParamField extends JPanel { | @@ -196,7 +195,7 @@ public abstract class ParamField extends JPanel { | ||
196 | 195 | ||
197 | comboBox.setEditable(true); | 196 | comboBox.setEditable(true); |
198 | field = (JTextField) comboBox.getEditor().getEditorComponent(); | 197 | field = (JTextField) comboBox.getEditor().getEditorComponent(); |
199 | - addChangeListener(field, e -> onUpdate()); | 198 | + addChangeListener(this, field); |
200 | this.add(comboBox); | 199 | this.add(comboBox); |
201 | } | 200 | } |
202 | 201 | ||
@@ -219,15 +218,21 @@ public abstract class ParamField extends JPanel { | @@ -219,15 +218,21 @@ public abstract class ParamField extends JPanel { | ||
219 | return targetNames; | 218 | return targetNames; |
220 | } | 219 | } |
221 | 220 | ||
222 | - private void onUpdate() { | ||
223 | - String content = field.getText(); | ||
224 | - if (content.length() >= 2 && !content.equals(lastContent)) { | ||
225 | - lastContent = content; | ||
226 | - int nbItems = comboBox.getItemCount(); | 221 | + Runnable updateComboBox = new Runnable() { |
222 | + @Override | ||
223 | + public void run() { | ||
227 | comboBox.removeAllItems(); | 224 | comboBox.removeAllItems(); |
228 | - for (String s : getItems(content)) { | 225 | + for (String s : getItems(lastContent)) { |
229 | comboBox.addItem(s); | 226 | comboBox.addItem(s); |
230 | } | 227 | } |
228 | + } | ||
229 | + }; | ||
230 | + | ||
231 | + public void update(JTextField field) { | ||
232 | + String content = field.getText(); | ||
233 | + if (content.length() >= 2 && !content.equals(lastContent)) { | ||
234 | + lastContent = content; | ||
235 | + SwingUtilities.invokeLater(updateComboBox); | ||
231 | field.setText(content); | 236 | field.setText(content); |
232 | requestView.updateParam(paramName, content); | 237 | requestView.updateParam(paramName, content); |
233 | } | 238 | } |
@@ -303,50 +308,23 @@ public abstract class ParamField extends JPanel { | @@ -303,50 +308,23 @@ public abstract class ParamField extends JPanel { | ||
303 | } | 308 | } |
304 | } | 309 | } |
305 | 310 | ||
306 | - // public class EnumParamField extends ParamField { | ||
307 | - // EnumParamField() { | ||
308 | - // super(); | ||
309 | - // } | ||
310 | - // } | ||
311 | - | ||
312 | - public static void addChangeListener(JTextComponent text, ChangeListener changeListener) { | ||
313 | - Objects.requireNonNull(text); | ||
314 | - Objects.requireNonNull(changeListener); | ||
315 | - DocumentListener dl = new DocumentListener() { | ||
316 | - private int lastChange = 0, lastNotifiedChange = 0; | 311 | + interface TextFieldListener { |
312 | + void update(JTextField field); | ||
313 | + } | ||
317 | 314 | ||
318 | - @Override | ||
319 | - public void insertUpdate(DocumentEvent e) { | ||
320 | - changedUpdate(e); | 315 | + static void addChangeListener(TextFieldListener changeListener, JTextField field) { |
316 | + field.getDocument().addDocumentListener(new DocumentListener() { | ||
317 | + public void removeUpdate(DocumentEvent de) { | ||
318 | + changeListener.update(field); | ||
321 | } | 319 | } |
322 | 320 | ||
323 | - @Override | ||
324 | - public void removeUpdate(DocumentEvent e) { | ||
325 | - changedUpdate(e); | 321 | + public void insertUpdate(DocumentEvent de) { |
322 | + changeListener.update(field); | ||
326 | } | 323 | } |
327 | 324 | ||
328 | - @Override | ||
329 | - public void changedUpdate(DocumentEvent e) { | ||
330 | - lastChange++; | ||
331 | - SwingUtilities.invokeLater(() -> { | ||
332 | - if (lastNotifiedChange != lastChange) { | ||
333 | - lastNotifiedChange = lastChange; | ||
334 | - changeListener.stateChanged(new ChangeEvent(text)); | ||
335 | - } | ||
336 | - }); | 325 | + public void changedUpdate(DocumentEvent de) { |
326 | + changeListener.update(field); | ||
337 | } | 327 | } |
338 | - }; | ||
339 | - text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> { | ||
340 | - Document d1 = (Document) e.getOldValue(); | ||
341 | - Document d2 = (Document) e.getNewValue(); | ||
342 | - if (d1 != null) | ||
343 | - d1.removeDocumentListener(dl); | ||
344 | - if (d2 != null) | ||
345 | - d2.addDocumentListener(dl); | ||
346 | - dl.changedUpdate(null); | ||
347 | }); | 328 | }); |
348 | - Document d = text.getDocument(); | ||
349 | - if (d != null) | ||
350 | - d.addDocumentListener(dl); | ||
351 | } | 329 | } |
352 | } | 330 | } |
353 | \ No newline at end of file | 331 | \ No newline at end of file |