source: trunk/src/org/expeditee/agents/DefaultAgent.java@ 309

Last change on this file since 309 was 179, checked in by ra33, 16 years ago

Made a few minor changes...

Also

For interactive widgets the border will not be selected if the user is inside the enclosed area

File size: 4.1 KB
Line 
1package org.expeditee.agents;
2
3import org.expeditee.gui.Frame;
4import org.expeditee.gui.FrameGraphics;
5import org.expeditee.gui.MessageBay;
6import org.expeditee.gui.TimeKeeper;
7import org.expeditee.items.Item;
8import org.expeditee.stats.SessionStats;
9
10/**
11 * The framework for loading of agents accounts for two possible constructors.
12 * The first takes no parametres and is called when the agent is run without
13 * parametres. The second takes a single string parametre which is the
14 * constructor called when an agent is run with a parametre.
15 *
16 * @author johnathon, mike
17 *
18 */
19public abstract class DefaultAgent implements Agent {
20 public static final String CLIPBOARD = "Clipboard";
21
22 protected long _timeRemaining = 0;
23
24 // The shortest delay between frames
25 public static final long TIMER_RESOLUTION = 10;
26
27 protected Item _clicked = null;
28
29 protected Frame _start = null;
30
31 protected Frame _end = null;
32
33 protected boolean _running = true;
34
35 protected boolean _stop = false;
36
37 protected int _frameCount = 0;
38
39 protected int _itemCount = 0;
40
41 protected TimeKeeper _timer;
42
43 // delay between frames, in ms
44 protected long _delay = 0;
45
46 public DefaultAgent(String delay) {
47 super();
48 try {
49 _delay = (int) (Double.parseDouble(delay) * 1000);
50 } catch (Exception e) {
51 }
52 }
53
54 public DefaultAgent() {
55 super();
56 }
57
58 /**
59 * Performs any post-processing actions, such as displaying a completion
60 * message to the user
61 *
62 * @param frame
63 * The starting Frame this Agent was executed on
64 */
65 protected void finalise(Frame start) {
66 if (_frameCount == 0)
67 _frameCount++;
68
69 int framesPerSecond = (int) Math.round(_frameCount
70 / (_timer.getElapsedMillis() / 1000.0));
71 String stats = (_itemCount > 0 ? ("Items: " + _itemCount + ", ") : "")
72 + (_frameCount > 1 ? ("Frames: " + _frameCount + ", ") : "")
73 + "Time: " + _timer.getElapsedStringSeconds()
74 + (framesPerSecond > 1 ? (", FPS: " + framesPerSecond) : "");
75 String msg = this.getClass().getSimpleName() + " stats- ";
76
77 message(msg + stats);
78 }
79
80 /**
81 * Performs any pre-processing of the starting frame, which may include
82 * searching the Frame for tags that determine the Agent behaviour
83 *
84 * @param start
85 */
86 public boolean initialise(Frame init, Item launcher) {
87 _start = init;
88 _clicked = launcher;
89 message("Starting " + this.getClass().getSimpleName() + "...");
90 _timer = new TimeKeeper();
91 return true;
92 }
93
94 public void run() {
95 SessionStats.setEnabled(false);
96 if (_start != null)
97 _start.change();
98 _end = process(_start);
99
100 finalise(_start);
101
102 _running = false;
103
104 FrameGraphics.requestRefresh(true);
105 SessionStats.setEnabled(true);
106 }
107
108 public boolean hasResultFrame() {
109 return _end != null;
110 }
111
112 public boolean hasResultString() {
113 return false;
114 }
115
116 public Frame getResultFrame() {
117 return _end;
118 }
119
120 /**
121 * Processes the given Frame, behaviour depends on individual
122 * implementation. If this JAG displays any kind of completion Frame to the
123 * user it should be returned from this method, otherwise null can be
124 * returned.
125 *
126 * @param frame
127 * the Frame to process
128 * @return The completion Frame to show to the user, or null
129 */
130 protected abstract Frame process(Frame frame);
131
132 /**
133 * Displays a message to the user
134 *
135 * @param message
136 * The message to display to the user
137 */
138 protected void message(String message) {
139 MessageBay.displayMessageAlways(message);
140 }
141
142 protected void overwriteMessage(String message) {
143 MessageBay.overwriteMessage(message);
144 }
145
146 public boolean isRunning() {
147 return _running;
148 }
149
150 public void stop() {
151 _timeRemaining = 0;
152 _stop = true;
153 }
154
155 public void interrupt() {
156 if (_timeRemaining > 0)
157 _timeRemaining = 0;
158 else
159 stop();
160 }
161
162 /**
163 * Pauses the execution of this Agent for the given time period (in ms.)
164 * This is used for the delay between displaying frames.
165 *
166 * @param time
167 */
168 protected void pause(long time) {
169 try {
170 if (time < 0)
171 _timeRemaining = Long.MAX_VALUE;
172 else
173 _timeRemaining = time;
174
175 while (_timeRemaining > 0) {
176 Thread.yield();
177 Thread.sleep(TIMER_RESOLUTION);
178 _timeRemaining -= TIMER_RESOLUTION;
179 }
180 } catch (InterruptedException e) {
181 }
182 }
183}
Note: See TracBrowser for help on using the repository browser.