source: trunk/org/expeditee/agents/DisplayTree.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 3.8 KB
Line 
1package org.expeditee.agents;
2
3import org.expeditee.gui.DisplayIO;
4import org.expeditee.gui.Frame;
5import org.expeditee.gui.FrameUtils;
6import org.expeditee.gui.TimeKeeper;
7import org.expeditee.items.Item;
8import org.expeditee.items.ItemUtils;
9import org.expeditee.items.Text;
10
11/**
12 * Displays the Tree of Frames starting from the given frame. The delay between
13 * frames can be adjusted, either per-frame with the
14 *
15 * @DisplayFramePause tag, or mid-stream with the
16 * @DisplayTreePause tag, both tags should be followed with the desired delay in
17 * ms. The default delay between Frames is 50ms.
18 * @author jdm18
19 *
20 */
21public class DisplayTree extends TreeProcessor {
22
23 // delay between frames, in ms
24 private long _delay = 50;
25
26 // The shortest delay between frames
27 private static final long TIMER_RESOLUTION = 10;
28
29 private long _timeRemaining = 0;
30
31 private int _frameCount = 0;
32
33 private TimeKeeper _timer;
34
35 public DisplayTree(String delay) {
36 super();
37 try {
38 _delay = Long.parseLong(delay);
39 } catch (Exception e) {
40 }
41 }
42
43 public DisplayTree() {
44 super();
45 }
46
47 @Override
48 public boolean initialise(Frame start) {
49 _timer = new TimeKeeper();
50
51 // push current frame on to back-stack
52 DisplayIO.addToBack(start);
53
54 // MIKE: Doesnt think the line below is needed
55 // update the delays (if necessary)
56 // delay(start);
57
58 return true;
59 }
60
61 @Override
62 protected void processFrame(Frame toProcess) {
63 // ignore loops
64 if (toProcess != DisplayIO.getCurrentFrame())
65 FrameUtils.DisplayFrame(toProcess, false);
66
67 FrameUtils.ResponseTimer.restart();
68
69 // parse the frame for any pause settings
70 delay(toProcess);
71
72 _frameCount++;
73 }
74
75 /**
76 * Parses through the given Frame to find any
77 *
78 * @DisplayTreePause or
79 * @DisplayFramePause tags, and updates the delay or pause as necessary
80 * @param toSearch
81 */
82 private void delay(Frame toSearch) {
83 // check for change in globaly delay time
84 Item delay = ItemUtils.FindTag(toSearch.getItems(),
85 "@DisplayTreePause:");
86 if (delay != null) {
87 try {
88 // attempt to read in the delay value
89 _delay = Long.parseLong(ItemUtils.StripTag(((Text) delay)
90 .getFirstLine(), "@DisplayTreePause:"));
91 message("DisplayTree delay changed to: " + _delay + "ms");
92 } catch (NumberFormatException nfe) {
93 message("Incorrect paramter for DisplayTreePause");
94 }
95
96 }
97
98 // check for change in delay for this frame only
99 delay = ItemUtils.FindTag(toSearch.getItems(), "@DisplayFramePause:");
100 if (delay != null) {
101 try {
102 // attempt to read in the delay value
103 long pause = Long.parseLong(ItemUtils.StripTag(((Text) delay)
104 .getFirstLine(), "@DisplayFramePause:"));
105 pause(pause);
106 } catch (NumberFormatException nfe) {
107 message("Incorrect paramter for DisplayFramePause");
108 }
109 } else
110 pause(_delay);
111 }
112
113 @Override
114 protected void finalise(Frame start) {
115 if (_frameCount == 0)
116 _frameCount++;
117
118 long avg = _timer.getElapsedMillis() / _frameCount;
119 String stats = "Frames: " + _frameCount + ", Time: "
120 + _timer.getElapsedStringMillis() + ", Average: " + avg;
121 String msg = "Tree successfully displayed. ";
122
123 // return the user to the Frame they started on
124 if (!_stop) {
125 DisplayIO.Back();
126 } else {
127 msg = "Agent halted by user. ";
128 }
129
130 message(msg + stats);
131 }
132
133 /**
134 * Pauses the execution of this Agent for the given time period (in ms.)
135 * This is used for the delay between displaying frames.
136 *
137 * @param time
138 */
139 private void pause(long time) {
140 try {
141 if (time < 0)
142 _timeRemaining = Long.MAX_VALUE;
143 else
144 _timeRemaining = time;
145
146 while (_timeRemaining > 0) {
147 Thread.yield();
148 Thread.sleep(TIMER_RESOLUTION);
149 _timeRemaining -= TIMER_RESOLUTION;
150 }
151 } catch (InterruptedException e) {
152 }
153 }
154
155 @Override
156 public void interrupt() {
157 _timeRemaining = 0;
158 }
159
160 @Override
161 public void stop() {
162 _timeRemaining = 0;
163 super.stop();
164 }
165
166}
Note: See TracBrowser for help on using the repository browser.