Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java 5.85 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;
0d0b44fe   Nathanael Jourdane   All parameters fi...
24
25
import java.util.HashMap;
import java.util.Map;
4a03be16   Nathanael Jourdane   Put queryArea in ...
26

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

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

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

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

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

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

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

1bc7a17a   Nathanael Jourdane   Improve parameter...
58
	// TODO: Use one map for paramFields, paramValues, paramTypes.
0d0b44fe   Nathanael Jourdane   All parameters fi...
59
60
61
	/**
	 * The parameters fields for the request.
	 */
1bc7a17a   Nathanael Jourdane   Improve parameter...
62
	private Map<String, JTextField> paramFields;
0d0b44fe   Nathanael Jourdane   All parameters fi...
63
64
65
66

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

	private Map<String, Class> paramTypes;
0d0b44fe   Nathanael Jourdane   All parameters fi...
70

204e830a   Nathanael Jourdane   Add query paramet...
71
72
	/** The height of the buttons panel. */
	private static final int BUTTON_PANEL_HEIGHT = 15;
4a03be16   Nathanael Jourdane   Put queryArea in ...
73
74
75
76
77
78
79
80

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

c79e5664   Nathanael Jourdane   bugFix: Fields ar...
83
		// TODO: Get max row number from the GUI
1bc7a17a   Nathanael Jourdane   Improve parameter...
84
85
86
87
88
89
90
91
92
		// TODO: auto-building range field by checking *_min / *_max attribute names

		paramTypes = new HashMap<>();
		paramTypes.put("target_name", String.class);
		paramTypes.put("dataproduct_type", String.class);
		paramTypes.put("minTime", Float.class);
		paramTypes.put("maxTime", Float.class);
		paramTypes.put("spectral_range_min", Float.class);
		paramTypes.put("spectral_range_max", Float.class);
0d0b44fe   Nathanael Jourdane   All parameters fi...
93

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

204e830a   Nathanael Jourdane   Add query paramet...
96
97
98
		this.add(buildParamPanel(), this);
		this.add(buildQueryPanel(), this);
		this.add(buildButtonPanel(), this);
204e830a   Nathanael Jourdane   Add query paramet...
99
100
101
102
103
	}

	/**
	 * @return A JPanel containing graphical elements for the service parameters.
	 */
35919668   Nathanael Jourdane   Update the query ...
104
	private JPanel buildParamPanel() {
1bc7a17a   Nathanael Jourdane   Improve parameter...
105
		paramFields = new HashMap();
0d0b44fe   Nathanael Jourdane   All parameters fi...
106
107
108
109
110

		// TODO: Find a way to set numerical value to "" by default.
		// 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 <, <=, =, =>, >.
1bc7a17a   Nathanael Jourdane   Improve parameter...
111
112
113
		for (Map.Entry<String, Class> paramType : paramTypes.entrySet()) {
			paramFields.put(paramType.getKey(),
					new ParamField(this, paramType.getKey(), paramType.getValue()));
0d0b44fe   Nathanael Jourdane   All parameters fi...
114
		}
35919668   Nathanael Jourdane   Update the query ...
115

204e830a   Nathanael Jourdane   Add query paramet...
116
117
118
		JPanel paramPanel = new JPanel(new GridLayout(0, 2));
		paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters"));

1bc7a17a   Nathanael Jourdane   Improve parameter...
119
		for (Map.Entry<String, JTextField> e : paramFields.entrySet()) {
0d0b44fe   Nathanael Jourdane   All parameters fi...
120
121
			JLabel label = new JLabel(e.getKey());
			paramPanel.add(label);
204e830a   Nathanael Jourdane   Add query paramet...
122

0d0b44fe   Nathanael Jourdane   All parameters fi...
123
124
125
			// TODO: Add tooltip text based on rr.table_column.column_description
			paramPanel.add(e.getValue());
		}
204e830a   Nathanael Jourdane   Add query paramet...
126
127
128
129
130
131
132
133
134
135

		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 ...
136
		queryArea = new JTextArea("");
204e830a   Nathanael Jourdane   Add query paramet...
137
		queryArea.setToolTipText("The query sent to the service(s).");
5576be01   Nathanael Jourdane   Make panels resiz...
138
		queryArea.setLineWrap(true);
204e830a   Nathanael Jourdane   Add query paramet...
139
140
		queryPanel.setLayout(new BorderLayout());
		queryPanel.add(queryArea, BorderLayout.CENTER);
4cc84b63   Nathanael Jourdane   Add the possibili...
141

204e830a   Nathanael Jourdane   Add query paramet...
142
143
		return queryPanel;
	}
4cc84b63   Nathanael Jourdane   Add the possibili...
144

1bc7a17a   Nathanael Jourdane   Improve parameter...
145
146
147
148
149
	public void updateParam(String paramName, Object value) {
		logger.info("uploading " + paramName + ": " + value);
		paramValues.put(paramName, value);
	}

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

	/**
204e830a   Nathanael Jourdane   Add query paramet...
159
160
161
162
163
164
165
166
167
168
	 * @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 ...
169

204e830a   Nathanael Jourdane   Add query paramet...
170
		return buttonPanel;
4cc84b63   Nathanael Jourdane   Add the possibili...
171
172
173
	}

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

}