Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java 5.65 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
import java.awt.Dimension;
4cc84b63   Nathanael Jourdane   Add the possibili...
21
22
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
5ea2b82c   Nathanael Jourdane   Implement several...
23
import java.util.ArrayList;
0d0b44fe   Nathanael Jourdane   All parameters fi...
24
import java.util.HashMap;
5ea2b82c   Nathanael Jourdane   Implement several...
25
import java.util.List;
0d0b44fe   Nathanael Jourdane   All parameters fi...
26
import java.util.Map;
4a03be16   Nathanael Jourdane   Put queryArea in ...
27

204e830a   Nathanael Jourdane   Add query paramet...
28
29
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
4cc84b63   Nathanael Jourdane   Add the possibili...
30
import javax.swing.JButton;
4a03be16   Nathanael Jourdane   Put queryArea in ...
31
32
import javax.swing.JPanel;
import javax.swing.JTextArea;
4a03be16   Nathanael Jourdane   Put queryArea in ...
33

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

4cc84b63   Nathanael Jourdane   Add the possibili...
37
import eu.omp.irap.vespa.epntapclient.utils.Queries;
5ea2b82c   Nathanael Jourdane   Implement several...
38
39
40
41
import eu.omp.irap.vespa.epntapclient.view.ParamField.DataProductTypeField;
import eu.omp.irap.vespa.epntapclient.view.ParamField.DateRangeField;
import eu.omp.irap.vespa.epntapclient.view.ParamField.FloatRangeField;
import eu.omp.irap.vespa.epntapclient.view.ParamField.TargetNameField;
4dd68979   Nathanael Jourdane   Improve Exception...
42
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
4cc84b63   Nathanael Jourdane   Add the possibili...
43

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

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

4a03be16   Nathanael Jourdane   Put queryArea in ...
52
53
54
55
	/** 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...
56
	private EpnTapMainView mainView;
4a03be16   Nathanael Jourdane   Put queryArea in ...
57

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

1bc7a17a   Nathanael Jourdane   Improve parameter...
61
	// TODO: Use one map for paramFields, paramValues, paramTypes.
5ea2b82c   Nathanael Jourdane   Implement several...
62

0d0b44fe   Nathanael Jourdane   All parameters fi...
63
64
65
	/**
	 * The parameters fields for the request.
	 */
5ea2b82c   Nathanael Jourdane   Implement several...
66
	private List<ParamField> paramFields;
0d0b44fe   Nathanael Jourdane   All parameters fi...
67
68
69
70

	/**
	 * The parameters fields for the request.
	 */
1bc7a17a   Nathanael Jourdane   Improve parameter...
71
72
	private Map<String, Object> paramValues;

204e830a   Nathanael Jourdane   Add query paramet...
73
	/** The height of the buttons panel. */
68ae1315   Nathanael Jourdane   Improve GUI appar...
74
	private static final int BUTTON_PANEL_HEIGHT = 20;
4a03be16   Nathanael Jourdane   Put queryArea in ...
75
76
77
78
79
80
81
82

	/**
	 * Method constructor
	 * 
	 * @param mainView The EPN-TAP main view.
	 */
	public RequestView(EpnTapMainView mainView) {
		this.mainView = mainView;
204e830a   Nathanael Jourdane   Add query paramet...
83
84
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

c79e5664   Nathanael Jourdane   bugFix: Fields ar...
85
		// TODO: Get max row number from the GUI
0d0b44fe   Nathanael Jourdane   All parameters fi...
86

1bc7a17a   Nathanael Jourdane   Improve parameter...
87
		paramValues = new HashMap<>();
0d0b44fe   Nathanael Jourdane   All parameters fi...
88

204e830a   Nathanael Jourdane   Add query paramet...
89
90
91
		this.add(buildParamPanel(), this);
		this.add(buildQueryPanel(), this);
		this.add(buildButtonPanel(), this);
204e830a   Nathanael Jourdane   Add query paramet...
92
93
94
95
96
	}

	/**
	 * @return A JPanel containing graphical elements for the service parameters.
	 */
