Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java 10.4 KB
eb483599   Nathanael Jourdane   Add missing Param...
1
2
3
4
package eu.omp.irap.vespa.epntapclient.view;

import java.awt.Color;
import java.awt.Dimension;
a99d92fa   Nathanael Jourdane   Add JComboBox for...
5
6
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
eb483599   Nathanael Jourdane   Add missing Param...
7
8
9
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
e7647bba   Nathanael Jourdane   change query to g...
10
11
12
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
eb483599   Nathanael Jourdane   Add missing Param...
13
import java.util.Locale;
5d3c344e   Nathanael Jourdane   Use Java logging ...
14
import java.util.logging.Logger;
eb483599   Nathanael Jourdane   Add missing Param...
15
16

import javax.swing.BoxLayout;
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
17
import javax.swing.JComboBox;
eb483599   Nathanael Jourdane   Add missing Param...
18
19
20
21
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
eb483599   Nathanael Jourdane   Add missing Param...
22
23
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
eb483599   Nathanael Jourdane   Add missing Param...
24

05b140ed   Nathanael Jourdane   TargetNameField J...
25
26
27
28
29
30
31
32
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection;
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException;
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException;

eb483599   Nathanael Jourdane   Add missing Param...
33
public abstract class ParamField extends JPanel {
5d3c344e   Nathanael Jourdane   Use Java logging ...
34
35
	/** The logger for the class ParamField. */
	private static final Logger logger = Logger.getLogger(ParamField.class.getName());
eb483599   Nathanael Jourdane   Add missing Param...
36

68ae1315   Nathanael Jourdane   Improve GUI appar...
37
38
39
40
41
	private static final int MIN_FIELD_WIDTH = 30;
	private static final int FIELD_HEIGHT = 20;
	private static final int MAX_FIELD_WIDTH = 400;
	private static final int LABEL_WIDTH = 140;

05b140ed   Nathanael Jourdane   TargetNameField J...
42
	private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete";
eb483599   Nathanael Jourdane   Add missing Param...
43
44
	private static final String DATE_FORMAT = "dd/MM/yyyy";
	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)$)";
eb483599   Nathanael Jourdane   Add missing Param...
45
46
47
48
49
50
	private static final String MIN_SUFFIX = "min";
	private static final String MAX_SUFFIX = "max";

	protected static RequestView requestView;
	protected String paramName;

