1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import org.expeditee.gui.DisplayIO;
|
---|
4 | import org.expeditee.gui.Frame;
|
---|
5 | import org.expeditee.gui.MessageBay;
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 | import org.expeditee.items.ItemUtils;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Displays the Tree of Frames starting from the given frame. The delay between
|
---|
12 | * frames can be adjusted, either per-frame with the
|
---|
13 | *
|
---|
14 | * @DisplayFramePause tag, or mid-stream with the
|
---|
15 | * @DisplayTreePause tag, both tags should be followed with the desired delay in
|
---|
16 | * ms. The default delay between Frames is 50ms.
|
---|
17 | * @author jdm18
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public class DisplayTree extends TreeProcessor {
|
---|
21 |
|
---|
22 | public static final int GARBAGE_COLLECTION_THRESHOLD = 100000;
|
---|
23 |
|
---|
24 | private Runtime _runtime = Runtime.getRuntime();
|
---|
25 |
|
---|
26 | public DisplayTree(String delay) {
|
---|
27 | super(delay);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public DisplayTree() {
|
---|
31 | super();
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public boolean initialise(Frame start) {
|
---|
36 |
|
---|
37 | // push current frame on to back-stack
|
---|
38 | DisplayIO.addToBack(start);
|
---|
39 |
|
---|
40 | return super.initialise(start);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected void finalise(Frame start) {
|
---|
45 | // return the user to the Frame they started on
|
---|
46 | if (!_stop) {
|
---|
47 | DisplayIO.Back();
|
---|
48 | }
|
---|
49 | super.finalise(start);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | protected void processFrame(Frame toProcess) {
|
---|
54 | long freeMemory = _runtime.freeMemory();
|
---|
55 | if (freeMemory < GARBAGE_COLLECTION_THRESHOLD) {
|
---|
56 | _runtime.gc();
|
---|
57 | MessageBay.displayMessage("Force Garbage Collection!");
|
---|
58 | }
|
---|
59 |
|
---|
60 | // FrameUtils.ResponseTimer.restart();
|
---|
61 |
|
---|
62 | // ignore loops
|
---|
63 | if (toProcess != DisplayIO.getCurrentFrame())
|
---|
64 | DisplayIO.setCurrentFrame(toProcess);
|
---|
65 | // FrameUtils.DisplayFrame(toProcess,false);
|
---|
66 | // parse the frame for any pause settings
|
---|
67 | delay(toProcess);
|
---|
68 |
|
---|
69 | _frameCount++;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Parses through the given Frame to find any
|
---|
74 | *
|
---|
75 | * @DisplayTreePause or
|
---|
76 | * @DisplayFramePause tags, and updates the delay or pause as necessary
|
---|
77 | * @param toSearch
|
---|
78 | */
|
---|
79 | private void delay(Frame toSearch) {
|
---|
80 | // check for change in globaly delay time
|
---|
81 | Item delay = ItemUtils.FindTag(toSearch.getItems(),
|
---|
82 | "@DisplayTreePause:");
|
---|
83 |
|
---|
84 | if (delay != null) {
|
---|
85 | try {
|
---|
86 | // attempt to read in the delay value
|
---|
87 | _delay = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
88 | .getFirstLine(), "@DisplayTreePause"));
|
---|
89 | message("DisplayTree delay changed to: " + _delay + "ms");
|
---|
90 | } catch (NumberFormatException nfe) {
|
---|
91 | message("Incorrect paramter for DisplayTreePause");
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | // check for change in delay for this frame only
|
---|
97 | delay = ItemUtils.FindTag(toSearch.getItems(), "@DisplayFramePause:");
|
---|
98 | if (delay != null) {
|
---|
99 | try {
|
---|
100 | // attempt to read in the delay value
|
---|
101 | long pause = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
102 | .getFirstLine(), "@DisplayFramePause"));
|
---|
103 | pause(pause);
|
---|
104 | } catch (NumberFormatException nfe) {
|
---|
105 | message("Incorrect paramter for DisplayFramePause");
|
---|
106 | }
|
---|
107 | } else
|
---|
108 | pause(_delay);
|
---|
109 | }
|
---|
110 | }
|
---|