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