Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java 11.8 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
	private static final String MIN_SUFFIX = "min";
	private static final String MAX_SUFFIX = "max";

5af53be7   Nathanael Jourdane   Make the applicat...
64
	protected static EpnTapMainView mainView;
eb483599   Nathanael Jourdane   Add missing Param...
65
66
	protected String paramName;

5af53be7   Nathanael Jourdane   Make the applicat...
67
	public ParamField(EpnTapMainView mainView, String paramName) {
eb483599   Nathanael Jourdane   Add missing Param...
68
		super();
5af53be7   Nathanael Jourdane   Make the applicat...
69
70
71
72

		this.mainView = mainView;
		this.paramName = paramName;

eb483599   Nathanael Jourdane   Add missing Param...
73
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
68ae1315   Nathanael Jourdane   Improve GUI appar...
74
		this.setMaximumSize(new Dimension(MAX_FIELD_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
75
76
		String strLabel = paramName.replaceAll("_", " ").trim();
		JLabel label = new JLabel(strLabel.substring(0, 1).toUpperCase() + strLabel.substring(1));
68ae1315   Nathanael Jourdane   Improve GUI appar...
77
		label.setPreferredSize(new Dimension(LABEL_WIDTH, FIELD_HEIGHT));
eb483599   Nathanael Jourdane   Add missing Param...
78
		this.add(label);
41133475   Nathanael Jourdane   build floatRangeF...
79
		// TODO: Add tooltip text based on rr.table_column.column_description
eb483599   Nathanael Jourdane   Add missing Param...
80
81
	}

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

5af53be7   Nathanael Jourdane   Make the applicat...
85
86
		public StringField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
667dd80c   Nathanael Jourdane   JTextField as Par...
87
			field = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
88
			addChangeListener(this, field);
eb483599   Nathanael Jourdane   Add missing Param...
89
90
91
			this.add(field);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
92
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
93
			if ("".equals(field.getText())) {
5af53be7   Nathanael Jourdane   Make the applicat...
94
				mainView.event(Event.paramChanged, paramName, null);
eb483599   Nathanael Jourdane   Add missing Param...
95
			} else {
5af53be7   Nathanael Jourdane   Make the applicat...
96
				mainView.event(Event.paramChanged, paramName, field.getText());
eb483599   Nathanael Jourdane   Add missing Param...
97
98
99
100
			}
		}
	}

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

5af53be7   Nathanael Jourdane   Make the applicat...
104
105
		public FloatField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
667dd80c   Nathanael Jourdane   JTextField as Par...
106
			field = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
107
			addChangeListener(this, field);
eb483599   Nathanael Jourdane   Add missing Param...
108
109
110
			this.add(field);
		}

e97b40e8   Nathanael Jourdane   Make ParamField c...
111
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
112
113
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
5af53be7   Nathanael Jourdane   Make the applicat...
114
				mainView.event(Event.paramRemoved, paramName);
