RequestView.java
6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* 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;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.omp.irap.vespa.epntapclient.utils.Queries;
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
/**
* @author N. Jourdane
*/
public class RequestView extends JPanel implements ActionListener {
/** The logger for this class. */
private static final Logger logger = LogManager.getLogger(RequestView.class);
/** The serial version UID (affected with a random number). */
private static final long serialVersionUID = 1262856496809315405L;
/** The EPN-TAP main view. */
private EpnTapMainView mainView;
/** The text area where the user put the query. */
private JTextArea queryArea;
/**
* The parameters fields for the request.
*/
private Map<String, JTextField> paramsFields;
/**
* The parameters fields for the request.
*/
private Map<String, Object> params;
/** The height of the buttons panel. */
private static final int BUTTON_PANEL_HEIGHT = 15;
/**
* Method constructor
*
* @param mainView The EPN-TAP main view.
*/
public RequestView(EpnTapMainView mainView) {
this.mainView = mainView;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// TODO: Get max row number from the GUI
params = new HashMap<>();
params.put("target_name", "Galileo");
params.put("dataproduct_type", "sp");
params.put("minTime", new Float(0.1));
params.put("maxTime", new Float(0.2));
params.put("spectral_range_min", new Float(0.0));
params.put("spectral_range_max", new Float(0.0));
this.add(buildParamPanel(), this);
this.add(buildQueryPanel(), this);
this.add(buildButtonPanel(), this);
}
/**
* @return A JPanel containing graphical elements for the service parameters.
*/
private JPanel buildParamPanel() {
paramsFields = new HashMap();
// 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 <, <=, =, =>, >.
for (Map.Entry<String, Object> param : params.entrySet()) {
Class paramClass = param.getValue().getClass();
if (paramClass == String.class) {
paramsFields.put(param.getKey(), new JTextField(param.getValue().toString()));
} else if (paramClass == Float.class) {
paramsFields.put(param.getKey(), new JFormattedTextField(param.getValue()));
} else if (paramClass == Integer.class) {
paramsFields.put(param.getKey(), new JFormattedTextField(param.getValue()));
} else {
logger.error("Unreconized parameter type " + param.getValue() + " for "
+ param.getKey() + ".");
}
}
DocumentListener paramListener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {
try {
for (Map.Entry<String, JTextField> e : paramsFields.entrySet()) {
Class paramClass = params.get(e.getKey()).getClass();
if (paramClass == String.class) {
params.put(e.getKey(), e.getValue().getText());
} else if (paramClass == Float.class || paramClass == Integer.class) {
params.put(e.getKey(), ((JFormattedTextField) e.getValue()).getValue());
} else {
logger.error("Unreconized parameter type " + e.getValue() + " for "
+ paramClass + ".");
}
}
updateQueryArea();
} 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);
}
};
JPanel paramPanel = new JPanel(new GridLayout(0, 2));
paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters"));
for (Map.Entry<String, JTextField> e : paramsFields.entrySet()) {
JLabel label = new JLabel(e.getKey());
paramPanel.add(label);
e.getValue().getDocument().addDocumentListener(paramListener);
// TODO: Add tooltip text based on rr.table_column.column_description
paramPanel.add(e.getValue());
}
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)"));
queryArea = new JTextArea("");
queryArea.setToolTipText("The query sent to the service(s).");
queryArea.setLineWrap(true);
queryPanel.setLayout(new BorderLayout());
queryPanel.add(queryArea, BorderLayout.CENTER);
return queryPanel;
}
/**
* Update the query JTextArea according to the parameters values.
*/
public void updateQueryArea() {
String tableName = mainView.getController().getSelectedTable();
queryArea.setText(Queries.getQuery(tableName, params, 10));
}
/**
* @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));
return buttonPanel;
}
@Override
public void actionPerformed(ActionEvent evt) {
if (((JButton) evt.getSource()).getName() == "btnSend") {
try {
mainView.getController().sendQuery(queryArea.getText());
} catch (VOTableException e) {
logger.error("Can not send query when clicking on the send button.", e);
}
}
}
}