package com.kfmanager.ui; import javax.swing.*; import java.awt.*; import java.io.File; import java.util.ArrayList; import java.util.List; /** * Dialog for selecting a drive */ public class DriveSelector extends JDialog { private File selectedDrive = null; public DriveSelector(Frame parent) { super(parent, "Select drive", true); initComponents(); setSize(400, 300); setLocationRelativeTo(parent); } private void initComponents() { setLayout(new BorderLayout(10, 10)); ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Získat seznam dostupných disků File[] roots = File.listRoots(); List drives = new ArrayList<>(); for (File root : roots) { drives.add(new DriveInfo(root)); } // Seznam disků JList driveList = new JList<>(drives.toArray(new DriveInfo[0])); driveList.setFont(new Font("Monospaced", Font.PLAIN, 14)); driveList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Custom renderer to display drive information driveList.setCellRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof DriveInfo) { DriveInfo info = (DriveInfo) value; setText(info.getDisplayText()); } return this; } }); // Double-click pro výběr driveList.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent e) { if (e.getClickCount() == 2) { int index = driveList.locationToIndex(e.getPoint()); if (index >= 0) { selectedDrive = drives.get(index).getRoot(); dispose(); } } } }); // Enter pro výběr driveList.addKeyListener(new java.awt.event.KeyAdapter() { @Override public void keyPressed(java.awt.event.KeyEvent e) { if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) { int index = driveList.getSelectedIndex(); if (index >= 0) { selectedDrive = drives.get(index).getRoot(); dispose(); } } } }); JScrollPane scrollPane = new JScrollPane(driveList); add(scrollPane, BorderLayout.CENTER); // Panel s tlačítky JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JButton okButton = new JButton("OK"); okButton.addActionListener(e -> { int index = driveList.getSelectedIndex(); if (index >= 0) { selectedDrive = drives.get(index).getRoot(); dispose(); } else { JOptionPane.showMessageDialog(this, "Select a drive", "Drive selection", JOptionPane.INFORMATION_MESSAGE); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(e -> dispose()); buttonPanel.add(okButton); buttonPanel.add(cancelButton); add(buttonPanel, BorderLayout.SOUTH); // Automaticky vybrat první disk if (drives.size() > 0) { driveList.setSelectedIndex(0); } driveList.requestFocus(); } public File getSelectedDrive() { return selectedDrive; } /** * Pomocná třída pro informace o disku */ private static class DriveInfo { private final File root; public DriveInfo(File root) { this.root = root; } public File getRoot() { return root; } public String getDisplayText() { String path = root.getAbsolutePath(); long totalSpace = root.getTotalSpace(); long freeSpace = root.getFreeSpace(); long usedSpace = totalSpace - freeSpace; if (totalSpace > 0) { return String.format("%s %s / %s free", path, formatSize(usedSpace), formatSize(freeSpace)); } else { return path; } } private String formatSize(long size) { if (size < 1024) { return size + " B"; } else if (size < 1024L * 1024) { return String.format("%.1f KB", size / 1024.0); } else if (size < 1024L * 1024 * 1024) { return String.format("%.1f MB", size / (1024.0 * 1024.0)); } else if (size < 1024L * 1024 * 1024 * 1024) { return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0)); } else { return String.format("%.1f TB", size / (1024.0 * 1024.0 * 1024.0 * 1024.0)); } } } }