📄 zfindreplace.java
字号:
if (result >= 0) {
us.scrollCellIntoView(cell.getRow(), cell.getCol());
if (moveFocus) {
us.setFocus(new ZCellID(cell.getRow(), cell.getCol()));
us.setSelection(new ZRect(cell.getCol(), cell.getRow(), cell.getCol(), cell.getRow()));
}
return true;
}
}
JOptionPane.showMessageDialog(this, "Finished searching the document.", "Find", JOptionPane.INFORMATION_MESSAGE);
resetSearchVariables();
return false;
}
private int searchNext () {
if (cells.size() == 0)
return -1;
if (curIndex >= cells.size())
curIndex = 0;
if (curIndex < 0)
curIndex = cells.size() - 1;
int to = curIndex;
if (searchDown.isSelected()) {
curIndex++;
}
else {
curIndex--;
}
if (curIndex == endIndex)
return -1;
else
return to;
}
private int searchFor (String needle, String haystack, int offset) {
int index = 0;
while (index >= 0) {
index = haystack.indexOf(needle, index);
if (index >= 0) // be found
{
if (matchWord.isSelected() && !isWord(haystack, index, needle.length())) {
index++;
continue;
}
return index;
}
}
return -1;
}
private boolean isWord (String haystack, int offset, int length) {
int leftSide = offset - 1;
int rightSide = offset + length;
if (isDelimiter(haystack, leftSide) && isDelimiter(haystack, rightSide))
return true;
else
return false;
}
private boolean isDelimiter (String haystack, int offset) {
if ((offset < 0) || (offset >= haystack.length()))
return true;
return !Character.isLetterOrDigit(haystack.charAt(offset));
}
private boolean processReplace (ZCmdGroup group) {
String needle = findData.getText();
String replaceText = replaceData.getText();
String cellText = cell.getText();
if (!matchCase.isSelected()) {
cellText = cellText.toLowerCase();
needle = needle.toLowerCase();
}
int index = 0;
boolean changed = false;
while (index >= 0) {
index = cellText.indexOf(needle, index);
if (index >= 0) // be found
{
if (matchWord.isSelected() && !isWord(cellText, index, needle.length())) {
index++;
continue;
}
if (index > 0)
cellText = cellText.substring(0, index - 1) + replaceText + cellText.substring(index + needle.length());
else
cellText = replaceText + cellText.substring(index + needle.length());
index += replaceText.length();
changed = true;
}
}
if (changed) {
if (group != null) {
boolean autoUndo = ZCmdFormat.getAutoUndo();
ZCmdFormat.setAutoUndo(false);
ZCmdFormat cmd = ((ZDefaultSheet)us.getSheet()).setCellText(cell.getRow(), cell.getCol(), cellText);
group.add(cmd);
ZCmdFormat.setAutoUndo(autoUndo);
}
else
((ZDefaultSheet)us.getSheet()).setCellText(cell.getRow(), cell.getCol(), cellText);
}
return processFindNext(group == null);
}
private void processReplaceAll () {
resetSearchVariables();
ZCmdGroup cmd = new ZCmdGroup(us.getSheet(),(ZRect)us.getSelection().clone(),0);
while (processReplace(cmd));
}
private void processClose () {
setVisible(false);
}
private void resetSearchVariables () {
cells = null;
}
}
class ZSearchGotoDialog extends JDialog
implements ActionListener {
private static ZSearchGotoDialog sharedInstance;
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);
private JTextField rowData;
private JTextField colData;
private JLabel rowLabel;
private JLabel colLabel;
private JPanel rowPanel;
private JPanel colPanel;
private JPanel commandPanel;
private JButton okButton;
private JButton cancelButton;
private ZSheetState us;
ZSearchGotoDialog (JFrame owner) {
super(owner, "go to ...", true);
setSize(280, 150);
setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
// setResizable(false);
// row data
rowData = new JTextField(TEXT_FIELD_SIZE);
rowData.setMaximumSize(rowData.getPreferredSize());
rowLabel = new JLabel(" Row:");
rowLabel.setDisplayedMnemonic('R');
rowLabel.setLabelFor(rowData);
rowPanel = new JPanel();
rowPanel.setBorder(EMPTY_BORDER);
rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
rowPanel.add(rowLabel);
rowPanel.add(Box.createHorizontalGlue());
rowPanel.add(Box.createHorizontalStrut(COMPONENT_GAP));
rowPanel.add(rowData);
// col data
colData = new JTextField(TEXT_FIELD_SIZE);
colData.setMaximumSize(colData.getPreferredSize());
colLabel = new JLabel("Column:");
colLabel.setDisplayedMnemonic('C');
colLabel.setLabelFor(colData);
colPanel = new JPanel();
colPanel.setBorder(EMPTY_BORDER);
colPanel.setLayout(new BoxLayout(colPanel, BoxLayout.X_AXIS));
colPanel.add(colLabel);
colPanel.add(Box.createHorizontalGlue());
colPanel.add(Box.createHorizontalStrut(COMPONENT_GAP));
colPanel.add(colData);
// buttons
commandPanel = new JPanel();
okButton = new JButton("Ok");
okButton.addActionListener(this);
commandPanel.add(okButton);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
commandPanel.add(cancelButton);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(EMPTY_BORDER);
panel.add(rowPanel);
panel.add(colPanel);
panel.add(commandPanel);
setContentPane(panel);
getRootPane().setDefaultButton(okButton);
ZToolkit.moveCenter(this);
}
public void actionPerformed (ActionEvent e) {
Object o = e.getSource();
if (o == okButton)
processOk();
if (o == cancelButton)
processCancel();
}
private void processOk () {
setVisible(false);
try {
int row = Integer.parseInt(rowData.getText());
int col = Integer.parseInt(colData.getText());
ZRect selectable = new ZRect(1, 1, us.getSheet().getLastCol(), us.getSheet().getLastRow());
if (selectable.containCell(row, col))
us.setVisibleCells(new ZRect(col, row, col, row));
else
throw new Exception();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "The cell [" + rowData.getText() + "," + colData.getText() + "] not exist !", "alert",
JOptionPane.ERROR_MESSAGE);
}
}
private void processCancel () {
setVisible(false);
}
public void show (ZSheetState us) {
this.us = us;
rowData.requestFocus() ;
rowData.setSelectionStart(0) ;
rowData.setSelectionEnd(rowData.getText().length() );
setVisible(true);
}
public static ZSearchGotoDialog getSharedInstance (JFrame owner) {
if (sharedInstance == null) {
sharedInstance = new ZSearchGotoDialog(owner);
}
return sharedInstance;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -