view src/samer/core_/util/AgentAdapter.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	AgentAdapter.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.core.util;
import  samer.core.*;
import  java.awt.*;
import  java.awt.event.*;

/**
	This converts action events and item state
	events (eg from buttons, menus, or check box
	items) into Agent commands.
  */

public class AgentAdapter implements ActionListener, ItemListener
{
	private Agent		agent;

	public AgentAdapter(Agent a) { agent=a; }

	public Agent getAgent() { return agent; }
	
	public void actionPerformed(ActionEvent e) {
		try {	agent.execute(e.getActionCommand(), new UserEnvironment(Shell.env())); }
		catch (Exception ex) {
			Shell.print("*** error in agent action: "+agent);
			Shell.print("*** exception: "+ex);
			ex.printStackTrace();
		}
	}

	/**
		This sends provides a boolean argument to the command
		The value comes from the new state of the item.
	  */

	public void itemStateChanged(ItemEvent e)	
	{
		Object	obj=e.getItem();
		String	cmd;

		if (obj instanceof String) cmd = (String)obj;
		else if (obj instanceof Component) cmd = ((Component)obj).getName();
		else return;
		if (cmd==null) return;

		boolean	state = (e.getStateChange()==ItemEvent.SELECTED);
		try { agent.execute(cmd,new UserEnvironment(Shell.env(),state)); }
		catch (Exception ex) {
			Shell.print("*** error in agent action: "+agent);
			Shell.print("*** exception: "+ex);
			ex.printStackTrace();
		}
	}
}