eb483599   Nathanael Jourdane   Add missing Param...
51
52
53
	public ParamField(RequestView requestView, String paramName) {
		super();
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
68ae1315   Nathanael Jourdane   Improve GUI appar...
54
		this.setMaximumSize(new Dimension(MAX_FIELD_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
55
56
		String strLabel = paramName.replaceAll("_", " ").trim();
		JLabel label = new JLabel(strLabel.substring(0, 1).toUpperCase() + strLabel.substring(1));
68ae1315   Nathanael Jourdane   Improve GUI appar...
57
		label.setPreferredSize(new Dimension(LABEL_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
58
		this.add(label);
41133475   Nathanael Jourdane   build floatRangeF...
59
		// TODO: Add tooltip text based on rr.table_column.column_description
eb483599   Nathanael Jourdane   Add missing Param...
60
61
62
63
		this.requestView = requestView;
		this.paramName = paramName;
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
64
	public static class StringField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
65
66
		JTextField field;

eb483599   Nathanael Jourdane   Add missing Param...
67
68
		StringField(RequestView requestView, String paramName) {
			super(requestView, paramName);
667dd80c   Nathanael Jourdane   JTextField as Par...
69
			field = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
70
			addChangeListener(this, field);
eb483599   Nathanael Jourdane   Add missing Param...
71
72
73
			this.add(field);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
74
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
75
76
77
78
79
80
81
82
			if ("".equals(field.getText())) {
				requestView.updateParam(paramName, null);
			} else {
				requestView.updateParam(paramName, field.getText());
			}
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
83
	public static class FloatField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
84
85
		JTextField field;

eb483599   Nathanael Jourdane   Add missing Param...
86
87
		FloatField(RequestView requestView, String paramName) {
			super(requestView, paramName);
667dd80c   Nathanael Jourdane   JTextField as Par...
88
			field = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
89
			addChangeListener(this, field);
eb483599   Nathanael Jourdane   Add missing Param...
90
91
92
			this.add(field);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
93
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
				requestView.updateParam(paramName, null);
			} else {
				try {
					requestView.updateParam(paramName, Float.parseFloat(field.getText()));
					field.setBackground(Color.WHITE);
				} catch (NumberFormatException e) {
					field.setBackground(Color.PINK);
				}
			}
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
108
	public static class DateRangeField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
109
110
111
		JTextField fieldMin;
		JTextField fieldMax;

eb483599   Nathanael Jourdane   Add missing Param...
112
113
		DateRangeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
68ae1315   Nathanael Jourdane   Improve GUI appar...
114
			this.add(new JLabel("min "));
667dd80c   Nathanael Jourdane   JTextField as Par...
115
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
116
			fieldMin.setName(MIN_SUFFIX);
68ae1315   Nathanael Jourdane   Improve GUI appar...
117
			fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
e97b40e8   Nathanael Jourdane   Make ParamField c...
118
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
119
120
			this.add(fieldMin);

68ae1315   Nathanael Jourdane   Improve GUI appar...
121
			this.add(new JLabel("max "));
667dd80c   Nathanael Jourdane   JTextField as Par...
122
			fieldMax = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
123
			fieldMax.setName(MAX_SUFFIX);
68ae1315   Nathanael Jourdane   Improve GUI appar...
124
			fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
e97b40e8   Nathanael Jourdane   Make ParamField c...
125
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
126
127
128
			this.add(fieldMax);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
129
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
130
131
132
			DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
e97b40e8   Nathanael Jourdane   Make ParamField c...
133
				requestView.updateParam(paramName + field.getName(), null);
eb483599   Nathanael Jourdane   Add missing Param...
134
135
136
137
			} else if (field.getText().matches(DATE_REGEX)) {
				try {
					long date = df.parse(field.getText()).getTime();
					date = (Math.round((date / 86400000.0) + 2440587.5)); // to JD
e97b40e8   Nathanael Jourdane   Make ParamField c...
138
					requestView.updateParam(paramName + field.getName(), date);
eb483599   Nathanael Jourdane   Add missing Param...
139
140
141
142
143
144
145
146
147
148
149
					field.setBackground(Color.WHITE);
				} catch (ParseException e) {
					field.setBackground(Color.PINK);
				}
				// TODO: check if date min < date max
			} else {
				field.setBackground(Color.PINK);
			}
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
150
	public static class FloatRangeField extends ParamField implements TextFieldListener {
41133475   Nathanael Jourdane   build floatRangeF...
151
152
		JTextField fieldMin;
		JTextField fieldMax;
eb483599   Nathanael Jourdane   Add missing Param...
153
154
155

		FloatRangeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
41133475   Nathanael Jourdane   build floatRangeF...
156
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
157
158
			fieldMin.setName(MIN_SUFFIX);
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
159
160
			this.add(fieldMin);

41133475   Nathanael Jourdane   build floatRangeF...
161
			fieldMax = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
162
163
			fieldMax.setName(MAX_SUFFIX);
			addChangeListener(this, fieldMax);
eb483599   Nathanael Jourdane   Add missing Param...
164
165
			this.add(fieldMax);
		}
41133475   Nathanael Jourdane   build floatRangeF...
166

e97b40e8   Nathanael Jourdane   Make ParamField c...
167
		public void update(JTextField field) {
41133475   Nathanael Jourdane   build floatRangeF...
168
169
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
e97b40e8   Nathanael Jourdane   Make ParamField c...
170
				requestView.updateParam(paramName + field.getName(), null);
41133475   Nathanael Jourdane   build floatRangeF...
171
172
			} else {
				try {
e97b40e8   Nathanael Jourdane   Make ParamField c...
173
174
					requestView.updateParam(paramName + field.getName(),
							Float.parseFloat(field.getText()));
41133475   Nathanael Jourdane   build floatRangeF...
175
176
177
178
179
180
					field.setBackground(Color.WHITE);
				} catch (NumberFormatException e) {
					field.setBackground(Color.PINK);
				}
			}
		}
eb483599   Nathanael Jourdane   Add missing Param...
181
182
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
183
	public static class TargetNameField extends ParamField implements TextFieldListener {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
184
		JComboBox<String> comboBox;
667dd80c   Nathanael Jourdane   JTextField as Par...
185
		JTextField field;
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
186
		String lastContent;
667dd80c   Nathanael Jourdane   JTextField as Par...
187

eb483599   Nathanael Jourdane   Add missing Param...
188
189
		TargetNameField(RequestView requestView, String paramName) {
			super(requestView, paramName);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
190
			comboBox = new JComboBox();
68ae1315   Nathanael Jourdane   Improve GUI appar...
191
192
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));

f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
193
194
			comboBox.setEditable(true);
			field = (JTextField) comboBox.getEditor().getEditorComponent();
e97b40e8   Nathanael Jourdane   Make ParamField c...
195
			addChangeListener(this, field);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
196
197
198
			this.add(comboBox);
		}

a99d92fa   Nathanael Jourdane   Add JComboBox for...
199
		private String[] getItems(String begining) {
05b140ed   Nathanael Jourdane   TargetNameField J...
200
201
202
203
			StringBuilder resolverResult = null;
			try {
				resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\"");
			} catch (HTTPRequestException | BadRequestException e) {
5d3c344e   Nathanael Jourdane   Use Java logging ...
204
				logger.severe("Can not send sersolver query: " + e);
05b140ed   Nathanael Jourdane   TargetNameField J...
205
206
207
208
209
210
211
212
213
214
			}
			JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject();
			int count = Integer.parseInt(root.get("count").toString());
			String[] targetNames = new String[count];
			JsonArray hits = root.getAsJsonArray("hits");
			for (int i = 0; i < count; i++) {
				JsonObject elmt = hits.get(i).getAsJsonObject();
				targetNames[i] = elmt.get("name").toString().replace("\"", "");
				// TODO: Display "[name] ([type])" on the JComboBox, but only "[name]" on the query.
			}
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
215
			return targetNames;
eb483599   Nathanael Jourdane   Add missing Param...
216
217
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
218
219
220
		Runnable updateComboBox = new Runnable() {
			@Override
			public void run() {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
221
				comboBox.removeAllItems();
e97b40e8   Nathanael Jourdane   Make ParamField c...
222
				for (String s : getItems(lastContent)) {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
223
224
					comboBox.addItem(s);
				}
42819b99   Nathanael Jourdane   Make EpnTAPClient...
225
				comboBox.getEditor().setItem(lastContent);
e97b40e8   Nathanael Jourdane   Make ParamField c...
226
227
228
229
230
231
232
233
			}
		};

		public void update(JTextField field) {
			String content = field.getText();
			if (content.length() >= 2 && !content.equals(lastContent)) {
				lastContent = content;
				SwingUtilities.invokeLater(updateComboBox);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
234
				requestView.updateParam(paramName, content);
eb483599   Nathanael Jourdane   Add missing Param...
235
236
237
238
239
			}
		}
	}

	public static class DataProductTypeField extends ParamField {
a99d92fa   Nathanael Jourdane   Add JComboBox for...
240
		JComboBox<String> comboBox;
667dd80c   Nathanael Jourdane   JTextField as Par...
241

eb483599   Nathanael Jourdane   Add missing Param...
242
243
		DataProductTypeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
e7647bba   Nathanael Jourdane   change query to g...
244
			comboBox = new JComboBox(getItems().keySet().toArray());
68ae1315   Nathanael Jourdane   Improve GUI appar...
245
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
246
247
248
249
250
251
252
253
254
			comboBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onUpdate();
				}
			});
			this.add(comboBox);
		}

