1 19 20 package org.netbeans.editor; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.util.List ; 26 import java.util.Map ; 27 import javax.swing.text.JTextComponent ; 28 import javax.swing.text.BadLocationException ; 29 import org.netbeans.modules.editor.lib2.search.EditorFindSupport; 30 31 38 public class FindSupport { 39 40 public static final String REVERT_MAP = EditorFindSupport.REVERT_MAP; 41 public static final String FIND_HISTORY_PROP = EditorFindSupport.FIND_HISTORY_PROP; 42 public static final String FIND_HISTORY_CHANGED_PROP = EditorFindSupport.FIND_HISTORY_CHANGED_PROP; 43 44 45 static FindSupport findSupport; 46 47 48 private Map findProps; 49 50 51 WeakPropertyChangeSupport changeSupport 52 = new WeakPropertyChangeSupport(); 53 54 SearchPatternWrapper lastSelected; 55 List historyList; 56 57 private FindSupport() { 58 } 60 61 62 public static FindSupport getFindSupport() { 63 if (findSupport == null) { 64 findSupport = new FindSupport(); 65 } 66 return findSupport; 67 } 68 69 public Map getDefaultFindProperties() { 70 return EditorFindSupport.getInstance().getDefaultFindProperties(); 71 } 72 73 public Map getFindProperties() { 74 return EditorFindSupport.getInstance().getFindProperties(); 75 } 76 77 78 public Object getFindProperty(String name) { 79 return EditorFindSupport.getInstance().getFindProperty(name); 80 } 81 82 int[] getBlocks(int[] blocks, BaseDocument doc, 83 int startPos, int endPos) throws BadLocationException { 84 return EditorFindSupport.getInstance().getBlocks(blocks, doc, startPos, endPos); 85 } 86 87 91 Object getPropertyNoInit(String name) { 92 return EditorFindSupport.getInstance().getPropertyNoInit(name); 93 } 94 95 97 public void putFindProperty(String name, Object newValue) { 98 EditorFindSupport.getInstance().putFindProperty(name, newValue); 99 } 100 101 106 public void putFindProperties(Map propsToAdd) { 107 EditorFindSupport.getInstance().putFindProperties(propsToAdd); 108 } 109 110 public void setBlockSearchHighlight(int startSelection, int endSelection){ 111 EditorFindSupport.getInstance().setBlockSearchHighlight(startSelection, endSelection); 112 } 113 114 public boolean incSearch(Map props, int caretPos) { 115 return EditorFindSupport.getInstance().incSearch(props, caretPos); 116 } 117 118 public void incSearchReset() { 119 EditorFindSupport.getInstance().incSearchReset(); 120 } 121 122 126 public boolean find(Map props, boolean oppositeDir) { 127 return EditorFindSupport.getInstance().find(props, oppositeDir); 128 } 129 130 145 public int[] findInBlock(JTextComponent c, int startPos, int blockStartPos, 146 int blockEndPos, Map props, boolean oppositeDir) throws BadLocationException 147 { 148 return EditorFindSupport.getInstance().findInBlock( 149 c, startPos, blockStartPos, blockEndPos, props, oppositeDir); 150 } 151 152 public boolean replace(Map props, boolean oppositeDir) throws BadLocationException { 153 return EditorFindSupport.getInstance().replace(props, oppositeDir); 154 } 155 156 public void replaceAll(Map props) { 157 EditorFindSupport.getInstance().replaceAll(props); 158 } 159 160 171 175 public void addPropertyChangeListener(PropertyChangeListener l) { 176 EditorFindSupport.getInstance().addPropertyChangeListener(new WeakPropL(l)); 177 } 178 179 public synchronized void addPropertyChangeListener( 180 String findPropertyName, PropertyChangeListener l) 181 { 182 EditorFindSupport.getInstance().addPropertyChangeListener( 183 findPropertyName, new WeakPropL(l)); 184 } 185 186 187 public void removePropertyChangeListener(PropertyChangeListener l) { 188 } 190 191 void firePropertyChange(String settingName, Object oldValue, Object newValue) { 192 EditorFindSupport.getInstance().firePropertyChange(settingName, oldValue, newValue); 193 } 194 195 public void setHistory(List spwList){ 196 EditorFindSupport.getInstance().setHistory(spwList); 197 } 198 199 public List getHistory(){ 200 return EditorFindSupport.getInstance().getHistory(); 201 } 202 203 public void setLastSelected(SearchPatternWrapper spw) { 204 EditorFindSupport.SPW editorSpw = new EditorFindSupport.SPW( 205 spw.getSearchExpression(), spw.isWholeWords(), spw.isMatchCase(), spw.isRegExp() 206 ); 207 EditorFindSupport.getInstance().setLastSelected(editorSpw); 208 } 209 210 public SearchPatternWrapper getLastSelected(){ 211 EditorFindSupport.SPW spw = EditorFindSupport.getInstance().getLastSelected(); 212 return new SearchPatternWrapper(spw.getSearchExpression(), spw.isWholeWords(), spw.isMatchCase(), spw.isRegExp()); 213 } 214 215 public void addToHistory(SearchPatternWrapper spw){ 216 EditorFindSupport.SPW editorSpw = new EditorFindSupport.SPW( 217 spw.getSearchExpression(), spw.isWholeWords(), spw.isMatchCase(), spw.isRegExp() 218 ); 219 EditorFindSupport.getInstance().addToHistory(editorSpw); 220 } 221 222 public static class SearchPatternWrapper{ 223 private String searchExpression; 224 private boolean wholeWords; 225 private boolean matchCase; 226 private boolean regExp; 227 228 public SearchPatternWrapper(String searchExpression, boolean wholeWords, 229 boolean matchCase, boolean regExp){ 230 this.searchExpression = searchExpression; 231 this.wholeWords = wholeWords; 232 this.matchCase = matchCase; 233 this.regExp = regExp; 234 } 235 236 237 public String getSearchExpression(){ 238 return searchExpression; 239 } 240 241 242 public boolean isWholeWords(){ 243 return wholeWords; 244 } 245 246 247 public boolean isMatchCase(){ 248 return matchCase; 249 } 250 251 252 public boolean isRegExp(){ 253 return regExp; 254 } 255 256 public boolean equals(Object obj){ 257 if (!(obj instanceof SearchPatternWrapper)){ 258 return false; 259 } 260 SearchPatternWrapper sp = (SearchPatternWrapper)obj; 261 return (this.searchExpression.equals(sp.getSearchExpression()) && 262 this.wholeWords == sp.isWholeWords() && 263 this.matchCase == sp.isMatchCase() && 264 this.regExp == sp.isRegExp()); 265 } 266 267 public int hashCode() { 268 int result = 17; 269 result = 37*result + (this.wholeWords ? 1:0); 270 result = 37*result + (this.matchCase ? 1:0); 271 result = 37*result + (this.regExp ? 1:0); 272 result = 37*result + this.searchExpression.hashCode(); 273 return result; 274 } 275 276 public String toString(){ 277 StringBuffer sb = new StringBuffer ("[SearchPatternWrapper:]\nsearchExpression:"+searchExpression); sb.append('\n'); 279 sb.append("wholeWords:"); sb.append(wholeWords); 281 sb.append('\n'); 282 sb.append("matchCase:"); sb.append(matchCase); 284 sb.append('\n'); 285 sb.append("regExp:"); sb.append(regExp); 287 return sb.toString(); 288 } 289 } 291 private static final class WeakPropL extends WeakReference implements PropertyChangeListener { 292 293 public WeakPropL(PropertyChangeListener origL) { 294 super(origL); 295 } 296 297 public void propertyChange(PropertyChangeEvent evt) { 298 PropertyChangeListener origL = (PropertyChangeListener ) get(); 299 if (origL != null) { 300 origL.propertyChange(new PropertyChangeEvent (this, evt.getPropertyName(), 301 convert(evt.getOldValue()), convert(evt.getNewValue()))); 302 } else { 303 EditorFindSupport.getInstance().removePropertyChangeListener(this); 304 } 305 } 306 307 private Object convert(Object o) { 308 if (o instanceof EditorFindSupport.SPW) { 309 EditorFindSupport.SPW spw = (EditorFindSupport.SPW) o; 310 return new SearchPatternWrapper( 311 spw.getSearchExpression(), spw.isWholeWords(), spw.isMatchCase(), spw.isRegExp()); 312 } else { 313 return o; 314 } 315 } 316 } } 318 | Popular Tags |