[134] | 1 | package org.expeditee.agents;
|
---|
| 2 |
|
---|
| 3 | import java.util.Collection;
|
---|
| 4 | import java.util.HashSet;
|
---|
| 5 |
|
---|
| 6 | import org.expeditee.gui.Frame;
|
---|
| 7 | import org.expeditee.gui.FrameGraphics;
|
---|
| 8 | import org.expeditee.gui.FrameIO;
|
---|
| 9 | import org.expeditee.items.Text;
|
---|
| 10 |
|
---|
| 11 | public class SearchTree extends SearchAgent {
|
---|
[291] | 12 | public SearchTree(String searchText) {
|
---|
| 13 | super(searchText);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[134] | 16 | @Override
|
---|
| 17 | protected Frame process(Frame frame) {
|
---|
| 18 | try {
|
---|
| 19 | searchTree(new HashSet<String>(), _startName);
|
---|
| 20 | } catch (Exception e) {
|
---|
| 21 | e.printStackTrace();
|
---|
| 22 | }
|
---|
| 23 | _results.save();
|
---|
| 24 |
|
---|
| 25 | String resultFrameName = _results.getName();
|
---|
| 26 | if (_clicked != null)
|
---|
| 27 | _clicked.setLink(resultFrameName);
|
---|
| 28 |
|
---|
| 29 | return _results.getFirstFrame();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public boolean searchTree(Collection<String> visitedFrames, String frameName)
|
---|
| 33 | throws Exception {
|
---|
| 34 | if (_stop) {
|
---|
| 35 | return false;
|
---|
| 36 | }
|
---|
| 37 | // Avoid infinate loops
|
---|
| 38 | if (visitedFrames.contains(frameName))
|
---|
| 39 | return false;
|
---|
| 40 | visitedFrames.add(frameName);
|
---|
| 41 |
|
---|
| 42 | Frame frameToSearch = FrameIO.LoadFrame(frameName);
|
---|
| 43 | if (frameToSearch == null)
|
---|
| 44 | return false;
|
---|
| 45 |
|
---|
| 46 | overwriteMessage("Searching " + frameName);
|
---|
| 47 |
|
---|
[156] | 48 | for (Text itemToSearch : frameToSearch.getTextItems()) {
|
---|
[134] | 49 | // Search for the item and add it to the results page if
|
---|
| 50 | // it is found
|
---|
| 51 | if (searchItem(itemToSearch, _pattern,
|
---|
| 52 | _replacementString)) {
|
---|
| 53 | // Add a linked item to the results frame
|
---|
| 54 | _results.addText(frameName, null, frameName, null, false);
|
---|
| 55 | }
|
---|
| 56 | if (!itemToSearch.isAnnotation()) {
|
---|
| 57 | String link = itemToSearch.getAbsoluteLink();
|
---|
| 58 | if (link != null) {
|
---|
| 59 | searchTree(visitedFrames, link);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | FrameGraphics.requestRefresh(true);
|
---|
| 64 | FrameIO.SaveFrame(frameToSearch, false);
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|