1 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 ; 41 import java.util.regex.Pattern ; 42 43 public class FindAndReplace implements XJFrameDelegate { 44 45 public static final String BEGIN_QUOTE = "\\Q"; 46 public static final String END_QUOTE = "\\E"; 47 48 public CEditorGrammar editor; 49 public String findString; 50 public String replaceString; 51 public int flags; 52 public String prefix = ""; 53 public String suffix = ""; 54 public String prefixRegex = BEGIN_QUOTE; 55 public String 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 getCompilableString() { 68 return prefix+prefixRegex+findString+suffixRegex+suffix; 69 } 70 71 public Pattern getCompiledPattern() { 72 Pattern p = null; 73 try { 74 p = Pattern.compile(getCompilableString(), flags); 75 } catch(Exception 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 p = getCompiledPattern(); 93 if(p == null) 94 return false; 95 96 String text = editor.getText(); 97 Matcher 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 text = editor.getText(); 112 113 Pattern p = getCompiledPattern(); 114 if(p == null) 115 return false; 116 117 Matcher 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 text = editor.getText(); 132 133 Pattern p = getCompiledPattern(); 134 if(p == null) 135 return false; 136 137 Matcher 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 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 p = getCompiledPattern(); 163 if(p == null) 164 return; 165 166 Matcher m = p.matcher(editor.getText()); 167 String 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 string) { 184 this.findString = string; 185 } 186 187 public void setReplaceString(String 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: break; 214 case 1: prefix = "\\b"; 216 break; 217 case 2: prefix = "\\b"; 219 suffix = "\\b"; 220 break; 221 case 3: suffix = "\\b"; 223 break; 224 } 225 } 226 227 public void frameDidClose(XJFrame frame) { 228 dialog = null; 229 } 230 231 } 232 | Popular Tags |