[919] | 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 |
|
---|
[4] | 19 | package org.expeditee.agents;
|
---|
| 20 |
|
---|
[1102] | 21 | import org.expeditee.gui.DisplayController;
|
---|
[4] | 22 | import org.expeditee.gui.Frame;
|
---|
[121] | 23 | import org.expeditee.gui.MessageBay;
|
---|
[4] | 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 |
|
---|
[42] | 40 | public static final int GARBAGE_COLLECTION_THRESHOLD = 100000;
|
---|
| 41 |
|
---|
[25] | 42 | private Runtime _runtime = Runtime.getRuntime();
|
---|
[70] | 43 |
|
---|
[4] | 44 | public DisplayTree(String delay) {
|
---|
[70] | 45 | super(delay);
|
---|
[4] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public DisplayTree() {
|
---|
| 49 | super();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | @Override
|
---|
[133] | 53 | public boolean initialise(Frame start, Item launcher) {
|
---|
[4] | 54 |
|
---|
| 55 | // push current frame on to back-stack
|
---|
[1102] | 56 | DisplayController.addToBack(start);
|
---|
[4] | 57 |
|
---|
[133] | 58 | return super.initialise(start, launcher);
|
---|
[4] | 59 | }
|
---|
[70] | 60 |
|
---|
[45] | 61 | @Override
|
---|
| 62 | protected void finalise(Frame start) {
|
---|
| 63 | // return the user to the Frame they started on
|
---|
| 64 | if (!_stop) {
|
---|
[1102] | 65 | DisplayController.Back();
|
---|
[45] | 66 | }
|
---|
| 67 | super.finalise(start);
|
---|
| 68 | }
|
---|
[4] | 69 |
|
---|
| 70 | @Override
|
---|
| 71 | protected void processFrame(Frame toProcess) {
|
---|
[25] | 72 | long freeMemory = _runtime.freeMemory();
|
---|
[45] | 73 | if (freeMemory < GARBAGE_COLLECTION_THRESHOLD) {
|
---|
[25] | 74 | _runtime.gc();
|
---|
[121] | 75 | MessageBay.displayMessage("Force Garbage Collection!");
|
---|
[42] | 76 | }
|
---|
[45] | 77 |
|
---|
| 78 | // FrameUtils.ResponseTimer.restart();
|
---|
| 79 |
|
---|
[4] | 80 | // ignore loops
|
---|
[1102] | 81 | if (toProcess != DisplayController.getCurrentFrame())
|
---|
| 82 | DisplayController.setCurrentFrame(toProcess, false);
|
---|
[4] | 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
|
---|
[1415] | 98 | Item delay = ItemUtils.FindTag(toSearch.getSortedItems(),
|
---|
[4] | 99 | "@DisplayTreePause:");
|
---|
[108] | 100 |
|
---|
[4] | 101 | if (delay != null) {
|
---|
| 102 | try {
|
---|
| 103 | // attempt to read in the delay value
|
---|
| 104 | _delay = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
[86] | 105 | .getFirstLine(), "@DisplayTreePause"));
|
---|
[4] | 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
|
---|
[1415] | 114 | delay = ItemUtils.FindTag(toSearch.getSortedItems(), "@DisplayFramePause:");
|
---|
[4] | 115 | if (delay != null) {
|
---|
| 116 | try {
|
---|
| 117 | // attempt to read in the delay value
|
---|
| 118 | long pause = Long.parseLong(ItemUtils.StripTag(((Text) delay)
|
---|
[86] | 119 | .getFirstLine(), "@DisplayFramePause"));
|
---|
[4] | 120 | pause(pause);
|
---|
| 121 | } catch (NumberFormatException nfe) {
|
---|
| 122 | message("Incorrect paramter for DisplayFramePause");
|
---|
| 123 | }
|
---|
| 124 | } else
|
---|
| 125 | pause(_delay);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|