KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > search > SearchDialog


1 /*
2  * SearchDialog.java - Search and replace dialog
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.search;
24
25 //{{{ Imports
26
import javax.swing.border.*;
27 import javax.swing.*;
28
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.gjt.sp.jedit.browser.VFSBrowser;
35 import org.gjt.sp.jedit.gui.*;
36 import org.gjt.sp.jedit.io.*;
37 import org.gjt.sp.jedit.msg.SearchSettingsChanged;
38 import org.gjt.sp.jedit.*;
39 //}}}
40

41 /**
42  * Search and replace dialog.
43  * @author Slava Pestov
44  * @version $Id: SearchDialog.java 8248 2006-12-21 21:31:28Z kpouer $
45  */

46 public class SearchDialog extends EnhancedDialog implements EBComponent
47 {
48     //{{{ Constants
49
/**
50      * Default file set.
51      * @since jEdit 3.2pre2
52      */

53     public static final int CURRENT_BUFFER = 0;
54     public static final int ALL_BUFFERS = 1;
55     public static final int DIRECTORY = 2;
56     //}}}
57

58     //{{{ getSearchDialog() method
59
public static SearchDialog getSearchDialog(View view)
60     {
61         if(Debug.DISABLE_SEARCH_DIALOG_POOL)
62             return new SearchDialog(view);
63         else
64         {
65
66             SearchDialog searchDialog = viewHash.get(view);
67             if (searchDialog == null)
68             {
69                 searchDialog = new SearchDialog(view);
70                 viewHash.put(view, searchDialog);
71             }
72             return searchDialog;
73         }
74     } //}}}
75

76     //{{{ preloadSearchDialog() method
77
/**
78      * Preloads the search dialog for the given for so that it can be
79      * quickly displayed later.
80      *
81      * @param view the view from which you want to preload the SearchDialog
82      * @since jEdit 4.2pre3
83      * @deprecated no reason to use this method anymore now we have JIT compiler
84      */

85     @Deprecated JavaDoc
86     public static void preloadSearchDialog(View view)
87     {
88         if(Debug.DISABLE_SEARCH_DIALOG_POOL)
89             return;
90
91         SearchDialog dialog = new SearchDialog(view);
92         viewHash.put(view,dialog);
93     } //}}}
94

95     //{{{ showSearchDialog() method
96
/**
97      * Displays a search and replace dialog box, reusing an existing one
98      * if necessary.
99      * @param view The view
100      * @param searchString The search string
101      * @param searchIn One of CURRENT_BUFFER, ALL_BUFFERS, or DIRECTORY
102      * @since jEdit 4.0pre6
103      */

104     public static void showSearchDialog(View view, String JavaDoc searchString,
105         int searchIn)
106     {
107         final SearchDialog dialog = getSearchDialog(view);
108
109         dialog.setSearchString(searchString,searchIn);
110
111         // ugly workaround
112
if(OperatingSystem.isUnix() && !OperatingSystem.isMacOS())
113             dialog.setVisible(false);
114
115         // I'm not sure if calling requestFocus() is strictly necessary
116
// (focus looks fine without this, on Linux at least), but
117
// it doesn't hurt to leave it here.
118
SwingUtilities.invokeLater(new Runnable JavaDoc() {
119             public void run() {
120                 dialog.toFront();
121                 dialog.find.requestFocus();
122             }
123         });
124         dialog.setVisible(true);
125     } //}}}
126

127     //{{{ setSearchString() method
128
/**
129      * Sets the search string.
130      *
131      * @param searchString The search string
132      * @param searchIn One of {@link #CURRENT_BUFFER}, {@link #ALL_BUFFERS}, or {@link #DIRECTORY}
133      * @since jEdit 4.0pre5
134      */

135     public void setSearchString(String JavaDoc searchString, int searchIn)
136     {
137         find.setText(null);
138         replace.setText(null);
139
140         if(searchString == null)
141             searchCurrentBuffer.setSelected(true);
142         else
143         {
144             if(searchString.indexOf('\n') == -1)
145             {
146                 if(SearchAndReplace.getRegexp())
147                 {
148                     find.setText(SearchAndReplace.escapeRegexp(
149                         searchString,true));
150                 }
151                 else
152                     find.setText(searchString);
153                 find.selectAll();
154                 searchCurrentBuffer.setSelected(true);
155             }
156             else if(searchIn == CURRENT_BUFFER)
157             {
158                 searchSelection.setSelected(true);
159                 hyperSearch.setSelected(true);
160             }
161         }
162
163         if(searchIn == CURRENT_BUFFER)
164         {
165             if(!searchSelection.isSelected())
166             {
167                 // might be already selected, see above.
168
searchCurrentBuffer.setSelected(true);
169
170                 /* this property is only loaded and saved if
171                  * the 'current buffer' file set is selected.
172                  * otherwise, it defaults to on. */

173                 hyperSearch.setSelected(jEdit.getBooleanProperty(
174                     "search.hypersearch.toggle"));
175             }
176         }
177         else if(searchIn == ALL_BUFFERS)
178         {
179             searchAllBuffers.setSelected(true);
180             hyperSearch.setSelected(true);
181         }
182         else if(searchIn == DIRECTORY)
183         {
184             SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
185
186             if(fileset instanceof DirectoryListSet)
187             {
188                 filter.setText(((DirectoryListSet)fileset)
189                     .getFileFilter());
190                 directory.setText(((DirectoryListSet)fileset)
191                     .getDirectory());
192                 searchSubDirectories.setSelected(((DirectoryListSet)fileset)
193                     .isRecursive());
194             }
195
196             hyperSearch.setSelected(true);
197             searchDirectory.setSelected(true);
198         }
199
200         updateEnabled();
201     } //}}}
202

203     //{{{ ok() method
204
public void ok()
205     {
206         try
207         {
208             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
209
210             if(!save(false))
211                 return;
212
213             if(searchSelection.isSelected()
214                 && view.getTextArea().getSelectionCount() == 0)
215             {
216                 GUIUtilities.error(view,"search-no-selection",null);
217                 return;
218             }
219
220             if(hyperSearch.isSelected() || searchSelection.isSelected())
221             {
222                 if(SearchAndReplace.hyperSearch(view,
223                     searchSelection.isSelected()))
224                     closeOrKeepDialog();
225             }
226             else
227             {
228                 if(SearchAndReplace.find(view))
229                     closeOrKeepDialog();
230                 else
231                 {
232                     toFront();
233                     requestFocus();
234                     find.requestFocus();
235                 }
236             }
237         }
238         finally
239         {
240             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
241         }
242     } //}}}
243

244     //{{{ cancel() method
245
public void cancel()
246     {
247         save(true);
248         GUIUtilities.saveGeometry(this,"search");
249         setVisible(false);
250     } //}}}
251

252     //{{{ handleMessage() method
253
public void handleMessage(EBMessage msg)
254     {
255         if(msg instanceof SearchSettingsChanged)
256         {
257             if(!saving)
258                 load();
259         }
260     } //}}}
261

262     //{{{ dispose() method
263
public void dispose()
264     {
265         EditBus.removeFromBus(this);
266         viewHash.remove(view);
267         super.dispose();
268     } //}}}
269

270     //{{{ Private members
271

272     private static final Map JavaDoc<View, SearchDialog> viewHash = new HashMap JavaDoc<View, SearchDialog>();
273
274     //{{{ Instance variables
275
private final View view;
276
277     // fields
278
private HistoryTextArea find, replace;
279
280     private JRadioButton stringReplace, beanShellReplace;
281
282     // search settings
283
private JCheckBox keepDialog, ignoreCase, regexp, hyperSearch,
284         wrap;
285     private JRadioButton searchBack, searchForward;
286     private JRadioButton searchSelection, searchCurrentBuffer,
287         searchAllBuffers, searchDirectory;
288
289     // multifile settings
290
private HistoryTextField filter, directory;
291     private JCheckBox searchSubDirectories;
292     private JCheckBox skipBinaryFiles;
293     private JCheckBox skipHidden;
294     
295     private JButton choose;
296     private JButton synchronize;
297
298     // buttons
299
private JButton findBtn, /* replaceBtn, */ replaceAndFindBtn, replaceAllBtn,
300         closeBtn;
301
302     private boolean saving;
303     //}}}
304

305     //{{{ SearchDialog constructor
306
/**
307      * Creates a new search and replace dialog box.
308      * @param view The view
309      */

310     private SearchDialog(View view)
311     {
312         super(view,jEdit.getProperty("search.title"),false);
313
314         this.view = view;
315
316         JPanel content = new JPanel(new BorderLayout());
317         content.setBorder(new EmptyBorder(0,12,12,12));
318         setContentPane(content);
319
320         JPanel centerPanel = new JPanel(new BorderLayout());
321         centerPanel.add(BorderLayout.CENTER,createFieldPanel());
322         centerPanel.add(BorderLayout.SOUTH,createSearchSettingsPanel());
323         content.add(BorderLayout.CENTER,centerPanel);
324         content.add(BorderLayout.SOUTH,createMultiFilePanel());
325
326         content.add(BorderLayout.EAST,createButtonsPanel());
327
328         pack();
329         jEdit.unsetProperty("search.width");
330         jEdit.unsetProperty("search.d-width");
331         jEdit.unsetProperty("search.height");
332         jEdit.unsetProperty("search.d-height");
333         GUIUtilities.loadGeometry(this,"search");
334
335         load();
336
337         EditBus.addToBus(this);
338     } //}}}
339

340     //{{{ createFindLabelAndField() method
341
private void createFindLabelAndField(JPanel fieldPanel,
342         GridBagConstraints cons)
343     {
344         JLabel label = new JLabel(jEdit.getProperty("search.find"));
345         
346         label.setDisplayedMnemonic(jEdit.getProperty("search.find.mnemonic")
347             .charAt(0));
348         find = new HistoryTextArea("find");
349         find.setColumns(25);
350         find.setToolTipText(jEdit.getProperty("search.find.tooltip"));
351         label.setToolTipText(jEdit.getProperty("search.find.tooltip"));
352         label.setLabelFor(find);
353         label.setBorder(new EmptyBorder(12,0,2,0));
354
355         cons.gridx = 0;
356         cons.weightx = 0.0;
357         cons.weighty = 0.0;
358         fieldPanel.add(label,cons);
359         cons.gridy++;
360         cons.weightx = 1.0;
361         cons.weighty = 1.0;
362         fieldPanel.add(new JScrollPane(find),cons);
363         cons.gridy++;
364     } //}}}
365

366     //{{{ createReplaceLabelAndField() method
367
private void createReplaceLabelAndField(JPanel fieldPanel,
368         GridBagConstraints cons)
369     {
370         JLabel label = new JLabel(jEdit.getProperty("search.replace"));
371         label.setDisplayedMnemonic(jEdit.getProperty("search.replace.mnemonic")
372             .charAt(0));
373         label.setBorder(new EmptyBorder(12,0,0,0));
374
375         cons.gridx = 0;
376         cons.weightx = 0.0;
377         cons.weighty = 0.0;
378         fieldPanel.add(label,cons);
379         cons.gridy++;
380
381         ButtonGroup grp = new ButtonGroup();
382         ReplaceActionHandler replaceActionHandler = new ReplaceActionHandler();
383
384         // we use a custom JRadioButton subclass that returns
385
// false for isFocusTraversable() so that the user can
386
// tab from the search field to the replace field with
387
// one keystroke
388

389         stringReplace = new MyJRadioButton(jEdit.getProperty(
390             "search.string-replace-btn"));
391         stringReplace.addActionListener(replaceActionHandler);
392         grp.add(stringReplace);
393         cons.gridwidth = 1;
394         fieldPanel.add(stringReplace,cons);
395         cons.gridx++;
396         cons.insets = new Insets(0,12,0,0);
397
398         beanShellReplace = new MyJRadioButton(jEdit.getProperty(
399             "search.beanshell-replace-btn"));
400         beanShellReplace.addActionListener(replaceActionHandler);
401         grp.add(beanShellReplace);
402         fieldPanel.add(beanShellReplace,cons);
403         cons.gridx = 0;
404         cons.gridwidth = 2;
405         cons.insets = new Insets(0,0,0,0);
406
407         replace = new HistoryTextArea("replace");
408         replace.setToolTipText(jEdit.getProperty("search.find.tooltip"));
409         label.setLabelFor(replace);
410
411         cons.gridx = 0;
412         cons.gridy++;
413         cons.weightx = 1.0;
414         cons.weighty = 1.0;
415         fieldPanel.add(new JScrollPane(replace),cons);
416         cons.gridy++;
417     } //}}}
418

419     //{{{ createFieldPanel() method
420
private JPanel createFieldPanel()
421     {
422         JPanel fieldPanel = new JPanel(new GridBagLayout());
423         fieldPanel.setBorder(new EmptyBorder(0,0,12,12));
424
425         GridBagConstraints cons = new GridBagConstraints();
426         cons.fill = GridBagConstraints.BOTH;
427         cons.gridy = 0;
428         cons.gridwidth = 2;
429
430         createFindLabelAndField(fieldPanel,cons);
431         createReplaceLabelAndField(fieldPanel,cons);
432
433         return fieldPanel;
434     } //}}}
435

436     //{{{ createSearchSettingsPanel() method
437
private JPanel createSearchSettingsPanel()
438     {
439         JPanel searchSettings = new JPanel(new VariableGridLayout(
440             VariableGridLayout.FIXED_NUM_COLUMNS,3));
441         searchSettings.setBorder(new EmptyBorder(0,0,12,12));
442
443         SettingsActionHandler actionHandler = new SettingsActionHandler();
444         ButtonGroup fileset = new ButtonGroup();
445         ButtonGroup direction = new ButtonGroup();
446
447         searchSettings.add(new JLabel(jEdit.getProperty("search.fileset")));
448
449         searchSettings.add(new JLabel(jEdit.getProperty("search.settings")));
450
451         searchSettings.add(new JLabel(jEdit.getProperty("search.direction")));
452
453         searchSelection = new JRadioButton(jEdit.getProperty("search.selection"));
454         searchSelection.setMnemonic(jEdit.getProperty("search.selection.mnemonic")
455             .charAt(0));
456         fileset.add(searchSelection);
457         searchSettings.add(searchSelection);
458         searchSelection.addActionListener(actionHandler);
459
460         keepDialog = new JCheckBox(jEdit.getProperty("search.keep"));
461         keepDialog.setMnemonic(jEdit.getProperty("search.keep.mnemonic")
462             .charAt(0));
463         searchSettings.add(keepDialog);
464
465         searchBack = new JRadioButton(jEdit.getProperty("search.back"));
466         searchBack.setMnemonic(jEdit.getProperty("search.back.mnemonic")
467             .charAt(0));
468         direction.add(searchBack);
469         searchSettings.add(searchBack);
470         searchBack.addActionListener(actionHandler);
471
472         searchCurrentBuffer = new JRadioButton(jEdit.getProperty("search.current"));
473         searchCurrentBuffer.setMnemonic(jEdit.getProperty("search.current.mnemonic")
474             .charAt(0));
475         fileset.add(searchCurrentBuffer);
476         searchSettings.add(searchCurrentBuffer);
477         searchCurrentBuffer.addActionListener(actionHandler);
478
479         ignoreCase = new JCheckBox(jEdit.getProperty("search.case"));
480         ignoreCase.setMnemonic(jEdit.getProperty("search.case.mnemonic")
481             .charAt(0));
482         searchSettings.add(ignoreCase);
483         ignoreCase.addActionListener(actionHandler);
484
485         searchForward = new JRadioButton(jEdit.getProperty("search.forward"));
486         searchForward.setMnemonic(jEdit.getProperty("search.forward.mnemonic")
487             .charAt(0));
488         direction.add(searchForward);
489         searchSettings.add(searchForward);
490         searchForward.addActionListener(actionHandler);
491
492         searchAllBuffers = new JRadioButton(jEdit.getProperty("search.all"));
493         searchAllBuffers.setMnemonic(jEdit.getProperty("search.all.mnemonic")
494             .charAt(0));
495         fileset.add(searchAllBuffers);
496         searchSettings.add(searchAllBuffers);
497         searchAllBuffers.addActionListener(actionHandler);
498
499         regexp = new JCheckBox(jEdit.getProperty("search.regexp"));
500         regexp.setMnemonic(jEdit.getProperty("search.regexp.mnemonic")
501             .charAt(0));
502         searchSettings.add(regexp);
503         regexp.addActionListener(actionHandler);
504
505         wrap = new JCheckBox(jEdit.getProperty("search.wrap"));
506         wrap.setMnemonic(jEdit.getProperty("search.wrap.mnemonic")
507             .charAt(0));
508         searchSettings.add(wrap);
509         wrap.addActionListener(actionHandler);
510
511         searchDirectory = new JRadioButton(jEdit.getProperty("search.directory"));
512         searchDirectory.setMnemonic(jEdit.getProperty("search.directory.mnemonic")
513             .charAt(0));
514         fileset.add(searchDirectory);
515         searchSettings.add(searchDirectory);
516         searchDirectory.addActionListener(actionHandler);
517
518         hyperSearch = new JCheckBox(jEdit.getProperty("search.hypersearch"));
519         hyperSearch.setMnemonic(jEdit.getProperty("search.hypersearch.mnemonic")
520             .charAt(0));
521         searchSettings.add(hyperSearch);
522         hyperSearch.addActionListener(actionHandler);
523
524         return searchSettings;
525     } //}}}
526

527     //{{{ createMultiFilePanel() method
528
private JPanel createMultiFilePanel()
529     {
530         JPanel multifile = new JPanel();
531
532         GridBagLayout layout = new GridBagLayout();
533         multifile.setLayout(layout);
534
535         GridBagConstraints cons = new GridBagConstraints();
536         cons.gridy = cons.gridwidth = cons.gridheight = 1;
537         cons.anchor = GridBagConstraints.WEST;
538         cons.fill = GridBagConstraints.HORIZONTAL;
539
540         MultiFileActionHandler actionListener = new MultiFileActionHandler();
541         filter = new HistoryTextField("search.filter");
542         
543         filter.setToolTipText(jEdit.getProperty("search.filterField.tooltip"));
544         filter.addActionListener(actionListener);
545
546         cons.insets = new Insets(0,0,3,0);
547
548         JLabel label = new JLabel(jEdit.getProperty("search.filterField"),
549             SwingConstants.RIGHT);
550         label.setBorder(new EmptyBorder(0,0,0,12));
551         label.setDisplayedMnemonic(jEdit.getProperty("search.filterField.mnemonic")
552             .charAt(0));
553         label.setLabelFor(filter);
554         cons.weightx = 0.0;
555         layout.setConstraints(label,cons);
556         multifile.add(label);
557
558         cons.gridwidth = 2;
559         cons.insets = new Insets(0,0,3,6);
560         cons.weightx = 1.0;
561         layout.setConstraints(filter,cons);
562         multifile.add(filter);
563
564         cons.gridwidth = 1;
565         cons.weightx = 0.0;
566         cons.insets = new Insets(0,0,3,0);
567
568         synchronize = new JButton(jEdit.getProperty(
569             "search.synchronize"));
570         synchronize.setMnemonic(jEdit.getProperty(
571             "search.synchronize.mnemonic")
572             .charAt(0));
573         synchronize.addActionListener(actionListener);
574         layout.setConstraints(synchronize,cons);
575         multifile.add(synchronize);
576
577         cons.gridy++;
578
579         directory = new HistoryTextField("search.directory");
580         directory.setColumns(25);
581         directory.addActionListener(actionListener);
582
583         label = new JLabel(jEdit.getProperty("search.directoryField"),
584             SwingConstants.RIGHT);
585         label.setBorder(new EmptyBorder(0,0,0,12));
586
587         label.setDisplayedMnemonic(jEdit.getProperty("search.directoryField.mnemonic")
588             .charAt(0));
589         label.setLabelFor(directory);
590         cons.insets = new Insets(0,0,3,0);
591         cons.weightx = 0.0;
592         layout.setConstraints(label,cons);
593         multifile.add(label);
594
595         cons.insets = new Insets(0,0,3,6);
596         cons.weightx = 1.0;
597         cons.gridwidth = 2;
598         layout.setConstraints(directory,cons);
599         multifile.add(directory);
600
601         choose = new JButton(jEdit.getProperty("search.choose"));
602         choose.setMnemonic(jEdit.getProperty("search.choose.mnemonic")
603             .charAt(0));
604         cons.insets = new Insets(0,0,3,0);
605         cons.weightx = 0.0;
606         cons.gridwidth = 1;
607         layout.setConstraints(choose,cons);
608         multifile.add(choose);
609         choose.addActionListener(actionListener);
610
611         cons.insets = new Insets(0,0,0,0);
612         cons.gridy++;
613         cons.gridwidth = 3;
614
615         JPanel dirCheckBoxPanel = new JPanel();
616         searchSubDirectories = new JCheckBox(jEdit.getProperty(
617             "search.subdirs"));
618         String JavaDoc mnemonic = jEdit.getProperty(
619             "search.subdirs.mnemonic");
620         searchSubDirectories.setMnemonic(mnemonic.charAt(0));
621         searchSubDirectories.setSelected(jEdit.getBooleanProperty("search.subdirs.toggle"));
622         skipHidden = new JCheckBox(jEdit.getProperty("search.skipHidden"));
623         skipHidden.setSelected(jEdit.getBooleanProperty("search.skipHidden.toggle", true));
624         skipBinaryFiles = new JCheckBox(jEdit.getProperty("search.skipBinary"));
625         skipBinaryFiles.setSelected(jEdit.getBooleanProperty("search.skipBinary.toggle", true));
626         dirCheckBoxPanel.add(searchSubDirectories);
627         dirCheckBoxPanel.add(skipHidden);
628         dirCheckBoxPanel.add(skipBinaryFiles);
629
630         cons.insets = new Insets(0, 0, 0, 0);
631         cons.gridy++;
632         cons.gridwidth = 4;
633         layout.setConstraints(dirCheckBoxPanel, cons);
634
635         multifile.add(dirCheckBoxPanel);
636
637         return multifile;
638     } //}}}
639

640     //{{{ createButtonsPanel() method
641
private Box createButtonsPanel()
642     {
643         Box box = new Box(BoxLayout.Y_AXIS);
644
645         ButtonActionHandler actionHandler = new ButtonActionHandler();
646
647         box.add(Box.createVerticalStrut(12));
648
649         JPanel grid = new JPanel(new GridLayout(5,1,0,12));
650
651         findBtn = new JButton(jEdit.getProperty("search.findBtn"));
652         /* findBtn.setMnemonic(jEdit.getProperty("search.findBtn.mnemonic")
653             .charAt(0)); */

654         getRootPane().setDefaultButton(findBtn);
655         grid.add(findBtn);
656         findBtn.addActionListener(actionHandler);
657
658         /* replaceBtn = new JButton(jEdit.getProperty("search.replaceBtn"));
659         replaceBtn.setMnemonic(jEdit.getProperty("search.replaceBtn.mnemonic")
660             .charAt(0));
661         grid.add(replaceBtn);
662         replaceBtn.addActionListener(actionHandler); */

663
664         replaceAndFindBtn = new JButton(jEdit.getProperty("search.replaceAndFindBtn"));
665         replaceAndFindBtn.setMnemonic(jEdit.getProperty("search.replaceAndFindBtn.mnemonic")
666             .charAt(0));
667         grid.add(replaceAndFindBtn);
668         replaceAndFindBtn.addActionListener(actionHandler);
669
670         replaceAllBtn = new JButton(jEdit.getProperty("search.replaceAllBtn"));
671         replaceAllBtn.setMnemonic(jEdit.getProperty("search.replaceAllBtn.mnemonic")
672             .charAt(0));
673         grid.add(replaceAllBtn);
674         replaceAllBtn.addActionListener(actionHandler);
675
676         closeBtn = new JButton(jEdit.getProperty("common.close"));
677         grid.add(closeBtn);
678         closeBtn.addActionListener(actionHandler);
679
680         grid.setMaximumSize(grid.getPreferredSize());
681
682         box.add(grid);
683         box.add(Box.createGlue());
684
685         return box;
686     } //}}}
687

688     //{{{ updateEnabled() method
689
private void updateEnabled()
690     {
691         wrap.setEnabled(!hyperSearch.isSelected()
692             && !searchSelection.isSelected());
693
694         boolean reverseEnabled = !hyperSearch.isSelected()
695             && searchCurrentBuffer.isSelected()
696             && !regexp.isSelected();
697         searchBack.setEnabled(reverseEnabled);
698         searchForward.setEnabled(reverseEnabled);
699         if(!reverseEnabled)
700             searchForward.setSelected(true);
701
702         filter.setEnabled(searchAllBuffers.isSelected()
703             || searchDirectory.isSelected());
704
705         boolean searchDirs = searchDirectory.isSelected();
706         directory.setEnabled(searchDirs);
707         choose.setEnabled(searchDirs);
708         searchSubDirectories.setEnabled(searchDirs);
709         skipHidden.setEnabled(searchDirs);
710         skipBinaryFiles.setEnabled(searchDirs);
711         
712         synchronize.setEnabled(searchAllBuffers.isSelected()
713             || searchDirectory.isSelected());
714
715         findBtn.setEnabled(!searchSelection.isSelected()
716             || hyperSearch.isSelected());
717         replaceAndFindBtn.setEnabled(!hyperSearch.isSelected()
718             && !searchSelection.isSelected());
719     } //}}}
720

721     //{{{ save() method
722
/**
723      * @param cancel If true, we don't bother the user with warning messages
724      */

725     private boolean save(boolean cancel)
726     {
727         try
728         {
729             // prevents us from handling SearchSettingsChanged
730
// as a result of below
731
saving = true;
732             SearchAndReplace.setIgnoreCase(ignoreCase.isSelected());
733             SearchAndReplace.setRegexp(regexp.isSelected());
734             SearchAndReplace.setReverseSearch(searchBack.isSelected());
735             SearchAndReplace.setAutoWrapAround(wrap.isSelected());
736             jEdit.setBooleanProperty("search.subdirs.toggle", searchSubDirectories.isSelected());
737             jEdit.setBooleanProperty("search.skipHidden.toggle", skipHidden.isSelected());
738             jEdit.setBooleanProperty("search.skipBinary.toggle", skipBinaryFiles.isSelected());
739
740             String JavaDoc filter = this.filter.getText();
741             this.filter.addCurrentToHistory();
742             if(filter.length() == 0)
743                 filter = "*";
744
745             SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
746
747             boolean recurse = searchSubDirectories.isSelected();
748
749             if(searchSelection.isSelected())
750                 fileset = new CurrentBufferSet();
751             else if(searchCurrentBuffer.isSelected())
752             {
753                 fileset = new CurrentBufferSet();
754
755                 jEdit.setBooleanProperty("search.hypersearch.toggle",
756                     hyperSearch.isSelected());
757             }
758             else if(searchAllBuffers.isSelected())
759                 fileset = new AllBufferSet(filter);
760             else if(searchDirectory.isSelected())
761             {
762                 String JavaDoc directory = this.directory.getText();
763                 this.directory.addCurrentToHistory();
764                 directory = MiscUtilities.constructPath(
765                     view.getBuffer().getDirectory(),directory);
766
767                 if((VFSManager.getVFSForPath(directory).getCapabilities()
768                     & VFS.LOW_LATENCY_CAP) == 0)
769                 {
770                     if(cancel)
771                         return false;
772
773                     int retVal = GUIUtilities.confirm(
774                         this,"remote-dir-search",
775                         null,JOptionPane.YES_NO_OPTION,
776                         JOptionPane.WARNING_MESSAGE);
777                     if(retVal != JOptionPane.YES_OPTION)
778                         return false;
779                 }
780
781                 if(fileset instanceof DirectoryListSet)
782                 {
783                     DirectoryListSet dset = (DirectoryListSet)fileset;
784                     dset.setDirectory(directory);
785                     dset.setFileFilter(filter);
786                     dset.setRecursive(recurse);
787                     EditBus.send(new SearchSettingsChanged(null));
788                 }
789                 else
790                     fileset = new DirectoryListSet(directory,filter,recurse);
791             }
792             else
793             {
794                 // can't happen
795
fileset = null;
796             }
797
798             jEdit.setBooleanProperty("search.subdirs.toggle",
799                 recurse);
800             jEdit.setBooleanProperty("search.keepDialog.toggle",
801                 keepDialog.isSelected());
802
803             SearchAndReplace.setSearchFileSet(fileset);
804
805             replace.addCurrentToHistory();
806             SearchAndReplace.setReplaceString(replace.getText());
807
808             if(find.getText().length() == 0)
809             {
810                 if(!cancel)
811                     getToolkit().beep();
812                 return false;
813             }
814
815             find.addCurrentToHistory();
816             SearchAndReplace.setSearchString(find.getText());
817
818             return true;
819         }
820         finally
821         {
822             saving = false;
823         }
824     } //}}}
825

826     //{{{ closeOrKeepDialog() method
827
private void closeOrKeepDialog()
828     {
829         if(keepDialog.isSelected())
830         {
831             // Windows bug workaround in case a YES/NO confirm
832
// was shown
833

834             // ... but if HyperSearch results window is floating,
835
// the workaround causes problems!
836
if(!hyperSearch.isSelected())
837             {
838                 toFront();
839                 requestFocus();
840                 find.requestFocus();
841             }
842         }
843         else
844         {
845             GUIUtilities.saveGeometry(this,"search");
846             setVisible(false);
847         }
848     } //}}}
849

850     //{{{ load() method
851
private void load()
852     {
853         ignoreCase.setSelected(SearchAndReplace.getIgnoreCase());
854         regexp.setSelected(SearchAndReplace.getRegexp());
855         wrap.setSelected(SearchAndReplace.getAutoWrapAround());
856
857         if(SearchAndReplace.getReverseSearch())
858             searchBack.setSelected(true);
859         else
860             searchForward.setSelected(true);
861
862         if(SearchAndReplace.getBeanShellReplace())
863         {
864             replace.setModel("replace.script");
865             beanShellReplace.setSelected(true);
866         }
867         else
868         {
869             replace.setModel("replace");
870             stringReplace.setSelected(true);
871         }
872
873         SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
874
875         HistoryModel model = filter.getModel();
876         if(model.getSize() != 0)
877             filter.setText(model.getItem(0));
878         else
879         {
880             filter.setText('*' + MiscUtilities
881                 .getFileExtension(view.getBuffer()
882                 .getName()));
883         }
884         model = directory.getModel();
885         if(model.getSize() != 0)
886             directory.setText(model.getItem(0));
887         else
888             directory.setText(view.getBuffer().getDirectory());
889
890         searchSubDirectories.setSelected(jEdit.getBooleanProperty(
891             "search.subdirs.toggle"));
892
893         if(fileset instanceof DirectoryListSet)
894         {
895             filter.setText(((DirectoryListSet)fileset)
896                 .getFileFilter());
897             directory.setText(((DirectoryListSet)fileset)
898                 .getDirectory());
899             searchSubDirectories.setSelected(((DirectoryListSet)fileset)
900                 .isRecursive());
901         }
902         else if(fileset instanceof AllBufferSet)
903         {
904             filter.setText(((AllBufferSet)fileset)
905                 .getFileFilter());
906         }
907
908         directory.addCurrentToHistory();
909
910         keepDialog.setSelected(jEdit.getBooleanProperty(
911             "search.keepDialog.toggle"));
912     } //}}}
913

914     //}}}
915

916     //{{{ Inner classes
917

918     //{{{ MyJRadioButton class
919

920     // used for the stringReplace and beanShell replace radio buttons,
921
// so that the user can press tab to go from the find field to the
922
// replace field in one go
923
static class MyJRadioButton extends JRadioButton
924     {
925         MyJRadioButton(String JavaDoc label)
926         {
927             super(label);
928         }
929
930         public boolean isFocusable()
931         {
932             return false;
933         }
934     } //}}}
935

936     //{{{ ReplaceActionHandler class
937
class ReplaceActionHandler implements ActionListener
938     {
939         public void actionPerformed(ActionEvent evt)
940         {
941             replace.setModel(beanShellReplace.isSelected()
942                 ? "replace.script"
943                 : "replace");
944             SearchAndReplace.setBeanShellReplace(
945                 beanShellReplace.isSelected());
946         }
947     } //}}}
948

949     //{{{ SettingsActionHandler class
950
class SettingsActionHandler implements ActionListener
951     {
952         public void actionPerformed(ActionEvent evt)
953         {
954             Object JavaDoc source = evt.getSource();
955
956             if(source == searchCurrentBuffer)
957                 hyperSearch.setSelected(false);
958             else if(source == searchSelection
959                 || source == searchAllBuffers
960                 || source == searchDirectory)
961                 hyperSearch.setSelected(true);
962
963             save(true);
964             updateEnabled();
965         }
966     } //}}}
967

968     //{{{ MultiFileActionHandler class
969
class MultiFileActionHandler implements ActionListener
970     {
971         public void actionPerformed(ActionEvent evt)
972         {
973             if(evt.getSource() == choose)
974             {
975                 String JavaDoc[] dirs = GUIUtilities.showVFSFileDialog(
976                     view,directory.getText(),
977                     VFSBrowser.CHOOSE_DIRECTORY_DIALOG,
978                     false);
979                 if(dirs != null)
980                     directory.setText(dirs[0]);
981             }
982             else if(evt.getSource() == synchronize)
983             {
984                 synchronizeMultiFileSettings();
985             }
986             else // source is directory or filter field
987
{
988                 // just as if Enter was pressed in another
989
// text field
990
ok();
991             }
992         }
993
994
995         //{{{ synchronizeMultiFileSettings() method
996
private void synchronizeMultiFileSettings()
997         {
998             directory.setText(view.getBuffer().getDirectory());
999
1000            SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
1001
1002            if(fileset instanceof AllBufferSet)
1003            {
1004                filter.setText(((AllBufferSet)fileset)
1005                    .getFileFilter());
1006            }
1007            else
1008            {
1009                filter.setText('*' + MiscUtilities
1010                    .getFileExtension(view.getBuffer()
1011                    .getName()));
1012            }
1013        } //}}}
1014
} //}}}
1015

1016    //{{{ ButtonActionHandler class
1017
class ButtonActionHandler implements ActionListener
1018    {
1019        public void actionPerformed(ActionEvent evt)
1020        {
1021            Object JavaDoc source = evt.getSource();
1022
1023            if(source == closeBtn)
1024                cancel();
1025            else if(source == findBtn || source == find
1026                || source == replace)
1027            {
1028                ok();
1029            }
1030            else if(source == replaceAndFindBtn)
1031            {
1032                save(false);
1033                if(SearchAndReplace.replace(view))
1034                    ok();
1035                else
1036                    getToolkit().beep();
1037            }
1038            else if(source == replaceAllBtn)
1039            {
1040                if(searchSelection.isSelected() &&
1041                    view.getTextArea().getSelectionCount()
1042                    == 0)
1043                {
1044                    GUIUtilities.error(view,"search-no-selection",null);
1045                    return;
1046                }
1047
1048                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
1049
1050                if(!save(false))
1051                {
1052                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1053                    getToolkit().beep();
1054                    return;
1055                }
1056
1057                if(searchSelection.isSelected())
1058                {
1059                    if(SearchAndReplace.replace(view))
1060                        closeOrKeepDialog();
1061                    else
1062                        getToolkit().beep();
1063                }
1064                else
1065                {
1066                    if(SearchAndReplace.replaceAll(view))
1067                        closeOrKeepDialog();
1068                    else
1069                        getToolkit().beep();
1070                }
1071
1072                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1073            }
1074        }
1075    } //}}}
1076

1077    //}}}
1078
}
1079
Popular Tags