eb483599   Nathanael Jourdane   Add missing Param...
115
116
			} else {
				try {
5af53be7   Nathanael Jourdane   Make the applicat...
117
118
					mainView.event(Event.paramChanged, paramName,
							Float.parseFloat(field.getText()));
eb483599   Nathanael Jourdane   Add missing Param...
119
120
121
122
123
124
125
126
					field.setBackground(Color.WHITE);
				} catch (NumberFormatException e) {
					field.setBackground(Color.PINK);
				}
			}
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
127
	public static class DateRangeField extends ParamField implements TextFieldListener {
667dd80c   Nathanael Jourdane   JTextField as Par...
128
129
130
		JTextField fieldMin;
		JTextField fieldMax;

5af53be7   Nathanael Jourdane   Make the applicat...
131
132
		public DateRangeField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
68ae1315   Nathanael Jourdane   Improve GUI appar...
133
			this.add(new JLabel("min "));
667dd80c   Nathanael Jourdane   JTextField as Par...
134
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
135
			fieldMin.setName(MIN_SUFFIX);
68ae1315   Nathanael Jourdane   Improve GUI appar...
136
			fieldMin.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
e97b40e8   Nathanael Jourdane   Make ParamField c...
137
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
138
139
			this.add(fieldMin);

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

e97b40e8   Nathanael Jourdane   Make ParamField c...
148
		public void update(JTextField field) {
eb483599   Nathanael Jourdane   Add missing Param...
149
150
151
			DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
5af53be7   Nathanael Jourdane   Make the applicat...
152
				mainView.event(Event.paramRemoved, paramName + field.getName());
eb483599   Nathanael Jourdane   Add missing Param...
153
154
155
			} else if (field.getText().matches(DATE_REGEX)) {
				try {
					long date = df.parse(field.getText()).getTime();
5af53be7   Nathanael Jourdane   Make the applicat...
156
157
					date = Math.round((date / 86400000.0) + 2440587.5); // to JD
					mainView.event(Event.paramChanged, paramName + field.getName(), date);
eb483599   Nathanael Jourdane   Add missing Param...
158
159
160
161
162
163
164
165
166
167
168
					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...
169
	public static class FloatRangeField extends ParamField implements TextFieldListener {
41133475   Nathanael Jourdane   build floatRangeF...
170
171
		JTextField fieldMin;
		JTextField fieldMax;
eb483599   Nathanael Jourdane   Add missing Param...
172

5af53be7   Nathanael Jourdane   Make the applicat...
173
174
		public FloatRangeField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
41133475   Nathanael Jourdane   build floatRangeF...
175
			fieldMin = new JTextField();
e97b40e8   Nathanael Jourdane   Make ParamField c...
176
177
			fieldMin.setName(MIN_SUFFIX);
			addChangeListener(this, fieldMin);
eb483599   Nathanael Jourdane   Add missing Param...
178
179
			this.add(fieldMin);

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

e97b40e8   Nathanael Jourdane   Make ParamField c...
186
		public void update(JTextField field) {
41133475   Nathanael Jourdane   build floatRangeF...
187
188
			if ("".equals(field.getText())) {
				field.setBackground(Color.WHITE);
5af53be7   Nathanael Jourdane   Make the applicat...
189
				mainView.event(Event.paramRemoved, paramName + field.getName());
41133475   Nathanael Jourdane   build floatRangeF...
190
191
			} else {
				try {
5af53be7   Nathanael Jourdane   Make the applicat...
192
					mainView.event(Event.paramChanged, paramName + field.getName(),
e97b40e8   Nathanael Jourdane   Make ParamField c...
193
							Float.parseFloat(field.getText()));
41133475   Nathanael Jourdane   build floatRangeF...
194
195
196
197
198
199
					field.setBackground(Color.WHITE);
				} catch (NumberFormatException e) {
					field.setBackground(Color.PINK);
				}
			}
		}
eb483599   Nathanael Jourdane   Add missing Param...
200
201
	}

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

5af53be7   Nathanael Jourdane   Make the applicat...
207
208
		public TargetNameField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
209
			comboBox = new JComboBox();
68ae1315   Nathanael Jourdane   Improve GUI appar...
210
211
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));

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

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

e97b40e8   Nathanael Jourdane   Make ParamField c...
237
238
239
		Runnable updateComboBox = new Runnable() {
			@Override
			public void run() {
3d80f3c3   Nathanael Jourdane   bugFix: targetNam...
240
				String content = field.getText();
5af53be7   Nathanael Jourdane   Make the applicat...
241
242
243
244
245
246
247
248
249
250
251
252
253
				if (!content.equals(lastContent)) {
					if (content.length() >= 2) {
						lastContent = content;
						comboBox.removeAllItems();
						for (String s : getItems(content)) {
							comboBox.addItem(s);
						}
						comboBox.getEditor().setItem(content);
					}
					if ("".equals(content)) {
						mainView.event(Event.paramRemoved, paramName);
					} else {
						mainView.event(Event.paramChanged, paramName, content);
3d80f3c3   Nathanael Jourdane   bugFix: targetNam...
254
					}
f00cb6bb   Nathanael Jourdane   Add a JComboBox f...
255
				}
e97b40e8   Nathanael Jourdane   Make ParamField c...
256
257
258
259
			}
		};

		public void update(JTextField field) {
3d80f3c3   Nathanael Jourdane   bugFix: targetNam...
260
			SwingUtilities.invokeLater(updateComboBox);
eb483599   Nathanael Jourdane   Add missing Param...
261
262
263
264
		}
	}

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

