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

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import eu.omp.irap.vespa.votable.gui.VOTablePanelView;

/**
 * @author N. Jourdane
 */
public class ServicesPanelView extends VOTablePanelView {

	/** The serial version UID. */
	private static final long serialVersionUID = 1L;

	/** The text field to manually set the service URL. */
	private JTextField serviceUrlTextField;

	/** The text field to manually set the service table name. */
	private JTextField tableNameTextField;

	/** The listener of the services panel view. */
	private ServicesPanelListener listener;


	/**
	 * Method constructor which customize the services panel, but don't build it from scratch since
	 * VOTableView is already created by ServicesController. Add also the JTable listener.
	 *
	 * @param listener The listener of the services panel.
	 */
	public ServicesPanelView(final ServicesPanelListener listener) {
		super(listener);
		this.listener = listener;
		buildServicesPanel();
	}

	/**
	 * Add to a textField a listener to listen for text update events (changed, inserted, removed).
	 *
	 * @param textField the textField to listen.
	 */
	public void addEventListener(JTextField textField) {
		textField.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent e) {
				listener.onServiceListUpdated();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				listener.onServiceListUpdated();
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				listener.onServiceListUpdated();
			}
		});
	}

	/**
	 * Returns the field to set a custom service URL. Create it if doesn't exist.
	 *
	 * @return The service URL JTextField.
	 */
	public JTextField getServiceUrlTextField() {
		return serviceUrlTextField;
	}

	/**
	 * Returns the field to set a custom table name. Create it if doesn't exist.
	 *
	 * @return The table name JTextField.
	 */
	public JTextField getTableNameTextField() {
		return tableNameTextField;
	}

	/** Build the service panel and add the graphical elements on it. */
	private void buildServicesPanel() {
		serviceUrlTextField = new JTextField(10);
		addEventListener(serviceUrlTextField);
		tableNameTextField = new JTextField(10);
		addEventListener(tableNameTextField);

		JPanel addServicePanel = new JPanel();
		addServicePanel.add(new JLabel("Service URL"));
		addServicePanel.add(serviceUrlTextField);
		addServicePanel.add(new JLabel("Table name"));
		addServicePanel.add(tableNameTextField);

		add(addServicePanel, BorderLayout.SOUTH);
	}

}
