package ws.prova;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import org.mandarax.reference.VariableTermImpl;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Adaptor for Swing events. This class is created and used by ws.prova.Calc.
 * <p>Title: Prova </p>
 * <p>Description: Built-in predicates</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 2.0 <25 March 2007>
 */
public class SwingAdaptor implements
		ActionListener,
		ChangeListener,
		MouseListener {

	private reagent prova;

	private Messenger messenger;

	public SwingAdaptor(reagent prova) {
		this.prova = prova;
		this.messenger = prova.getMessenger();
	}

	/**
	 * actionPerformed
	 *
	 * @param e ActionEvent
	 */
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		Object cmd = e.getActionCommand();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[action,%0,%1,%2]", new Object[]{cmd, src, e})}, ret);
	}

	/**
	 * stateChanged
	 *
	 * @param e ChangeEvent
	 */
	public void stateChanged(ChangeEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[change,%0,%1]", new Object[]{src, e})}, ret);
	}

	/**
	 * mouseClicked
	 *
	 * @param e MouseEvent
	 */
	public void mouseClicked(MouseEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[mouse,clicked,%0,%1]", new Object[]{src, e})}, ret);
	}

	/**
	 * mouseEntered
	 *
	 * @param e MouseEvent
	 */
	public void mouseEntered(MouseEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[mouse,entered,%0,%1]", new Object[]{src, e})}, ret);
	}

	/**
	 * mouseExited
	 *
	 * @param e MouseEvent
	 */
	public void mouseExited(MouseEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[mouse,exited,%0,%1]", new Object[]{src, e})}, ret);
	}

	/**
	 * mousePressed
	 *
	 * @param e MouseEvent
	 */
	public void mousePressed(MouseEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[mouse,pressed,%0,%1]", new Object[]{src, e})}, ret);
	}

	/**
	 * mouseReleased
	 *
	 * @param e MouseEvent
	 */
	public void mouseReleased(MouseEvent e) {
		Object src = e.getSource();
		List ret = new ArrayList();
		messenger.sendMsg(new Object[]{new VariableTermImpl(), "self", "0", "swing",
								   RListUtils.rlist("[mouse,released,%0,%1]", new Object[]{e, src})}, ret);
	}
}
