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

Last change on this file since 294 was 294, checked in by ra33, 16 years ago
File size: 3.3 KB
Line 
1package org.expeditee.agents;
2
3import org.expeditee.gui.Frame;
4import org.expeditee.gui.FrameCreator;
5import org.expeditee.gui.FrameGraphics;
6import org.expeditee.gui.FrameIO;
7import org.expeditee.io.Conversion;
8import org.expeditee.items.Item;
9import org.expeditee.items.Text;
10
11public abstract class SearchAgent extends DefaultAgent {
12
13 private static final String DEFAULT_RESULTS_FRAMESET = "SearchResults";
14
15 protected FrameCreator _results;
16
17 protected String _pattern;
18
19 protected String _replacementString;
20
21 protected String _startName;
22
23 public SearchAgent(String searchText) {
24 _pattern = searchText;
25 }
26
27 @Override
28 public boolean initialise(Frame frame, Item item) {
29 String pattern = item.getText();
30 String resultFrameset = null;
31
32 // TODO use a results frame specified on the profile frame
33 if (item.getLink() == null) {
34 resultFrameset = DEFAULT_RESULTS_FRAMESET;
35 } else {
36 resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
37 false);
38 }
39 return initialise(frame, item, frame.getFramesetName(), resultFrameset,
40 null, pattern);
41 }
42
43 /**
44 *
45 * @param frame
46 * @param item
47 * @param startName
48 * @param resultsFrameset
49 * @param replacementString
50 * @param pattern
51 * is ignored if the pattern has already been set earlier.
52 * @return
53 */
54 public boolean initialise(Frame frame, Item item, String startName,
55 String resultsFrameset, String replacementString, String pattern) {
56 //TODO: Put the init params in the constructor!! Dont want to be setting _pattern in two places!
57
58 if (_pattern == null)
59 _pattern = pattern.toLowerCase();
60 _replacementString = replacementString;
61 _startName = startName;
62
63 // Create a frame to put the results on with the search query
64 // and type as the title
65 String title = this.getClass().getSimpleName() + " [" + startName
66 + "] [" + _pattern + "]";
67 _results = new FrameCreator(resultsFrameset, null, title, false, true);
68 // Set the frame to be displayed after running the agent
69 _end = _results.getFirstFrame();
70
71 return super.initialise(frame, item);
72 }
73
74 public String getResultsFrameName() {
75 return _results.getName();
76 }
77
78 public static boolean searchItem(Text itemToSearch, String pattern,
79 String replacementString) {
80 String searchStr = itemToSearch.getText().toLowerCase();
81 boolean bFound = searchStr.contains(pattern.toLowerCase());
82 // If it is a find and replace... then replace with the replacement
83 // string
84 if (bFound && replacementString != null) {
85 itemToSearch.setText(searchStr.replaceAll(pattern,
86 replacementString));
87 }
88 return bFound;
89 }
90
91 public static boolean searchFrame(FrameCreator results, String frameName,
92 String pattern, String replacementString) {
93 int oldMode = FrameGraphics.getMode();
94 FrameGraphics.setMode(FrameGraphics.MODE_XRAY, false);
95 Frame frameToSearch = FrameIO.LoadFrame(frameName);
96 FrameGraphics.setMode(oldMode, false);
97 if (frameToSearch == null)
98 return false;
99 for (Text itemToSearch : frameToSearch.getTextItems()) {
100 // Search for the item and add it to the results page if
101 // it is found
102 if (searchItem(itemToSearch, pattern, replacementString)) {
103 // Add a linked item to the results frame
104 results.addText(frameName, null, frameName, null, false);
105 }
106 }
107 FrameGraphics.requestRefresh(true);
108 FrameIO.SaveFrame(frameToSearch, false);
109 return true;
110 }
111}
Note: See TracBrowser for help on using the repository browser.