📄 searchdialog.java
字号:
/* * SearchDialog.java - Search and replace dialog * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1998, 1999, 2000, 2001 Slava Pestov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.gjt.sp.jedit.search;//{{{ Importsimport javax.swing.border.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.File;import java.util.HashMap;import org.gjt.sp.jedit.browser.VFSBrowser;import org.gjt.sp.jedit.gui.*;import org.gjt.sp.jedit.io.*;import org.gjt.sp.jedit.msg.SearchSettingsChanged;import org.gjt.sp.jedit.msg.ViewUpdate;import org.gjt.sp.jedit.*;//}}}/** * Search and replace dialog. * @author Slava Pestov * @version $Id: SearchDialog.java,v 1.34 2003/02/18 22:03:20 spestov Exp $ */public class SearchDialog extends EnhancedDialog implements EBComponent{ //{{{ Constants /** * Default file set. * @since jEdit 3.2pre2 */ public static final int CURRENT_BUFFER = 0; public static final int ALL_BUFFERS = 1; public static final int DIRECTORY = 2; //}}} //{{{ getSearchDialog() method public static SearchDialog getSearchDialog(View view) { return (SearchDialog)viewHash.get(view); } //}}} //{{{ showSearchDialog() method /** * Displays a search and replace dialog box, reusing an existing one * if necessary. * @param view The view * @param searchString The search string * @param searchIn One of CURRENT_BUFFER, ALL_BUFFERS, or DIRECTORY * @since jEdit 4.0pre6 */ public static void showSearchDialog(View view, String searchString, int searchIn) { SearchDialog dialog = (SearchDialog)viewHash.get(view); if(dialog != null) { // ugly workaround if(OperatingSystem.isUnix() && !OperatingSystem.isMacOS()) { dialog.setVisible(false); dialog.setVisible(true); } dialog.setSearchString(searchString,searchIn); GUIUtilities.requestFocus(dialog,dialog.find); dialog.toFront(); dialog.requestFocus(); } else { dialog = new SearchDialog(view,searchString,searchIn); viewHash.put(view,dialog); } } //}}} //{{{ SearchDialog constructor /** * Creates a new search and replace dialog box. * @param view The view * @param searchString The search string */ public SearchDialog(View view, String searchString) { this(view,searchString,CURRENT_BUFFER); } //}}} //{{{ SearchDialog constructor /** * Creates a new search and replace dialog box. * @param view The view * @param searchString The search string * @param searchIn One of CURRENT_BUFFER, ALL_BUFFERS, or DIRECTORY * @since jEdit 3.2pre2 */ public SearchDialog(View view, String searchString, int searchIn) { super(view,jEdit.getProperty("search.title"),false); this.view = view; JPanel content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(0,12,12,12)); setContentPane(content); JPanel centerPanel = new JPanel(new BorderLayout()); centerPanel.add(BorderLayout.NORTH,createFieldPanel()); centerPanel.add(BorderLayout.CENTER,createSearchSettingsPanel()); content.add(BorderLayout.CENTER,centerPanel); content.add(BorderLayout.SOUTH,createMultiFilePanel()); content.add(BorderLayout.EAST,createButtonsPanel()); ignoreCase.setSelected(SearchAndReplace.getIgnoreCase()); regexp.setSelected(SearchAndReplace.getRegexp()); wrap.setSelected(SearchAndReplace.getAutoWrapAround()); if(SearchAndReplace.getReverseSearch()) searchBack.setSelected(true); else searchForward.setSelected(true); if(SearchAndReplace.getBeanShellReplace()) { replace.setModel("replace.script"); beanShellReplace.setSelected(true); } else { replace.setModel("replace"); stringReplace.setSelected(true); } SearchFileSet fileset = SearchAndReplace.getSearchFileSet(); if(fileset instanceof DirectoryListSet) { filter.setText(((DirectoryListSet)fileset) .getFileFilter()); directory.setText(((DirectoryListSet)fileset) .getDirectory()); searchSubDirectories.setSelected(((DirectoryListSet)fileset) .isRecursive()); } else { String path = view.getBuffer().getDirectory(); if(path.endsWith("/") || path.endsWith(File.separator)) path = path.substring(0,path.length() - 1); directory.setText(path); if(fileset instanceof AllBufferSet) { filter.setText(((AllBufferSet)fileset) .getFileFilter()); } else { filter.setText("*" + MiscUtilities .getFileExtension(view.getBuffer() .getName())); } searchSubDirectories.setSelected(true); } directory.addCurrentToHistory(); keepDialog.setSelected(jEdit.getBooleanProperty( "search.keepDialog.toggle")); setSearchString(searchString,searchIn); pack(); jEdit.unsetProperty("search.width"); jEdit.unsetProperty("search.d-width"); jEdit.unsetProperty("search.height"); jEdit.unsetProperty("search.d-height"); GUIUtilities.loadGeometry(this,"search"); show(); EditBus.addToBus(this); GUIUtilities.requestFocus(this,find); } //}}} //{{{ setSearchString() method /** * Sets the search string. * @since jEdit 4.0pre5 */ public void setSearchString(String searchString, int searchIn) { if(searchString == null) find.setText(null); else { if(searchString.indexOf('\n') == -1) { find.setText(searchString); find.selectAll(); } else if(searchIn == CURRENT_BUFFER) { searchSelection.setSelected(true); hyperSearch.setSelected(true); } } if(searchIn == CURRENT_BUFFER) { if(!searchSelection.isSelected()) { // might be already selected, see above. searchCurrentBuffer.setSelected(true); /* this property is only loaded and saved if * the 'current buffer' file set is selected. * otherwise, it defaults to on. */ hyperSearch.setSelected(jEdit.getBooleanProperty( "search.hypersearch.toggle")); } } else if(searchIn == ALL_BUFFERS) { searchAllBuffers.setSelected(true); hyperSearch.setSelected(true); } else if(searchIn == DIRECTORY) { hyperSearch.setSelected(true); searchDirectory.setSelected(true); } updateEnabled(); } //}}} //{{{ ok() method public void ok() { try { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); if(!save(false)) return; if(hyperSearch.isSelected() || searchSelection.isSelected()) { if(SearchAndReplace.hyperSearch(view, searchSelection.isSelected())) closeOrKeepDialog(); } else { if(SearchAndReplace.find(view)) closeOrKeepDialog(); else { toFront(); requestFocus(); find.requestFocus(); } } } finally { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } } //}}} //{{{ cancel() method public void cancel() { save(true); GUIUtilities.saveGeometry(this,"search"); dispose(); } //}}} //{{{ handleMessage() method public void handleMessage(EBMessage msg) { if(msg instanceof SearchSettingsChanged) { ignoreCase.setSelected(SearchAndReplace.getIgnoreCase()); regexp.setSelected(SearchAndReplace.getRegexp()); } else if(msg instanceof ViewUpdate) { ViewUpdate vmsg = (ViewUpdate)msg; if(vmsg.getView() == view && vmsg.getWhat() == ViewUpdate.CLOSED) { viewHash.remove(view); } } } //}}} //{{{ dispose() method public void dispose() { EditBus.removeFromBus(this); viewHash.remove(view); super.dispose(); } //}}} //{{{ Private members private static HashMap viewHash = new HashMap(); //{{{ Instance variables private View view; // fields private HistoryTextField find, replace; private JRadioButton stringReplace, beanShellReplace; // search settings private JCheckBox keepDialog, ignoreCase, regexp, hyperSearch, wrap; private JRadioButton searchBack, searchForward; private JRadioButton searchSelection, searchCurrentBuffer, searchAllBuffers, searchDirectory; // multifile settings private HistoryTextField filter, directory; private JCheckBox searchSubDirectories; private JButton choose; // buttons private JButton findBtn, /* replaceBtn, */ replaceAndFindBtn, replaceAllBtn, closeBtn; //}}} //{{{ createFieldPanel() method private JPanel createFieldPanel() { ButtonActionHandler actionHandler = new ButtonActionHandler(); JPanel fieldPanel = new JPanel(new VariableGridLayout( VariableGridLayout.FIXED_NUM_COLUMNS,1)); fieldPanel.setBorder(new EmptyBorder(0,0,12,12)); JLabel label = new JLabel(jEdit.getProperty("search.find")); label.setDisplayedMnemonic(jEdit.getProperty("search.find.mnemonic") .charAt(0)); find = new HistoryTextField("find"); find.setColumns(25); find.addActionListener(actionHandler); label.setLabelFor(find); label.setBorder(new EmptyBorder(12,0,2,0)); fieldPanel.add(label); fieldPanel.add(find); label = new JLabel(jEdit.getProperty("search.replace")); label.setDisplayedMnemonic(jEdit.getProperty("search.replace.mnemonic") .charAt(0)); label.setBorder(new EmptyBorder(12,0,0,0)); fieldPanel.add(label); ButtonGroup grp = new ButtonGroup(); ReplaceActionHandler replaceActionHandler = new ReplaceActionHandler(); // we use a custom JRadioButton subclass that returns // false for isFocusTraversable() so that the user can // tab from the search field to the replace field with // one keystroke Box replaceModeBox = new Box(BoxLayout.X_AXIS); stringReplace = new MyJRadioButton(jEdit.getProperty( "search.string-replace-btn")); stringReplace.addActionListener(replaceActionHandler); grp.add(stringReplace); replaceModeBox.add(stringReplace); replaceModeBox.add(Box.createHorizontalStrut(12)); beanShellReplace = new MyJRadioButton(jEdit.getProperty( "search.beanshell-replace-btn")); beanShellReplace.addActionListener(replaceActionHandler); grp.add(beanShellReplace); replaceModeBox.add(beanShellReplace); fieldPanel.add(replaceModeBox); fieldPanel.add(Box.createVerticalStrut(3)); replace = new HistoryTextField("replace"); replace.addActionListener(actionHandler); label.setLabelFor(replace); fieldPanel.add(replace); return fieldPanel; } //}}} //{{{ createSearchSettingsPanel() method private JPanel createSearchSettingsPanel() { JPanel searchSettings = new JPanel(new VariableGridLayout( VariableGridLayout.FIXED_NUM_COLUMNS,3)); searchSettings.setBorder(new EmptyBorder(0,0,12,12)); SettingsActionHandler actionHandler = new SettingsActionHandler(); ButtonGroup fileset = new ButtonGroup(); ButtonGroup direction = new ButtonGroup(); searchSettings.add(new JLabel(jEdit.getProperty("search.fileset"))); searchSettings.add(new JLabel(jEdit.getProperty("search.settings"))); searchSettings.add(new JLabel(jEdit.getProperty("search.direction"))); searchSelection = new JRadioButton(jEdit.getProperty("search.selection")); searchSelection.setMnemonic(jEdit.getProperty("search.selection.mnemonic") .charAt(0)); fileset.add(searchSelection); searchSettings.add(searchSelection); searchSelection.addActionListener(actionHandler); keepDialog = new JCheckBox(jEdit.getProperty("search.keep")); keepDialog.setMnemonic(jEdit.getProperty("search.keep.mnemonic") .charAt(0)); searchSettings.add(keepDialog); searchBack = new JRadioButton(jEdit.getProperty("search.back")); searchBack.setMnemonic(jEdit.getProperty("search.back.mnemonic") .charAt(0)); direction.add(searchBack); searchSettings.add(searchBack); searchBack.addActionListener(actionHandler);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -