⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findreplace.java

📁 java写的多功能文件编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 03/01/2002 - 19:53:59 * * FindReplace.java - The Jext's find dialog * Copyright (C) 1998-2001 Romain Guy * romain.guy@jext.org * www.jext.org * * 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.jext.search;import gnu.regexp.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import org.jext.*;import org.jext.gui.*;/** * The <code>FindReplace</code> class is a component which displays * a dialog for either finding either replacing text. It provides * two combo lists, which holds latest patterns, and many buttons * or check boxes for options. * @author Romain Guy */public class FindReplace extends JDialog implements ActionListener{  // Constant declarations  /** Defines a search only dialog */  public static final int SEARCH = 1;  /** Defines a search and replace dialog */  public static final int REPLACE = 2;  // Private declarations  private int type;  private JextFrame parent;  private JComboBox fieldSearch;  private JComboBox fieldReplace;  private JTextField fieldSearchEditor, fieldReplaceEditor, script;  private JextHighlightButton btnFind, btnReplace, btnReplaceAll, btnCancel;  private JextCheckBox checkIgnoreCase, saveStates, useRegexp, allFiles, scripted;  // This method is used to easily build the GridBagLayout  private void buildConstraints(GridBagConstraints agbc, int agx, int agy, int agw, int agh,                                int awx, int awy)  {    agbc.gridx = agx;    agbc.gridy = agy;    agbc.gridwidth = agw;    agbc.gridheight = agh;    agbc.weightx = awx;    agbc.weighty = awy;    agbc.insets = new Insets(2, 2, 2, 2);  }  /**   * Constructs a new find dialog according to the specified   * type of dialog requested. The dialog can be either a   * FIND dialog, either a REPLACE dialog. In both cases, components   * displayed remain the sames, but the ones specific to replace   * feature are grayed out.   * @param parent The window holder   * @param type The type of the dialog: <code>FindReplace.FIND</code>   *             or <code>FindReplace.REPLACE</code>   * @param modal Displays dialog as a modal window if true   */  public FindReplace(JextFrame parent, int type, boolean modal)  {    super(parent, type == REPLACE ? Jext.getProperty("replace.title") :                                    Jext.getProperty("find.title"), modal);    this.parent = parent;    this.type = type;    fieldSearch = new JComboBox();    fieldSearch.setRenderer(new ModifiedCellRenderer());    fieldSearch.setEditable(true);    fieldReplace = new JComboBox();    fieldReplace.setRenderer(new ModifiedCellRenderer());    fieldReplace.setEditable(true);    KeyHandler handler = new KeyHandler();    fieldSearchEditor = (JTextField) fieldSearch.getEditor().getEditorComponent();    fieldSearchEditor.addKeyListener(handler);    fieldReplaceEditor = (JTextField) fieldReplace.getEditor().getEditorComponent();    fieldReplaceEditor.addKeyListener(handler);    GridBagLayout gridbag = new GridBagLayout();    GridBagConstraints constraints = new GridBagConstraints();    getContentPane().setLayout(gridbag);    ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));    JLabel findLabel = new JLabel(Jext.getProperty("find.label"));    buildConstraints(constraints, 0, 0, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(findLabel, constraints);    getContentPane().add(findLabel);    buildConstraints(constraints, 1, 0, 1, 1, 25, 50);    constraints.fill = GridBagConstraints.HORIZONTAL;    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(fieldSearch, constraints);    getContentPane().add(fieldSearch);    btnFind = new JextHighlightButton(Jext.getProperty("find.button"));    btnFind.setToolTipText(Jext.getProperty("find.tip"));    btnFind.setMnemonic(Jext.getProperty("find.mnemonic").charAt(0));    btnFind.addActionListener(this);    buildConstraints(constraints, 2, 0, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(btnFind, constraints);    getContentPane().add(btnFind);    getRootPane().setDefaultButton(btnFind);    btnCancel = new JextHighlightButton(Jext.getProperty("general.cancel.button"));    btnCancel.setMnemonic(Jext.getProperty("general.cancel.mnemonic").charAt(0));    btnCancel.addActionListener(this);    buildConstraints(constraints, 3, 0, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(btnCancel, constraints);    getContentPane().add(btnCancel);    JLabel replaceLabel = new JLabel(Jext.getProperty("replace.label"));    buildConstraints(constraints, 0, 1, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(replaceLabel, constraints);    getContentPane().add(replaceLabel);// patch added by gandalf march 25 2003    if (type != REPLACE)      replaceLabel.setEnabled(false);// patch added by gandalf march 25 2003    buildConstraints(constraints, 1, 1, 1, 1, 25, 50);    constraints.fill = GridBagConstraints.HORIZONTAL;    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(fieldReplace, constraints);    getContentPane().add(fieldReplace);    if (type != REPLACE)      fieldReplace.setEnabled(false);    btnReplace = new JextHighlightButton(Jext.getProperty("replace.button"));    btnReplace.setToolTipText(Jext.getProperty("replace.tip"));    btnReplace.setMnemonic(Jext.getProperty("replace.mnemonic").charAt(0));    if (type != REPLACE)      btnReplace.setEnabled(false);    btnReplace.addActionListener(this);    buildConstraints(constraints, 2, 1, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(btnReplace, constraints);    getContentPane().add(btnReplace);    btnReplaceAll = new JextHighlightButton(Jext.getProperty("replace.all.button"));    btnReplaceAll.setToolTipText(Jext.getProperty("replace.all.tip"));    btnReplaceAll.setMnemonic(Jext.getProperty("replace.all.mnemonic").charAt(0));    if (type != REPLACE)      btnReplaceAll.setEnabled(false);    btnReplaceAll.addActionListener(this);    buildConstraints(constraints, 3, 1, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(btnReplaceAll, constraints);    getContentPane().add(btnReplaceAll);    scripted = new JextCheckBox(Jext.getProperty("replace.script"), Search.getPythonScript());    if (type != REPLACE)      scripted.setEnabled(false);    else    {      fieldReplace.setEnabled(!scripted.isSelected());      scripted.addActionListener(this);    }    buildConstraints(constraints, 0, 2, 1, 1, 50, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(scripted, constraints);    getContentPane().add(scripted);    script = new JTextField();    if (type != REPLACE)      script.setEnabled(false);    else      script.setEnabled(scripted.isSelected());    script.setText(Search.getPythonScriptString());    buildConstraints(constraints, 1, 2, 1, 1, 50, 50);    constraints.anchor = GridBagConstraints.CENTER;    gridbag.setConstraints(script, constraints);    getContentPane().add(script);    checkIgnoreCase = new JextCheckBox(Jext.getProperty("find.ignorecase.label"), Search.getIgnoreCase());    buildConstraints(constraints, 0, 3, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(checkIgnoreCase, constraints);    getContentPane().add(checkIgnoreCase);    JPanel cPane = new JPanel();    saveStates = new JextCheckBox(Jext.getProperty("find.savevalues.label"),                               Jext.getBooleanProperty("savestates"));    allFiles = new JextCheckBox(Jext.getProperty("find.allFiles.label"),                             Jext.getBooleanProperty("allfiles"));    cPane.add(saveStates);    cPane.add(allFiles);    buildConstraints(constraints, 1, 3, 1, 1, 25, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(cPane, constraints);    getContentPane().add(cPane);    useRegexp = new JextCheckBox(Jext.getProperty("find.useregexp.label"), Search.getRegexp());    buildConstraints(constraints, 2, 3, 2, 1, 50, 50);    constraints.anchor = GridBagConstraints.WEST;    gridbag.setConstraints(useRegexp, constraints);    getContentPane().add(useRegexp);    load();    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);    addKeyListener(new AbstractDisposer(this));    addWindowListener(new WindowAdapter()    {      public void windowClosing(WindowEvent e)      {        exit();      }    });    FontMetrics fm = getFontMetrics(getFont());    fieldSearch.setPreferredSize(new Dimension(18 * fm.charWidth('m'),                                 (int) fieldSearch.getPreferredSize().height));    fieldReplace.setPreferredSize(new Dimension(18 * fm.charWidth('m'),                                  (int) fieldReplace.getPreferredSize().height));    pack();    setResizable(false);    Utilities.centerComponentChild(parent, this);//patch by MJB 8/1/2002		  btnFind.addKeyListener(handler);	btnReplace.addKeyListener(handler);	btnReplaceAll.addKeyListener(handler);	btnCancel.addKeyListener(handler);	checkIgnoreCase.addKeyListener(handler);	saveStates.addKeyListener(handler);	useRegexp.addKeyListener(handler);	allFiles.addKeyListener(handler);	scripted.addKeyListener(handler);	script.addKeyListener(handler);//end MJB patch		    show();  }  // load the search and replace histories from user  // properties. It also selects latest pattern from  // the list.  private void load()  {    String s;    for (int i = 0; i < 25; i++)    {      s = Jext.getProperty("search.history." + i);      if (s != null)        fieldSearch.addItem(s);      else        break;    }    JextTextArea textArea = parent.getTextArea();    if (!Jext.getBooleanProperty("use.selection"))    {      s = Search.getFindPattern();      if (s != null)      {        addSearchHistory(s);        fieldSearch.setSelectedItem(s);      }    } else if ((s = textArea.getSelectedText()) != null) {      char c = '\0';      StringBuffer buf = new StringBuffer(s.length());out:  for (int i = 0; i < s.length(); i++)      {        switch (c = s.charAt(i))        {          case '\n':            break out;          default:            buf.append(c);        }      }      s = buf.toString();      addSearchHistory(s);      fieldSearch.setSelectedItem(s);    }    if (type == REPLACE)    {      for (int i = 0; i < 25; i++)      {        s = Jext.getProperty("replace.history." + i);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -