fixed search dialog

This commit is contained in:
Radek Davidek 2026-06-09 20:39:49 +02:00
parent 197e367ca4
commit fac3a35984
4 changed files with 70 additions and 22 deletions

View File

@ -766,6 +766,11 @@ public class FileEditor extends JFrame {
return name.endsWith(".md") || name.endsWith(".markdown") || name.endsWith(".mdown") || name.endsWith(".mkd"); return name.endsWith(".md") || name.endsWith(".markdown") || name.endsWith(".mdown") || name.endsWith(".mkd");
} }
private boolean isNfoFile() {
String name = virtualPath != null ? virtualPath : file.getName();
return name.toLowerCase().endsWith(".nfo");
}
private void setMarkdownMode(boolean on) { private void setMarkdownMode(boolean on) {
if (on && (!readOnly || !isMarkdownFile() || hexMode || isImageFile(file))) { if (on && (!readOnly || !isMarkdownFile() || hexMode || isImageFile(file))) {
on = false; on = false;
@ -1500,7 +1505,13 @@ public class FileEditor extends JFrame {
} else if (hexMode) { } else if (hexMode) {
buildHexViewText(0L); buildHexViewText(0L);
} else { } else {
String content = new String(fileBytes, "UTF-8"); String encoding = isNfoFile() ? "Cp437" : "UTF-8";
String content;
try {
content = new String(fileBytes, encoding);
} catch (java.io.UnsupportedEncodingException e) {
content = new String(fileBytes, "UTF-8");
}
textArea.setText(content); textArea.setText(content);
textArea.setCaretPosition(0); textArea.setCaretPosition(0);
} }
@ -1569,7 +1580,13 @@ public class FileEditor extends JFrame {
} else if (hexMode) { } else if (hexMode) {
buildHexViewText(0L); buildHexViewText(0L);
} else { } else {
String content = new String(fileBytes, "UTF-8"); String encoding = isNfoFile() ? "Cp437" : "UTF-8";
String content;
try {
content = new String(fileBytes, encoding);
} catch (java.io.UnsupportedEncodingException e) {
content = new String(fileBytes, "UTF-8");
}
textArea.setText(content); textArea.setText(content);
textArea.setCaretPosition(0); textArea.setCaretPosition(0);
} }
@ -1590,7 +1607,7 @@ public class FileEditor extends JFrame {
} }
private boolean isBinaryForCurrentFile(byte[] bytes) { private boolean isBinaryForCurrentFile(byte[] bytes) {
if (isMarkdownFile()) { if (isMarkdownFile() || isNfoFile()) {
return containsNulByte(bytes); return containsNulByte(bytes);
} }
return isBinary(bytes); return isBinary(bytes);

View File

@ -3359,7 +3359,9 @@ public class FilePanelTab extends JPanel {
briefCurrentColumn = column; briefCurrentColumn = column;
fileTable.setRowSelectionInterval(row, row); fileTable.setRowSelectionInterval(row, row);
if (scrollToItem) { if (scrollToItem) {
scrollBriefCellToColumnStart(row, column); final int r = row;
final int c = column;
SwingUtilities.invokeLater(() -> scrollBriefCellToColumnStart(r, c));
} }
fileTable.repaint(); fileTable.repaint();
if (requestFocus) { if (requestFocus) {
@ -3376,7 +3378,10 @@ public class FilePanelTab extends JPanel {
if (item != null && item.getName().equalsIgnoreCase(name)) { if (item != null && item.getName().equalsIgnoreCase(name)) {
fileTable.setRowSelectionInterval(i, i); fileTable.setRowSelectionInterval(i, i);
if (scrollToItem) { if (scrollToItem) {
fileTable.scrollRectToVisible(fileTable.getCellRect(i, 0, true)); final int row = i;
SwingUtilities.invokeLater(() -> {
fileTable.scrollRectToVisible(fileTable.getCellRect(row, 0, true));
});
} }
if (requestFocus) { if (requestFocus) {
fileTable.requestFocusInWindow(); fileTable.requestFocusInWindow();
@ -3594,11 +3599,14 @@ public class FilePanelTab extends JPanel {
int row = index % tableModel.briefRowsPerColumn; int row = index % tableModel.briefRowsPerColumn;
if (briefCurrentColumn < fileTable.getColumnCount()) { if (briefCurrentColumn < fileTable.getColumnCount()) {
fileTable.setRowSelectionInterval(row, row); fileTable.setRowSelectionInterval(row, row);
scrollBriefCellToColumnStart(row, briefCurrentColumn); final int r = row;
final int c = briefCurrentColumn;
SwingUtilities.invokeLater(() -> scrollBriefCellToColumnStart(r, c));
} }
} else { } else {
fileTable.setRowSelectionInterval(index, index); fileTable.setRowSelectionInterval(index, index);
fileTable.scrollRectToVisible(fileTable.getCellRect(index, 0, true)); final int r = index;
SwingUtilities.invokeLater(() -> fileTable.scrollRectToVisible(fileTable.getCellRect(r, 0, true)));
} }
fileTable.repaint(); fileTable.repaint();
fileTable.requestFocusInWindow(); fileTable.requestFocusInWindow();

View File

@ -2702,18 +2702,16 @@ public class MainWindow extends JFrame {
// Load directory and then select item by name on the EDT // Load directory and then select item by name on the EDT
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
chosen.loadDirectory(parentDir); FilePanelTab tab = chosen.getCurrentTab();
if (tab != null) {
tab.loadDirectory(parentDir, false, true, () -> {
tab.selectItem(file.getName());
tab.getFileTable().requestFocusInWindow();
});
}
// mark this panel active and refresh borders // mark this panel active and refresh borders
activePanel = chosen; activePanel = chosen;
updateActivePanelBorder(); updateActivePanelBorder();
// After loading, select the file name
SwingUtilities.invokeLater(() -> {
FilePanelTab tab = chosen.getCurrentTab();
if (tab != null) {
tab.selectItem(file.getName());
tab.getFileTable().requestFocusInWindow();
}
});
}); });
} }

View File

@ -332,9 +332,12 @@ public class SearchDialog extends JDialog {
stopButton.setMaximumSize(new Dimension(120, 30)); stopButton.setMaximumSize(new Dimension(120, 30));
stopButton.setEnabled(false); stopButton.setEnabled(false);
stopButton.addActionListener(e -> { stopButton.addActionListener(e -> {
if (currentWorker != null) {
currentWorker.cancel(true);
}
searching = false; searching = false;
stopButton.setEnabled(false); stopButton.setEnabled(false);
statusLabel.setText("Cancelling..."); statusLabel.setText("Canceled — found " + foundCount + " files");
}); });
eastPanel.add(searchButton); eastPanel.add(searchButton);
@ -490,10 +493,21 @@ public class SearchDialog extends JDialog {
} }
}); });
// Escape closes the dialog (and cancels ongoing search) // Escape cancels ongoing search or closes the dialog
getRootPane().registerKeyboardAction(e -> { getRootPane().registerKeyboardAction(e -> {
searching = false; if (searching) {
dispose(); if (currentWorker != null) {
currentWorker.cancel(true);
}
searching = false;
statusLabel.setText("Canceled — found " + foundCount + " files");
statusProgressBar.setIndeterminate(false);
statusProgressBar.setValue(0);
searchButton.setEnabled(true);
stopButton.setEnabled(false);
} else {
dispose();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
// F3 = view, F4 = edit when dialog is active // F3 = view, F4 = edit when dialog is active
@ -556,7 +570,12 @@ public class SearchDialog extends JDialog {
/** /**
* Provede vyhledávání * Provede vyhledávání
*/ */
private SwingWorker<?, ?> currentWorker;
private void performSearch() { private void performSearch() {
if (currentWorker != null && !currentWorker.isDone()) {
currentWorker.cancel(true);
}
String namePat = ""; String namePat = "";
try { try {
Object it = patternCombo.getEditor().getItem(); Object it = patternCombo.getEditor().getItem();
@ -669,7 +688,7 @@ public class SearchDialog extends JDialog {
statusProgressBar.setIndeterminate(true); statusProgressBar.setIndeterminate(true);
// Spustit vyhledávání v samostatném vlákně // Spustit vyhledávání v samostatném vlákně
SwingWorker<Void, Object[]> worker = new SwingWorker<Void, Object[]>() { currentWorker = new SwingWorker<Void, Object[]>() {
private String currentSearchPath = ""; private String currentSearchPath = "";
@Override @Override
@ -745,6 +764,10 @@ public class SearchDialog extends JDialog {
statusProgressBar.setIndeterminate(false); statusProgressBar.setIndeterminate(false);
statusProgressBar.setVisible(false); statusProgressBar.setVisible(false);
try { try {
if (isCancelled()) {
statusLabel.setText("Canceled — found " + foundCount + " files");
return;
}
get(); get();
statusLabel.setText("Done — found " + foundCount + " files"); statusLabel.setText("Done — found " + foundCount + " files");
if (tableModel.getRowCount() == 0) { if (tableModel.getRowCount() == 0) {
@ -753,6 +776,8 @@ public class SearchDialog extends JDialog {
"Result", "Result",
JOptionPane.INFORMATION_MESSAGE); JOptionPane.INFORMATION_MESSAGE);
} }
} catch (java.util.concurrent.CancellationException e) {
statusLabel.setText("Canceled — found " + foundCount + " files");
} catch (Exception e) { } catch (Exception e) {
statusLabel.setText("Error"); statusLabel.setText("Error");
JOptionPane.showMessageDialog(SearchDialog.this, JOptionPane.showMessageDialog(SearchDialog.this,
@ -763,7 +788,7 @@ public class SearchDialog extends JDialog {
} }
}; };
worker.execute(); currentWorker.execute();
} }
/** /**