RequestView.java 5.68 KB
/*
 * 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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

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.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;
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;

	// TODO: Use one map for paramFields, paramValues, paramTypes.

	/**
	 * The parameters fields for the request.
	 */
	private List<ParamField> paramFields;

	/**
	 * The parameters fields for the request.
	 */
	private Map<String, Object> paramValues;

	/** 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

		paramValues = new HashMap<>();

		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() {
		// 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 <, <=, =, =>, >.
		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"));

		JPanel paramPanel = new JPanel();
		paramPanel.setLayout(new BoxLayout(paramPanel, BoxLayout.Y_AXIS));
		paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters"));

		for (ParamField field : paramFields) {
			paramPanel.add(field);
		}

		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;
	}

	public void updateParam(String paramName, Object value) {
		if (value == null) {
			paramValues.remove(paramName);
			logger.info("removed " + paramName);
		} else {
			paramValues.put(paramName, value);
			logger.info("uploaded " + paramName + ": " + value);
		}
		updateQueryArea();
	}

	/**
	 * Update the query JTextArea according to the parameters values.
	 */
	public void updateQueryArea() {
		String tableName = mainView.getController().getSelectedTable();
		queryArea.setText(Queries.getQuery(tableName, paramValues, 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);
			}
		}
	}

}