2024-10-08 23:45:06 +03:00
|
|
|
|
package _VisualDVM.Visual.Windows;
|
2024-10-07 14:22:52 +03:00
|
|
|
|
import Common.Utils.CommonUtils;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import Common.Visual.CommonUI;
|
2024-10-08 22:33:49 +03:00
|
|
|
|
import Common.Visual.Windows.Form;
|
2024-10-09 22:01:19 +03:00
|
|
|
|
import _VisualDVM.Current;
|
2024-10-09 20:35:18 +03:00
|
|
|
|
import Common.Visual.TextField.StyledTextField;
|
|
|
|
|
|
import Common.Visual.Trees.StyledTree;
|
2024-10-09 22:01:19 +03:00
|
|
|
|
import _VisualDVM.Visual.UI;
|
2024-10-09 23:37:58 +03:00
|
|
|
|
import Visual_DVM_2021.Passes.PassCode;
|
2024-10-10 23:57:36 +03:00
|
|
|
|
import Common.Passes.Pass;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
import javafx.util.Pair;
|
|
|
|
|
|
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
|
|
|
|
|
import org.fife.ui.rtextarea.SearchContext;
|
|
|
|
|
|
import org.fife.ui.rtextarea.SearchEngine;
|
|
|
|
|
|
import org.fife.ui.rtextarea.SearchResult;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
|
public class SearchReplaceForm extends Form {
|
|
|
|
|
|
public JPanel MainPanel;
|
|
|
|
|
|
public boolean forward = true;
|
|
|
|
|
|
SearchContext context = null;
|
|
|
|
|
|
RSyntaxTextArea editor = null;
|
|
|
|
|
|
boolean replace_mode = false;
|
|
|
|
|
|
private JTextField tfFind;
|
|
|
|
|
|
private JTextField tfReplace;
|
|
|
|
|
|
private JCheckBox replaceOn;
|
|
|
|
|
|
private JCheckBox registerOn;
|
|
|
|
|
|
private JCheckBox wholeWordOn;
|
|
|
|
|
|
private JRadioButton rbUp;
|
|
|
|
|
|
private JRadioButton rbDown;
|
|
|
|
|
|
private JButton bAll;
|
|
|
|
|
|
private JButton bNext;
|
|
|
|
|
|
private JCheckBox loopOn;
|
|
|
|
|
|
private JLabel lCount;
|
|
|
|
|
|
private SearchResult result = null;
|
|
|
|
|
|
//https://techarks.ru/qa/java/kak-uznat-kakoj-iz-jlist-s-Y4/
|
|
|
|
|
|
public void ClearMarkers() {
|
|
|
|
|
|
//сброс выделения. решается подсовыванием пустой строки
|
|
|
|
|
|
context.setSearchFor("");
|
|
|
|
|
|
SearchEngine.find(editor, context);
|
|
|
|
|
|
DropMatchCount();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void DropMatchCount() {
|
|
|
|
|
|
lCount.setText("0");
|
|
|
|
|
|
}
|
2024-10-08 23:21:47 +03:00
|
|
|
|
public void updateEditor(RSyntaxTextArea editor_in) {
|
2023-09-17 22:13:42 +03:00
|
|
|
|
editor = editor_in;
|
2024-10-08 23:21:47 +03:00
|
|
|
|
if (isVisible())
|
|
|
|
|
|
requestFocus();
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
protected JPanel getMainPanel() {
|
|
|
|
|
|
return MainPanel;
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void Close() {
|
|
|
|
|
|
ClearMarkers();
|
|
|
|
|
|
super.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void setMode(boolean replace) {
|
|
|
|
|
|
replace_mode = replace;
|
|
|
|
|
|
tfReplace.setEnabled(replace_mode);
|
|
|
|
|
|
String prefix = replace_mode ? "Заменить" : "Найти";
|
|
|
|
|
|
bNext.setText(prefix + " далее");
|
|
|
|
|
|
bAll.setText(prefix + " всё");
|
|
|
|
|
|
}
|
|
|
|
|
|
public void ShowMode() {
|
|
|
|
|
|
replaceOn.setSelected(replace_mode);
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Component getRelative() {
|
|
|
|
|
|
return (Component) UI.getMainWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getDefaultWidth() {
|
|
|
|
|
|
return 650;
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getDefaultHeight() {
|
|
|
|
|
|
return 400;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Refresh() {
|
|
|
|
|
|
String text = editor.getSelectedText();
|
|
|
|
|
|
if ((text != null) && !text.isEmpty())
|
|
|
|
|
|
tfFind.setText(text);
|
|
|
|
|
|
tfFind.requestFocus();
|
|
|
|
|
|
}
|
|
|
|
|
|
//-------------------------------
|
|
|
|
|
|
public void SwitchDirection(boolean direction_in) {
|
|
|
|
|
|
forward = direction_in;
|
|
|
|
|
|
if (forward) {
|
|
|
|
|
|
rbUp.setSelected(false);
|
|
|
|
|
|
rbDown.setSelected(true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
rbDown.setSelected(false);
|
|
|
|
|
|
rbUp.setSelected(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void applyParams() {
|
2024-10-07 14:22:52 +03:00
|
|
|
|
String toFind = CommonUtils.hideRegularMetasymbols(tfFind.getText());
|
|
|
|
|
|
String toReplace = CommonUtils.hideRegularMetasymbols(tfReplace.getText());
|
2023-09-17 22:13:42 +03:00
|
|
|
|
context.setSearchFor(toFind);
|
|
|
|
|
|
context.setMatchCase(registerOn.isSelected());
|
|
|
|
|
|
context.setWholeWord(wholeWordOn.isSelected());
|
|
|
|
|
|
if (replace_mode)
|
|
|
|
|
|
context.setReplaceWith(toReplace);
|
|
|
|
|
|
DropMatchCount();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void onAll() {
|
|
|
|
|
|
applyParams();
|
|
|
|
|
|
result = replace_mode ?
|
|
|
|
|
|
SearchEngine.replaceAll(editor, context) : SearchEngine.markAll(editor, context);
|
|
|
|
|
|
lCount.setText(String.valueOf(
|
|
|
|
|
|
replace_mode ? result.getCount() : result.getMarkedCount()));
|
|
|
|
|
|
}
|
|
|
|
|
|
public void onNext() {
|
|
|
|
|
|
applyParams();
|
|
|
|
|
|
context.setSearchForward(forward);
|
|
|
|
|
|
result = replace_mode ? SearchEngine.replace(editor, context) : SearchEngine.find(editor, context);
|
|
|
|
|
|
if (loopOn.isSelected() && !result.wasFound()) SwitchDirection(!forward);
|
|
|
|
|
|
lCount.setText(String.valueOf(result.getMarkedCount()));
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
2024-10-08 22:33:49 +03:00
|
|
|
|
protected String getFormKey() {
|
2024-10-08 23:21:47 +03:00
|
|
|
|
return "SearchReplace";
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
private void createUIComponents() {
|
|
|
|
|
|
// TODO: place custom component creation code here
|
|
|
|
|
|
tfFind = new StyledTextField();
|
|
|
|
|
|
tfReplace = new StyledTextField();
|
|
|
|
|
|
}
|
|
|
|
|
|
//------------------
|
|
|
|
|
|
//-
|
|
|
|
|
|
private JButton bSearchInFiles;
|
|
|
|
|
|
private JPanel filesTreePanel;
|
|
|
|
|
|
private JLabel lFilesMatchesCount;
|
|
|
|
|
|
public static String lastProjectPath = "";
|
|
|
|
|
|
public static LinkedHashMap<String, Long> matches = new LinkedHashMap<>();
|
|
|
|
|
|
public void dropFilesMatches() {
|
|
|
|
|
|
matches = new LinkedHashMap<>();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void showNoFilesMatches() {
|
|
|
|
|
|
lFilesMatchesCount.setText("—");
|
2024-10-07 17:46:38 +03:00
|
|
|
|
CommonUI.Clear(filesTreePanel);
|
2023-09-17 22:13:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
public void showFilesMatches() {
|
|
|
|
|
|
long total = 0;
|
|
|
|
|
|
DefaultMutableTreeNode res = new DefaultMutableTreeNode("файлов " + matches.size());
|
|
|
|
|
|
for (String fileName : matches.keySet()) {
|
|
|
|
|
|
DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(new Pair(fileName, matches.get(fileName)) {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
|
return getKey() + ":" + getValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
res.add(fileNode);
|
|
|
|
|
|
total += matches.get(fileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
StyledTree matchesTree = new StyledTree(res) {
|
|
|
|
|
|
{
|
|
|
|
|
|
setRootVisible(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void LeftMouseAction2() {
|
|
|
|
|
|
DefaultMutableTreeNode selectedFile = (DefaultMutableTreeNode) getLastSelectedPathComponent();
|
|
|
|
|
|
if (selectedFile != null) {
|
|
|
|
|
|
Pair<String, Long> info = (Pair<String, Long>) selectedFile.getUserObject();
|
|
|
|
|
|
if (Current.getProject().db.files.containsKey(info.getKey())) {
|
2024-10-09 23:37:58 +03:00
|
|
|
|
Pass.passes.get(PassCode.OpenCurrentFile).Do(Current.getProject().db.files.get(info.getKey()));
|
2023-09-17 22:13:42 +03:00
|
|
|
|
//--->>>
|
|
|
|
|
|
replaceOn.setSelected(false);
|
|
|
|
|
|
setMode(false);
|
|
|
|
|
|
SwitchDirection(true);
|
|
|
|
|
|
//-
|
|
|
|
|
|
bNext.requestFocus();
|
|
|
|
|
|
bNext.doClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
filesTreePanel.add(matchesTree);
|
|
|
|
|
|
filesTreePanel.revalidate();
|
|
|
|
|
|
filesTreePanel.repaint();
|
|
|
|
|
|
lFilesMatchesCount.setText(String.valueOf(total));
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void Show() {
|
|
|
|
|
|
super.Show();
|
|
|
|
|
|
//------->>
|
|
|
|
|
|
showNoFilesMatches();
|
|
|
|
|
|
if (lastProjectPath.equals(Current.getProject().Home.getAbsolutePath()))
|
|
|
|
|
|
showFilesMatches();
|
|
|
|
|
|
else
|
|
|
|
|
|
dropFilesMatches();
|
|
|
|
|
|
//------->>
|
|
|
|
|
|
Refresh();
|
|
|
|
|
|
// setAlwaysOnTop(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
public SearchReplaceForm() {
|
|
|
|
|
|
context = new SearchContext();
|
|
|
|
|
|
//-
|
|
|
|
|
|
context.setRegularExpression(true);
|
|
|
|
|
|
//-
|
|
|
|
|
|
rbDown.addActionListener(e -> SwitchDirection(!forward));
|
|
|
|
|
|
rbUp.addActionListener(e -> SwitchDirection(!forward));
|
|
|
|
|
|
bAll.addActionListener(e -> onAll());
|
|
|
|
|
|
bNext.addActionListener(e -> onNext());
|
|
|
|
|
|
replaceOn.addActionListener(e -> {
|
|
|
|
|
|
setMode(replaceOn.isSelected());
|
|
|
|
|
|
});
|
|
|
|
|
|
bNext.addKeyListener(new KeyAdapter() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
|
|
if (e.getKeyCode() == KeyEvent.VK_ENTER)
|
|
|
|
|
|
bNext.doClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tfFind.addKeyListener(new KeyAdapter() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
|
|
switch (e.getKeyCode()) {
|
|
|
|
|
|
case KeyEvent.VK_ENTER:
|
|
|
|
|
|
bNext.requestFocus();
|
|
|
|
|
|
bNext.doClick();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
bSearchInFiles.addActionListener(new ActionListener() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
showNoFilesMatches();
|
|
|
|
|
|
lastProjectPath = Current.getProject().Home.getAbsolutePath();
|
|
|
|
|
|
matches = Current.getProject().getMatches(
|
|
|
|
|
|
tfFind.getText(),
|
|
|
|
|
|
registerOn.isSelected(),
|
|
|
|
|
|
wholeWordOn.isSelected());
|
|
|
|
|
|
showFilesMatches();
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|