source: trunk/src/org/expeditee/agents/SearchTreeFast.java@ 291

Last change on this file since 291 was 291, checked in by ra33, 16 years ago

Fixed up search stuff... to adjust to getting params off the cursor

File size: 3.1 KB
Line 
1package org.expeditee.agents;
2
3import java.io.BufferedReader;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.HashSet;
10import java.util.LinkedList;
11import java.util.Map;
12
13import org.expeditee.gui.Frame;
14import org.expeditee.gui.FrameGraphics;
15import org.expeditee.gui.FrameIO;
16import org.expeditee.gui.UserSettings;
17import org.expeditee.io.Conversion;
18
19public class SearchTreeFast extends SearchAgent {
20 private Map<String, Collection<String>> _searchResults = new HashMap<String, Collection<String>>();
21
22 public SearchTreeFast(String searchText) {
23 super(searchText);
24 }
25
26 @Override
27 protected Frame process(Frame frame) {
28 if(frame == null)
29 frame = FrameIO.LoadFrame(_startName);
30
31 searchTree(frame.getName(), _pattern, _searchResults, new HashSet<String>());
32
33 _results.save();
34
35 String resultFrameName = _results.getName();
36 if (_clicked != null)
37 _clicked.setLink(resultFrameName);
38
39 return _results.getFirstFrame();
40 }
41
42 /**
43 * Returns a list of the frames searched and any matches on those frames.
44 * @param frameName the name of the top not in the tree of frames to search
45 * @param pattern the pattern to search for
46 * @param results a list of frames on which matches were found and the text that matched the pattern
47 * @param visited a list of the frames that were visited in the searchTree
48 */
49 public void searchTree(String frameName, String pattern, Map<String, Collection<String>> results, Collection<String> visited) {
50 //Check if this node has already been visited
51 if(visited.contains(frameName))
52 return;
53
54 visited.add(frameName);
55
56 String fullPath = null;
57 for (String possiblePath : UserSettings.FrameDirs) {
58 fullPath = FrameIO.getFrameFullPathName(possiblePath, frameName);
59 if (fullPath != null)
60 break;
61 }
62
63 // If the frame was not located return null
64 if (fullPath == null)
65 return;
66
67 _frameCount++;
68 overwriteMessage("Searching " + frameName);
69
70 String frameset = Conversion.getFramesetName(frameName);
71
72 Collection<String> frameResults = new LinkedList<String>();
73 // Open the file and search the text items
74 try {
75 BufferedReader reader = new BufferedReader(new FileReader(fullPath));
76 String next;
77 while (reader.ready() && ((next = reader.readLine()) != null)) {
78 if (next.startsWith("T")) {
79 String toSearch = next.substring(2);
80 if (toSearch.toLowerCase().contains(pattern))
81 frameResults.add(toSearch);
82 }else if (next.startsWith("F")) {
83 String link = next.substring(2);
84 if(!FrameIO.isValidFrameName(link))
85 link = frameset + link;
86 searchTree(link, pattern, results, visited);
87 }
88 }
89 //Only add the results if a match was found on the frame
90 if(frameResults.size() > 0){
91 results.put(frameName, frameResults);
92 }
93 } catch (FileNotFoundException e) {
94 e.printStackTrace();
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 int size = frameResults.size();
99 if(size > 0){
100 String repeats = size > 1? ("("+ size+ ")") : "";
101 _results.addText(frameName + repeats, null, frameName, null, false);
102 FrameGraphics.requestRefresh(true);
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.