/*
 * 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.gui.requestpanel;

import java.io.File;

import eu.omp.irap.vespa.epntapclient.epntap.request.RequestCtrl;
import eu.omp.irap.vespa.epntapclient.gui.mainpanel.MainPanelListener;

/**
 * @author N. Jourdane
 */
public class RequestPanelCtrl extends RequestCtrl implements RequestPanelListener {

	/** The controller of the main panel. */
	private MainPanelListener listener;

	/** The view of the request panel. */
	private RequestPanelView view;


	/**
	 * Constructor of RequestPanelCtrl
	 *
	 * @param listener The listener of the main view.
	 */
	public RequestPanelCtrl(MainPanelListener listener) {
		this.listener = listener;
		view = new RequestPanelView(this);
	}

	/**
	 * @return The view of the request panel. Used in MainPanelCtrl to add panels in the main
	 *         window.
	 */
	public RequestPanelView getView() {
		return view;
	}

	@Override
	public void onParameterRemoved(String paramName) {
		removeParameter(paramName);
		updateQuery();
	}

	@Override
	public void onParameterUpdated(String paramName, Object paramValue) {
		updateParameter(paramName, paramValue);
		updateQuery();
	}

	@Override
	public void onSaveButtonClicked(File file) {
		listener.saveCurrentVOTable(file);
	}

	@Override
	public void onSendButtonClicked() {
		setQuery(view.getQueryArea().getText());
		listener.sendQueries();
	}

	/**
	 * Update the query and the query area with a working ADQL query, based on the parameters list.
	 */
	@Override
	public void updateQuery() {
		super.updateQuery();
		view.updateQueryArea(getQuery());
	}

}
