From fac3a35984e6c838e5d7b910b9c275e96f2b1b89 Mon Sep 17 00:00:00 2001 From: Radek Davidek Date: Tue, 9 Jun 2026 20:39:49 +0200 Subject: [PATCH] fixed search dialog --- .../cz/kamma/kfmanager/ui/FileEditor.java | 23 ++++++++++-- .../cz/kamma/kfmanager/ui/FilePanelTab.java | 16 ++++++-- .../cz/kamma/kfmanager/ui/MainWindow.java | 16 ++++---- .../cz/kamma/kfmanager/ui/SearchDialog.java | 37 ++++++++++++++++--- 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java b/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java index 4c85dec..ce329dc 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java @@ -766,6 +766,11 @@ public class FileEditor extends JFrame { 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) { if (on && (!readOnly || !isMarkdownFile() || hexMode || isImageFile(file))) { on = false; @@ -1500,7 +1505,13 @@ public class FileEditor extends JFrame { } else if (hexMode) { buildHexViewText(0L); } 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.setCaretPosition(0); } @@ -1569,7 +1580,13 @@ public class FileEditor extends JFrame { } else if (hexMode) { buildHexViewText(0L); } 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.setCaretPosition(0); } @@ -1590,7 +1607,7 @@ public class FileEditor extends JFrame { } private boolean isBinaryForCurrentFile(byte[] bytes) { - if (isMarkdownFile()) { + if (isMarkdownFile() || isNfoFile()) { return containsNulByte(bytes); } return isBinary(bytes); diff --git a/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java b/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java index 6a12e89..46cbf21 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java @@ -3359,7 +3359,9 @@ public class FilePanelTab extends JPanel { briefCurrentColumn = column; fileTable.setRowSelectionInterval(row, row); if (scrollToItem) { - scrollBriefCellToColumnStart(row, column); + final int r = row; + final int c = column; + SwingUtilities.invokeLater(() -> scrollBriefCellToColumnStart(r, c)); } fileTable.repaint(); if (requestFocus) { @@ -3376,7 +3378,10 @@ public class FilePanelTab extends JPanel { if (item != null && item.getName().equalsIgnoreCase(name)) { fileTable.setRowSelectionInterval(i, i); if (scrollToItem) { - fileTable.scrollRectToVisible(fileTable.getCellRect(i, 0, true)); + final int row = i; + SwingUtilities.invokeLater(() -> { + fileTable.scrollRectToVisible(fileTable.getCellRect(row, 0, true)); + }); } if (requestFocus) { fileTable.requestFocusInWindow(); @@ -3594,11 +3599,14 @@ public class FilePanelTab extends JPanel { int row = index % tableModel.briefRowsPerColumn; if (briefCurrentColumn < fileTable.getColumnCount()) { fileTable.setRowSelectionInterval(row, row); - scrollBriefCellToColumnStart(row, briefCurrentColumn); + final int r = row; + final int c = briefCurrentColumn; + SwingUtilities.invokeLater(() -> scrollBriefCellToColumnStart(r, c)); } } else { 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.requestFocusInWindow(); diff --git a/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java b/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java index 73b1ba3..c53481c 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java +++ b/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java @@ -2702,18 +2702,16 @@ public class MainWindow extends JFrame { // Load directory and then select item by name on the EDT 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 activePanel = chosen; updateActivePanelBorder(); - // After loading, select the file name - SwingUtilities.invokeLater(() -> { - FilePanelTab tab = chosen.getCurrentTab(); - if (tab != null) { - tab.selectItem(file.getName()); - tab.getFileTable().requestFocusInWindow(); - } - }); }); } diff --git a/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java b/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java index 3ea47fc..bfe18fe 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java @@ -332,9 +332,12 @@ public class SearchDialog extends JDialog { stopButton.setMaximumSize(new Dimension(120, 30)); stopButton.setEnabled(false); stopButton.addActionListener(e -> { + if (currentWorker != null) { + currentWorker.cancel(true); + } searching = false; stopButton.setEnabled(false); - statusLabel.setText("Cancelling..."); + statusLabel.setText("Canceled — found " + foundCount + " files"); }); 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 -> { - searching = false; - dispose(); + if (searching) { + 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); // F3 = view, F4 = edit when dialog is active @@ -556,7 +570,12 @@ public class SearchDialog extends JDialog { /** * Provede vyhledávání */ + private SwingWorker currentWorker; + private void performSearch() { + if (currentWorker != null && !currentWorker.isDone()) { + currentWorker.cancel(true); + } String namePat = ""; try { Object it = patternCombo.getEditor().getItem(); @@ -669,7 +688,7 @@ public class SearchDialog extends JDialog { statusProgressBar.setIndeterminate(true); // Spustit vyhledávání v samostatném vlákně - SwingWorker worker = new SwingWorker() { + currentWorker = new SwingWorker() { private String currentSearchPath = ""; @Override @@ -745,6 +764,10 @@ public class SearchDialog extends JDialog { statusProgressBar.setIndeterminate(false); statusProgressBar.setVisible(false); try { + if (isCancelled()) { + statusLabel.setText("Canceled — found " + foundCount + " files"); + return; + } get(); statusLabel.setText("Done — found " + foundCount + " files"); if (tableModel.getRowCount() == 0) { @@ -753,6 +776,8 @@ public class SearchDialog extends JDialog { "Result", JOptionPane.INFORMATION_MESSAGE); } + } catch (java.util.concurrent.CancellationException e) { + statusLabel.setText("Canceled — found " + foundCount + " files"); } catch (Exception e) { statusLabel.setText("Error"); JOptionPane.showMessageDialog(SearchDialog.this, @@ -763,7 +788,7 @@ public class SearchDialog extends JDialog { } }; - worker.execute(); + currentWorker.execute(); } /**