Commit eb48359993e25c9eb4ba8df5fbb55ad09246175b
1 parent
5ea2b82c
Exists in
master
Add missing ParamField.java and bugFixes: date format
Showing
1 changed file
with
256 additions
and
0 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java
0 → 100644
@@ -0,0 +1,256 @@ | @@ -0,0 +1,256 @@ | ||
1 | +package eu.omp.irap.vespa.epntapclient.view; | ||
2 | + | ||
3 | +import java.awt.Color; | ||
4 | +import java.awt.Dimension; | ||
5 | +import java.beans.PropertyChangeEvent; | ||
6 | +import java.text.DateFormat; | ||
7 | +import java.text.ParseException; | ||
8 | +import java.text.SimpleDateFormat; | ||
9 | +import java.util.Locale; | ||
10 | +import java.util.Objects; | ||
11 | + | ||
12 | +import javax.swing.BoxLayout; | ||
13 | +import javax.swing.JLabel; | ||
14 | +import javax.swing.JPanel; | ||
15 | +import javax.swing.JTextField; | ||
16 | +import javax.swing.SwingUtilities; | ||
17 | +import javax.swing.event.ChangeEvent; | ||
18 | +import javax.swing.event.ChangeListener; | ||
19 | +import javax.swing.event.DocumentEvent; | ||
20 | +import javax.swing.event.DocumentListener; | ||
21 | +import javax.swing.text.Document; | ||
22 | +import javax.swing.text.JTextComponent; | ||
23 | + | ||
24 | +import org.apache.logging.log4j.LogManager; | ||
25 | +import org.apache.logging.log4j.Logger; | ||
26 | + | ||
27 | +public abstract class ParamField extends JPanel { | ||
28 | + | ||
29 | + /** The logger for this class. */ | ||
30 | + private static final Logger logger = LogManager.getLogger(ParamField.class); | ||
31 | + | ||
32 | + private static final String DATE_FORMAT = "dd/MM/yyyy"; | ||
33 | + private static final String DATE_REGEX = "(^(((0[1-9]|1[0-9]|2[0-8])[\\/](0[1-9]|1[012]))|((29|30|31)[\\/](0[13578]|1[02]))|((29|30)[\\/](0[4,6,9]|11)))[\\/](19|[2-9][0-9])\\d\\d$)|(^29[\\/]02[\\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)"; | ||
34 | + | ||
35 | + private static final String MIN_SUFFIX = "min"; | ||
36 | + private static final String MAX_SUFFIX = "max"; | ||
37 | + | ||
38 | + protected static RequestView requestView; | ||
39 | + protected String paramName; | ||
40 | + | ||
41 | + public ParamField(RequestView requestView, String paramName) { | ||
42 | + super(); | ||
43 | + this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); | ||
44 | + String strLabel = paramName.replaceAll("_", " ").trim(); | ||
45 | + JLabel label = new JLabel(strLabel.substring(0, 1).toUpperCase() + strLabel.substring(1)); | ||
46 | + label.setPreferredSize(new Dimension(140, 15)); | ||
47 | + this.add(label); | ||
48 | + // TODO: Add tooltip text based on rr.table_column.column_description | ||
49 | + this.requestView = requestView; | ||
50 | + this.paramName = paramName; | ||
51 | + } | ||
52 | + | ||
53 | + public static class StringField extends ParamField { | ||
54 | + StringField(RequestView requestView, String paramName) { | ||
55 | + super(requestView, paramName); | ||
56 | + JTextField field = new JTextField(); | ||
57 | + addChangeListener(field, e -> onUpdate(field)); | ||
58 | + this.add(field); | ||
59 | + } | ||
60 | + | ||
61 | + private void onUpdate(JTextField field) { | ||
62 | + if ("".equals(field.getText())) { | ||
63 | + requestView.updateParam(paramName, null); | ||
64 | + } else { | ||
65 | + requestView.updateParam(paramName, field.getText()); | ||
66 | + } | ||
67 | + } | ||
68 | + } | ||
69 | + | ||
70 | + public static class FloatField extends ParamField { | ||
71 | + FloatField(RequestView requestView, String paramName) { | ||
72 | + super(requestView, paramName); | ||
73 | + JTextField field = new JTextField(); | ||
74 | + addChangeListener(field, e -> onUpdate(field)); | ||
75 | + this.add(field); | ||
76 | + } | ||
77 | + | ||
78 | + private void onUpdate(JTextField field) { | ||
79 | + if ("".equals(field.getText())) { | ||
80 | + field.setBackground(Color.WHITE); | ||
81 | + requestView.updateParam(paramName, null); | ||
82 | + } else { | ||
83 | + try { | ||
84 | + requestView.updateParam(paramName, Float.parseFloat(field.getText())); | ||
85 | + field.setBackground(Color.WHITE); | ||
86 | + } catch (NumberFormatException e) { | ||
87 | + field.setBackground(Color.PINK); | ||
88 | + } | ||
89 | + } | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + public static class DateRangeField extends ParamField { | ||
94 | + DateRangeField(RequestView requestView, String paramName) { | ||
95 | + super(requestView, paramName); | ||
96 | + JTextField fieldMin = new JTextField(); | ||
97 | + addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); | ||
98 | + this.add(fieldMin); | ||
99 | + | ||
100 | + JTextField fieldMax = new JTextField(); | ||
101 | + addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); | ||
102 | + this.add(fieldMax); | ||
103 | + } | ||
104 | + | ||
105 | + private void onUpdate(JTextField field, String suffix) { | ||
106 | + DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH); | ||
107 | + if ("".equals(field.getText())) { | ||
108 | + field.setBackground(Color.WHITE); | ||
109 | + requestView.updateParam(paramName + suffix, null); | ||
110 | + } else if (field.getText().matches(DATE_REGEX)) { | ||
111 | + try { | ||
112 | + long date = df.parse(field.getText()).getTime(); | ||
113 | + date = (Math.round((date / 86400000.0) + 2440587.5)); // to JD | ||
114 | + requestView.updateParam(paramName + suffix, date); | ||
115 | + field.setBackground(Color.WHITE); | ||
116 | + } catch (ParseException e) { | ||
117 | + field.setBackground(Color.PINK); | ||
118 | + } | ||
119 | + // TODO: check if date min < date max | ||
120 | + } else { | ||
121 | + field.setBackground(Color.PINK); | ||
122 | + } | ||
123 | + } | ||
124 | + } | ||
125 | + | ||
126 | + public static class FloatRangeField extends ParamField { | ||
127 | + JTextField fieldMin; | ||
128 | + JTextField fieldMax; | ||
129 | + | ||
130 | + FloatRangeField(RequestView requestView, String paramName) { | ||
131 | + super(requestView, paramName); | ||
132 | + fieldMin = new JTextField(); | ||
133 | + addChangeListener(fieldMin, e -> onUpdate(fieldMin, MIN_SUFFIX)); | ||
134 | + this.add(fieldMin); | ||
135 | + | ||
136 | + fieldMax = new JTextField(); | ||
137 | + addChangeListener(fieldMax, e -> onUpdate(fieldMax, MAX_SUFFIX)); | ||
138 | + this.add(fieldMax); | ||
139 | + } | ||
140 | + | ||
141 | + private void onUpdate(JTextField field, String suffix) { | ||
142 | + if ("".equals(field.getText())) { | ||
143 | + field.setBackground(Color.WHITE); | ||
144 | + requestView.updateParam(paramName + suffix, null); | ||
145 | + } else { | ||
146 | + try { | ||
147 | + requestView.updateParam(paramName + suffix, Float.parseFloat(field.getText())); | ||
148 | + field.setBackground(Color.WHITE); | ||
149 | + } catch (NumberFormatException e) { | ||
150 | + field.setBackground(Color.PINK); | ||
151 | + } | ||
152 | + } | ||
153 | + } | ||
154 | + } | ||
155 | + | ||
156 | + public static class TargetNameField extends ParamField { | ||
157 | + TargetNameField(RequestView requestView, String paramName) { | ||
158 | + super(requestView, paramName); | ||
159 | + JTextField field = new JTextField(); | ||
160 | + addChangeListener(field, e -> onUpdate(field)); | ||
161 | + this.add(field); | ||
162 | + } | ||
163 | + | ||
164 | + private void onUpdate(JTextField field) { | ||
165 | + // TODO: add resolver | ||
166 | + if ("".equals(field.getText())) { | ||
167 | + requestView.updateParam(paramName, null); | ||
168 | + } else { | ||
169 | + requestView.updateParam(paramName, field.getText()); | ||
170 | + } | ||
171 | + } | ||
172 | + } | ||
173 | + | ||
174 | + public static class DataProductTypeField extends ParamField { | ||
175 | + DataProductTypeField(RequestView requestView, String paramName) { | ||
176 | + super(requestView, paramName); | ||
177 | + JTextField field = new JTextField(); | ||
178 | + // TODO: listbox with enumerated values instead of JTextField | ||
179 | + addChangeListener(field, e -> onUpdate(field)); | ||
180 | + this.add(field); | ||
181 | + } | ||
182 | + | ||
183 | + private void onUpdate(JTextField field) { | ||
184 | + if ("".equals(field.getText())) { | ||
185 | + requestView.updateParam(paramName, null); | ||
186 | + } else { | ||
187 | + requestView.updateParam(paramName, field.getText()); | ||
188 | + } | ||
189 | + } | ||
190 | + } | ||
191 | + | ||
192 | + public static class TargetClassField extends ParamField { | ||
193 | + TargetClassField(RequestView requestView, String paramName) { | ||
194 | + super(requestView, paramName); | ||
195 | + JTextField field = new JTextField(); | ||
196 | + // TODO: listbox with enumerated values instead of JTextField | ||
197 | + addChangeListener(field, e -> onUpdate(field)); | ||
198 | + this.add(field); | ||
199 | + } | ||
200 | + | ||
201 | + private void onUpdate(JTextField field) { | ||
202 | + if ("".equals(field.getText())) { | ||
203 | + requestView.updateParam(paramName, null); | ||
204 | + } else { | ||
205 | + requestView.updateParam(paramName, field.getText()); | ||
206 | + } | ||
207 | + } | ||
208 | + } | ||
209 | + | ||
210 | + // public class EnumParamField extends ParamField { | ||
211 | + // EnumParamField() { | ||
212 | + // super(); | ||
213 | + // } | ||
214 | + // } | ||
215 | + | ||
216 | + public static void addChangeListener(JTextComponent text, ChangeListener changeListener) { | ||
217 | + Objects.requireNonNull(text); | ||
218 | + Objects.requireNonNull(changeListener); | ||
219 | + DocumentListener dl = new DocumentListener() { | ||
220 | + private int lastChange = 0, lastNotifiedChange = 0; | ||
221 | + | ||
222 | + @Override | ||
223 | + public void insertUpdate(DocumentEvent e) { | ||
224 | + changedUpdate(e); | ||
225 | + } | ||
226 | + | ||
227 | + @Override | ||
228 | + public void removeUpdate(DocumentEvent e) { | ||
229 | + changedUpdate(e); | ||
230 | + } | ||
231 | + | ||
232 | + @Override | ||
233 | + public void changedUpdate(DocumentEvent e) { | ||
234 | + lastChange++; | ||
235 | + SwingUtilities.invokeLater(() -> { | ||
236 | + if (lastNotifiedChange != lastChange) { | ||
237 | + lastNotifiedChange = lastChange; | ||
238 | + changeListener.stateChanged(new ChangeEvent(text)); | ||
239 | + } | ||
240 | + }); | ||
241 | + } | ||
242 | + }; | ||
243 | + text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> { | ||
244 | + Document d1 = (Document) e.getOldValue(); | ||
245 | + Document d2 = (Document) e.getNewValue(); | ||
246 | + if (d1 != null) | ||
247 | + d1.removeDocumentListener(dl); | ||
248 | + if (d2 != null) | ||
249 | + d2.addDocumentListener(dl); | ||
250 | + dl.changedUpdate(null); | ||
251 | + }); | ||
252 | + Document d = text.getDocument(); | ||
253 | + if (d != null) | ||
254 | + d.addDocumentListener(dl); | ||
255 | + } | ||
256 | +} | ||
0 | \ No newline at end of file | 257 | \ No newline at end of file |