Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java 11.4 KB
506d0c0b   Nathanael Jourdane   Add licence heade...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 * 
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

eb483599   Nathanael Jourdane   Add missing Param...
17
18
19
20
package eu.omp.irap.vespa.epntapclient.view;

import java.awt.Color;
import java.awt.Dimension;
a99d92fa   Nathanael Jourdane   Add JComboBox for...
21
22
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
eb483599   Nathanael Jourdane   Add missing Param...
23
24
25
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
e7647bba   Nathanael Jourdane   change query to g...
26
27
28
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
eb483599   Nathanael Jourdane   Add missing Param...
29
import java.util.Locale;
5d3c344e   Nathanael Jourdane   Use Java logging ...
30
import java.util.logging.Logger;
eb483599   Nathanael Jourdane   Add missing Param...
31
32

import javax.swing.BoxLayout;
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
33
import javax.swing.JComboBox;
eb483599   Nathanael Jourdane   Add missing Param...
34
35
36
37
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
eb483599   Nathanael Jourdane   Add missing Param...
38
39
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
eb483599   Nathanael Jourdane   Add missing Param...
40

05b140ed   Nathanael Jourdane   TargetNameField J...
41
42
43
44
45
46
47
48
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...
49
public abstract class ParamField extends JPanel {
5d3c344e   Nathanael Jourdane   Use Java logging ...
50
51
	/** The logger for the class ParamField. */
	private static final Logger logger = Logger.getLogger(ParamField.class.getName());
eb483599   Nathanael Jourdane   Add missing Param...
52

68ae1315   Nathanael Jourdane   Improve GUI appar...
53
54
55
56
57
	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...
58
	private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete";
eb483599   Nathanael Jourdane   Add missing Param...
59
60
	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...
61
62
63
64
65
66
	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...
67
68
69
	public ParamField(RequestView requestView, String paramName) {
		super();
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
68ae1315   Nathanael Jourdane   Improve GUI appar...
70
		this.setMaximumSize(new Dimension(MAX_FIELD_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
71
72
		String strLabel = paramName.replaceAll("_", " ").trim();
		JLabel label = new JLabel(strLabel.substring(0, 1).toUpperCase() + strLabel.substring(1));
68ae1315   Nathanael Jourdane   Improve GUI appar...
73
		label.setPreferredSize(new Dimension(LABEL_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
74
		this.add(label);
41133475   Nathanael Jourdane   build floatRangeF...
75
		// TODO: Add tooltip text based on rr.table_column.column_description
eb483599   Nathanael Jourdane   Add missing Param...
76
77
78
79
		this.requestView = requestView;
		this.paramName = paramName;
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
80
	public static class StringField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
81
82
		JTextField field;

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

e97b40e8   Nathanael Jourdane   Make ParamField c...
90
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
91
92
93
94
95
96
97
98
			if ("".equals(field.getText())) {
				requestView.updateParam(paramName, null);
			} else {
				requestView.updateParam(paramName, field.getText());
			}
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
99
	public static class FloatField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
100
101
		JTextField field;

eb483599   Nathanael Jourdane   Add missing Param...
102
103
		FloatField(RequestView requestView, String paramName) {
			super(requestView, paramName);
667dd80c   Nathanael Jourdane   JTextField as Par...
104
			field = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
105
			addChangeListener(this, field);
eb483599   Nathanael Jourdane   Add missing Param...
106
107
108
			this.add(field);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
109
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
			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...
124
	public static class DateRangeField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
125
126
127
		JTextField fieldMin;
		JTextField fieldMax;

eb483599   Nathanael Jourdane   Add missing Param...
128
129
		DateRangeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
68ae1315   Nathanael Jourdane   Improve GUI appar...
130
			this.add(new JLabel("min "));
667dd80c   Nathanael Jourdane   JTextField as Par...
131
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
132
			fieldMin.setName(MIN_SUFFIX);
68ae1315   Nathanael Jourdane   Improve GUI appar...
133
			fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
e97b40e8   Nathanael Jourdane   Make ParamField c...
134
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
135
136
			this.add(fieldMin);

68ae1315   Nathanael Jourdane   Improve GUI appar...
137
			this.add(new JLabel("max "));
667dd80c   Nathanael Jourdane   JTextField as Par...
138
			fieldMax = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
139
			fieldMax.setName(MAX_SUFFIX);
68ae1315   Nathanael Jourdane   Improve GUI appar...
140
			fieldMax.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
e97b40e8   Nathanael Jourdane   Make ParamField c...
141
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
142
143
144
			this.add(fieldMax);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
145
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
146
147
148
			DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
e97b40e8   Nathanael Jourdane   Make ParamField c...
149
				requestView.updateParam(paramName + field.getName(), null);
eb483599   Nathanael Jourdane   Add missing Param...
150
151
152
153
			} 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...
154
					requestView.updateParam(paramName + field.getName(), date);
eb483599   Nathanael Jourdane   Add missing Param...
155
156
157
158
159
160
161
162
163
164
165
					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...
166
	public static class FloatRangeField extends ParamField implements TextFieldListener {
41133475   Nathanael Jourdane   build floatRangeF...
167
168
		JTextField fieldMin;
		JTextField fieldMax;
eb483599   Nathanael Jourdane   Add missing Param...
169
170
171

		FloatRangeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
41133475   Nathanael Jourdane   build floatRangeF...
172
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
173
174
			fieldMin.setName(MIN_SUFFIX);
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
175
176
			this.add(fieldMin);

41133475   Nathanael Jourdane   build floatRangeF...
177
			fieldMax = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
178
179
			fieldMax.setName(MAX_SUFFIX);
			addChangeListener(this, fieldMax);
eb483599   Nathanael Jourdane   Add missing Param...
180
181
			this.add(fieldMax);
		}
41133475   Nathanael Jourdane   build floatRangeF...
182

e97b40e8   Nathanael Jourdane   Make ParamField c...
183
		public void update(JTextField field) {
41133475   Nathanael Jourdane   build floatRangeF...
184
185
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
e97b40e8   Nathanael Jourdane   Make ParamField c...
186
				requestView.updateParam(paramName + field.getName(), null);
41133475   Nathanael Jourdane   build floatRangeF...
187
188
			} else {
				try {
e97b40e8   Nathanael Jourdane   Make ParamField c...
189
190
					requestView.updateParam(paramName + field.getName(),
							Float.parseFloat(field.getText()));
41133475   Nathanael Jourdane   build floatRangeF...
191
192
193
194
195
196
					field.setBackground(Color.WHITE);
				} catch (NumberFormatException e) {
					field.setBackground(Color.PINK);
				}
			}
		}
eb483599   Nathanael Jourdane   Add missing Param...
197
198
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
199
	public static class TargetNameField extends ParamField implements TextFieldListener {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
200
		JComboBox<String> comboBox;
667dd80c   Nathanael Jourdane   JTextField as Par...
201
		JTextField field;
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
202
		String lastContent;
667dd80c   Nathanael Jourdane   JTextField as Par...
203

eb483599   Nathanael Jourdane   Add missing Param...
204
205
		TargetNameField(RequestView requestView, String paramName) {
			super(requestView, paramName);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
206
			comboBox = new JComboBox();
68ae1315   Nathanael Jourdane   Improve GUI appar...
207
208
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));

f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
209
210
			comboBox.setEditable(true);
			field = (JTextField) comboBox.getEditor().getEditorComponent();
e97b40e8   Nathanael Jourdane   Make ParamField c...
211
			addChangeListener(this, field);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
212
213
214
			this.add(comboBox);
		}

a99d92fa   Nathanael Jourdane   Add JComboBox for...
215
		private String[] getItems(String begining) {
05b140ed   Nathanael Jourdane   TargetNameField J...
216
217
218
219
			StringBuilder resolverResult = null;
			try {
				resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\"");
			} catch (HTTPRequestException | BadRequestException e) {
5d3c344e   Nathanael Jourdane   Use Java logging ...
220
				logger.severe("Can not send sersolver query: " + e);
05b140ed   Nathanael Jourdane   TargetNameField J...
221
222
223
224
225
226
227
228
229
230
			}
			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...
231
			return targetNames;
eb483599   Nathanael Jourdane   Add missing Param...
232
233
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
234
235
236
		Runnable updateComboBox = new Runnable() {
			@Override
			public void run() {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
237
				comboBox.removeAllItems();
e97b40e8   Nathanael Jourdane   Make ParamField c...
238
				for (String s : getItems(lastContent)) {
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
239
240
					comboBox.addItem(s);
				}
42819b99   Nathanael Jourdane   Make EpnTAPClient...
241
				comboBox.getEditor().setItem(lastContent);
e97b40e8   Nathanael Jourdane   Make ParamField c...
242
243
244
245
246
247
248
249
			}
		};

		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...
250
				requestView.updateParam(paramName, content);
eb483599   Nathanael Jourdane   Add missing Param...
251
252
253
254
255
			}
		}
	}

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

eb483599   Nathanael Jourdane   Add missing Param...
258
259
		DataProductTypeField(RequestView requestView, String paramName) {
			super(requestView, paramName);
e7647bba   Nathanael Jourdane   change query to g...
260
			comboBox = new JComboBox(getItems().keySet().toArray());
68ae1315   Nathanael Jourdane   Improve GUI appar...
261
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
262
263
264
265
266
267
268
269
270
			comboBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onUpdate();
				}
			});
			this.add(comboBox);
		}