5af53be7   Nathanael Jourdane   Make the applicat...
267
268
		public DataProductTypeField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
e7647bba   Nathanael Jourdane   change query to g...
269
			comboBox = new JComboBox(getItems().keySet().toArray());
68ae1315   Nathanael Jourdane   Improve GUI appar...
270
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
271
272
273
274
275
276
277
278
279
			comboBox.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					onUpdate();
				}
			});
			this.add(comboBox);
		}

e7647bba   Nathanael Jourdane   change query to g...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
		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...
295
296
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
297
		private void onUpdate() {
e7647bba   Nathanael Jourdane   change query to g...
298
299
300
301
			String key = comboBox.getSelectedItem().toString();
			List<String> item = new ArrayList();
			item.add(key.replace(" ", "-").toLowerCase());
			item.add(getItems().get(key));
5af53be7   Nathanael Jourdane   Make the applicat...
302
303
304
305
306
			if ("All".equals(key)) {
				mainView.event(Event.paramRemoved, paramName);
			} else {
				mainView.event(Event.paramChanged, paramName, item);
			}
eb483599   Nathanael Jourdane   Add missing Param...
307
308
309
310
		}
	}

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

5af53be7   Nathanael Jourdane   Make the applicat...
313
314
		public TargetClassField(EpnTapMainView mainView, String paramName) {
			super(mainView, paramName);
a99d92fa   Nathanael Jourdane   Add JComboBox for...
315
			comboBox = new JComboBox(getItems());
68ae1315   Nathanael Jourdane   Improve GUI appar...
316
			comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
a99d92fa   Nathanael Jourdane   Add JComboBox for...
317
318
319
320
321
322
323
324
325
326
327
328
			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...
329
330
		}

667dd80c   Nathanael Jourdane   JTextField as Par...
331
		private void onUpdate() {
a99d92fa   Nathanael Jourdane   Add JComboBox for...
332
			String value = comboBox.getSelectedItem().toString().replace(" ", "_").toLowerCase();
5af53be7   Nathanael Jourdane   Make the applicat...
333
334
335
336
337
			if ("All".equals(value)) {
				mainView.event(Event.paramRemoved, paramName);
			} else {
				mainView.event(Event.paramChanged, paramName, value);
			}
eb483599   Nathanael Jourdane   Add missing Param...
338
339
340
		}
	}

e97b40e8   Nathanael Jourdane   Make ParamField c...
341
342
343
	interface TextFieldListener {
		void update(JTextField field);
	}
eb483599   Nathanael Jourdane   Add missing Param...
344

42819b99   Nathanael Jourdane   Make EpnTAPClient...
345
	static void addChangeListener(final TextFieldListener changeListener, final JTextField field) {
e97b40e8   Nathanael Jourdane   Make ParamField c...
346
347
348
		field.getDocument().addDocumentListener(new DocumentListener() {
			public void removeUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
349
350
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
351
352
			public void insertUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
353
354
			}

e97b40e8   Nathanael Jourdane   Make ParamField c...
355
356
			public void changedUpdate(DocumentEvent de) {
				changeListener.update(field);
eb483599   Nathanael Jourdane   Add missing Param...
357
			}
eb483599   Nathanael Jourdane   Add missing Param...
358
		});
eb483599   Nathanael Jourdane   Add missing Param...
359
360
	}
}