KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > search > EditorFindDialogSupport


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.modules.editor.lib2.search;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.*;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import javax.swing.*;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import javax.swing.text.BadLocationException JavaDoc;
38 import javax.swing.text.Position JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import javax.swing.text.Document JavaDoc;
41 import org.netbeans.modules.editor.lib2.DocumentsRegistry;
42 import org.netbeans.modules.editor.lib2.KeyEventBlocker;
43 import org.netbeans.modules.editor.lib2.DocUtils;
44 import org.netbeans.modules.editor.lib2.ComponentUtils;
45 import org.netbeans.modules.editor.lib2.DialogSupport;
46 import org.netbeans.modules.editor.lib2.highlighting.BlockHighlighting;
47 import org.netbeans.modules.editor.lib2.highlighting.Factory;
48 import org.netbeans.modules.editor.lib2.search.EditorFindSupport.SPW;
49 import org.openide.util.NbBundle;
50
51 /**
52 * Support for displaying find and replace dialogs
53 *
54 * <p><b>IMPORTANT:</b> Do not subclass this class and do not use its constructor.
55 * Always use <code>getInstance</code> method.
56 *
57 * @author Miloslav Metelka
58 * @version 1.00
59 */

60 public class EditorFindDialogSupport extends WindowAdapter implements ActionListener {
61
62     private static final Logger JavaDoc LOG = Logger.getLogger(EditorFindDialogSupport.class.getName());
63     
64     /** This lock is used to create a barrier between showing/hiding/changing
65      * the dialog and testing if the dialog is already shown.
66      * it is used to make test-and-change / test-and-display actions atomic.
67      * It covers the following four fields: findDialog, isReplaceDialog,
68      * findPanel, findButtons
69      */

70     private static Object JavaDoc dialogLock = new Object JavaDoc();
71     
72     /** Whether the currently visible dialog is for replace */
73     private static boolean isReplaceDialog = false;
74
75     /** The buttons used in the visible dialog */
76     private static JButton findButtons[];
77     
78     private static JButton findDialogButtons[];
79     private static JButton replaceDialogButtons[];
80     
81     /** The FindPanel used inside the visible dialog */
82     private static FindPanel findPanel;
83
84     /** Currently visible dialog */
85     private static Dialog JavaDoc findDialog = null;
86
87     private int caretPosition;
88
89     private static EditorFindDialogSupport singleton = null;
90     private static PropertyChangeListener JavaDoc historyChangeListener;
91     
92     private boolean findPerformed = false;
93     
94     private static int xPos = Integer.MIN_VALUE;
95     private static int yPos = Integer.MIN_VALUE;
96
97     /** Flag for determining a dialog invocation. It the dialog
98      * is invoked by keystroke or by the menu the value is true.
99      * If the dialog was already shown and the focus was bring to it only,
100      * value is false - needed for fixing the issue #68021
101      */

102     private static boolean dialogInvokedViaKeystroke;
103     
104     public static EditorFindDialogSupport getInstance() {
105         if (singleton == null) {
106             singleton = new EditorFindDialogSupport();
107         }
108         return singleton;
109     }
110
111     /**
112      * <p><b>IMPORTANT:</b> This is public only to maintain backwards compatibility
113      * of the FindDialogSupport class. Do not use this constructor, use <code>getInstance</code>
114      * method instead.
115      */

116     public EditorFindDialogSupport() {
117     }
118
119     private void createFindButtons() {
120         if (findButtons == null) {
121             ResourceBundle JavaDoc bundle = NbBundle.getBundle(EditorFindDialogSupport.class);
122             findButtons = new JButton[] {
123                 new JButton(bundle.getString("find-button-find")), // NOI18N
124
new JButton(bundle.getString("find-button-replace")), // NOI18N
125
new JButton(bundle.getString("find-button-replace-all")), // NOI18N
126
new JButton(bundle.getString("find-button-cancel")) // NOI18N
127
};
128
129             findButtons[0].setMnemonic(bundle.getString("find-button-find-mnemonic").charAt(0)); // NOI18N
130
findButtons[1].setMnemonic(bundle.getString("find-button-replace-mnemonic").charAt(0)); // NOI18N
131
findButtons[2].setMnemonic(bundle.getString("find-button-replace-all-mnemonic").charAt(0)); // NOI18N
132

133             findButtons[0].getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_find-button-find")); // NOI18N
134
findButtons[1].getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_find-button-replace")); // NOI18N
135
findButtons[2].getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_find-button-replace-all")); // NOI18N
136
findButtons[3].getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_find-button-cancel")); // NOI18N
137

138             findDialogButtons = new JButton[2];
139             findDialogButtons[0] = findButtons[0];
140             findDialogButtons[1] = findButtons[3];
141             
142             replaceDialogButtons = new JButton[4];
143             replaceDialogButtons[0] = findButtons[0];
144             replaceDialogButtons[1] = findButtons[1];
145             replaceDialogButtons[2] = findButtons[2];
146             replaceDialogButtons[3] = findButtons[3];
147         }
148     }
149
150     private void createFindPanel() {
151         if (findPanel == null) {
152             findPanel = new FindPanel();
153         }
154     }
155
156     private Dialog JavaDoc createFindDialog(JPanel findPanel, final JButton[] buttons,
157                                       final ActionListener l) {
158         Dialog JavaDoc d = DialogSupport.getInstance().createDialog(
159                 isReplaceDialog
160                     ? NbBundle.getBundle(EditorFindDialogSupport.class).getString ("replace-title")
161                     : NbBundle.getBundle(EditorFindDialogSupport.class).getString ("find-title" ), // NOI18N
162
findPanel, false, // non-modal
163
buttons, true, // sidebuttons,
164
0, // defaultIndex = 0 => findButton
165
isReplaceDialog ? 3 : 1, // cancelIndex = 3 => cancelButton
166
l //listener
167
);
168
169         return d;
170     }
171
172     private void showFindDialogImpl( boolean isReplace, KeyEventBlocker blocker) {
173         dialogInvokedViaKeystroke = true;
174         synchronized( dialogLock ) {
175             if (findDialog != null && isReplaceDialog != isReplace ) {
176                 xPos = findDialog.getLocation().x;
177                 yPos = findDialog.getLocation().y;
178                 findDialog.dispose();
179                 findDialog = null;
180             }
181             if (findDialog == null) { // create and show new dialog of required type
182
isReplaceDialog = isReplace;
183                 createFindButtons();
184                 createFindPanel();
185                 findPanel.changeVisibility(isReplace);
186                 
187                 findDialog = createFindDialog( findPanel, isReplace ? replaceDialogButtons : findDialogButtons, this );
188                 findDialog.addWindowListener( this );
189                 ((JDialog)findDialog).getRootPane().setFocusable(false);
190                 if(xPos > Integer.MIN_VALUE){
191                     findDialog.setLocation(xPos, yPos);
192                 }
193             }
194         } // end of synchronized section
195

196         findDialog.pack();
197         findPanel.init(isReplace, blocker);
198         findDialog.setVisible(true);
199         findPanel.showNotify();
200         updateCaretPosition();
201     }
202
203     private void updateCaretPosition() {
204         JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
205         if (c != null) {
206             caretPosition = c.getCaret().getDot();
207         }
208     }
209
210     public void windowActivated(WindowEvent evt) {
211         findPerformed = false;
212         createFindPanel();
213         findPanel.initBlockSearch();
214         updateCaretPosition();
215     }
216        
217     public void windowDeactivated(WindowEvent evt) {
218         Map JavaDoc<String JavaDoc, Object JavaDoc> findProps = findPanel.getFindProps();
219         JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
220         if (c != null) {
221             boolean blockSearch = getBooleanProp(EditorFindSupport.FIND_BLOCK_SEARCH, findProps);
222             if (blockSearch && !findPerformed){
223                 Integer JavaDoc bsStartInt = (Integer JavaDoc)findProps.get(EditorFindSupport.FIND_BLOCK_SEARCH_START);
224                 int bsStart = (bsStartInt == null) ? -1 : bsStartInt.intValue();
225                 Position JavaDoc pos = (Position JavaDoc) findProps.get(EditorFindSupport.FIND_BLOCK_SEARCH_END);
226                 int bsEnd = (pos != null) ? pos.getOffset() : -1;
227                 if (bsStart >=0 && bsEnd > 0){
228                     c.select(bsStart, bsEnd);
229                 }
230             } else {
231                 BlockHighlighting layer = EditorFindSupport.getInstance()
232                     .findLayer(c, Factory.INC_SEARCH_LAYER);
233                 
234                 if (layer != null) {
235                     int[] block = layer.gethighlightedBlock();
236                     if (block != null) {
237                         c.select(block[0], block[1]);
238                     }
239                 }
240                 
241 // EditorUI editorUI = ((BaseTextUI)c.getUI()).getEditorUI();
242
// DrawLayerFactory.IncSearchLayer incLayer
243
// = (DrawLayerFactory.IncSearchLayer)editorUI.findLayer(
244
// DrawLayerFactory.INC_SEARCH_LAYER_NAME);
245
// if (incLayer != null) {
246
// if (incLayer.isEnabled()) {
247
// int offs = incLayer.getOffset();
248
// int len = incLayer.getLength();
249
// if (len > 0){
250
// c.select(offs, offs + len);
251
// }
252
// }
253
// }
254
}
255         }
256         EditorFindSupport.getInstance().incSearchReset();
257         findPanel.resetBlockSearch();
258         KeyEventBlocker blocker = findPanel.getBlocker();
259         if (blocker!=null){
260             blocker.stopBlocking(false);
261         }
262     }
263
264     public void windowClosing(WindowEvent e) {
265         hideDialog();
266     }
267
268     public void windowClosed(WindowEvent e) {
269         synchronized (dialogLock) {
270             if (findDialog != null){
271                 xPos = findDialog.getLocation().x;
272                 yPos = findDialog.getLocation().y;
273             }
274         }
275         Map JavaDoc<String JavaDoc, Object JavaDoc> findProps = findPanel.getFindProps();
276         EditorFindSupport.getInstance().incSearchReset();
277         findPanel.resetBlockSearch();
278         EditorFindSupport.getInstance().setBlockSearchHighlight(0, 0);
279         findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE);
280         findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, new Integer JavaDoc(0));
281         findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_END, null);
282         EditorFindSupport.getInstance().putFindProperties(findProps);
283         KeyEventBlocker blocker = findPanel.getBlocker();
284         if (blocker!=null){
285             blocker.stopBlocking(false);
286         }
287         ComponentUtils.returnFocus();
288     }
289
290     public void showFindDialog(KeyEventBlocker blocker) {
291         showFindDialogImpl(false, blocker);
292     }
293
294     public void showReplaceDialog(KeyEventBlocker blocker) {
295         showFindDialogImpl(true, blocker);
296     }
297
298     public void hideDialog() {
299         synchronized (dialogLock) {
300             if (findDialog != null){
301                 xPos = findDialog.getLocation().x;
302                 yPos = findDialog.getLocation().y;
303                 findDialog.dispose();
304             }
305             findDialog = null;
306         }
307     }
308     
309     private Vector JavaDoc<String JavaDoc> getHistoryVector(){
310         List JavaDoc<SPW> histList = EditorFindSupport.getInstance().getHistory();
311         if (histList == null) histList = new ArrayList JavaDoc<SPW>();
312         boolean isRegExpChecked = ((Boolean JavaDoc)findPanel.getFindProps().get(EditorFindSupport.FIND_REG_EXP)).booleanValue();
313         Vector JavaDoc<String JavaDoc> vec = new Vector JavaDoc<String JavaDoc>();
314         for (int i=0; i<histList.size(); i++){
315             SPW spw = (SPW)histList.get(i);
316             String JavaDoc searchExpression = spw.getSearchExpression();
317             if (isRegExpChecked == spw.isRegExp() && !vec.contains(searchExpression)){
318                 vec.add(searchExpression);
319             }
320         }
321         return vec;
322     }
323     
324     private boolean getBooleanProp(String JavaDoc propName, Map JavaDoc map){
325         Boolean JavaDoc b = (Boolean JavaDoc) map.get(propName);
326         return (b!=null) ? b.booleanValue() : false;
327     }
328
329     public void actionPerformed(ActionEvent evt) {
330         if( findButtons == null ) return;
331         
332         Object JavaDoc src = evt.getSource();
333         EditorFindSupport fSup = EditorFindSupport.getInstance();
334         Map JavaDoc<String JavaDoc, Object JavaDoc> findPanelMap = findPanel.getFindProps();
335         
336         SPW spw = new SPW((String JavaDoc)findPanelMap.get(EditorFindSupport.FIND_WHAT),
337                 getBooleanProp(EditorFindSupport.FIND_WHOLE_WORDS, findPanelMap),
338                 getBooleanProp(EditorFindSupport.FIND_MATCH_CASE, findPanelMap),
339                 getBooleanProp(EditorFindSupport.FIND_REG_EXP, findPanelMap));
340
341         if (src == findButtons[0]) { // Find button
342
fSup.addToHistory(spw);
343             fSup.putFindProperties(findPanelMap);
344             fSup.find(null, false);
345             updateCaretPosition();
346             findPerformed = true;
347         } else if (src == findButtons[1]) { // Replace button
348
fSup.addToHistory(spw);
349             findPanel.updateReplaceHistory();
350             fSup.putFindProperties(findPanelMap);
351             try {
352                 if (fSup.replace(null, false)) { // replaced
353
fSup.find(null, false);
354                 }
355             } catch (BadLocationException JavaDoc e) {
356                 // replace in guarded block
357
if (!ComponentUtils.isGuardedException(e)) {
358                     LOG.log(Level.WARNING, e.getMessage(), e);
359                 }
360             }
361             updateCaretPosition();
362             findPerformed = true;
363         } else if (src == findButtons[2]) { // Replace All button
364
fSup.addToHistory(spw);
365             findPanel.updateReplaceHistory();
366             fSup.putFindProperties(findPanelMap);
367             fSup.replaceAll(null);
368             findPerformed = true;
369         } else if (src == findButtons[3]) { // Cancel button
370
hideDialog();
371         }
372     }
373     
374     private int getBlockEndOffset(){
375         Position JavaDoc pos = (Position JavaDoc) EditorFindSupport.getInstance().getFindProperties().get(EditorFindSupport.FIND_BLOCK_SEARCH_END);
376         return (pos != null) ? pos.getOffset() : -1;
377     }
378
379     /** Panel that holds the find logic */
380     private class FindPanel extends EditorFindDialogPanel
381         implements ItemListener, KeyListener, ActionListener, FocusListener {
382
383         private Map JavaDoc<String JavaDoc, Object JavaDoc> findProps = Collections.synchronizedMap(new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(20));
384         private Map JavaDoc<Object JavaDoc, String JavaDoc> objToProps = Collections.synchronizedMap(new HashMap JavaDoc<Object JavaDoc, String JavaDoc>(20));
385
386         private javax.swing.DefaultComboBoxModel JavaDoc findHistory = new javax.swing.DefaultComboBoxModel JavaDoc();
387         private javax.swing.DefaultComboBoxModel JavaDoc replaceHistory = new javax.swing.DefaultComboBoxModel JavaDoc();
388
389         private KeyEventBlocker blocker;
390         
391         private int blockSearchStartPos = 0;
392         private int blockSearchEndPos = 0;
393
394         FindPanel() {
395             objToProps.put(findWhat, EditorFindSupport.FIND_WHAT);
396             objToProps.put(replaceWith, EditorFindSupport.FIND_REPLACE_WITH);
397             objToProps.put(highlightSearch, EditorFindSupport.FIND_HIGHLIGHT_SEARCH);
398             objToProps.put(incSearch, EditorFindSupport.FIND_INC_SEARCH);
399             objToProps.put(matchCase, EditorFindSupport.FIND_MATCH_CASE);
400             //objToProps.put(smartCase, FindSupport.FIND_SMART_CASE);
401
objToProps.put(wholeWords, EditorFindSupport.FIND_WHOLE_WORDS);
402             objToProps.put(regExp, EditorFindSupport.FIND_REG_EXP);
403             objToProps.put(bwdSearch, EditorFindSupport.FIND_BACKWARD_SEARCH);
404             objToProps.put(wrapSearch, EditorFindSupport.FIND_WRAP_SEARCH);
405             objToProps.put(blockSearch, EditorFindSupport.FIND_BLOCK_SEARCH);
406             
407             findProps.putAll(EditorFindSupport.getInstance().getFindProperties());
408             revertMap();
409
410             findWhat.setModel(findHistory);
411             findWhat.getEditor().setItem(getProperty(findWhat));
412             replaceWith.setModel(replaceHistory);
413             replaceWith.getEditor().setItem(getProperty(replaceWith));
414             highlightSearch.setSelected(getBooleanProperty(highlightSearch));
415             incSearch.setSelected(getBooleanProperty(incSearch));
416             matchCase.setSelected(getBooleanProperty(matchCase));
417             //smartCase.setSelected(getBooleanProperty(smartCase));
418
wholeWords.setSelected(getBooleanProperty(wholeWords));
419             regExp.setSelected(getBooleanProperty(regExp));
420             bwdSearch.setSelected(getBooleanProperty(bwdSearch));
421             wrapSearch.setSelected(getBooleanProperty(wrapSearch));
422
423             findWhat.getEditor().getEditorComponent().addKeyListener(this);
424             findWhat.addActionListener(this);
425             replaceWith.getEditor().getEditorComponent().addKeyListener(this);
426             replaceWith.addActionListener(this);
427             highlightSearch.addItemListener(this);
428             incSearch.addItemListener(this);
429             matchCase.addItemListener(this);
430             //smartCase.addItemListener(this);
431
wholeWords.addItemListener(this);
432             regExp.addItemListener(this);
433             bwdSearch.addItemListener(this);
434             wrapSearch.addItemListener(this);
435             blockSearch.addItemListener(this);
436             historyChangeListener = new PropertyChangeListener JavaDoc(){
437                 public void propertyChange(PropertyChangeEvent JavaDoc evt){
438                     if (evt == null || !EditorFindSupport.FIND_HISTORY_CHANGED_PROP.equals(evt.getPropertyName())){
439                         return;
440                     }
441                     updateFindHistory();
442                 }
443             };
444             EditorFindSupport.getInstance().addPropertyChangeListener(historyChangeListener);
445         }
446
447         protected Map JavaDoc<String JavaDoc, Object JavaDoc> getFindProps() {
448             return findProps;
449         }
450         
451         private KeyEventBlocker getBlocker(){
452             return blocker;
453         }
454
455         private void putProperty(Object JavaDoc component, Object JavaDoc value) {
456             String JavaDoc prop = (String JavaDoc)objToProps.get(component);
457             if (prop != null) {
458                 findProps.put(prop, value);
459             }
460         }
461
462         private Object JavaDoc getProperty(Object JavaDoc component) {
463             String JavaDoc prop = (String JavaDoc)objToProps.get(component);
464             return (prop != null) ? findProps.get(prop) : null;
465         }
466
467         private boolean getBooleanProperty(Object JavaDoc component) {
468             Object JavaDoc prop = getProperty(component);
469             return (prop != null) ? ((Boolean JavaDoc)prop).booleanValue() : false;
470         }
471
472         protected void changeVisibility(boolean v) {
473             replaceWith.setVisible(v);
474             replaceWithLabel.setVisible(v);
475         }
476
477         public void resetBlockSearch(){
478             blockSearch.setSelected(false);
479             blockSearch.setEnabled(false);
480             findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE);
481             findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, new Integer JavaDoc(0));
482             findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_END, null);
483             blockSearchStartPos = 0;
484             blockSearchEndPos = 0;
485             EditorFindSupport.getInstance().setBlockSearchHighlight(0,0);
486             EditorFindSupport.getInstance().putFindProperties(findProps);
487         }
488         
489         private void initBlockSearch(){
490             JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
491             String JavaDoc selText = null;
492             int startSelection = 0;
493             int endSelection = 0;
494             boolean blockSearchVisible = false;
495             
496             if (c != null) {
497                 startSelection = c.getSelectionStart();
498                 endSelection = c.getSelectionEnd();
499                 
500                 Document JavaDoc doc = c.getDocument();
501                 try{
502                     int startLine = DocUtils.getLineOffset(doc, startSelection);
503                     int endLine = DocUtils.getLineOffset(doc, endSelection);
504                     if (endLine > startLine) {
505                         blockSearchVisible = true;
506                     }
507                 } catch (BadLocationException JavaDoc ble){
508                 }
509
510                 caretPosition = bwdSearch.isSelected() ? c.getSelectionEnd() : c.getSelectionStart();
511                 
512                 if (blockSearchVisible == false && dialogInvokedViaKeystroke){
513                     dialogInvokedViaKeystroke = false;
514                     selText = c.getSelectedText();
515                     if (selText != null) {
516                         int n = selText.indexOf( '\n' );
517                         if (n >= 0 ) selText = selText.substring(0, n);
518                         findWhat.getEditor().setItem(selText);
519                         changeFindWhat(true);
520                     }
521                 }
522             
523                 blockSearchStartPos = blockSearchVisible ? startSelection : 0;
524                 blockSearchEndPos = blockSearchVisible ? endSelection : 0;
525                 
526                 try{
527                     blockSearch.setEnabled(blockSearchVisible);
528                     blockSearch.setSelected(blockSearchVisible);
529                     findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.valueOf(blockSearchVisible));
530                     findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, new Integer JavaDoc(blockSearchStartPos));
531                     int be = getBlockEndOffset();
532                     if (be < 0){
533                         findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_END, doc.createPosition(blockSearchEndPos));
534                     }else{
535                         blockSearchEndPos = be;
536                     }
537                     EditorFindSupport.getInstance().setBlockSearchHighlight(blockSearchStartPos, blockSearchEndPos);
538                 }catch(BadLocationException JavaDoc ble){
539                     blockSearch.setSelected(false);
540                     findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE);
541                     findProps.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, null);
542                 }
543             }
544             
545         }
546
547         protected void init(boolean isReplace, KeyEventBlocker blocker) {
548             this.blocker = blocker;
549             findHistory.setSelectedItem(null);
550             replaceHistory.setSelectedItem(null);
551             findWhat.getEditor().getEditorComponent().addFocusListener(this);
552             if (isReplace) {
553                 replaceWith.getEditor().getEditorComponent().addFocusListener(this);
554             }
555
556             findProps.putAll(EditorFindSupport.getInstance().getFindProperties());
557             revertMap();
558
559             highlightSearch.setSelected(getBooleanProperty(highlightSearch));
560             incSearch.setSelected(getBooleanProperty(incSearch));
561             matchCase.setSelected(getBooleanProperty(matchCase));
562             //smartCase.setSelected(getBooleanProperty(smartCase));
563
wholeWords.setSelected(getBooleanProperty(wholeWords));
564             boolean regExpValue = getBooleanProperty(regExp);
565             regExp.setSelected(regExpValue);
566             wholeWords.setEnabled(!regExpValue);
567             incSearch.setEnabled(!regExpValue);
568             bwdSearch.setSelected(getBooleanProperty(bwdSearch));
569             wrapSearch.setSelected(getBooleanProperty(wrapSearch));
570             findHistory = new DefaultComboBoxModel(getHistoryVector());
571             findWhat.setModel(findHistory);
572         }
573
574         protected void showNotify() {
575             // fix of issue #66217
576
boolean focused = findWhat.getEditor().getEditorComponent().requestFocusInWindow();
577             if (focused == false){
578                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
579                     public void run(){
580                         findWhat.getEditor().getEditorComponent().requestFocusInWindow();
581                     }
582                 });
583             }
584         }
585
586         private void updateHistory(JComboBox c, javax.swing.DefaultComboBoxModel JavaDoc history) {
587             Object JavaDoc item = c.getEditor().getItem();
588             if( item != null && !item.equals("")) { //NOI18N
589
history.removeElement(item);
590                 history.insertElementAt(item, 0);
591                 history.setSelectedItem(null);
592             }
593             c.getEditor().setItem(item);
594         }
595         
596         protected void updateFindHistory() {
597             //updateHistory(findWhat, findHistory);
598
/*
599             List list = new ArrayList();
600             for (int i = 0; i<findHistory.getSize(); i++){
601                 list.add(findHistory.getElementAt(i));
602             }
603             FindSupport.getFindSupport().putFindProperty(FindSupport.FIND_HISTORY, list);
604             findProps.put(FindSupport.FIND_HISTORY, list);
605              */

606             Object JavaDoc obj = findWhat.getEditor().getItem();
607             findHistory = new DefaultComboBoxModel(getHistoryVector());
608             findWhat.setModel(findHistory);
609             if (obj != null){
610                 findWhat.getEditor().setItem(obj);
611             }
612         }
613
614         protected void updateReplaceHistory() {
615             updateHistory(replaceWith, replaceHistory);
616         }
617
618         private void revertMap(){
619             Object JavaDoc prop = findProps.get(EditorFindSupport.REVERT_MAP);
620             if (!(prop instanceof Map JavaDoc)) return;
621             Map JavaDoc revertMap = (Map JavaDoc)prop;
622
623             for( Iterator JavaDoc i = revertMap.keySet().iterator(); i.hasNext(); ) {
624                 String JavaDoc key = (String JavaDoc)i.next();
625
626                 Object JavaDoc obj = findProps.get(key);
627                 boolean value = ( obj != null ) ? ((Boolean JavaDoc)obj).booleanValue() : false;
628                 if (value != ((Boolean JavaDoc)revertMap.get(key)).booleanValue());
629                     findProps.put(key, value ? Boolean.FALSE : Boolean.TRUE);
630             }
631
632             findProps.put(EditorFindSupport.REVERT_MAP, null);
633         }
634
635         private void changeFindWhat(boolean performIncSearch) {
636             Object JavaDoc old = getProperty(findWhat);
637             Object JavaDoc cur = findWhat.getEditor().getItem();
638             if ((old == null && cur != null && !cur.equals("")) || (old != null && !old.equals(cur))) { // NOI18N
639
putProperty(findWhat, cur);
640                 if (performIncSearch){
641                     findPerformed = EditorFindSupport.getInstance().incSearch(getFindProps(), caretPosition);
642                 }
643             }
644         }
645
646         private void changeReplaceWith() {
647             Object JavaDoc old = getProperty(replaceWith);
648             Object JavaDoc cur = replaceWith.getEditor().getItem();
649             if ((old == null && cur != null && !cur.equals("")) || (old != null && !old.equals(cur))) { // NOI18N
650
putProperty(replaceWith, cur);
651             }
652         }
653
654         private void postChangeCombos(final boolean performIncSearch) {
655             SwingUtilities.invokeLater(
656                 new Runnable JavaDoc() {
657                     public void run() {
658                         changeFindWhat(performIncSearch);
659                         changeReplaceWith();
660                     }
661                 }
662             );
663         }
664
665         public void keyPressed(KeyEvent evt) {
666             if (evt.getKeyChar() == '\n') {
667                 evt.consume();
668             }
669         }
670
671         public void keyReleased(KeyEvent evt) {
672             if (evt.getKeyChar() == '\n') {
673                 evt.consume();
674             } else if (evt.getKeyCode() == KeyEvent.VK_INSERT){
675                 postChangeCombos(true);
676             }
677         }
678
679         public void keyTyped(KeyEvent evt) {
680             if (evt.getKeyChar() == '\n') {
681                 findButtons[0].doClick(20);
682                 evt.consume();
683                 ((JComboBox)((JTextField)evt.getSource()).getParent()).hidePopup();
684             } else {
685                 postChangeCombos(true);
686             }
687         }
688
689         public void itemStateChanged(ItemEvent evt) {
690             Boolean JavaDoc val = (evt.getStateChange() == ItemEvent.SELECTED) ? Boolean.TRUE
691                           : Boolean.FALSE;
692             if (evt.getItem() == bwdSearch){
693                 if (blockSearch.isEnabled() && blockSearch.isSelected()) {
694                     boolean value = val.booleanValue();
695                     JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
696                     if (c!=null){
697                         c.getCaret().setDot(value ? blockSearchEndPos : blockSearchStartPos);
698                         updateCaretPosition();
699                     }
700                     
701                 }
702             }
703             if (evt.getItem() == regExp){
704                 boolean value = !val.booleanValue();
705                 incSearch.setEnabled(value);
706                 wholeWords.setEnabled(value);
707             }
708             if (evt.getItem() == blockSearch){
709                 boolean value = val.booleanValue();
710                 if (value){
711                     if (blockSearchStartPos <= 0 && blockSearchEndPos <= 0 ) {
712                         initBlockSearch();
713                     }else{
714                         JTextComponent JavaDoc c = DocumentsRegistry.getMostActiveComponent();
715                         if (c!=null){
716                             c.getCaret().setDot(bwdSearch.isSelected() ? blockSearchEndPos : blockSearchStartPos);
717                             updateCaretPosition();
718                         }
719                     }
720                     EditorFindSupport.getInstance().setBlockSearchHighlight(blockSearchStartPos, blockSearchEndPos);
721                 } else {
722                     EditorFindSupport.getInstance().putFindProperty(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE);
723                     EditorFindSupport.getInstance().setBlockSearchHighlight(0, 0);
724                 }
725             }
726                           
727             putProperty(evt.getSource(), val);
728             
729             if (evt.getItem() == regExp){
730                 updateFindHistory();
731             }
732         }
733
734         public void actionPerformed(ActionEvent evt) {
735             postChangeCombos(false);
736         }
737
738         public void focusGained(FocusEvent e) {
739             if (e.getSource() instanceof JTextField) {
740                 ((JTextField)e.getSource()).selectAll();
741                 if (blocker != null){
742                     blocker.stopBlocking();
743                 }
744             }
745             ((JComponent)e.getSource()).removeFocusListener(this);
746         }
747
748         public void focusLost(FocusEvent e) {
749             
750         }
751
752
753
754     }
755
756 }
757
Popular Tags