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