comparison src/samer/core_/util/AgentAdapter.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * AgentAdapter.java
3 *
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
5 * All rights reserved.
6 *
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 */
11
12 package samer.core.util;
13 import samer.core.*;
14 import java.awt.*;
15 import java.awt.event.*;
16
17 /**
18 This converts action events and item state
19 events (eg from buttons, menus, or check box
20 items) into Agent commands.
21 */
22
23 public class AgentAdapter implements ActionListener, ItemListener
24 {
25 private Agent agent;
26
27 public AgentAdapter(Agent a) { agent=a; }
28
29 public Agent getAgent() { return agent; }
30
31 public void actionPerformed(ActionEvent e) {
32 try { agent.execute(e.getActionCommand(), new UserEnvironment(Shell.env())); }
33 catch (Exception ex) {
34 Shell.print("*** error in agent action: "+agent);
35 Shell.print("*** exception: "+ex);
36 ex.printStackTrace();
37 }
38 }
39
40 /**
41 This sends provides a boolean argument to the command
42 The value comes from the new state of the item.
43 */
44
45 public void itemStateChanged(ItemEvent e)
46 {
47 Object obj=e.getItem();
48 String cmd;
49
50 if (obj instanceof String) cmd = (String)obj;
51 else if (obj instanceof Component) cmd = ((Component)obj).getName();
52 else return;
53 if (cmd==null) return;
54
55 boolean state = (e.getStateChange()==ItemEvent.SELECTED);
56 try { agent.execute(cmd,new UserEnvironment(Shell.env(),state)); }
57 catch (Exception ex) {
58 Shell.print("*** error in agent action: "+agent);
59 Shell.print("*** exception: "+ex);
60 ex.printStackTrace();
61 }
62 }
63 }