KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > FindSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import org.netbeans.modules.editor.lib2.search.EditorFindSupport;
30
31 /**
32 * Find management
33 *
34 * @author Miloslav Metelka
35 * @version 1.00
36 * @deprecated Without any replacement.
37 */

38 public class FindSupport {
39
40     public static final String JavaDoc REVERT_MAP = EditorFindSupport.REVERT_MAP;
41     public static final String JavaDoc FIND_HISTORY_PROP = EditorFindSupport.FIND_HISTORY_PROP;
42     public static final String JavaDoc FIND_HISTORY_CHANGED_PROP = EditorFindSupport.FIND_HISTORY_CHANGED_PROP;
43     
44     /** Shared instance of FindSupport class */
45     static FindSupport findSupport;
46
47     /** Find properties */
48     private Map JavaDoc findProps;
49
50     /** Support for firing change events */
51     WeakPropertyChangeSupport changeSupport
52     = new WeakPropertyChangeSupport();
53     
54     SearchPatternWrapper lastSelected;
55     List JavaDoc historyList;/*<SearchPatternWrapper>*/
56
57     private FindSupport() {
58         // prevent instance creation
59
}
60
61     /** Get shared instance of find support */
62     public static FindSupport getFindSupport() {
63         if (findSupport == null) {
64             findSupport = new FindSupport();
65         }
66         return findSupport;
67     }
68
69     public Map JavaDoc getDefaultFindProperties() {
70         return EditorFindSupport.getInstance().getDefaultFindProperties();
71     }
72
73     public Map JavaDoc getFindProperties() {
74         return EditorFindSupport.getInstance().getFindProperties();
75     }
76
77     /** Get find property with specified name */
78     public Object JavaDoc getFindProperty(String JavaDoc name) {
79         return EditorFindSupport.getInstance().getFindProperty(name);
80     }
81
82     int[] getBlocks(int[] blocks, BaseDocument doc,
83                     int startPos, int endPos) throws BadLocationException JavaDoc {
84         return EditorFindSupport.getInstance().getBlocks(blocks, doc, startPos, endPos);
85     }
86
87     /** Get find property without performing initialization
88     * of find properties. This is useful for example for base document
89     * when it wants to query whether it should do highlight search.
90     */

91     Object JavaDoc getPropertyNoInit(String JavaDoc name) {
92         return EditorFindSupport.getInstance().getPropertyNoInit(name);
93     }
94
95     /** Set find property with specified name and fire change.
96     */

97     public void putFindProperty(String JavaDoc name, Object JavaDoc newValue) {
98         EditorFindSupport.getInstance().putFindProperty(name, newValue);
99     }
100
101     /** Add/replace properties from some other map
102     * to current find properties. If the added properties
103     * are different than the original ones,
104     * the property change is fired.
105     */

106     public void putFindProperties(Map JavaDoc 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 JavaDoc props, int caretPos) {
115         return EditorFindSupport.getInstance().incSearch(props, caretPos);
116     }
117
118     public void incSearchReset() {
119         EditorFindSupport.getInstance().incSearchReset();
120     }
121     
122     /** Find the text from the caret position.
123     * @param props search properties
124     * @param oppositeDir whether search in opposite direction
125     */

126     public boolean find(Map JavaDoc props, boolean oppositeDir) {
127         return EditorFindSupport.getInstance().find(props, oppositeDir);
128     }
129
130     /** Find the searched expression
131     * @param startPos position from which to search. It must be inside the block.
132     * @param blockStartPos starting position of the block. It must
133     * be valid position greater or equal than zero. It must be lower than
134     * or equal to blockEndPos (except blockEndPos=-1).
135     * @param blockEndPos ending position of the block. It can be -1 for the end
136     * of document. It must be greater or equal than blockStartPos (except blockEndPos=-1).
137     * @param props search properties
138     * @param oppositeDir whether search in opposite direction
139     * @param displayWrap whether display messages about the wrapping
140     * @return either null when nothing was found or integer array with three members
141     * ret[0] - starting position of the found string
142     * ret[1] - ending position of the found string
143     * ret[2] - 1 or 0 when wrap was or wasn't performed in order to find the string
144     */

145     public int[] findInBlock(JTextComponent JavaDoc c, int startPos, int blockStartPos,
146         int blockEndPos, Map JavaDoc props, boolean oppositeDir) throws BadLocationException JavaDoc
147     {
148         return EditorFindSupport.getInstance().findInBlock(
149             c, startPos, blockStartPos, blockEndPos, props, oppositeDir);
150     }
151
152     public boolean replace(Map JavaDoc props, boolean oppositeDir) throws BadLocationException JavaDoc {
153         return EditorFindSupport.getInstance().replace(props, oppositeDir);
154     }
155
156     public void replaceAll(Map JavaDoc props) {
157         EditorFindSupport.getInstance().replaceAll(props);
158     }
159
160 // TODO: remove
161
// /** Get position of wrap mark for some document */
162
// public int getWrapSearchMarkPos(BaseDocument doc) {
163
// return EditorFindSupport.getInstance().getWrapSearchMarkPos(doc);
164
// }
165
//
166
// /** Set new position of wrap mark for some document */
167
// public void setWrapSearchMarkPos(BaseDocument doc, int pos) {
168
// EditorFindSupport.getInstance().setWrapSearchMarkPos(doc, pos);
169
// }
170

171     /** Add weak listener to listen to change of any property. The caller must
172     * hold the listener object in some instance variable to prevent it
173     * from being garbage collected.
174     */

175     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
176         EditorFindSupport.getInstance().addPropertyChangeListener(new WeakPropL(l));
177     }
178
179     public synchronized void addPropertyChangeListener(
180         String JavaDoc findPropertyName, PropertyChangeListener JavaDoc l)
181     {
182         EditorFindSupport.getInstance().addPropertyChangeListener(
183             findPropertyName, new WeakPropL(l));
184     }
185
186     /** Remove listener for changes in properties */
187     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
188         // no-op
189
}
190
191     void firePropertyChange(String JavaDoc settingName, Object JavaDoc oldValue, Object JavaDoc newValue) {
192         EditorFindSupport.getInstance().firePropertyChange(settingName, oldValue, newValue);
193     }
194
195     public void setHistory(List JavaDoc/*<SearchPatternWrapper>*/ spwList){
196         EditorFindSupport.getInstance().setHistory(spwList);
197     }
198     
199     public List JavaDoc/*<SearchPatternWrapper>*/ 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 JavaDoc searchExpression;
224         private boolean wholeWords;
225         private boolean matchCase;
226         private boolean regExp;
227         
228         public SearchPatternWrapper(String JavaDoc 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         /** @return searchExpression */
237         public String JavaDoc getSearchExpression(){
238             return searchExpression;
239         }
240
241         /** @return true if the wholeWords parameter was used during search performing */
242         public boolean isWholeWords(){
243             return wholeWords;
244         }
245
246         /** @return true if the matchCase parameter was used during search performing */
247         public boolean isMatchCase(){
248             return matchCase;
249         }
250
251         /** @return true if the regExp parameter was used during search performing */
252         public boolean isRegExp(){
253             return regExp;
254         }
255         
256         public boolean equals(Object JavaDoc 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 JavaDoc toString(){
277             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[SearchPatternWrapper:]\nsearchExpression:"+searchExpression);//NOI18N
278
sb.append('\n');
279             sb.append("wholeWords:");//NOI18N
280
sb.append(wholeWords);
281             sb.append('\n');
282             sb.append("matchCase:");//NOI18N
283
sb.append(matchCase);
284             sb.append('\n');
285             sb.append("regExp:");//NOI18N
286
sb.append(regExp);
287             return sb.toString();
288         }
289     } // End of SearchPatternWrapper class
290

291     private static final class WeakPropL extends WeakReference JavaDoc implements PropertyChangeListener JavaDoc {
292         
293         public WeakPropL(PropertyChangeListener JavaDoc origL) {
294             super(origL);
295         }
296         
297         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
298             PropertyChangeListener JavaDoc origL = (PropertyChangeListener JavaDoc) get();
299             if (origL != null) {
300                 origL.propertyChange(new PropertyChangeEvent JavaDoc(this, evt.getPropertyName(),
301                     convert(evt.getOldValue()), convert(evt.getNewValue())));
302             } else {
303                 EditorFindSupport.getInstance().removePropertyChangeListener(this);
304             }
305         }
306         
307         private Object JavaDoc convert(Object JavaDoc 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     } // End of WeakPropL class
317
}
318
Popular Tags