drive combo improved
This commit is contained in:
parent
7074a0d0ad
commit
04ad8605eb
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user