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

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

Added calculate action

File size: 2.8 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 protected FrameCreator _results;
14
15 protected String _pattern;
16
17 protected String _replacementString;
18
19 protected String _startName;
20
21 @Override
22 public boolean initialise(Frame frame, Item item) {
23 String pattern = item.getText();
24 String resultFrameset = null;
25
26 //TODO use a results frame specified on the profile frame
27 if (item.getLink() == null) {
28 resultFrameset = frame.getFramesetName();
29 } else {
30 resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
31 false);
32 }
33 return initialise(frame, item, frame.getFramesetName(), resultFrameset, null, pattern);
34 }
35
36 public boolean initialise(Frame frame, Item item, String startName, String resultsFrameset,
37 String replacementString, String pattern) {
38 _pattern = pattern;
39 _replacementString = replacementString;
40 _startName = startName;
41
42 // Create a frame to put the results on with the search query
43 // and type as the title
44 String title = this.getClass().getSimpleName() + " [" + startName + "] [" + pattern
45 + "]";
46 _results = new FrameCreator(resultsFrameset, null, title, false);
47 // Set the frame to be displayed after running the agent
48 _end = _results.getFirstFrame();
49
50 return super.initialise(frame, item);
51 }
52
53 public String getResultsFrameName() {
54 return _results.getName();
55 }
56
57 public static boolean searchItem(Text itemToSearch, String pattern,
58 String replacementString) {
59 String searchStr = itemToSearch.getText().toLowerCase();
60 String[] result = searchStr.split(pattern.toLowerCase(), 2);
61 boolean bFound = result.length > 1;
62 // If it is a find and replace... then replace with the replacement
63 // string
64 if (bFound && replacementString != null) {
65 itemToSearch.setText(searchStr.replaceAll(pattern,
66 replacementString));
67 }
68 return bFound;
69 }
70
71 public static boolean searchFrame(FrameCreator results, String frameName,
72 String pattern, String replacementString) {
73 int oldMode = FrameGraphics.getMode();
74 FrameGraphics.setMode(FrameGraphics.MODE_XRAY, false);
75 Frame frameToSearch = FrameIO.LoadFrame(frameName);
76 FrameGraphics.setMode(oldMode, false);
77 if (frameToSearch == null)
78 return false;
79 for (Text itemToSearch : frameToSearch.getTextItems()) {
80 // Search for the item and add it to the results page if
81 // it is found
82 if (searchItem(itemToSearch, pattern,
83 replacementString)) {
84 // Add a linked item to the results frame
85 results.addText(frameName, null, frameName, null, false);
86 }
87 }
88 FrameGraphics.requestRefresh(true);
89 FrameIO.SaveFrame(frameToSearch, false);
90 return true;
91 }
92}
Note: See TracBrowser for help on using the repository browser.