RequestView.java 7.99 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.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

		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() {
		JTextField jtfTargetName = new JTextField("Galileo");
		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));
		JFormattedTextField jtfMaxRows = new JFormattedTextField(new Integer(10));

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

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

		jtfTargetName.getDocument().addDocumentListener(paramListener);
		jtfTargetName.setToolTipText("The target name, for example 'Jupiter'.");
		paramPanel.add(jtfTargetName);

		// Time
		JLabel timeLabel = new JLabel("Time (min, max)");
		paramPanel.add(timeLabel);
		JPanel timePanel = new JPanel(new GridLayout(0, 2));

		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);
		paramPanel.add(timePanel);

		// Data product type
		JLabel productTypeLabel = new JLabel("Product type");
		paramPanel.add(productTypeLabel);

		jtfProductType.getDocument().addDocumentListener(paramListener);
		jtfProductType.setToolTipText("The product type, for example '...'.");
		paramPanel.add(jtfProductType);

		JLabel spectralRangeLabel = new JLabel("Spectral range");
		paramPanel.add(spectralRangeLabel);
		JPanel spectralRangePanel = new JPanel(new GridLayout(0, 2));

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

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

	/**
	 * @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) {
		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));
	}

	/**
	 * @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);
			}
		}
	}

}