e7647bba   Nathanael Jourdane   change query to g...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
		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...
286
287
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
288
		private void onUpdate() {
e7647bba   Nathanael Jourdane   change query to g...
289
290
291
292
293
			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...
294
295
296
297
		}
	}

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

eb483599   Nathanael Jourdane   Add missing Param...
300
301
		TargetClassField(RequestView requestView, String paramName) {
			super(requestView, paramName);
a99d92fa   Nathanael Jourdane   Add JComboBox for...
302
			comboBox = new JComboBox(getItems());
68ae1315   Nathanael Jourdane   Improve GUI appar...
303
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
304
305
306
307
308
309
310
311
312
313
314
315
			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...
316
317
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
318
		private void onUpdate() {
a99d92fa   Nathanael Jourdane   Add JComboBox for...
319
320
			String value = comboBox.getSelectedItem().toString().replace(" ", "_").toLowerCase();
			requestView.updateParam(paramName, "All".equals(value) ? null : value);
eb483599   Nathanael Jourdane   Add missing Param...
321
322
323
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
324
325
326
	interface TextFieldListener {
		void update(JTextField field);
	}
eb483599   Nathanael Jourdane   Add missing Param...
327

42819b99   Nathanael Jourdane   Make EpnTAPClient...
328
	static void addChangeListener(final TextFieldListener changeListener, final JTextField field) {
e97b40e8   Nathanael Jourdane   Make ParamField c...
329
330
331
		field.getDocument().addDocumentListener(new DocumentListener() {
			public void removeUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
332
333
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
334
335
			public void insertUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
336
337
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
338
339
			public void changedUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
340
			}
eb483599   Nathanael Jourdane   Add missing Param...
341
		});
eb483599   Nathanael Jourdane   Add missing Param...
342
343
	}
}