1 | /*
|
---|
2 | * 02/17/2012
|
---|
3 | *
|
---|
4 | * SearchContext.java - Container for options of a search/replace operation.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rtextarea;
|
---|
10 |
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Contains information about a find/replace operation. Applications can
|
---|
14 | * keep an instance of this class around and use it to maintain the user's
|
---|
15 | * selection for options such as "match case," "regular expression," etc.,
|
---|
16 | * between search operations. They can then pass the instance as a parameter
|
---|
17 | * to the public {@link SearchEngine} methods to do the actual searching.
|
---|
18 | *
|
---|
19 | * @author Robert Futrell
|
---|
20 | * @version 1.0
|
---|
21 | * @see SearchEngine
|
---|
22 | */
|
---|
23 | public class SearchContext {
|
---|
24 |
|
---|
25 | private String searchFor;
|
---|
26 |
|
---|
27 | private String replaceWith;
|
---|
28 |
|
---|
29 | private boolean forward;
|
---|
30 |
|
---|
31 | private boolean matchCase;
|
---|
32 |
|
---|
33 | private boolean wholeWord;
|
---|
34 |
|
---|
35 | private boolean regex;
|
---|
36 |
|
---|
37 | private boolean selectionOnly;
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Creates a new search context. Specifies a forward search,
|
---|
42 | * case-insensitive, not whole-word, not a regular expression.
|
---|
43 | */
|
---|
44 | public SearchContext() {
|
---|
45 | this(null);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a new search context. Specifies a forward search,
|
---|
51 | * case-insensitive, not whole-word, not a regular expression.
|
---|
52 | *
|
---|
53 | * @param searchFor The text to search for.
|
---|
54 | */
|
---|
55 | public SearchContext(String searchFor) {
|
---|
56 | this.searchFor = searchFor;
|
---|
57 | forward = true;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Returns whether case should be honored while searching.
|
---|
63 | *
|
---|
64 | * @return Whether case should be honored.
|
---|
65 | * @see #setMatchCase(boolean)
|
---|
66 | */
|
---|
67 | public boolean getMatchCase() {
|
---|
68 | return matchCase;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns the text to replace with, if doing a replace operation.
|
---|
74 | *
|
---|
75 | * @return The text to replace with.
|
---|
76 | * @see #setReplaceWith(String)
|
---|
77 | * @see #getSearchFor()
|
---|
78 | */
|
---|
79 | public String getReplaceWith() {
|
---|
80 | return replaceWith;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Returns the text to search for.
|
---|
86 | *
|
---|
87 | * @return The text to search for.
|
---|
88 | * @see #setSearchFor(String)
|
---|
89 | * @see #getReplaceWith()
|
---|
90 | */
|
---|
91 | public String getSearchFor() {
|
---|
92 | return searchFor;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns whether the search should be forward through the text (vs.
|
---|
98 | * backwards).
|
---|
99 | *
|
---|
100 | * @return Whether we should search forwards.
|
---|
101 | * @see #setSearchForward(boolean)
|
---|
102 | */
|
---|
103 | public boolean getSearchForward() {
|
---|
104 | return forward;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns whether the search should only be done in the selected text.
|
---|
110 | * This flag is currently not supported.
|
---|
111 | *
|
---|
112 | * @return Whether only the selected text should be searched.
|
---|
113 | * @see #setSearchSelectionOnly(boolean)
|
---|
114 | */
|
---|
115 | public boolean getSearchSelectionOnly() {
|
---|
116 | return selectionOnly;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Returns whether only "whole word" matches should be returned. A match
|
---|
122 | * is considered to be "whole word" if the character on either side of the
|
---|
123 | * matched text is a non-word character, or if there is no character on
|
---|
124 | * one side of the word, such as when it's at the beginning or end of a
|
---|
125 | * line.
|
---|
126 | *
|
---|
127 | * @return Whether only "whole word" matches should be returned.
|
---|
128 | * @see #setWholeWord(boolean)
|
---|
129 | */
|
---|
130 | public boolean getWholeWord() {
|
---|
131 | return wholeWord;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns whether a regular expression search should be done.
|
---|
137 | *
|
---|
138 | * @return Whether a regular expression search should be done.
|
---|
139 | * @see #setRegularExpression(boolean)
|
---|
140 | */
|
---|
141 | public boolean isRegularExpression() {
|
---|
142 | return regex;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Sets whether case should be honored while searching.
|
---|
148 | *
|
---|
149 | * @param matchCase Whether case should be honored.
|
---|
150 | * @see #getMatchCase()
|
---|
151 | */
|
---|
152 | public void setMatchCase(boolean matchCase) {
|
---|
153 | this.matchCase = matchCase;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Sets whether a regular expression search should be done.
|
---|
159 | *
|
---|
160 | * @param regex Whether a regular expression search should be done.
|
---|
161 | * @see #isRegularExpression()
|
---|
162 | */
|
---|
163 | public void setRegularExpression(boolean regex) {
|
---|
164 | this.regex = regex;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Sets the text to replace with, if doing a replace operation.
|
---|
170 | *
|
---|
171 | * @param replaceWith The text to replace with.
|
---|
172 | * @see #getReplaceWith()
|
---|
173 | * @see #setSearchFor(String)
|
---|
174 | */
|
---|
175 | public void setReplaceWith(String replaceWith) {
|
---|
176 | this.replaceWith = replaceWith;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Sets the text to search for.
|
---|
182 | *
|
---|
183 | * @param searchFor The text to search for.
|
---|
184 | * @see #getSearchFor()
|
---|
185 | * @see #setReplaceWith(String)
|
---|
186 | */
|
---|
187 | public void setSearchFor(String searchFor) {
|
---|
188 | this.searchFor = searchFor;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Sets whether the search should be forward through the text (vs.
|
---|
194 | * backwards).
|
---|
195 | *
|
---|
196 | * @param forward Whether we should search forwards.
|
---|
197 | * @see #getSearchForward()
|
---|
198 | */
|
---|
199 | public void setSearchForward(boolean forward) {
|
---|
200 | this.forward = forward;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Sets whether only the selected text should be searched.
|
---|
206 | * This flag is currently not supported.
|
---|
207 | *
|
---|
208 | * @param selectionOnly Whether only selected text should be searched.
|
---|
209 | * @see #getSearchSelectionOnly()
|
---|
210 | */
|
---|
211 | public void setSearchSelectionOnly(boolean selectionOnly) {
|
---|
212 | this.selectionOnly = selectionOnly;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Sets whether only "whole word" matches should be returned. A match
|
---|
218 | * is considered to be "whole word" if the character on either side of the
|
---|
219 | * matched text is a non-word character, or if there is no character on
|
---|
220 | * one side of the word, such as when it's at the beginning or end of a
|
---|
221 | * line.
|
---|
222 | *
|
---|
223 | * @param wholeWord Whether only "whole word" matches should be returned.
|
---|
224 | * @see #getWholeWord()
|
---|
225 | */
|
---|
226 | public void setWholeWord(boolean wholeWord) {
|
---|
227 | this.wholeWord = wholeWord;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | } |
---|