Commit e97b40e899a11f372c465bea23e061c655ea86c5

Authored by Nathanael Jourdane
1 parent 68ae1315
Exists in master

Make ParamField class compatible with Java7.

src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java
... ... @@ -4,7 +4,6 @@ import java.awt.Color;
4 4 import java.awt.Dimension;
5 5 import java.awt.event.ActionEvent;
6 6 import java.awt.event.ActionListener;
7   -import java.beans.PropertyChangeEvent;
8 7 import java.text.DateFormat;
9 8 import java.text.ParseException;
10 9 import java.text.SimpleDateFormat;
... ... @@ -12,7 +11,6 @@ import java.util.ArrayList;
12 11 import java.util.HashMap;
13 12 import java.util.List;
14 13 import java.util.Locale;
15   -import java.util.Objects;
16 14  
17 15 import javax.swing.BoxLayout;
18 16 import javax.swing.JComboBox;
... ... @@ -20,12 +18,8 @@ import javax.swing.JLabel;
20 18 import javax.swing.JPanel;
21 19 import javax.swing.JTextField;
22 20 import javax.swing.SwingUtilities;
23   -import javax.swing.event.ChangeEvent;
24   -import javax.swing.event.ChangeListener;
25 21 import javax.swing.event.DocumentEvent;
26 22 import javax.swing.event.DocumentListener;
27   -import javax.swing.text.Document;
28   -import javax.swing.text.JTextComponent;
29 23  
30 24 import org.apache.logging.log4j.LogManager;
31 25 import org.apache.logging.log4j.Logger;
... ... @@ -70,17 +64,17 @@ public abstract class ParamField extends JPanel {
70 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 68 JTextField field;
75 69  
76 70 StringField(RequestView requestView, String paramName) {
77 71 super(requestView, paramName);
78 72 field = new JTextField();
79   - addChangeListener(field, e -> onUpdate());
  73 + addChangeListener(this, field);
80 74 this.add(field);
81 75 }
82 76  
83   - private void onUpdate() {
  77 + public void update(JTextField field) {
84 78 if ("".equals(field.getText())) {
85 79 requestView.updateParam(paramName, null);
86 80 } else {
... ... @@ -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 87 JTextField field;
94 88  
95 89 FloatField(RequestView requestView, String paramName) {
96 90 super(requestView, paramName);
97 91 field = new JTextField();
98   - addChangeListener(field, e -> onUpdate());
  92 + addChangeListener(this, field);
99 93 this.add(field);
100 94 }
101 95  
102   - private void onUpdate() {
  96 + public void update(JTextField field) {
103 97 if ("".equals(field.getText())) {
104 98 field.setBackground(Color.WHITE);
105 99 requestView.updateParam(paramName, null);
... ... @@ -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 112 JTextField fieldMin;
119 113 JTextField fieldMax;
120 114  
... ... @@ -122,27 +116,29 @@ public abstract class ParamField extends JPanel {
122 116 super(requestView, paramName);
123 117 this.add(new JLabel("min "));
124 118 fieldMin = new JTextField();
  119 + fieldMin.setName(MIN_SUFFIX);
125 120 fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
126   - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX));
  121 + addChangeListener(this, fieldMin);
127 122 this.add(fieldMin);
128 123  
129 124 this.add(new JLabel("max "));
130 125 fieldMax = new JTextField();
  126 + fieldMax.setName(MAX_SUFFIX);
131 127 fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
132   - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX));
  128 + addChangeListener(this, fieldMin);
133 129 this.add(fieldMax);
134 130 }
135 131  
136   - private void onUpdate(JTextField field, String suffix) {
  132 + public void update(JTextField field) {
137 133 DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
138 134 if ("".equals(field.getText())) {
139 135 field.setBackground(Color.WHITE);
140   - requestView.updateParam(paramName + suffix, null);
  136 + requestView.updateParam(paramName + field.getName(), null);
141 137 } else if (field.getText().matches(DATE_REGEX)) {
142 138 try {
143 139 long date = df.parse(field.getText()).getTime();
144 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 142 field.setBackground(Color.WHITE);
147 143 } catch (ParseException e) {
148 144 field.setBackground(Color.PINK);
... ... @@ -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 154 JTextField fieldMin;
159 155 JTextField fieldMax;
160 156  
161 157 FloatRangeField(RequestView requestView, String paramName) {
162 158 super(requestView, paramName);
163 159 fieldMin = new JTextField();
164   - addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX));
  160 + fieldMin.setName(MIN_SUFFIX);
  161 + addChangeListener(this, fieldMin);
165 162 this.add(fieldMin);
166 163  
167 164 fieldMax = new JTextField();
168   - addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX));
  165 + fieldMax.setName(MAX_SUFFIX);
  166 + addChangeListener(this, fieldMax);
169 167 this.add(fieldMax);
170 168 }
171 169  
172   - private void onUpdate(JTextField field, String suffix) {
  170 + public void update(JTextField field) {
173 171 if ("".equals(field.getText())) {
174 172 field.setBackground(Color.WHITE);
175   - requestView.updateParam(paramName + suffix, null);
  173 + requestView.updateParam(paramName + field.getName(), null);
176 174 } else {
177 175 try {
178   - requestView.updateParam(paramName + suffix, Float.parseFloat(field.getText()));
  176 + requestView.updateParam(paramName + field.getName(),
  177 + Float.parseFloat(field.getText()));
179 178 field.setBackground(Color.WHITE);
180 179 } catch (NumberFormatException e) {
181 180 field.setBackground(Color.PINK);
... ... @@ -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 187 JComboBox<String> comboBox;
189 188 JTextField field;
190 189 String lastContent;
... ... @@ -196,7 +195,7 @@ public abstract class ParamField extends JPanel {
196 195  
197 196 comboBox.setEditable(true);
198 197 field = (JTextField) comboBox.getEditor().getEditorComponent();
199   - addChangeListener(field, e -> onUpdate());
  198 + addChangeListener(this, field);
200 199 this.add(comboBox);
201 200 }
202 201  
... ... @@ -219,15 +218,21 @@ public abstract class ParamField extends JPanel {
219 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 224 comboBox.removeAllItems();
228   - for (String s : getItems(content)) {
  225 + for (String s : getItems(lastContent)) {
229 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 236 field.setText(content);
232 237 requestView.updateParam(paramName, content);
233 238 }
... ... @@ -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 331 \ No newline at end of file
... ...