From 04ad8605eb63064566c21b95583f015c03993d0c Mon Sep 17 00:00:00 2001 From: Radek Davidek Date: Tue, 10 Feb 2026 20:07:26 +0100 Subject: [PATCH] drive combo improved --- .../java/cz/kamma/kfmanager/ui/FilePanel.java | 75 ++++++++----------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java b/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java index 7a84f2d..ab49dc5 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java @@ -4,6 +4,8 @@ import cz.kamma.kfmanager.MainApp; import cz.kamma.kfmanager.model.FileItem; import javax.swing.*; +import javax.swing.event.PopupMenuEvent; +import javax.swing.event.PopupMenuListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; @@ -90,41 +92,6 @@ public class FilePanel extends JPanel { // Allow the combo to receive focus so keyboard navigation and popup interaction work driveCombo.setFocusable(true); driveCombo.setToolTipText("Select drive"); - // renderer to show friendly name and path - driveCombo.setRenderer(new DefaultListCellRenderer() { - private final javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView(); - @Override - public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { - super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - if (value instanceof File f) { - String name = fsv.getSystemDisplayName(f); - if (name == null) name = ""; - name = name.trim(); - // Strip surrounding parentheses if present (e.g. "(C)") - if (name.startsWith("(") && name.endsWith(")") && name.length() > 1) { - name = name.substring(1, name.length() - 1).trim(); - } - String path = f.getAbsolutePath(); - String driveLabel; - // Prefer Windows-style drive letter like "C:" when available - if (path != null && path.length() >= 2 && path.charAt(1) == ':') { - driveLabel = path.substring(0, 2); - } else { - // Fall back to path or empty - driveLabel = path != null ? path : ""; - } - // Remove redundant drive-letter fragments from name (e.g. "C", "C:", "(C)") - if (!driveLabel.isEmpty() && !name.isEmpty()) { - // remove occurrences of driveLabel or driveLabel + ':' and stray parentheses - name = name.replace(driveLabel, "").replace(driveLabel + ":", "").replace("(", "").replace(")", "").trim(); - } - String text = driveLabel; - if (!name.isEmpty()) text = text + " " + name; - setText(text); - } - return this; - } - }); // Small label next to the combo to show drive info (label, capacity, free space) driveInfoLabel = new JLabel(); @@ -157,6 +124,30 @@ public class FilePanel extends JPanel { } } }); + + // Add popup listener to refresh drives when dropdown is opened + driveCombo.addPopupMenuListener(new PopupMenuListener() { + @Override + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { + populateDrives(); + } + + @Override + public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { + } + + @Override + public void popupMenuCanceled(PopupMenuEvent e) { + } + }); + + // Update info label on selection changes (visual selection only) + driveCombo.addItemListener(ev -> { + if (ev.getStateChange() == java.awt.event.ItemEvent.SELECTED) { + updateDriveInfoFromSelection(); + } + }); + // Compose a small left panel with the combo and info label JPanel leftPanel = new JPanel(); leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS)); @@ -453,6 +444,7 @@ public class FilePanel extends JPanel { } private void populateDrives() { + Object currentSelection = driveCombo.getSelectedItem(); ignoreComboActions = true; try { driveCombo.removeAllItems(); @@ -506,8 +498,10 @@ public class FilePanel extends JPanel { } catch (Exception ignore) {} } - // Initialize selection - if (driveCombo.getItemCount() > 0) { + // Restore selection if possible, otherwise use first item + if (currentSelection != null && driveSet.contains(currentSelection)) { + driveCombo.setSelectedItem(currentSelection); + } else if (driveCombo.getItemCount() > 0) { driveCombo.setSelectedIndex(0); } @@ -516,13 +510,6 @@ public class FilePanel extends JPanel { } finally { ignoreComboActions = false; } - - // Update info label on selection changes (visual selection only) - driveCombo.addItemListener(ev -> { - if (ev.getStateChange() == java.awt.event.ItemEvent.SELECTED) { - updateDriveInfoFromSelection(); - } - }); } /**