source: trunk/src/org/expeditee/agents/SearchTreeAndReplace.java@ 636

Last change on this file since 636 was 376, checked in by ra33, 16 years ago
File size: 1.6 KB
Line 
1package org.expeditee.agents;
2
3import java.util.Collection;
4import java.util.HashSet;
5
6import org.expeditee.gui.Frame;
7import org.expeditee.gui.FrameGraphics;
8import org.expeditee.gui.FrameIO;
9import org.expeditee.items.Text;
10
11public class SearchTreeAndReplace extends SearchAgent {
12 public SearchTreeAndReplace(String searchText) {
13 super(searchText);
14 }
15
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
48 for (Text itemToSearch : frameToSearch.getTextItems()) {
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}
Note: See TracBrowser for help on using the repository browser.