source: trunk/org/expeditee/agents/TreeProcessor.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: 2.9 KB
Line 
1package org.expeditee.agents;
2
3import java.util.List;
4import java.util.Stack;
5
6import org.expeditee.gui.Frame;
7import org.expeditee.gui.FrameIO;
8import org.expeditee.items.Item;
9
10/**
11 * This class provides some generic methods that process a tree of Frames.
12 * Subclasses that only use text items can simply implement
13 * processTextItem(Text, int)
14 */
15public abstract class TreeProcessor extends DefaultAgent {
16
17 // the list of frames currently being processed
18 private Stack<FrameCounter> _frames = new Stack<FrameCounter>();
19
20 /**
21 * Processes the Frame by calling processItem() for each unlinked item
22 * encountered. Linked items call processStartLinkItem(), then load the
23 * linked frame and process it recursively, upon returning
24 * processEndLinkItem() is called.
25 */
26 protected Frame process(Frame toProcess) {
27 if (_stop)
28 return null;
29
30 if (toProcess == null)
31 return null;
32
33 _frames.push(new FrameCounter(toProcess.getFrameName(), -1));
34
35 // process the entire tree of frames in depth-first order
36 while (_frames.size() > 0) {
37 if (_stop)
38 return null;
39
40 FrameCounter cur = _frames.pop();
41
42 Frame next = FrameIO.LoadFrame(cur.frame);
43 if (next == null) {
44 message("There is no " + cur.frame + " Frame to display");
45 return null;
46 }
47
48 if (cur.index < 0) {
49 if (next != null)
50 overwriteMessage("Processing: " + next.getFrameName());
51 processFrame(next);
52 }
53
54 // the items on the frame currently being processed.
55 List<Item> items = next.getItems();
56
57 // resume from the next item in the list
58 for (int i = cur.index + 1; i < items.size(); i++) {
59 Item item = items.get(i);
60
61 // ignore annotation and framenames
62 if (!item.isAnnotation() && item.getID() >= 0) {
63 if (item.getLink() != null) {
64 cur.index = i;
65 _frames.push(cur);
66
67 Frame linked = FrameIO.LoadFrame(item.getAbsoluteLink());
68
69 // if the linked frame was found, then display it next
70 if (linked != null) {
71 FrameCounter fc = new FrameCounter(linked
72 .getFrameName(), -1);
73 if (!_frames.contains(fc)) {
74 // remember what frame we are on before
75 // processing it
76 _frames.push(fc);
77
78 // processFrame(linked);
79
80 // process the loaded frame immediately
81 // (depth-first)
82 break;
83 }
84 }
85 }
86 }
87 }
88 }
89
90 return null;
91 }
92
93 protected abstract void processFrame(Frame toProcess);
94
95 /**
96 * Inner class used to keep track of what frames have been seen, as well as
97 * what Item in the Frame the processing was up to. Only Frame names are
98 * stored to keep memory usage down.
99 */
100 private class FrameCounter {
101 public int index;
102
103 public String frame;
104
105 public FrameCounter(String f, int i) {
106 frame = f.toLowerCase();
107 index = i;
108 }
109
110 @Override
111 public boolean equals(Object o) {
112 if (o instanceof FrameCounter)
113 return (((FrameCounter) o).frame.equals(frame));// && fc.index
114 // == index);
115
116 return false;
117 }
118 }
119
120}
Note: See TracBrowser for help on using the repository browser.