samer@0: /* samer@0: * AgentAdapter.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util; samer@0: import samer.core.*; samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: samer@0: /** samer@0: This converts action events and item state samer@0: events (eg from buttons, menus, or check box samer@0: items) into Agent commands. samer@0: */ samer@0: samer@0: public class AgentAdapter implements ActionListener, ItemListener samer@0: { samer@0: private Agent agent; samer@0: samer@0: public AgentAdapter(Agent a) { agent=a; } samer@0: samer@0: public Agent getAgent() { return agent; } samer@0: samer@0: public void actionPerformed(ActionEvent e) { samer@0: try { agent.execute(e.getActionCommand(), new UserEnvironment(Shell.env())); } samer@0: catch (Exception ex) { samer@0: Shell.print("*** error in agent action: "+agent); samer@0: Shell.print("*** exception: "+ex); samer@0: ex.printStackTrace(); samer@0: } samer@0: } samer@0: samer@0: /** samer@0: This sends provides a boolean argument to the command samer@0: The value comes from the new state of the item. samer@0: */ samer@0: samer@0: public void itemStateChanged(ItemEvent e) samer@0: { samer@0: Object obj=e.getItem(); samer@0: String cmd; samer@0: samer@0: if (obj instanceof String) cmd = (String)obj; samer@0: else if (obj instanceof Component) cmd = ((Component)obj).getName(); samer@0: else return; samer@0: if (cmd==null) return; samer@0: samer@0: boolean state = (e.getStateChange()==ItemEvent.SELECTED); samer@0: try { agent.execute(cmd,new UserEnvironment(Shell.env(),state)); } samer@0: catch (Exception ex) { samer@0: Shell.print("*** error in agent action: "+agent); samer@0: Shell.print("*** exception: "+ex); samer@0: ex.printStackTrace(); samer@0: } samer@0: } samer@0: }