/*
 * ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 * if any, must include the following acknowledgment:
 * "This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/)."
 * Alternately, this acknowledgment may appear in the software itself,
 * if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 * "Apache JMeter" must not be used to endorse or promote products
 * derived from this software without prior written permission. For
 * written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 * "Apache JMeter", nor may "Apache" appear in their name, without
 * prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package org.apache.jmeter.protocol.http.proxy.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import org.apache.jmeter.gui.*;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.protocol.http.proxy.ProxyControl;

/**
 * Title:        Jakarta-JMeter
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Apache
 * @author Michael Stover
 * @version 1.0
 */

public class ProxyControlGui extends JPanel implements ModelSupported,ActionListener,
		KeyListener
{
	private static final String STOP = "stop";
	private static final String START = "start";
	private static final String ADD_INCLUDE = "add_include";
	private static final String ADD_EXCLUDE = "add_exclude";
	private static final String CLEAR_INCLUDE = "clear_include";
	private static final String CLEAR_EXCLUDE = "clear_exclude";

	ProxyControl model;
	NamePanel namePanel;
	JTextField portField;

	JTextField addIncludeField;
	JList includeList;
	JButton clearInclude;

	JTextField addExcludeField;
	JList excludeList;
	JButton clearExclude;

	JButton stop,start;

	public ProxyControlGui()
	{
	}

	public void setModel(Object model)
	{
		this.model = (ProxyControl)model;
		init();
	}

	public void updateGui()
	{
		portField.setText(""+model.getPort());
		((JMeterJListModel)includeList.getModel()).setData(model.getIncludePatterns());
		((JMeterJListModel)excludeList.getModel()).setData(model.getExcludePatterns());
	}

	private void init()
	{
		this.setLayout(new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));

		// MAIN PANEL
		JPanel mainPanel = new JPanel();
		Border margin = new EmptyBorder(10, 10, 5, 10);
		mainPanel.setBorder(margin);
		mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));

		// TITLE
		JLabel panelTitleLabel = new JLabel(JMeterUtils.getResString("proxy_title"));
		Font curFont = panelTitleLabel.getFont();
		int curFontSize = curFont.getSize();
		curFontSize += 4;
		panelTitleLabel.setFont(new Font(curFont.getFontName(), curFont.getStyle(), curFontSize));
		mainPanel.add(panelTitleLabel);

		// NAME
		namePanel = new NamePanel(model);
		mainPanel.add(namePanel);

		mainPanel.add(createPortPanel());
		mainPanel.add(createIncludePanel());
		mainPanel.add(createExcludePanel());
		mainPanel.add(createControls());

		this.add(mainPanel);

	}

	private JPanel createControls()
	{
		JPanel panel = new JPanel();

		start = new JButton(JMeterUtils.getResString("start"));
		start.addActionListener(this);
		start.setActionCommand(START);

		stop = new JButton(JMeterUtils.getResString("stop"));
		stop.addActionListener(this);
		stop.setActionCommand(STOP);
		stop.setEnabled(false);

		panel.add(start);
		panel.add(stop);

		return panel;
	}

	private JPanel createPortPanel()
	{
		JPanel panel = new JPanel();

		panel.add(new JLabel(JMeterUtils.getResString("port")));

		portField = new JTextField(8);
		portField.setName(ProxyControl.PORT);
		portField.addKeyListener(this);
		portField.setText(""+model.getPort());
		panel.add(portField);

		return panel;
	}

	private JPanel createIncludePanel()
	{
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
				JMeterUtils.getResString("patterns_to_include")));
		panel.add(new JLabel(JMeterUtils.getResString("add_pattern")));

		addIncludeField = new JTextField(15);
		addIncludeField.addActionListener(this);
		addIncludeField.setActionCommand(ADD_INCLUDE);
		panel.add(addIncludeField);

		includeList = new JList(new JMeterJListModel());
		((JMeterJListModel)includeList.getModel()).setData(model.getIncludePatterns());
		includeList.setMinimumSize(new Dimension(20,10));
		includeList.setBorder(BorderFactory.createEtchedBorder());
		includeList.setVisibleRowCount(3);
		panel.add(includeList);

		clearInclude = new JButton(JMeterUtils.getResString("clear"));
		clearInclude.addActionListener(this);
		clearInclude.setActionCommand(CLEAR_INCLUDE);
		panel.add(clearInclude);

		return panel;
	}

	private JPanel createExcludePanel()
	{
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
				JMeterUtils.getResString("patterns_to_exclude")));
		panel.add(new JLabel(JMeterUtils.getResString("add_pattern")));

		addExcludeField = new JTextField(15);
		addExcludeField.addActionListener(this);
		addExcludeField.setActionCommand(ADD_EXCLUDE);
		panel.add(addExcludeField);

		excludeList = new JList(new JMeterJListModel());
		((JMeterJListModel)excludeList.getModel()).setData(model.getExcludePatterns());
		excludeList.setMinimumSize(new Dimension(20,10));
		excludeList.setBorder(BorderFactory.createEtchedBorder());
		excludeList.setVisibleRowCount(3);
		panel.add(excludeList);

		clearExclude = new JButton(JMeterUtils.getResString("clear"));
		clearExclude.addActionListener(this);
		clearExclude.setActionCommand(CLEAR_EXCLUDE);
		panel.add(clearExclude);

		return panel;
	}

	public void actionPerformed(ActionEvent action)
	{
		String command = action.getActionCommand();

		if(command.equals(STOP))
		{
			model.stopProxy();
			stop.setEnabled(false);
			start.setEnabled(true);
		}
		else if(command.equals(START))
		{
			model.startProxy();
			start.setEnabled(false);
			stop.setEnabled(true);
		}
		else if(command.equals(this.ADD_EXCLUDE))
		{
			String exclude = addExcludeField.getText();
			addExcludeField.setText("");
			model.addExcludedPattern(exclude);
			((JMeterJListModel)excludeList.getModel()).addItem(exclude);
		}
		else if(command.equals(this.ADD_INCLUDE))
		{
			String include = addIncludeField.getText();
			addIncludeField.setText("");
			model.addIncludedPattern(include);
			((JMeterJListModel)includeList.getModel()).addItem(include);
		}
		else if(command.equals(this.CLEAR_EXCLUDE))
		{
			((JMeterJListModel)excludeList.getModel()).clear();
			model.clearExcludedPatterns();
		}
		else if(command.equals(this.CLEAR_INCLUDE))
		{
			((JMeterJListModel)includeList.getModel()).clear();
			model.clearIncludedPatterns();
		}
	}

	public void keyPressed(KeyEvent e)
	{
	}

	public void keyTyped(KeyEvent e)
	{
	}

	public void keyReleased(KeyEvent e)
	{
		String fieldName = e.getComponent().getName();

		if (fieldName.equals(ProxyControl.PORT)) 
		{
			try 
			{
				model.setPort(Integer.parseInt(portField.getText()));
			} 
			catch (NumberFormatException nfe) 
			{
				if (portField.getText().length() > 0)
				{
					JOptionPane.showMessageDialog(this, "You must enter a valid number",
							"Invalid data", JOptionPane.WARNING_MESSAGE);
					model.setPort(model.getDefaultPort());

					// Right now, the cleanest thing to do is simply clear the 
					// entire text field. We do not want to set the text to
					// the default because that would be confusing to the user.
					// For example, the user typed "5t" instead of "56". After
					// the user closes the error dialog, the text would change
					// from "5t" to "1".  A litle confusing. If anything, it
					// should display just "5". Future enhancement...
					portField.setText("");
				}
				else
				{
					model.setPort(model.getDefaultPort());
				}
			}
		}
	}
}

