PageRenderTime 169ms CodeModel.GetById 128ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/search/SearchDialog.java

#
Java | 909 lines | 651 code | 142 blank | 116 comment | 115 complexity | 6af863c2ae3ef77c2f1f53b8d3a92742 MD5 | raw file
  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, 1999, 2000, 2001 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
 23package org.gjt.sp.jedit.search;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.event.*;
 28import javax.swing.*;
 29import java.awt.*;
 30import java.awt.event.*;
 31import java.io.File;
 32import java.util.HashMap;
 33import org.gjt.sp.jedit.browser.VFSBrowser;
 34import org.gjt.sp.jedit.gui.*;
 35import org.gjt.sp.jedit.io.*;
 36import org.gjt.sp.jedit.msg.SearchSettingsChanged;
 37import org.gjt.sp.jedit.msg.ViewUpdate;
 38import org.gjt.sp.jedit.*;
 39import org.gjt.sp.util.Log;
 40//}}}
 41
 42/**
 43 * Search and replace dialog.
 44 * @author Slava Pestov
 45 * @version $Id: SearchDialog.java 4261 2002-06-18 02:44:52Z spestov $
 46 */
 47public class SearchDialog extends EnhancedDialog implements EBComponent
 48{
 49	//{{{ Constants
 50	/**
 51	 * Default file set.
 52	 * @since jEdit 3.2pre2
 53	 */
 54	public static final int CURRENT_BUFFER = 0;
 55	public static final int ALL_BUFFERS = 1;
 56	public static final int DIRECTORY = 2;
 57	//}}}
 58
 59	//{{{ showSearchDialog() method
 60	/**
 61	 * Displays a search and replace dialog box, reusing an existing one
 62	 * if necessary.
 63	 * @param view The view
 64	 * @param searchString The search string
 65	 * @param searchIn One of CURRENT_BUFFER, ALL_BUFFERS, or DIRECTORY
 66	 * @since jEdit 4.0pre6
 67	 */
 68	public static void showSearchDialog(View view, String searchString,
 69		int searchIn)
 70	{
 71		SearchDialog dialog = (SearchDialog)viewHash.get(view);
 72		if(dialog != null)
 73		{
 74			// ugly workaround
 75			if(OperatingSystem.isUnix() && !OperatingSystem.isMacOS())
 76			{
 77				dialog.setVisible(false);
 78				dialog.setVisible(true);
 79			}
 80
 81			dialog.setSearchString(searchString,searchIn);
 82			GUIUtilities.requestFocus(dialog,dialog.find);
 83			dialog.toFront();
 84			dialog.requestFocus();
 85		}
 86		else
 87		{
 88			dialog = new SearchDialog(view,searchString,searchIn);
 89			viewHash.put(view,dialog);
 90		}
 91	} //}}}
 92
 93	//{{{ SearchDialog constructor
 94	/**
 95	 * Creates a new search and replace dialog box.
 96	 * @param view The view
 97	 * @param searchString The search string
 98	 */
 99	public SearchDialog(View view, String searchString)
100	{
101		this(view,searchString,CURRENT_BUFFER);
102	} //}}}
103
104	//{{{ SearchDialog constructor
105	/**
106	 * Creates a new search and replace dialog box.
107	 * @param view The view
108	 * @param searchString The search string
109	 * @param searchIn One of CURRENT_BUFFER, ALL_BUFFERS, or DIRECTORY
110	 * @since jEdit 3.2pre2
111	 */
112	public SearchDialog(View view, String searchString, int searchIn)
113	{
114		super(view,jEdit.getProperty("search.title"),false);
115
116		this.view = view;
117
118		JPanel content = new JPanel(new BorderLayout());
119		content.setBorder(new EmptyBorder(0,12,12,12));
120		setContentPane(content);
121
122		JPanel centerPanel = new JPanel(new BorderLayout());
123		centerPanel.add(BorderLayout.NORTH,createFieldPanel());
124		centerPanel.add(BorderLayout.CENTER,createSearchSettingsPanel());
125		content.add(BorderLayout.CENTER,centerPanel);
126		content.add(BorderLayout.SOUTH,createMultiFilePanel());
127
128		content.add(BorderLayout.EAST,createButtonsPanel());
129
130		ignoreCase.setSelected(SearchAndReplace.getIgnoreCase());
131		regexp.setSelected(SearchAndReplace.getRegexp());
132		wrap.setSelected(SearchAndReplace.getAutoWrapAround());
133
134		if(SearchAndReplace.getReverseSearch())
135			searchBack.setSelected(true);
136		else
137			searchForward.setSelected(true);
138
139		if(SearchAndReplace.getBeanShellReplace())
140		{
141			replace.setModel("replace.script");
142			beanShellReplace.setSelected(true);
143		}
144		else
145		{
146			replace.setModel("replace");
147			stringReplace.setSelected(true);
148		}
149
150		SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
151
152		if(fileset instanceof DirectoryListSet)
153		{
154			filter.setText(((DirectoryListSet)fileset)
155				.getFileFilter());
156			directory.setText(((DirectoryListSet)fileset)
157				.getDirectory());
158			searchSubDirectories.setSelected(((DirectoryListSet)fileset)
159				.isRecursive());
160		}
161		else
162		{
163			String path = MiscUtilities.getParentOfPath(
164				view.getBuffer().getPath());
165
166			if(path.endsWith("/") || path.endsWith(File.separator))
167				path = path.substring(0,path.length() - 1);
168
169			directory.setText(path);
170
171			if(fileset instanceof AllBufferSet)
172			{
173				filter.setText(((AllBufferSet)fileset)
174					.getFileFilter());
175			}
176			else
177			{
178				filter.setText("*" + MiscUtilities
179					.getFileExtension(view.getBuffer()
180					.getName()));
181			}
182
183			searchSubDirectories.setSelected(true);
184		}
185
186		directory.addCurrentToHistory();
187
188		keepDialog.setSelected(jEdit.getBooleanProperty(
189			"search.keepDialog.toggle"));
190
191		setSearchString(searchString,searchIn);
192
193		pack();
194		jEdit.unsetProperty("search.width");
195		jEdit.unsetProperty("search.d-width");
196		jEdit.unsetProperty("search.height");
197		jEdit.unsetProperty("search.d-height");
198		GUIUtilities.loadGeometry(this,"search");
199		show();
200
201		EditBus.addToBus(this);
202
203		GUIUtilities.requestFocus(this,find);
204	} //}}}
205
206	//{{{ setSearchString() method
207	/**
208	 * Sets the search string.
209	 * @since jEdit 4.0pre5
210	 */
211	public void setSearchString(String searchString, int searchIn)
212	{
213		if(searchString == null)
214			find.setText(null);
215		else
216		{
217			if(searchString.indexOf('\n') == -1)
218			{
219				find.setText(searchString);
220				find.selectAll();
221			}
222			else if(searchIn == CURRENT_BUFFER)
223			{
224				searchSelection.setSelected(true);
225				hyperSearch.setSelected(true);
226			}
227		}
228
229		if(searchIn == CURRENT_BUFFER)
230		{
231			if(!searchSelection.isSelected())
232			{
233				// might be already selected, see above.
234				searchCurrentBuffer.setSelected(true);
235
236				/* this property is only loaded and saved if
237				 * the 'current buffer' file set is selected.
238				 * otherwise, it defaults to on. */
239				hyperSearch.setSelected(jEdit.getBooleanProperty(
240					"search.hypersearch.toggle"));
241			}
242		}
243		else if(searchIn == ALL_BUFFERS)
244		{
245			searchAllBuffers.setSelected(true);
246			hyperSearch.setSelected(true);
247		}
248		else if(searchIn == DIRECTORY)
249		{
250			hyperSearch.setSelected(true);
251			searchDirectory.setSelected(true);
252		}
253
254		updateEnabled();
255	} //}}}
256
257	//{{{ ok() method
258	public void ok()
259	{
260		try
261		{
262			setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
263
264			if(!save(false))
265				return;
266
267			if(hyperSearch.isSelected() || searchSelection.isSelected())
268			{
269				if(SearchAndReplace.hyperSearch(view,
270					searchSelection.isSelected()))
271					closeOrKeepDialog();
272			}
273			else
274			{
275				if(SearchAndReplace.find(view))
276					closeOrKeepDialog();
277				else
278				{
279					toFront();
280					requestFocus();
281					find.requestFocus();
282				}
283			}
284		}
285		finally
286		{
287			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
288		}
289	} //}}}
290
291	//{{{ cancel() method
292	public void cancel()
293	{
294		save(true);
295		GUIUtilities.saveGeometry(this,"search");
296		dispose();
297	} //}}}
298
299	//{{{ handleMessage() method
300	public void handleMessage(EBMessage msg)
301	{
302		if(msg instanceof SearchSettingsChanged)
303		{
304			ignoreCase.setSelected(SearchAndReplace.getIgnoreCase());
305			regexp.setSelected(SearchAndReplace.getRegexp());
306		}
307		else if(msg instanceof ViewUpdate)
308		{
309			ViewUpdate vmsg = (ViewUpdate)msg;
310			if(vmsg.getView() == view
311				&& vmsg.getWhat() == ViewUpdate.CLOSED)
312			{
313				viewHash.remove(view);
314			}
315		}
316	} //}}}
317
318	//{{{ dispose() method
319	public void dispose()
320	{
321		EditBus.removeFromBus(this);
322		viewHash.remove(view);
323		super.dispose();
324	} //}}}
325
326	//{{{ Private members
327
328	private static HashMap viewHash = new HashMap();
329
330	//{{{ Instance variables
331	private View view;
332
333	// fields
334	private HistoryTextField find, replace;
335
336	private JRadioButton stringReplace, beanShellReplace;
337
338	// search settings
339	private JCheckBox keepDialog, ignoreCase, regexp, hyperSearch,
340		wrap;
341	private JRadioButton searchBack, searchForward;
342	private JRadioButton searchSelection, searchCurrentBuffer, searchAllBuffers,
343		searchDirectory;
344
345	// multifile settings
346	private HistoryTextField filter, directory;
347	private JCheckBox searchSubDirectories;
348	private JButton choose;
349
350	// buttons
351	private JButton findBtn, /* replaceBtn, */ replaceAndFindBtn, replaceAllBtn,
352		closeBtn;
353	//}}}
354
355	//{{{ createFieldPanel() method
356	private JPanel createFieldPanel()
357	{
358		ButtonActionHandler actionHandler = new ButtonActionHandler();
359
360		JPanel fieldPanel = new JPanel(new VariableGridLayout(
361			VariableGridLayout.FIXED_NUM_COLUMNS,1));
362		fieldPanel.setBorder(new EmptyBorder(0,0,12,12));
363
364		JLabel label = new JLabel(jEdit.getProperty("search.find"));
365		label.setDisplayedMnemonic(jEdit.getProperty("search.find.mnemonic")
366			.charAt(0));
367		find = new HistoryTextField("find");
368
369		// don't want it to be too wide due to long strings
370		Dimension size = find.getPreferredSize();
371		size.width = find.getFontMetrics(find.getFont())
372			.charWidth('a') * 25;
373		find.setPreferredSize(size);
374
375		find.addActionListener(actionHandler);
376		label.setLabelFor(find);
377		label.setBorder(new EmptyBorder(12,0,2,0));
378		fieldPanel.add(label);
379		fieldPanel.add(find);
380
381		label = new JLabel(jEdit.getProperty("search.replace"));
382		label.setDisplayedMnemonic(jEdit.getProperty("search.replace.mnemonic")
383			.charAt(0));
384		label.setBorder(new EmptyBorder(12,0,0,0));
385		fieldPanel.add(label);
386
387		ButtonGroup grp = new ButtonGroup();
388		ReplaceActionHandler replaceActionHandler = new ReplaceActionHandler();
389
390		// we use a custom JRadioButton subclass that returns
391		// false for isFocusTraversable() so that the user can
392		// tab from the search field to the replace field with
393		// one keystroke
394
395		Box replaceModeBox = new Box(BoxLayout.X_AXIS);
396		stringReplace = new MyJRadioButton(jEdit.getProperty(
397			"search.string-replace-btn"));
398		stringReplace.addActionListener(replaceActionHandler);
399		grp.add(stringReplace);
400		replaceModeBox.add(stringReplace);
401
402		replaceModeBox.add(Box.createHorizontalStrut(12));
403
404		beanShellReplace = new MyJRadioButton(jEdit.getProperty(
405			"search.beanshell-replace-btn"));
406		beanShellReplace.addActionListener(replaceActionHandler);
407		grp.add(beanShellReplace);
408		replaceModeBox.add(beanShellReplace);
409
410		fieldPanel.add(replaceModeBox);
411
412		fieldPanel.add(Box.createVerticalStrut(3));
413
414		replace = new HistoryTextField("replace");
415		replace.addActionListener(actionHandler);
416		label.setLabelFor(replace);
417		fieldPanel.add(replace);
418
419		return fieldPanel;
420	} //}}}
421
422	//{{{ createSearchSettingsPanel() method
423	private JPanel createSearchSettingsPanel()
424	{
425		JPanel searchSettings = new JPanel(new VariableGridLayout(
426			VariableGridLayout.FIXED_NUM_COLUMNS,3));
427		searchSettings.setBorder(new EmptyBorder(0,0,12,12));
428
429		SettingsActionHandler actionHandler = new SettingsActionHandler();
430		ButtonGroup fileset = new ButtonGroup();
431		ButtonGroup direction = new ButtonGroup();
432
433		searchSettings.add(new JLabel(jEdit.getProperty("search.fileset")));
434
435		searchSettings.add(new JLabel(jEdit.getProperty("search.settings")));
436
437		searchSettings.add(new JLabel(jEdit.getProperty("search.direction")));
438
439		searchSelection = new JRadioButton(jEdit.getProperty("search.selection"));
440		searchSelection.setMnemonic(jEdit.getProperty("search.selection.mnemonic")
441			.charAt(0));
442		fileset.add(searchSelection);
443		searchSettings.add(searchSelection);
444		searchSelection.addActionListener(actionHandler);
445
446		keepDialog = new JCheckBox(jEdit.getProperty("search.keep"));
447		keepDialog.setMnemonic(jEdit.getProperty("search.keep.mnemonic")
448			.charAt(0));
449		searchSettings.add(keepDialog);
450
451		searchBack = new JRadioButton(jEdit.getProperty("search.back"));
452		searchBack.setMnemonic(jEdit.getProperty("search.back.mnemonic")
453			.charAt(0));
454		direction.add(searchBack);
455		searchSettings.add(searchBack);
456		searchBack.addActionListener(actionHandler);
457
458		searchCurrentBuffer = new JRadioButton(jEdit.getProperty("search.current"));
459		searchCurrentBuffer.setMnemonic(jEdit.getProperty("search.current.mnemonic")
460			.charAt(0));
461		fileset.add(searchCurrentBuffer);
462		searchSettings.add(searchCurrentBuffer);
463		searchCurrentBuffer.addActionListener(actionHandler);
464
465		ignoreCase = new JCheckBox(jEdit.getProperty("search.case"));
466		ignoreCase.setMnemonic(jEdit.getProperty("search.case.mnemonic")
467			.charAt(0));
468		searchSettings.add(ignoreCase);
469		ignoreCase.addActionListener(actionHandler);
470
471		searchForward = new JRadioButton(jEdit.getProperty("search.forward"));
472		searchForward.setMnemonic(jEdit.getProperty("search.forward.mnemonic")
473			.charAt(0));
474		direction.add(searchForward);
475		searchSettings.add(searchForward);
476		searchForward.addActionListener(actionHandler);
477
478		searchAllBuffers = new JRadioButton(jEdit.getProperty("search.all"));
479		searchAllBuffers.setMnemonic(jEdit.getProperty("search.all.mnemonic")
480			.charAt(0));
481		fileset.add(searchAllBuffers);
482		searchSettings.add(searchAllBuffers);
483		searchAllBuffers.addActionListener(actionHandler);
484
485		regexp = new JCheckBox(jEdit.getProperty("search.regexp"));
486		regexp.setMnemonic(jEdit.getProperty("search.regexp.mnemonic")
487			.charAt(0));
488		searchSettings.add(regexp);
489		regexp.addActionListener(actionHandler);
490
491		wrap = new JCheckBox(jEdit.getProperty("search.wrap"));
492		wrap.setMnemonic(jEdit.getProperty("search.wrap.mnemonic")
493			.charAt(0));
494		searchSettings.add(wrap);
495		wrap.addActionListener(actionHandler);
496
497		searchDirectory = new JRadioButton(jEdit.getProperty("search.directory"));
498		searchDirectory.setMnemonic(jEdit.getProperty("search.directory.mnemonic")
499			.charAt(0));
500		fileset.add(searchDirectory);
501		searchSettings.add(searchDirectory);
502		searchDirectory.addActionListener(actionHandler);
503
504		hyperSearch = new JCheckBox(jEdit.getProperty("search.hypersearch"));
505		hyperSearch.setMnemonic(jEdit.getProperty("search.hypersearch.mnemonic")
506			.charAt(0));
507		searchSettings.add(hyperSearch);
508		hyperSearch.addActionListener(actionHandler);
509
510		return searchSettings;
511	} //}}}
512
513	//{{{ createMultiFilePanel() method
514	private JPanel createMultiFilePanel()
515	{
516		JPanel multifile = new JPanel();
517
518		GridBagLayout layout = new GridBagLayout();
519		multifile.setLayout(layout);
520
521		GridBagConstraints cons = new GridBagConstraints();
522		cons.gridy = cons.gridwidth = cons.gridheight = 1;
523		cons.anchor = GridBagConstraints.WEST;
524		cons.fill = GridBagConstraints.HORIZONTAL;
525
526		MultiFileActionHandler actionListener = new MultiFileActionHandler();
527		filter = new HistoryTextField("search.filter");
528		filter.addActionListener(actionListener);
529
530		cons.insets = new Insets(0,0,3,0);
531
532		JLabel label = new JLabel(jEdit.getProperty("search.filterField"),
533			SwingConstants.RIGHT);
534		label.setBorder(new EmptyBorder(0,0,0,12));
535		label.setDisplayedMnemonic(jEdit.getProperty("search.filterField.mnemonic")
536			.charAt(0));
537		label.setLabelFor(filter);
538		cons.weightx = 0.0f;
539		layout.setConstraints(label,cons);
540		multifile.add(label);
541
542		cons.insets = new Insets(0,0,3,6);
543		cons.weightx = 1.0f;
544		layout.setConstraints(filter,cons);
545		multifile.add(filter);
546
547		cons.gridy++;
548
549		directory = new HistoryTextField("search.directory");
550		directory.addActionListener(actionListener);
551
552		label = new JLabel(jEdit.getProperty("search.directoryField"),
553			SwingConstants.RIGHT);
554		label.setBorder(new EmptyBorder(0,0,0,12));
555
556		label.setDisplayedMnemonic(jEdit.getProperty("search.directoryField.mnemonic")
557			.charAt(0));
558		label.setLabelFor(directory);
559		cons.insets = new Insets(0,0,3,0);
560		cons.weightx = 0.0f;
561		layout.setConstraints(label,cons);
562		multifile.add(label);
563
564		cons.insets = new Insets(0,0,3,6);
565		cons.weightx = 1.0f;
566		cons.gridwidth = 2;
567		layout.setConstraints(directory,cons);
568		multifile.add(directory);
569
570		choose = new JButton(jEdit.getProperty("search.choose"));
571		choose.setMnemonic(jEdit.getProperty("search.choose.mnemonic")
572			.charAt(0));
573		cons.insets = new Insets(0,0,3,0);
574		cons.weightx = 0.0f;
575		cons.gridwidth = 1;
576		layout.setConstraints(choose,cons);
577		multifile.add(choose);
578		choose.addActionListener(actionListener);
579
580		cons.insets = new Insets(0,0,0,0);
581		cons.gridy++;
582		cons.gridwidth = 4;
583
584		searchSubDirectories = new JCheckBox(jEdit.getProperty(
585			"search.subdirs"));
586		searchSubDirectories.setMnemonic(jEdit.getProperty("search.subdirs.mnemonic")
587			.charAt(0));
588		layout.setConstraints(searchSubDirectories,cons);
589		multifile.add(searchSubDirectories);
590
591		return multifile;
592	} //}}}
593
594	//{{{ createButtonsPanel() method
595	private Box createButtonsPanel()
596	{
597		Box box = new Box(BoxLayout.Y_AXIS);
598
599		ButtonActionHandler actionHandler = new ButtonActionHandler();
600
601		box.add(Box.createVerticalStrut(12));
602
603		JPanel grid = new JPanel(new GridLayout(5,1,0,12));
604
605		findBtn = new JButton(jEdit.getProperty("search.findBtn"));
606		getRootPane().setDefaultButton(findBtn);
607		grid.add(findBtn);
608		findBtn.addActionListener(actionHandler);
609
610		/* replaceBtn = new JButton(jEdit.getProperty("search.replaceBtn"));
611		replaceBtn.setMnemonic(jEdit.getProperty("search.replaceBtn.mnemonic")
612			.charAt(0));
613		grid.add(replaceBtn);
614		replaceBtn.addActionListener(actionHandler); */
615
616		replaceAndFindBtn = new JButton(jEdit.getProperty("search.replaceAndFindBtn"));
617		replaceAndFindBtn.setMnemonic(jEdit.getProperty("search.replaceAndFindBtn.mnemonic")
618			.charAt(0));
619		grid.add(replaceAndFindBtn);
620		replaceAndFindBtn.addActionListener(actionHandler);
621
622		replaceAllBtn = new JButton(jEdit.getProperty("search.replaceAllBtn"));
623		replaceAllBtn.setMnemonic(jEdit.getProperty("search.replaceAllBtn.mnemonic")
624			.charAt(0));
625		grid.add(replaceAllBtn);
626		replaceAllBtn.addActionListener(actionHandler);
627
628		closeBtn = new JButton(jEdit.getProperty("common.close"));
629		grid.add(closeBtn);
630		closeBtn.addActionListener(actionHandler);
631
632		grid.setMaximumSize(grid.getPreferredSize());
633
634		box.add(grid);
635		box.add(Box.createGlue());
636
637		return box;
638	} //}}}
639
640	//{{{ updateEnabled() method
641	private void updateEnabled()
642	{
643		wrap.setEnabled(!hyperSearch.isSelected()
644			&& !searchSelection.isSelected());
645
646		boolean reverseEnabled = !hyperSearch.isSelected()
647			&& searchCurrentBuffer.isSelected();
648		searchBack.setEnabled(reverseEnabled && !regexp.isSelected());
649		searchForward.setEnabled(reverseEnabled);
650		if(!reverseEnabled)
651			searchForward.setSelected(true);
652
653		filter.setEnabled(searchAllBuffers.isSelected()
654			|| searchDirectory.isSelected());
655
656		boolean directoryEnabled = searchDirectory.isSelected();
657
658		directory.setEnabled(directoryEnabled);
659		choose.setEnabled(directoryEnabled);
660		searchSubDirectories.setEnabled(directoryEnabled);
661
662		findBtn.setEnabled(!searchSelection.isSelected()
663			|| hyperSearch.isSelected());
664		replaceAndFindBtn.setEnabled(!hyperSearch.isSelected()
665			&& !searchSelection.isSelected());
666	} //}}}
667
668	//{{{ save() method
669	/**
670	 * @param cancel If true, we don't bother the user with warning messages
671	 */
672	private boolean save(boolean cancel)
673	{
674		String filter = this.filter.getText();
675		this.filter.addCurrentToHistory();
676		if(filter.length() == 0)
677			filter = "*";
678
679		SearchFileSet fileset = SearchAndReplace.getSearchFileSet();
680
681		if(searchSelection.isSelected())
682			fileset = new CurrentBufferSet();
683		else if(searchCurrentBuffer.isSelected())
684		{
685			fileset = new CurrentBufferSet();
686
687			jEdit.setBooleanProperty("search.hypersearch.toggle",
688				hyperSearch.isSelected());
689		}
690		else if(searchAllBuffers.isSelected())
691			fileset = new AllBufferSet(filter);
692		else if(searchDirectory.isSelected())
693		{
694			String directory = this.directory.getText();
695			this.directory.addCurrentToHistory();
696
697			if((VFSManager.getVFSForPath(directory).getCapabilities()
698				& VFS.LOW_LATENCY_CAP) == 0)
699			{
700				if(cancel)
701					return false;
702
703				int retVal = GUIUtilities.confirm(
704					SearchDialog.this,"remote-dir-search",
705					null,JOptionPane.YES_NO_OPTION,
706					JOptionPane.WARNING_MESSAGE);
707				if(retVal != JOptionPane.YES_OPTION)
708					return false;
709			}
710
711			boolean recurse = searchSubDirectories.isSelected();
712
713			if(fileset instanceof DirectoryListSet)
714			{
715				DirectoryListSet dset = (DirectoryListSet)fileset;
716				if(!dset.getDirectory().equals(directory)
717					|| !dset.getFileFilter().equals(filter)
718					|| !dset.isRecursive() == recurse)
719					fileset = new DirectoryListSet(directory,filter,recurse);
720			}
721			else
722				fileset = new DirectoryListSet(directory,filter,recurse);
723		}
724		else
725		{
726			// can't happen
727			fileset = null;
728		}
729
730		jEdit.setBooleanProperty("search.keepDialog.toggle",
731			keepDialog.isSelected());
732
733		boolean ok = true;
734
735		SearchAndReplace.setSearchFileSet(fileset);
736
737		if(find.getText().length() != 0)
738		{
739			find.addCurrentToHistory();
740			SearchAndReplace.setSearchString(find.getText());
741			replace.addCurrentToHistory();
742
743			SearchAndReplace.setReplaceString(replace.getText());
744		}
745		else
746			ok = false;
747
748		return ok;
749	} //}}}
750
751	//{{{ closeOrKeepDialog() method
752	private void closeOrKeepDialog()
753	{
754		if(keepDialog.isSelected())
755		{
756			// Windows bug workaround in case a YES/NO confirm
757			// was shown
758
759			// ... but if HyperSearch results window is floating,
760			// the workaround causes problems!
761			if(!hyperSearch.isSelected())
762			{
763				toFront();
764				requestFocus();
765				find.requestFocus();
766			}
767			return;
768		}
769		else
770		{
771			GUIUtilities.saveGeometry(this,"search");
772			dispose();
773		}
774	} //}}}
775
776	//}}}
777
778	//{{{ Inner classes
779
780	//{{{ MyJRadioButton class
781
782	// used for the stringReplace and beanShell replace radio buttons,
783	// so that the user can press tab to go from the find field to the
784	// replace field in one go
785	class MyJRadioButton extends JRadioButton
786	{
787		MyJRadioButton(String label)
788		{
789			super(label);
790		}
791
792		public boolean isFocusTraversable()
793		{
794			return false;
795		}
796	} //}}}
797
798	//{{{ ReplaceActionHandler class
799	class ReplaceActionHandler implements ActionListener
800	{
801		public void actionPerformed(ActionEvent evt)
802		{
803			replace.setModel(beanShellReplace.isSelected()
804				? "replace.script"
805				: "replace");
806			SearchAndReplace.setBeanShellReplace(
807				beanShellReplace.isSelected());
808		}
809	} //}}}
810
811	//{{{ SettingsActionHandler class
812	class SettingsActionHandler implements ActionListener
813	{
814		public void actionPerformed(ActionEvent evt)
815		{
816			Object source = evt.getSource();
817
818			if(source == ignoreCase)
819				SearchAndReplace.setIgnoreCase(ignoreCase.isSelected());
820			else if(source == regexp)
821				SearchAndReplace.setRegexp(regexp.isSelected());
822			else if(source == searchBack || source == searchForward)
823				SearchAndReplace.setReverseSearch(searchBack.isSelected());
824			else if(source == wrap)
825				SearchAndReplace.setAutoWrapAround(wrap.isSelected());
826			else if(source == searchCurrentBuffer)
827				hyperSearch.setSelected(false);
828			else if(source == searchSelection
829				|| source == searchAllBuffers
830				|| source == searchDirectory)
831				hyperSearch.setSelected(true);
832
833			updateEnabled();
834		}
835	} //}}}
836
837	//{{{ MultiFileActionHandler class
838	class MultiFileActionHandler implements ActionListener
839	{
840		public void actionPerformed(ActionEvent evt)
841		{
842			if(evt.getSource() == choose)
843			{
844				String[] dirs = GUIUtilities.showVFSFileDialog(
845					view,directory.getText(),
846					VFSBrowser.CHOOSE_DIRECTORY_DIALOG,
847					false);
848				if(dirs != null)
849					directory.setText(dirs[0]);
850			}
851			else // source is directory or filter field
852			{
853				// just as if Enter was pressed in another
854				// text field
855				ok();
856			}
857		}
858	} //}}}
859
860	//{{{ ButtonActionHandler class
861	class ButtonActionHandler implements ActionListener
862	{
863		public void actionPerformed(ActionEvent evt)
864		{
865			Object source = evt.getSource();
866
867			if(source == closeBtn)
868				cancel();
869			else if(source == findBtn || source == find
870				|| source == replace)
871			{
872				ok();
873			}
874			else if(source == replaceAndFindBtn)
875			{
876				save(false);
877				if(SearchAndReplace.replace(view))
878					ok();
879				else
880					getToolkit().beep();
881			}
882			else if(source == replaceAllBtn)
883			{
884				setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
885
886				save(false);
887
888				if(searchSelection.isSelected())
889				{
890					if(SearchAndReplace.replace(view))
891						closeOrKeepDialog();
892					else
893						getToolkit().beep();
894				}
895				else
896				{
897					if(SearchAndReplace.replaceAll(view))
898						closeOrKeepDialog();
899					else
900						getToolkit().beep();
901				}
902
903				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
904			}
905		}
906	} //}}}
907
908	//}}}
909}