1 | /**
|
---|
2 | * DisplayTree.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.agents;
|
---|
20 |
|
---|
21 | import org.expeditee.gui.DisplayIO;
|
---|
22 | import org.expeditee.gui.Frame;
|
---|
23 | import org.expeditee.gui.MessageBay;
|
---|
24 | import org.expeditee.items.Item;
|
---|
25 | import org.expeditee.items.ItemUtils;
|
---|
26 | import org.expeditee.items.Text;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Displays the Tree of Frames starting from the given frame. The delay between
|
---|
30 | * frames can be adjusted, either per-frame with the
|
---|
31 | *
|
---|
32 | * @DisplayFramePause tag, or mid-stream with the
|
---|
33 | * @DisplayTreePause tag, both tags should be followed with the desired delay in
|
---|
34 | * ms. The default delay between Frames is 50ms.
|
---|
35 | * @author jdm18
|
---|
36 | *
|
---|
37 | */
|
---|
38 | public class DisplayTree extends TreeProcessor {
|
---|
39 |
|
---|
40 | public static final int GARBAGE_COLLECTION_THRESHOLD = 100000;
|
---|
41 |
|
---|
42 | private Runtime _runtime = Runtime.getRuntime();
|
---|
43 |
|
---|
44 | public DisplayTree(String delay) {
|
---|
45 | super(delay);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public DisplayTree() {
|
---|
49 | super();
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public boolean initialise(Frame start, Item launcher) {
|
---|
54 |
|
---|
55 | // push current frame on to back-stack
|
---|
56 | DisplayIO.addToBack(start);
|
---|
57 |
|
---|
58 | return super.initialise(start, launcher);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | protected void finalise(Frame start) {
|
---|
63 | // return the user to the Frame they started on
|
---|
64 | if (!_stop) {
|
---|
65 | DisplayIO.Back();
|
---|
66 | }
|
---|
67 | super.finalise(start);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | protected void processFrame(Frame toProcess) {
|
---|
72 | long freeMemory = _runtime.freeMemory();
|
---|
73 | if (freeMemory < GARBAGE_COLLECTION_THRESHOLD) {
|
---|
74 | _runtime.gc();
|
---|
75 | MessageBay.displayMessage("Force Garbage Collection!");
|
---|
76 | }
|
---|
77 |
|
---|
78 | // FrameUtils.ResponseTimer.restart();
|
---|
79 |
|
---|
80 | // ignore loops
|
---|
81 | if (toProcess != DisplayIO.getCurrentFrame())
|
---|
82 | DisplayIO.setCurrentFrame(toProcess, false);
|
---|
83 | // parse the frame for any pause settings
|
---|
84 | delay(toProcess);
|
---|
85 |
|
---|
86 | _frameCount++;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Parses through the given Frame to find any
|
---|
91 | *
|
---|
92 | * @DisplayTreePause or
|
---|
93 | * @DisplayFramePause tags, and updates the delay or pause as necessary
|
---|
94 | * @param toSearch
|
---|
95 | */
|
---|
96 | private void delay(Frame toSearch) {
|
---|
97 | // check for change in globaly delay time
|
---|
98 | Item delay = ItemUtils.FindTag(toSearch.getItems(),
|
---|
99 | "@DisplayTreePause:");
|
---|
100 |
|
---|
101 | if (delay != null) {
|
---|
102 | try {
|
---|
103 | // attempt to read in the delay value
|
---|
104 | _delay = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
105 | .getFirstLine(), "@DisplayTreePause"));
|
---|
106 | message("DisplayTree delay changed to: " + _delay + "ms");
|
---|
107 | } catch (NumberFormatException nfe) {
|
---|
108 | message("Incorrect paramter for DisplayTreePause");
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | // check for change in delay for this frame only
|
---|
114 | delay = ItemUtils.FindTag(toSearch.getItems(), "@DisplayFramePause:");
|
---|
115 | if (delay != null) {
|
---|
116 | try {
|
---|
117 | // attempt to read in the delay value
|
---|
118 | long pause = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
119 | .getFirstLine(), "@DisplayFramePause"));
|
---|
120 | pause(pause);
|
---|
121 | } catch (NumberFormatException nfe) {
|
---|
122 | message("Incorrect paramter for DisplayFramePause");
|
---|
123 | }
|
---|
124 | } else
|
---|
125 | pause(_delay);
|
---|
126 | }
|
---|
127 | }
|
---|