35919668   Nathanael Jourdane   Update the query ...
97
	private JPanel buildParamPanel() {
0d0b44fe   Nathanael Jourdane   All parameters fi...
98
99
100
		// TODO: new GUI field column to allow the user to select the comparison operator:
		// - if the field is a String: listbox with 'xx', '%xx', 'xx%', and '%xx%'.
		// - if the field is a numeric value: listbox with <, <=, =, =>, >.
5ea2b82c   Nathanael Jourdane   Implement several...
101
102
103
104
105
		paramFields = new ArrayList();
		paramFields.add(new TargetNameField(this, "target_name"));
		paramFields.add(new DateRangeField(this, "time_"));
		paramFields.add(new FloatRangeField(this, "spectral_range_"));
		paramFields.add(new DataProductTypeField(this, "dataproduct_type"));
5ea2b82c   Nathanael Jourdane   Implement several...
106
107
		JPanel paramPanel = new JPanel();
		paramPanel.setLayout(new BoxLayout(paramPanel, BoxLayout.Y_AXIS));
204e830a   Nathanael Jourdane   Add query paramet...
108
109
		paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters"));

5ea2b82c   Nathanael Jourdane   Implement several...
110
111
		for (ParamField field : paramFields) {
			paramPanel.add(field);
0d0b44fe   Nathanael Jourdane   All parameters fi...
112
		}
204e830a   Nathanael Jourdane   Add query paramet...
113
114
115
116
117
118
119
120
121
122

		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 ...
123
		queryArea = new JTextArea("");
204e830a   Nathanael Jourdane   Add query paramet...
124
		queryArea.setToolTipText("The query sent to the service(s).");
5576be01   Nathanael Jourdane   Make panels resiz...
125
		queryArea.setLineWrap(true);
204e830a   Nathanael Jourdane   Add query paramet...
126
127
		queryPanel.setLayout(new BorderLayout());
		queryPanel.add(queryArea, BorderLayout.CENTER);
4cc84b63   Nathanael Jourdane   Add the possibili...
128

204e830a   Nathanael Jourdane   Add query paramet...
129
130
		return queryPanel;
	}
4cc84b63   Nathanael Jourdane   Add the possibili...
131

1bc7a17a   Nathanael Jourdane   Improve parameter...
132
	public void updateParam(String paramName, Object value) {
76db66a0   Nathanael Jourdane   auto-remove null ...
133
134
135
136
137
138
139
		if (value == null) {
			paramValues.remove(paramName);
			logger.info("removed " + paramName);
		} else {
			paramValues.put(paramName, value);
			logger.info("uploaded " + paramName + ": " + value);
		}
5ea2b82c   Nathanael Jourdane   Implement several...
140
		updateQueryArea();
1bc7a17a   Nathanael Jourdane   Improve parameter...
141
142
	}

204e830a   Nathanael Jourdane   Add query paramet...
143
	/**
0d0b44fe   Nathanael Jourdane   All parameters fi...
144
	 * Update the query JTextArea according to the parameters values.
35919668   Nathanael Jourdane   Update the query ...
145
	 */
0b616d80   Nathanael Jourdane   Updtate the query...
146
	public void updateQueryArea() {
6415d908   Nathanael Jourdane   Get all tap servi...
147
		String tableName = mainView.getController().getSelectedTable();
1bc7a17a   Nathanael Jourdane   Improve parameter...
148
		queryArea.setText(Queries.getQuery(tableName, paramValues, 10));
35919668   Nathanael Jourdane   Update the query ...
149
150
151
	}

	/**
204e830a   Nathanael Jourdane   Add query paramet...
152
153
154
155
156
157
158
159
160
	 * @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(
68ae1315   Nathanael Jourdane   Improve GUI appar...
161
				new Dimension(1000, BUTTON_PANEL_HEIGHT));
35919668   Nathanael Jourdane   Update the query ...
162

204e830a   Nathanael Jourdane   Add query paramet...
163
		return buttonPanel;
4cc84b63   Nathanael Jourdane   Add the possibili...
164
165
166
	}

	@Override
a7aff3e3   Nathanael Jourdane   Fix Sonar issues
167
	public void actionPerformed(ActionEvent evt) {
a7aff3e3   Nathanael Jourdane   Fix Sonar issues
168
		if (((JButton) evt.getSource()).getName() == "btnSend") {
4cc84b63   Nathanael Jourdane   Add the possibili...
169
170
			try {
				mainView.getController().sendQuery(queryArea.getText());
4dd68979   Nathanael Jourdane   Improve Exception...
171
			} catch (VOTableException e) {
7706bfa4   Nathanael Jourdane   Use log4j as logg...
172
				logger.error("Can not send query when clicking on the send button.", e);
4cc84b63   Nathanael Jourdane   Add the possibili...
173
174
			}
		}
4a03be16   Nathanael Jourdane   Put queryArea in ...
175
176
177
	}

}