KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > find > FindAndReplace


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32
33 package org.antlr.works.find;
34
35 import org.antlr.works.components.grammar.CEditorGrammar;
36 import org.antlr.xjlib.appkit.frame.XJFrame;
37 import org.antlr.xjlib.appkit.frame.XJFrameDelegate;
38 import org.antlr.xjlib.appkit.utils.XJAlert;
39
40 import java.util.regex.Matcher JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42
43 public class FindAndReplace implements XJFrameDelegate {
44
45     public static final String JavaDoc BEGIN_QUOTE = "\\Q";
46     public static final String JavaDoc END_QUOTE = "\\E";
47
48     public CEditorGrammar editor;
49     public String JavaDoc findString;
50     public String JavaDoc replaceString;
51     public int flags;
52     public String JavaDoc prefix = "";
53     public String JavaDoc suffix = "";
54     public String JavaDoc prefixRegex = BEGIN_QUOTE;
55     public String JavaDoc suffixRegex = END_QUOTE;
56
57     public FindAndReplaceDialog dialog;
58
59     public FindAndReplace(CEditorGrammar editor) {
60         this.editor = editor;
61     }
62
63     public void find() {
64         display();
65     }
66
67     public String JavaDoc getCompilableString() {
68         return prefix+prefixRegex+findString+suffixRegex+suffix;
69     }
70
71     public Pattern JavaDoc getCompiledPattern() {
72         Pattern JavaDoc p = null;
73         try {
74             p = Pattern.compile(getCompilableString(), flags);
75         } catch(Exception JavaDoc e) {
76             XJAlert.display(dialog.getJavaContainer(), "Regex Find", "Pattern error:\n"+e.toString());
77         }
78         return p;
79     }
80
81     public void setPositionToTop() {
82         editor.getTextPane().setSelectionStart(0);
83         editor.getTextPane().setSelectionEnd(0);
84     }
85
86     public void setPositionToBottom() {
87         editor.getTextPane().setSelectionStart(editor.getText().length()-1);
88         editor.getTextPane().setSelectionEnd(editor.getText().length()-1);
89     }
90
91     public boolean matching() {
92         Pattern JavaDoc p = getCompiledPattern();
93         if(p == null)
94             return false;
95
96         String JavaDoc text = editor.getText();
97         Matcher JavaDoc m = p.matcher(text);
98         if(m.find(0)) {
99             editor.selectTextRange(m.start(), m.end());
100             return true;
101         } else {
102             return false;
103         }
104     }
105
106     public boolean next() {
107         if(findString == null || findString.length() == 0)
108             return false;
109
110         int position = editor.getTextPane().getSelectionEnd();
111         String JavaDoc text = editor.getText();
112
113         Pattern JavaDoc p = getCompiledPattern();
114         if(p == null)
115             return false;
116
117         Matcher JavaDoc m = p.matcher(text);
118         if(m.find(position)) {
119             editor.selectTextRange(m.start(), m.end());
120             return true;
121         } else {
122             return false;
123         }
124     }
125
126     public boolean prev() {
127         if(findString == null || findString.length() == 0)
128             return false;
129
130         int position = editor.getTextPane().getSelectionStart();
131         String JavaDoc text = editor.getText();
132
133         Pattern JavaDoc p = getCompiledPattern();
134         if(p == null)
135             return false;
136
137         Matcher JavaDoc m = p.matcher(text.substring(0, position));
138         int matchStart = 0;
139         int matchEnd = 0;
140         boolean matched = false;
141         while(m.find(matchEnd)) {
142             matchStart = m.start();
143             matchEnd = m.end();
144             matched = true;
145         }
146         if(matched) {
147             editor.selectTextRange(matchStart, matchEnd);
148             return true;
149         } else {
150             return false;
151         }
152     }
153
154     public void replace() {
155         String JavaDoc t = editor.textEditor.getSelectedText();
156         if(t != null && t.length() > 0) {
157             editor.textEditor.replaceSelectedText(replaceString);
158         }
159     }
160
161     public void replaceAll() {
162         Pattern JavaDoc p = getCompiledPattern();
163         if(p == null)
164             return;
165
166         Matcher JavaDoc m = p.matcher(editor.getText());
167         String JavaDoc s = m.replaceAll(replaceString);
168
169         int oldCursorPosition = editor.getCaretPosition();
170         editor.setText(s);
171         editor.getTextEditor().setCaretPosition(oldCursorPosition, false, false);
172     }
173
174     public void display() {
175         if(dialog == null) {
176             dialog = new FindAndReplaceDialog(this);
177         }
178         dialog.setDelegate(this);
179         dialog.setFindText(editor.textEditor.getSelectedText());
180         dialog.show();
181     }
182
183     public void setFindString(String JavaDoc string) {
184         this.findString = string;
185     }
186
187     public void setReplaceString(String JavaDoc string) {
188         this.replaceString = string;
189     }
190
191     public void setIgnoreCase(boolean ignore) {
192         if(ignore)
193             flags = Pattern.CASE_INSENSITIVE;
194         else
195             flags = 0;
196     }
197
198     public void setRegex(boolean flag) {
199         if(flag) {
200             prefixRegex = "";
201             suffixRegex = "";
202         } else {
203             prefixRegex = BEGIN_QUOTE;
204             suffixRegex = END_QUOTE;
205         }
206     }
207
208     public void setOptions(int options) {
209         prefix = "";
210         suffix = "";
211         switch(options) {
212             case 0: // contains
213
break;
214             case 1: // starts with
215
prefix = "\\b";
216                 break;
217             case 2: // whole word
218
prefix = "\\b";
219                 suffix = "\\b";
220                 break;
221             case 3: // ends with
222
suffix = "\\b";
223                 break;
224         }
225     }
226
227     public void frameDidClose(XJFrame frame) {
228         dialog = null;
229     }
230
231 }
232
Popular Tags