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

Last change on this file since 636 was 376, checked in by ra33, 16 years ago
File size: 4.6 KB
Line 
1package org.expeditee.agents;
2
3import java.util.Collection;
4
5import org.expeditee.gui.Frame;
6import org.expeditee.gui.FrameCreator;
7import org.expeditee.gui.FrameGraphics;
8import org.expeditee.gui.FrameIO;
9import org.expeditee.io.Conversion;
10import org.expeditee.items.Item;
11import org.expeditee.items.Text;
12
13public abstract class SearchAgent extends DefaultAgent {
14
15 private static final String DEFAULT_RESULTS_FRAMESET = "SearchResults";
16
17 public static final int SURROGATE_LENGTH = 50;
18
19 protected FrameCreator _results;
20
21 protected String _pattern;
22
23 protected String _replacementString;
24
25 protected String _startName;
26
27 public SearchAgent(String searchText) {
28 if (searchText != null)
29 _pattern = searchText.toLowerCase();
30 }
31
32 @Override
33 public boolean initialise(Frame frame, Item item) {
34 String pattern = item.getText();
35 String resultFrameset = null;
36
37 // TODO use a results frame specified on the profile frame
38 if (item.getLink() == null) {
39 resultFrameset = DEFAULT_RESULTS_FRAMESET;
40 } else {
41 resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
42 false);
43 }
44 return initialise(frame, item, getSearchDescription(frame), resultFrameset,
45 null, pattern);
46 }
47
48 /**
49 * @param frame
50 * @return
51 */
52 protected String getSearchDescription(Frame frame) {
53 return frame.getFramesetName();
54 }
55
56 /**
57 *
58 * @param frame
59 * @param item
60 * @param startName
61 * @param resultsFrameset
62 * @param replacementString
63 * @param pattern
64 * is ignored if the pattern has already been set earlier.
65 * @return
66 */
67 public boolean initialise(Frame frame, Item item, String startName,
68 String resultsFrameset, String replacementString, String pattern) {
69 // TODO: Put the init params in the constructor!! Dont want to be
70 // setting _pattern in two places!
71
72 if (_pattern == null)
73 _pattern = pattern.toLowerCase();
74 _replacementString = replacementString;
75 _startName = startName;
76
77 // Create a frame to put the results on with the search query
78 // and type as the title
79 String title = this.getClass().getSimpleName() + " [" + startName
80 + "]"+getResultsTitleSuffix();
81 _results = new FrameCreator(resultsFrameset, FrameIO.FRAME_PATH, title,
82 false, true);
83 // Set the frame to be displayed after running the agent
84 _end = _results.getFirstFrame();
85
86 return super.initialise(frame, item);
87 }
88
89 protected String getResultsTitleSuffix() {
90 return " [" + _pattern + "]";
91 }
92
93 public String getResultsFrameName() {
94 return _results.getName();
95 }
96
97 public static boolean searchItem(Text itemToSearch, String pattern,
98 String replacementString) {
99 String searchStr = itemToSearch.getText().toLowerCase();
100 boolean bFound = searchStr.contains(pattern.toLowerCase());
101 // If it is a find and replace... then replace with the replacement
102 // string
103 if (bFound && replacementString != null) {
104 itemToSearch.setText(searchStr.replaceAll(pattern,
105 replacementString));
106 }
107 return bFound;
108 }
109
110 public static boolean searchFrame(FrameCreator results, String frameName,
111 String pattern, String replacementString) {
112 int oldMode = FrameGraphics.getMode();
113 FrameGraphics.setMode(FrameGraphics.MODE_XRAY, false);
114 Frame frameToSearch = FrameIO.LoadFrame(frameName);
115 FrameGraphics.setMode(oldMode, false);
116 if (frameToSearch == null)
117 return false;
118 for (Text itemToSearch : frameToSearch.getTextItems()) {
119 // Search for the item and add it to the results page if
120 // it is found
121 if (searchItem(itemToSearch, pattern, replacementString)) {
122 // Add a linked item to the results frame
123 results.addText(frameName, null, frameName, null, false);
124 }
125 }
126 FrameGraphics.requestRefresh(true);
127 FrameIO.SaveFrame(frameToSearch, false);
128 return true;
129 }
130
131 protected int addResults(String frameName, Collection<String> found) {
132 return addResults(frameName, frameName, found);
133 }
134
135 /**
136 * @param frameNumber
137 * @param frameName
138 * @param found
139 * @return
140 */
141 protected int addResults(String frameNumber, String frameName, Collection<String> found) {
142 int size = found == null ? 0 : found.size();
143 // If the frame exists
144 if (found != null)
145 _frameCount++;
146 if (size > 0) {
147 // String repeats = size > 1? ("("+ size+ ")") : "";
148 for (String s : found) {
149 StringBuffer surrogate = new StringBuffer();
150 surrogate.append("[").append(frameNumber).append("] ");
151 if (s.length() > SearchAgent.SURROGATE_LENGTH)
152 surrogate.append(
153 s.substring(0, SearchAgent.SURROGATE_LENGTH - 3))
154 .append("...");
155 else {
156 surrogate.append(s);
157 }
158
159 _results.addText(surrogate.toString(), null,
160 frameName, null, false);
161 FrameGraphics.requestRefresh(true);
162 }
163 }
164 return size;
165 }
166}
Note: See TracBrowser for help on using the repository browser.