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

📄 zfindreplace.java

📁 用Java写的报表.功能如下: 0.内建网络打印,网络预览功能! 1.文件操作。包括url 指定的文件。 2.全功能打印支持。包括打印预览。 3.Undo 和 redo。 4.合并单元格。 5.Cel
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * put your module comment here
 * formatted with JxBeauty (c) johann.langhofer@nextra.at
 */


package  ezcell;

import  java.awt.*;
import  java.awt.event.*;
import  java.io.*;
import  javax.swing.*;
import  javax.swing.text.*;
import  javax.swing.event.*;
import  javax.swing.border.*;
import  java.util.*;


public class ZFindReplace extends JDialog
        implements ActionListener {
    private final static int TEXT_FIELD_SIZE = 20;
    private final static int COMPONENT_GAP = 10;
    private final static Border EMPTY_BORDER = new EmptyBorder(5, 5, 5, 5);
    //  Shared instance of this class
    private static ZFindReplace sharedFindReplace;
    //  Visible components on the dialog
    private JLabel findLabel;
    private JLabel replaceLabel;
    private JTextField findData;
    private JTextField replaceData;
    private JCheckBox matchCase;
    private JCheckBox matchWord;
    private JRadioButton searchUp;
    private JRadioButton searchDown;
    private JButton findNextButton;
    private JButton closeButton;
    private JButton replaceButton;
    private JButton replaceAllButton;
    private JPanel findPanel;
    private JPanel replacePanel;
    private JPanel optionPanel;
    private JPanel commandPanel;
    private boolean searchWrap;
    // target
    private ZSheetState us;
    private Enumeration enum;
    private ZCell cell;
    private boolean moveFocus = true;
    private int curIndex = 0;
    private int endIndex = 0;
    private Vector cells;

    public ZFindReplace (JFrame owner) {
        super(owner, "", true);
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        setResizable(false);
        //  Create find panel
        findData = new JTextField(TEXT_FIELD_SIZE);
        findData.setMaximumSize(findData.getPreferredSize());
        findLabel = new JLabel("Find what:");
        findLabel.setDisplayedMnemonic('N');
        findLabel.setLabelFor(findData);
        findPanel = new JPanel();
        findPanel.setBorder(EMPTY_BORDER);
        findPanel.setLayout(new BoxLayout(findPanel, BoxLayout.X_AXIS));
        findPanel.add(findLabel);
        findPanel.add(Box.createHorizontalGlue());
        findPanel.add(Box.createHorizontalStrut(COMPONENT_GAP));
        findPanel.add(findData);
        //  Create replace panel
        replaceData = new JTextField(TEXT_FIELD_SIZE);
        replaceData.setMaximumSize(findData.getPreferredSize());
        replaceLabel = new JLabel("Replace with:");
        replaceLabel.setDisplayedMnemonic('P');
        replaceLabel.setLabelFor(replaceData);
        replacePanel = new JPanel();
        replacePanel.setBorder(EMPTY_BORDER);
        replacePanel.setLayout(new BoxLayout(replacePanel, BoxLayout.X_AXIS));
        replacePanel.add(replaceLabel);
        replacePanel.add(Box.createHorizontalGlue());
        replacePanel.add(Box.createHorizontalStrut(COMPONENT_GAP));
        replacePanel.add(replaceData);
        //  Create options panel
        JPanel matchPanel = new JPanel();
        matchPanel.setLayout(new GridLayout(2, 1));
        matchCase = new JCheckBox("Match case");
        matchCase.setMnemonic('C');
        matchWord = new JCheckBox("Match word");
        matchWord.setMnemonic('W');
        matchPanel.add(matchCase);
        matchPanel.add(matchWord);
        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new GridLayout(2, 1));
        searchDown = new JRadioButton("Search Down");
        searchDown.setMnemonic('D');
        searchDown.setSelected(true);
        searchDown.addActionListener(this);
        searchUp = new JRadioButton("Search Up");
        searchUp.setMnemonic('U');
        searchUp.addActionListener(this);
        searchPanel.add(searchDown);
        searchPanel.add(searchUp);
        ButtonGroup searchGroup = new ButtonGroup();
        searchGroup.add(searchDown);
        searchGroup.add(searchUp);
        optionPanel = new JPanel();
        optionPanel.setLayout(new GridLayout(1, 2));
        optionPanel.setBorder(new TitledBorder("Options"));
        optionPanel.add(matchPanel);
        optionPanel.add(searchPanel);
        //  Create command panel
        commandPanel = new JPanel();
        findNextButton = createButton(commandPanel, "Find Next", 'F');
        replaceButton = createButton(commandPanel, "Replace", 'R');
        replaceAllButton = createButton(commandPanel, "Replace All", 'a');
        closeButton = createButton(commandPanel, "Close", ' ');
        closeButton.setEnabled(true);
        //  Layout all the panels to build the dialog
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(EMPTY_BORDER);
        panel.add(findPanel);
        panel.add(replacePanel);
        panel.add(optionPanel);
        panel.add(commandPanel);
        setContentPane(panel);
        //  Set the default button for the dialog
        getRootPane().setDefaultButton(findNextButton);
        FocusAdapter resetDefaultButton = new FocusAdapter() {

            public void focusLost (FocusEvent e) {
                getRootPane().setDefaultButton(findNextButton);
            }
        };
        replaceButton.addFocusListener(resetDefaultButton);
        replaceAllButton.addFocusListener(resetDefaultButton);
        closeButton.addFocusListener(resetDefaultButton);
        findData.addKeyListener(new KeyAdapter() {

            public void keyReleased (KeyEvent e) {
                if (e.getKeyCode() != KeyEvent.VK_ENTER) {
                    boolean state = (findData.getText().length() > 0);
                    findNextButton.setEnabled(state);
                    replaceButton.setEnabled(state);
                    replaceAllButton.setEnabled(state);
                    resetSearchVariables();
                }
            }
        });
        pack();
        ZToolkit.moveCenter(owner,this);
    }

    public static ZFindReplace getSharedInstance (JFrame owner) {
        if (sharedFindReplace == null)
            sharedFindReplace = new ZFindReplace(owner);
        return  sharedFindReplace;
    }

    public void showFind (ZSheetState us) {
        setTitle("Find");
        this.us = us;
        showReplaceComponents(false);
        pack();
        setVisible(true);
        findData.requestFocus();
        resetSearchVariables();
    }

    public void showReplace (ZSheetState us) {
        setTitle("Find and Replace");
        this.us = us;
        showReplaceComponents(true);
        pack();
        setVisible(true);
        findData.requestFocus();
        resetSearchVariables();
    }

    private void showReplaceComponents (boolean value) {
        replacePanel.setVisible(value);
        replaceButton.setVisible(value);
        replaceAllButton.setVisible(value);
    }

    public void actionPerformed (ActionEvent e) {
        Object o = e.getSource();
        if (o == findNextButton)
            processFindNext(true);
        if (o == replaceButton)
            processReplace(null);
        if (o == replaceAllButton)
            processReplaceAll();
        if (o == closeButton)
            processClose();
        if (o == searchUp || o == searchDown)
            cells = null;
    }

    private void processDirection () {
        cells = us.getSheet().getSortedCells(true);
        curIndex = Collections.binarySearch(cells, us.getSheet().getCell(us.getFocus().row, us.getFocus().col));
        if (curIndex < 0) {
            if (searchUp.isSelected())
                curIndex = -curIndex - 2;
            else
                curIndex = -curIndex - 1;
        }
        endIndex = curIndex;
    }

    private JButton createButton (JPanel panel, String label, char mnemonic) {
        JButton button = new JButton(label);
        button.setMnemonic(mnemonic);
        button.setEnabled(false);
        button.addActionListener(this);
        panel.add(button);
        return  button;
    }

    private boolean processFindNext (boolean moveFocus) {
        if (cells == null) {
            processDirection();
        }
        int i;
        while ((i = searchNext()) >= 0) {
                      System.out.println(i);
            cell = (ZDefaultCell)cells.get(i);
            if (cell.isChild() || !((ZDefaultCell)cell).hasProperty(ZBase.PTY_Text) || cell.getText().equals(""))
                continue;
            String cellText = cell.getText();
            String needle = findData.getText();
            if (!matchCase.isSelected()) {
                cellText = cellText.toLowerCase();
                needle = needle.toLowerCase();
            }
            //  Search for the string and show the result
            int result = searchFor(needle, cellText, 0);

⌨️ 快捷键说明

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