Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java 7.99 KB
4a03be16   Nathanael Jourdane   Put queryArea in ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * 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/>.
 */

package eu.omp.irap.vespa.epntapclient.view;

import java.awt.BorderLayout;
204e830a   Nathanael Jourdane   Add query paramet...
20
21
import java.awt.Dimension;
import java.awt.GridLayout;
4cc84b63   Nathanael Jourdane   Add the possibili...
22
23
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
4a03be16   Nathanael Jourdane   Put queryArea in ...
24

204e830a   Nathanael Jourdane   Add query paramet...
25
26
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
4cc84b63   Nathanael Jourdane   Add the possibili...
27
import javax.swing.JButton;
204e830a   Nathanael Jourdane   Add query paramet...
28
import javax.swing.JFormattedTextField;
4a03be16   Nathanael Jourdane   Put queryArea in ...
29
30
31
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
204e830a   Nathanael Jourdane   Add query paramet...
32
import javax.swing.JTextField;
35919668   Nathanael Jourdane   Update the query ...
33
34
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
4a03be16   Nathanael Jourdane   Put queryArea in ...
35

5799764b   Nathanael Jourdane   Use Log4j2 instea...
36
37
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
7706bfa4   Nathanael Jourdane   Use log4j as logg...
38

4cc84b63   Nathanael Jourdane   Add the possibili...
39
import eu.omp.irap.vespa.epntapclient.utils.Queries;
4dd68979   Nathanael Jourdane   Improve Exception...
40
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
4cc84b63   Nathanael Jourdane   Add the possibili...
41

4a03be16   Nathanael Jourdane   Put queryArea in ...
42
43
44
/**
 * @author N. Jourdane
 */
4cc84b63   Nathanael Jourdane   Add the possibili...
45
public class RequestView extends JPanel implements ActionListener {
4a03be16   Nathanael Jourdane   Put queryArea in ...
46

7706bfa4   Nathanael Jourdane   Use log4j as logg...
47
	/** The logger for this class. */
5799764b   Nathanael Jourdane   Use Log4j2 instea...
48
	private static final Logger logger = LogManager.getLogger(RequestView.class);
7706bfa4   Nathanael Jourdane   Use log4j as logg...
49

4a03be16   Nathanael Jourdane   Put queryArea in ...
50
51
52
53
	/** The serial version UID (affected with a random number). */
	private static final long serialVersionUID = 1262856496809315405L;

	/** The EPN-TAP main view. */
204e830a   Nathanael Jourdane   Add query paramet...
54
	private EpnTapMainView mainView;
4a03be16   Nathanael Jourdane   Put queryArea in ...
55

4cc84b63   Nathanael Jourdane   Add the possibili...
56
	/** The text area where the user put the query. */
204e830a   Nathanael Jourdane   Add query paramet...
57
58
59
60
	private JTextArea queryArea;

	/** The height of the buttons panel. */
	private static final int BUTTON_PANEL_HEIGHT = 15;
4a03be16   Nathanael Jourdane   Put queryArea in ...
61
62
63
64
65
66
67
68

	/**
	 * Method constructor
	 * 
	 * @param mainView The EPN-TAP main view.
	 */
	public RequestView(EpnTapMainView mainView) {
		this.mainView = mainView;
204e830a   Nathanael Jourdane   Add query paramet...
69
70
71
72
73
74
75
76
77
78
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		this.add(buildParamPanel(), this);
		this.add(buildQueryPanel(), this);
		this.add(buildButtonPanel(), this);
	}

	/**
	 * @return A JPanel containing graphical elements for the service parameters.
	 */
35919668   Nathanael Jourdane   Update the query ...
79
	private JPanel buildParamPanel() {
e1222bd6   Nathanael Jourdane   Use working query...
80
		JTextField jtfTargetName = new JTextField("Galileo");
35919668   Nathanael Jourdane   Update the query ...
81
82
83
84
85
		JFormattedTextField jtfMinTime = new JFormattedTextField(new Float(0.0));
		JFormattedTextField jtfMaxTime = new JFormattedTextField(new Float(1.0));
		JTextField jtfProductType = new JTextField("SpectralRange");
		JFormattedTextField jtfMinSpectralRange = new JFormattedTextField(new Float(0.0));
		JFormattedTextField jtfMaxSpectralRange = new JFormattedTextField(new Float(1.0));
e1222bd6   Nathanael Jourdane   Use working query...
86
		JFormattedTextField jtfMaxRows = new JFormattedTextField(new Integer(10));
35919668   Nathanael Jourdane   Update the query ...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

		DocumentListener paramListener = new DocumentListener() {
			@Override
			public void insertUpdate(DocumentEvent de) {
				try {
					int maxRows = Integer.parseInt(jtfMaxRows.getText());
					String targetName = jtfTargetName.getText();
					float minTime = (float) jtfMinTime.getValue();
					float maxTime = (float) jtfMaxTime.getValue();
					String productType = jtfProductType.getText();
					float minSpectralRange = (float) jtfMinSpectralRange.getValue();
					float maxSpectralRange = (float) jtfMaxSpectralRange.getValue();

					updateQueryArea(maxRows, targetName, minTime, maxTime, productType,
							minSpectralRange, maxSpectralRange);
				} catch (Exception e) {
					logger.error("Can not parse parameters.", e);
				}

			}

			@Override
			public void removeUpdate(DocumentEvent de) {
				insertUpdate(de);
			}

			@Override
			public void changedUpdate(DocumentEvent de) {
				insertUpdate(de);
			}
		};

204e830a   Nathanael Jourdane   Add query paramet...
119
120
121
122
123
124
125
		JPanel paramPanel = new JPanel(new GridLayout(0, 2));
		paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters"));

		// Target name
		JLabel targetNameLabel = new JLabel("Target name");
		paramPanel.add(targetNameLabel);

35919668   Nathanael Jourdane   Update the query ...
126
127
128
		jtfTargetName.getDocument().addDocumentListener(paramListener);
		jtfTargetName.setToolTipText("The target name, for example 'Jupiter'.");
		paramPanel.add(jtfTargetName);
204e830a   Nathanael Jourdane   Add query paramet...
129
130
131
132

		// Time
		JLabel timeLabel = new JLabel("Time (min, max)");
		paramPanel.add(timeLabel);
204e830a   Nathanael Jourdane   Add query paramet...
133
		JPanel timePanel = new JPanel(new GridLayout(0, 2));
35919668   Nathanael Jourdane   Update the query ...
134
135
136
137
138
139
140
141

		jtfMinTime.getDocument().addDocumentListener(paramListener);
		jtfMinTime.setToolTipText("The minimum time, for example '0.01'.");
		timePanel.add(jtfMinTime);

		jtfMaxTime.getDocument().addDocumentListener(paramListener);
		jtfMaxTime.setToolTipText("The maximum time, for example '1.50'.");
		timePanel.add(jtfMaxTime);
204e830a   Nathanael Jourdane   Add query paramet...
142
143
144
		paramPanel.add(timePanel);

		// Data product type
35919668   Nathanael Jourdane   Update the query ...
145
		JLabel productTypeLabel = new JLabel("Product type");
204e830a   Nathanael Jourdane   Add query paramet...
146
147
		paramPanel.add(productTypeLabel);

35919668   Nathanael Jourdane   Update the query ...
148
149
150
		jtfProductType.getDocument().addDocumentListener(paramListener);
		jtfProductType.setToolTipText("The product type, for example '...'.");
		paramPanel.add(jtfProductType);
204e830a   Nathanael Jourdane   Add query paramet...
151

204e830a   Nathanael Jourdane   Add query paramet...
152
153
		JLabel spectralRangeLabel = new JLabel("Spectral range");
		paramPanel.add(spectralRangeLabel);
35919668   Nathanael Jourdane   Update the query ...
154
		JPanel spectralRangePanel = new JPanel(new GridLayout(0, 2));
204e830a   Nathanael Jourdane   Add query paramet...
155

35919668   Nathanael Jourdane   Update the query ...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
		jtfMinSpectralRange.getDocument().addDocumentListener(paramListener);
		jtfMinSpectralRange.setToolTipText("The minimum spectral range, for example '0.01'.");
		spectralRangePanel.add(jtfMinSpectralRange);

		jtfMaxSpectralRange.getDocument().addDocumentListener(paramListener);
		jtfMaxSpectralRange.setToolTipText("The maximum spectral range, for example '1.50'.");
		spectralRangePanel.add(jtfMaxSpectralRange);

		paramPanel.add(spectralRangePanel);

		// Number rows limit
		JLabel maxRowsLabel = new JLabel("Rows limit");
		paramPanel.add(maxRowsLabel);

		jtfMaxRows.setName("targetName");
		jtfMaxRows.addActionListener(this);
		jtfMaxRows.setToolTipText("The maximum number of rows to display.");
		paramPanel.add(jtfMaxRows);
204e830a   Nathanael Jourdane   Add query paramet...
174
175
176
177
178
179
180
181
182
183

		return paramPanel;
	}

	/**
	 * @return A JPanel containing graphical elements for the query.
	 */
	private JPanel buildQueryPanel() {
		JPanel queryPanel = new JPanel();
		queryPanel.setBorder(BorderFactory.createTitledBorder("Query for the selected service(s)"));
35919668   Nathanael Jourdane   Update the query ...
184
		queryArea = new JTextArea("");
204e830a   Nathanael Jourdane   Add query paramet...
185
		queryArea.setToolTipText("The query sent to the service(s).");
5576be01   Nathanael Jourdane   Make panels resiz...
186
		queryArea.setLineWrap(true);
204e830a   Nathanael Jourdane   Add query paramet...
187
188
		queryPanel.setLayout(new BorderLayout());
		queryPanel.add(queryArea, BorderLayout.CENTER);
4cc84b63   Nathanael Jourdane   Add the possibili...
189

204e830a   Nathanael Jourdane   Add query paramet...
190
191
		return queryPanel;
	}
4cc84b63   Nathanael Jourdane   Add the possibili...
192

204e830a   Nathanael Jourdane   Add query paramet...
193
	/**
35919668   Nathanael Jourdane   Update the query ...
194
195
196
197
198
199
200
201
202
203
	 * @param max_rows
	 * @param target_name
	 * @param time_min
	 * @param time_max
	 * @param dataproduct_type
	 * @param spectral_range_min
	 * @param spectral_range_max
	 */
	private void updateQueryArea(int max_rows, String target_name, float time_min, float time_max,
			String dataproduct_type, float spectral_range_min, float spectral_range_max) {
e1222bd6   Nathanael Jourdane   Use working query...
204
205
206
207
208
		queryArea.setText(
				String.format(Queries.SAMPLE_AMDA_QUERY, max_rows, target_name));
		// queryArea.setText(String.format(Queries.SAMPLE_AMDA_QUERY, max_rows, target_name,
		// time_min,
		// time_max, dataproduct_type, spectral_range_min, spectral_range_max));
35919668   Nathanael Jourdane   Update the query ...
209
210
211
	}

	/**
204e830a   Nathanael Jourdane   Add query paramet...
212
213
214
215
216
217
218
219
220
221
	 * @return A JPanel containing the button(s).
	 */
	private JPanel buildButtonPanel() {
		JPanel buttonPanel = new JPanel();
		JButton btnSend = new JButton("Send query");
		btnSend.setName("btnSend");
		btnSend.addActionListener(this);
		buttonPanel.add(btnSend);
		buttonPanel.setMaximumSize(
				new Dimension(EpnTapMainView.RIGHT_PANEL_WIDTH, BUTTON_PANEL_HEIGHT));
35919668   Nathanael Jourdane   Update the query ...
222

204e830a   Nathanael Jourdane   Add query paramet...
223
		return buttonPanel;
4cc84b63   Nathanael Jourdane   Add the possibili...
224
225
226
	}

	@Override
a7aff3e3   Nathanael Jourdane   Fix Sonar issues
227
	public void actionPerformed(ActionEvent evt) {
a7aff3e3   Nathanael Jourdane   Fix Sonar issues
228
		if (((JButton) evt.getSource()).getName() == "btnSend") {
4cc84b63   Nathanael Jourdane   Add the possibili...
229
230
			try {
				mainView.getController().sendQuery(queryArea.getText());
4dd68979   Nathanael Jourdane   Improve Exception...
231
			} catch (VOTableException e) {
7706bfa4   Nathanael Jourdane   Use log4j as logg...
232
				logger.error("Can not send query when clicking on the send button.", e);
4cc84b63   Nathanael Jourdane   Add the possibili...
233
234
			}
		}
4a03be16   Nathanael Jourdane   Put queryArea in ...
235
236
237
	}

}