KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > FindDialogSupport


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.ext;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.*;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Vector JavaDoc;
34 import javax.swing.*;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import javax.swing.text.BadLocationException JavaDoc;
38 import javax.swing.text.Position JavaDoc;
39 import org.netbeans.editor.BaseKit;
40 import org.netbeans.editor.BaseTextUI;
41 import org.netbeans.editor.DrawLayerFactory;
42 import org.netbeans.editor.EditorUI;
43 import org.netbeans.editor.FindSupport.SearchPatternWrapper;
44 import org.netbeans.editor.SettingsNames;
45 import org.netbeans.editor.SettingsUtil;
46 import org.netbeans.editor.FindSupport;
47 import org.netbeans.editor.DialogSupport;
48 import org.netbeans.editor.GuardedException;
49 import org.netbeans.editor.Utilities;
50 import org.netbeans.editor.BaseCaret;
51 import java.util.Iterator JavaDoc;
52 import javax.swing.text.Caret JavaDoc;
53 import javax.swing.text.DefaultEditorKit JavaDoc;
54 import javax.swing.text.Document JavaDoc;
55 import org.netbeans.editor.BaseDocument;
56 import org.openide.util.NbBundle;
57
58 /**
59 * Support for displaying find and replace dialogs
60 *
61 * @author Miloslav Metelka
62 * @version 1.00
63 * @deprecated Without any replacement.
64 */

65
66 public class FindDialogSupport extends WindowAdapter implements ActionListener {
67     
68     /** This lock is used to create a barrier between showing/hiding/changing
69      * the dialog and testing if the dialog is already shown.
70      * it is used to make test-and-change / test-and-display actions atomic.
71      * It covers the following four fields: findDialog, isReplaceDialog,
72      * findPanel, findButtons
73      */

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

106     private static boolean dialogInvokedViaKeystroke;
107     
108     public static FindDialogSupport getFindDialogSupport() {
109         if (singleton == null) {
110             singleton = new FindDialogSupport();
111         }
112         return singleton;
113     }
114
115     private FindDialogSupport() {
116     }
117
118     private void createFindButtons() {
119         if (findButtons == null) {
120             ResourceBundle JavaDoc bundle = NbBundle.getBundle(BaseKit.class);
121             findButtons = new JButton[] {
122                 new JButton(bundle.getString("find-button-find")), // NOI18N
123
new JButton(bundle.getString("find-button-replace")), // NOI18N
124
new JButton(bundle.getString("find-button-replace-all")), // NOI18N
125
new JButton(bundle.getString("find-button-cancel")) // NOI18N
126
};
127
128             findButtons[0].setMnemonic(bundle.getString("find-button-find-mnemonic").charAt(0)); // NOI18N
129
findButtons[1].setMnemonic(bundle.getString("find-button-replace-mnemonic").charAt(0)); // NOI18N
130
findButtons[2].setMnemonic(bundle.getString("find-button-replace-all-mnemonic").charAt(0)); // NOI18N
131

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

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

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

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