e7647bba   Nathanael Jourdane   change query to g...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
		private HashMap<String, String> getItems() {
			HashMap items = new HashMap<String, String>();
			items.put("All", "all");
			items.put("Image", "im");
			items.put("Spectrum", "sp");
			items.put("Dynamic spectrum", "ds");
			items.put("Spectral cube", "sc");
			items.put("Profile", "pr");
			items.put("Volume", "vo");
			items.put("Movie", "mo");
			items.put("Cube", "cu");
			items.put("Time series", "ts");
			items.put("Catalog", "ca");
			items.put("Spatial vector", "sv");
			return items;
eb483599   Nathanael Jourdane   Add missing Param...
270
271
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
272
		private void onUpdate() {
e7647bba   Nathanael Jourdane   change query to g...
273
274
275
276
277
			String key = comboBox.getSelectedItem().toString();
			List<String> item = new ArrayList();
			item.add(key.replace(" ", "-").toLowerCase());
			item.add(getItems().get(key));
			requestView.updateParam(paramName, "All".equals(key) ? null : item);
eb483599   Nathanael Jourdane   Add missing Param...
278
279
280
281
		}
	}

	public static class TargetClassField extends ParamField {
a99d92fa   Nathanael Jourdane   Add JComboBox for...
282
		JComboBox<String> comboBox;
667dd80c   Nathanael Jourdane   JTextField as Par...
283

eb483599   Nathanael Jourdane   Add missing Param...
284
285
		TargetClassField(RequestView requestView, String paramName) {
			super(requestView, paramName);
a99d92fa   Nathanael Jourdane   Add JComboBox for...
286
			comboBox = new JComboBox(getItems());
68ae1315   Nathanael Jourdane   Improve GUI appar...
287
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
288
289
290
291
292
293
294
295
296
297
298
299
			comboBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onUpdate();
				}
			});
			this.add(comboBox);
		}

		private String[] getItems() {
			return new String[] { "All", "Comet", "Exoplanet", "Interplanetary medium", "Ring",
					"Sample", "Sky", "Spacecraft", "Spacejunk", "Star" };
eb483599   Nathanael Jourdane   Add missing Param...
300
301
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
302
		private void onUpdate() {
a99d92fa   Nathanael Jourdane   Add JComboBox for...
303
304
			String value = comboBox.getSelectedItem().toString().replace(" ", "_").toLowerCase();
			requestView.updateParam(paramName, "All".equals(value) ? null : value);
eb483599   Nathanael Jourdane   Add missing Param...
305
306
307
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
308
309
310
	interface TextFieldListener {
		void update(JTextField field);
	}
eb483599   Nathanael Jourdane   Add missing Param...
311

42819b99   Nathanael Jourdane   Make EpnTAPClient...
312
	static void addChangeListener(final TextFieldListener changeListener, final JTextField field) {
e97b40e8   Nathanael Jourdane   Make ParamField c...
313
314
315
		field.getDocument().addDocumentListener(new DocumentListener() {
			public void removeUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
316
317
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
318
319
			public void insertUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
320
321
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
322
323
			public void changedUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
324
			}
eb483599   Nathanael Jourdane   Add missing Param...
325
		});
eb483599   Nathanael Jourdane   Add missing Param...
326
327
	}
}