diff --git a/pom.xml b/pom.xml index e137126..0938060 100644 --- a/pom.xml +++ b/pom.xml @@ -15,8 +15,8 @@ UTF-8 - 11 - 11 + 21 + 21 @@ -41,10 +41,9 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.14.1 - 11 - 11 + 21 diff --git a/rewrite.yml b/rewrite.yml new file mode 100644 index 0000000..e03ac9b --- /dev/null +++ b/rewrite.yml @@ -0,0 +1,7 @@ +--- +type: specs.openrewrite.org/v1beta/recipe +name: cz.kamma.kfmanager.UpgradeToJava21 +displayName: Upgrade to Java 21 +description: Upgrade the project to Java 21 using OpenRewrite. +recipeList: + - org.openrewrite.java.migrate.UpgradeToJava21 diff --git a/src/main/java/cz/kamma/kfmanager/MainApp.java b/src/main/java/cz/kamma/kfmanager/MainApp.java index a4dbf29..1a99cbd 100644 --- a/src/main/java/cz/kamma/kfmanager/MainApp.java +++ b/src/main/java/cz/kamma/kfmanager/MainApp.java @@ -138,8 +138,7 @@ public class MainApp { private static void setupGlobalKeyNavigation() { Toolkit.getDefaultToolkit().addAWTEventListener(event -> { - if (event instanceof KeyEvent) { - KeyEvent ke = (KeyEvent) event; + if (event instanceof KeyEvent ke) { if (ke.getID() == KeyEvent.KEY_PRESSED) { int code = ke.getKeyCode(); if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) { @@ -166,16 +165,15 @@ public class MainApp { public static void applyReflectiveCaretColor(Container container) { if (container == null) return; for (Component c : container.getComponents()) { - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { Color bg = tc.getBackground(); if (bg != null) { double darkness = 1 - (0.299 * bg.getRed() + 0.587 * bg.getGreen() + 0.114 * bg.getBlue()) / 255; tc.setCaretColor(darkness >= 0.5 ? Color.WHITE : Color.BLACK); } } - if (c instanceof Container) { - applyReflectiveCaretColor((Container) c); + if (c instanceof Container container1) { + applyReflectiveCaretColor(container1); } } } diff --git a/src/main/java/cz/kamma/kfmanager/config/AppConfig.java b/src/main/java/cz/kamma/kfmanager/config/AppConfig.java index b8ea2dd..e9dd31f 100644 --- a/src/main/java/cz/kamma/kfmanager/config/AppConfig.java +++ b/src/main/java/cz/kamma/kfmanager/config/AppConfig.java @@ -466,7 +466,7 @@ public class AppConfig { if (c == null) { properties.remove("appearance.bg"); } else { - properties.setProperty("appearance.bg", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue())); + properties.setProperty("appearance.bg", "#%02x%02x%02x".formatted(c.getRed(), c.getGreen(), c.getBlue())); } } @@ -480,7 +480,7 @@ public class AppConfig { if (c == null) { properties.remove("appearance.selection"); } else { - properties.setProperty("appearance.selection", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue())); + properties.setProperty("appearance.selection", "#%02x%02x%02x".formatted(c.getRed(), c.getGreen(), c.getBlue())); } } @@ -494,7 +494,7 @@ public class AppConfig { if (c == null) { properties.remove("appearance.marked"); } else { - properties.setProperty("appearance.marked", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue())); + properties.setProperty("appearance.marked", "#%02x%02x%02x".formatted(c.getRed(), c.getGreen(), c.getBlue())); } } @@ -508,7 +508,7 @@ public class AppConfig { if (c == null) { properties.remove("appearance.folder"); } else { - properties.setProperty("appearance.folder", String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue())); + properties.setProperty("appearance.folder", "#%02x%02x%02x".formatted(c.getRed(), c.getGreen(), c.getBlue())); } } diff --git a/src/main/java/cz/kamma/kfmanager/model/FileItem.java b/src/main/java/cz/kamma/kfmanager/model/FileItem.java index 1de5794..3162438 100644 --- a/src/main/java/cz/kamma/kfmanager/model/FileItem.java +++ b/src/main/java/cz/kamma/kfmanager/model/FileItem.java @@ -118,11 +118,11 @@ public class FileItem { if (size < 1024) { return size + " B"; } else if (size < 1024 * 1024) { - return String.format("%.1f KB", size / 1024.0); + return "%.1f KB".formatted(size / 1024.0); } else if (size < 1024 * 1024 * 1024) { - return String.format("%.1f MB", size / (1024.0 * 1024.0)); + return "%.1f MB".formatted(size / (1024.0 * 1024.0)); } else { - return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0)); + return "%.1f GB".formatted(size / (1024.0 * 1024.0 * 1024.0)); } } } diff --git a/src/main/java/cz/kamma/kfmanager/ui/DriveSelector.java b/src/main/java/cz/kamma/kfmanager/ui/DriveSelector.java index 23cb347..bcc5d99 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/DriveSelector.java +++ b/src/main/java/cz/kamma/kfmanager/ui/DriveSelector.java @@ -44,8 +44,7 @@ public class DriveSelector extends JDialog { int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - if (value instanceof DriveInfo) { - DriveInfo info = (DriveInfo) value; + if (value instanceof DriveInfo info) { setText(info.getDisplayText()); } @@ -142,10 +141,10 @@ public class DriveSelector extends JDialog { long usedSpace = totalSpace - freeSpace; if (totalSpace > 0) { - return String.format("%s %s / %s free", - path, - formatSize(usedSpace), - formatSize(freeSpace)); + return "%s %s / %s free".formatted( + path, + formatSize(usedSpace), + formatSize(freeSpace)); } else { return path; } @@ -155,13 +154,13 @@ public class DriveSelector extends JDialog { if (size < 1024) { return size + " B"; } else if (size < 1024L * 1024) { - return String.format("%.1f KB", size / 1024.0); + return "%.1f KB".formatted(size / 1024.0); } else if (size < 1024L * 1024 * 1024) { - return String.format("%.1f MB", size / (1024.0 * 1024.0)); + return "%.1f MB".formatted(size / (1024.0 * 1024.0)); } else if (size < 1024L * 1024 * 1024 * 1024) { - return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0)); + return "%.1f GB".formatted(size / (1024.0 * 1024.0 * 1024.0)); } else { - return String.format("%.1f TB", size / (1024.0 * 1024.0 * 1024.0 * 1024.0)); + return "%.1f TB".formatted(size / (1024.0 * 1024.0 * 1024.0 * 1024.0)); } } } diff --git a/src/main/java/cz/kamma/kfmanager/ui/FileChooserUtils.java b/src/main/java/cz/kamma/kfmanager/ui/FileChooserUtils.java index c8c9d04..763dafa 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FileChooserUtils.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FileChooserUtils.java @@ -35,8 +35,8 @@ public class FileChooserUtils { public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && chooser.isShowing()) { Window window = SwingUtilities.getWindowAncestor(chooser); - if (window instanceof JDialog) { - dialogRef[0] = (JDialog) window; + if (window instanceof JDialog dialog) { + dialogRef[0] = dialog; // Restore size and position int w = config.getFileChooserWidth(); int h = config.getFileChooserHeight(); diff --git a/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java b/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java index 8d33b50..1c12d75 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FileEditor.java @@ -137,7 +137,7 @@ public class FileEditor extends JFrame { } else if (config != null) { java.util.List hist = config.getContentSearchHistory(); if (hist != null && !hist.isEmpty()) { - lastSearchValue = hist.get(0); + lastSearchValue = hist.getFirst(); searchField.setText(lastSearchValue); } } @@ -243,7 +243,7 @@ public class FileEditor extends JFrame { if (config == null || text == null || text.isEmpty()) return; java.util.List hist = new java.util.ArrayList<>(config.getContentSearchHistory()); hist.remove(text); - hist.add(0, text); + hist.addFirst(text); if (hist.size() > 20) hist = hist.subList(0, 20); config.saveContentSearchHistory(hist); config.saveConfig(); @@ -405,14 +405,13 @@ public class FileEditor extends JFrame { if (c instanceof JLabel) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof Container) { - applyRecursiveColors((Container) c, bg, dark); + if (c instanceof Container container1) { + applyRecursiveColors(container1, bg, dark); } } } @@ -717,7 +716,7 @@ public class FileEditor extends JFrame { int bytesPerLine = 16; for (int i = 0; i < fileBytes.length; i += bytesPerLine) { long displayOffset = baseOffset + i; - sb.append(String.format("%08X ", displayOffset)); + sb.append("%08X ".formatted(displayOffset)); // hex bytes for (int j = 0; j < bytesPerLine; j++) { int idx = i + j; @@ -725,7 +724,7 @@ public class FileEditor extends JFrame { int b = fileBytes[idx] & 0xFF; int pos = sb.length(); byteTextOffsets.add(pos); - sb.append(String.format("%02X", b)); + sb.append("%02X".formatted(b)); } else { // placeholder for missing byte sb.append(" "); @@ -762,7 +761,7 @@ public class FileEditor extends JFrame { SwingUtilities.invokeLater(() -> { try { if (byteTextOffsets != null && !byteTextOffsets.isEmpty()) { - int pos = Math.max(0, byteTextOffsets.get(0)); + int pos = Math.max(0, byteTextOffsets.getFirst()); textArea.setCaretPosition(pos); // ensure the caret is visible at top-left Rectangle vis = textArea.getVisibleRect(); @@ -789,11 +788,11 @@ public class FileEditor extends JFrame { if (pageOffsetLabel != null) { long start = pageOffsetBytes; long end = pageOffsetBytes + page.length - 1; - pageOffsetLabel.setText(String.format("Offset: 0x%08X - 0x%08X", start, end)); + pageOffsetLabel.setText("Offset: 0x%08X - 0x%08X".formatted(start, end)); } // Position caret at first byte hex position for consistent mapping if (byteTextOffsets != null && !byteTextOffsets.isEmpty()) { - int pos = byteTextOffsets.get(0); + int pos = byteTextOffsets.getFirst(); textArea.setCaretPosition(Math.max(0, pos)); } else { textArea.setCaretPosition(0); @@ -987,13 +986,13 @@ public class FileEditor extends JFrame { label.setHorizontalAlignment(JLabel.CENTER); scrollPane.setViewportView(label); - String statusText = String.format("Image: %d x %d px", imgW, imgH); + String statusText = "Image: %d x %d px".formatted(imgW, imgH); if (scaled) statusText += " (scaled)"; statusPosLabel.setText(statusText); - String labelText = String.format("Size: %s", FileItem.formatSize(file.length())); + String labelText = "Size: %s".formatted(FileItem.formatSize(file.length())); if (imageFiles.size() > 1) { - labelText += String.format(" [%d / %d]", currentImageIndex + 1, imageFiles.size()); + labelText += " [%d / %d]".formatted(currentImageIndex + 1, imageFiles.size()); } statusSelLabel.setText(labelText); @@ -1217,7 +1216,7 @@ public class FileEditor extends JFrame { root.getActionMap().put("press", new AbstractAction() { @Override public void actionPerformed(java.awt.event.ActionEvent e) { java.awt.Component c = dlg.getFocusOwner(); - if (c instanceof JButton) ((JButton)c).doClick(); + if (c instanceof JButton button) button.doClick(); } }); @@ -1260,7 +1259,7 @@ public class FileEditor extends JFrame { long byteIndex = mapCaretToByteIndex(caret); long totalBytes = (raf != null && fileLength > 0) ? fileLength : (fileBytes != null ? fileBytes.length : 0); int percent = totalBytes > 0 ? (int)((byteIndex * 100L) / totalBytes) : 0; - statusPosLabel.setText(String.format("Offset %d/%d (%d%%)", byteIndex, totalBytes, percent)); + statusPosLabel.setText("Offset %d/%d (%d%%)".formatted(byteIndex, totalBytes, percent)); int selStart = textArea.getSelectionStart(); int selEnd = textArea.getSelectionEnd(); @@ -1271,7 +1270,7 @@ public class FileEditor extends JFrame { selBytes = Math.max(0L, b2 - b1 + 1L); } if (selBytes > 0) { - statusSelLabel.setText(String.format("Selected: %d bytes", selBytes)); + statusSelLabel.setText("Selected: %d bytes".formatted(selBytes)); } else { statusSelLabel.setText(" "); } @@ -1289,7 +1288,7 @@ public class FileEditor extends JFrame { col = caret - lineStart + 1; } int percent = total > 0 ? (int) ((caret * 100L) / total) : 0; - statusPosLabel.setText(String.format("Ln %d, Col %d | Offset %d/%d (%d%%)", line, col, caret, total, percent)); + statusPosLabel.setText("Ln %d, Col %d | Offset %d/%d (%d%%)".formatted(line, col, caret, total, percent)); int selStart = textArea.getSelectionStart(); int selEnd = textArea.getSelectionEnd(); @@ -1304,7 +1303,7 @@ public class FileEditor extends JFrame { } } if (selChars > 0) { - statusSelLabel.setText(String.format("Selected: %d chars / %d bytes", selChars, selBytes)); + statusSelLabel.setText("Selected: %d chars / %d bytes".formatted(selChars, selBytes)); } else { statusSelLabel.setText(" "); } diff --git a/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java b/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java index d416020..4d40ee1 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FilePanel.java @@ -96,8 +96,7 @@ public class FilePanel extends JPanel { @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) { - File f = (File) value; + if (value instanceof File f) { String name = fsv.getSystemDisplayName(f); if (name == null) name = ""; name = name.trim(); @@ -141,16 +140,14 @@ public class FilePanel extends JPanel { // Determine if we should trigger directory load. // We want to skip arrow keys and only react to Mouse or Enter. java.awt.AWTEvent currentEvent = java.awt.EventQueue.getCurrentEvent(); - if (currentEvent instanceof java.awt.event.KeyEvent) { - java.awt.event.KeyEvent ke = (java.awt.event.KeyEvent) currentEvent; + if (currentEvent instanceof java.awt.event.KeyEvent ke) { if (ke.getKeyCode() != java.awt.event.KeyEvent.VK_ENTER) { return; } } Object selObj = driveCombo.getSelectedItem(); - if (selObj instanceof File) { - File sel = (File) selObj; + if (selObj instanceof File sel) { FilePanelTab currentTab = getCurrentTab(); if (currentTab != null) { currentTab.loadDirectory(sel); @@ -237,8 +234,8 @@ public class FilePanel extends JPanel { }); } - if (comp instanceof Container) { - for (Component child : ((Container) comp).getComponents()) { + if (comp instanceof Container container) { + for (Component child : container.getComponents()) { addMouseListenerToComponents(child); } } @@ -362,8 +359,8 @@ public class FilePanel extends JPanel { // propagate to existing tabs for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).setAppConfig(cfg); + if (c instanceof FilePanelTab tab) { + tab.setAppConfig(cfg); } } } @@ -372,8 +369,7 @@ public class FilePanel extends JPanel { java.util.List paths = new java.util.ArrayList<>(); for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - FilePanelTab t = (FilePanelTab) c; + if (c instanceof FilePanelTab t) { File dir = t.getCurrentDirectory(); paths.add(dir != null ? dir.getAbsolutePath() : System.getProperty("user.home")); } @@ -385,8 +381,7 @@ public class FilePanel extends JPanel { java.util.List modes = new java.util.ArrayList<>(); for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - FilePanelTab t = (FilePanelTab) c; + if (c instanceof FilePanelTab t) { modes.add(t.getViewMode() != null ? t.getViewMode().name() : ViewMode.FULL.name()); } } @@ -397,8 +392,8 @@ public class FilePanel extends JPanel { java.util.List items = new java.util.ArrayList<>(); for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - cz.kamma.kfmanager.model.FileItem focused = ((FilePanelTab) c).getFocusedItem(); + if (c instanceof FilePanelTab tab) { + cz.kamma.kfmanager.model.FileItem focused = tab.getFocusedItem(); items.add(focused != null ? focused.getName() : null); } } @@ -559,8 +554,8 @@ public class FilePanel extends JPanel { // Propagate active state to all tabs for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).setActive(active); + if (c instanceof FilePanelTab tab) { + tab.setActive(active); } } @@ -576,8 +571,7 @@ public class FilePanel extends JPanel { int selectedIndex = tabbedPane.getSelectedIndex(); for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - FilePanelTab tab = (FilePanelTab) c; + if (c instanceof FilePanelTab tab) { File dir = tab.getCurrentDirectory(); String title = getTabTitle(dir != null ? dir.getAbsolutePath() : ""); @@ -648,8 +642,8 @@ public class FilePanel extends JPanel { private void updateDriveInfoFromSelection() { Object sel = driveCombo.getSelectedItem(); - if (sel instanceof File) { - updateDriveInfo((File) sel); + if (sel instanceof File file) { + updateDriveInfo(file); } else { driveInfoLabel.setText(""); } @@ -678,7 +672,7 @@ public class FilePanel extends JPanel { long free = drive.getUsableSpace(); String freeGb = formatGbShort(free); String totalGb = formatGbShort(total); - String info = String.format("%s %s GB free of %s GB", name, freeGb, totalGb); + String info = "%s %s GB free of %s GB".formatted(name, freeGb, totalGb); driveInfoLabel.setText(info); } catch (Exception ex) { driveInfoLabel.setText(""); @@ -688,7 +682,7 @@ public class FilePanel extends JPanel { private static String formatGbShort(long bytes) { if (bytes <= 0) return "0"; // fallback double gb = bytes / 1024.0 / 1024.0 / 1024.0; - return String.format("%.1f", gb); + return "%.1f".formatted(gb); } /** @@ -821,8 +815,8 @@ public class FilePanel extends JPanel { // Apply to all existing tabs for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).applyGlobalFont(font); + if (c instanceof FilePanelTab tab) { + tab.applyGlobalFont(font); } } } @@ -832,8 +826,8 @@ public class FilePanel extends JPanel { updateComponentBackground(this, bg); for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).applyBackgroundColor(bg); + if (c instanceof FilePanelTab tab) { + tab.applyBackgroundColor(bg); } } } @@ -849,8 +843,8 @@ public class FilePanel extends JPanel { if (c instanceof JLabel || c instanceof JCheckBox || c instanceof JRadioButton || c instanceof JButton) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -864,8 +858,8 @@ public class FilePanel extends JPanel { public void applySelectionColor(Color sel) { for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).applySelectionColor(sel); + if (c instanceof FilePanelTab tab) { + tab.applySelectionColor(sel); } } } @@ -873,8 +867,8 @@ public class FilePanel extends JPanel { public void applyMarkedColor(Color mark) { for (int i = 0; i < tabbedPane.getTabCount(); i++) { Component c = tabbedPane.getComponentAt(i); - if (c instanceof FilePanelTab) { - ((FilePanelTab) c).applyMarkedColor(mark); + if (c instanceof FilePanelTab tab) { + tab.applyMarkedColor(mark); } } } diff --git a/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java b/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java index e58fb40..f8fa9a7 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java +++ b/src/main/java/cz/kamma/kfmanager/ui/FilePanelTab.java @@ -85,8 +85,7 @@ public class FilePanelTab extends JPanel { // If already editing, special logic: select filename without extension if (fileTable.isEditing()) { Component ed = fileTable.getEditorComponent(); - if (ed instanceof JTextField) { - JTextField tf = (JTextField) ed; + if (ed instanceof JTextField tf) { // Ensure caret is visible Color bg = tf.getBackground(); @@ -142,8 +141,7 @@ public class FilePanelTab extends JPanel { boolean started = fileTable.editCellAt(selRow, editCol); if (started) { Component ed = fileTable.getEditorComponent(); - if (ed instanceof JTextField) { - JTextField tf = (JTextField) ed; + if (ed instanceof JTextField tf) { // Ensure caret is visible Color bg = tf.getBackground(); @@ -226,8 +224,7 @@ public class FilePanelTab extends JPanel { c instanceof JTabbedPane || c instanceof JSplitPane || c instanceof JList || c instanceof JComboBox || c instanceof JTable || c instanceof JButton) { c.setBackground(bg); - if (c instanceof JTable) { - JTable t = (JTable) c; + if (c instanceof JTable t) { if (t.getTableHeader() != null) { t.getTableHeader().setBackground(bg); t.getTableHeader().setForeground(dark ? Color.WHITE : Color.BLACK); @@ -238,14 +235,13 @@ public class FilePanelTab extends JPanel { c instanceof JButton || c instanceof JComboBox || c instanceof JList) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -1648,14 +1644,14 @@ public class FilePanelTab extends JPanel { dialog.setVisible(true); Object result = pane.getValue(); - int res = (result instanceof Integer) ? (Integer) result : JOptionPane.CLOSED_OPTION; + int res = (result instanceof Integer i) ? i : JOptionPane.CLOSED_OPTION; if (res == JOptionPane.YES_OPTION) { java.util.List toDelete = new java.util.ArrayList<>(); toDelete.add(item); final int rememberedIndex = getFocusedItemIndex(); Window parentWindow = SwingUtilities.getWindowAncestor(FilePanelTab.this); - ProgressDialog progressDialog = new ProgressDialog(parentWindow instanceof Frame ? (Frame)parentWindow : null, "Deleting"); + ProgressDialog progressDialog = new ProgressDialog(parentWindow instanceof Frame f ? f : null, "Deleting"); new Thread(() -> { try { @@ -2096,7 +2092,7 @@ public class FilePanelTab extends JPanel { File targetDir = getCurrentDirectory(); Window parentWindow = SwingUtilities.getWindowAncestor(this); String titleName = action == ClipboardService.ClipboardAction.CUT ? "Moving" : "Copying"; - ProgressDialog progressDialog = new ProgressDialog(parentWindow instanceof Frame ? (Frame)parentWindow : null, titleName); + ProgressDialog progressDialog = new ProgressDialog(parentWindow instanceof Frame f ? f : null, titleName); new Thread(() -> { try { @@ -2122,11 +2118,19 @@ public class FilePanelTab extends JPanel { try { SwingUtilities.invokeAndWait(() -> { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); - String message = String.format( - "File already exists: %s\n\n" + - "Source file:\n Size: %s\n Modified: %s\n\n" + - "Existing file:\n Size: %s\n Modified: %s\n\n" + - "Overwrite?", + String message = ( + """ + File already exists: %s + + Source file: + Size: %s + Modified: %s + + Existing file: + Size: %s + Modified: %s + + Overwrite?""").formatted( destination.getName(), FileItem.formatSize(source.length()), sdf.format(new Date(source.lastModified())), @@ -2201,8 +2205,8 @@ public class FilePanelTab extends JPanel { progressDialog.dispose(); loadDirectory(targetDir, false); if (!itemsToPaste.isEmpty()) { - String nameToSelect = itemsToPaste.get(0).getName(); - File firstSource = itemsToPaste.get(0).getFile(); + String nameToSelect = itemsToPaste.getFirst().getName(); + File firstSource = itemsToPaste.getFirst().getFile(); // Check if we were copying within the same directory - if so, it was renamed to copy-of-... if (action == ClipboardService.ClipboardAction.COPY && firstSource.getParentFile() != null && @@ -2291,8 +2295,7 @@ public class FilePanelTab extends JPanel { Component parent = fileTable.getParent(); if (parent instanceof JViewport) { Component scroll = parent.getParent(); - if (scroll instanceof JScrollPane) { - JScrollPane sp = (JScrollPane) scroll; + if (scroll instanceof JScrollPane sp) { sp.setColumnHeaderView(mode == ViewMode.BRIEF ? null : fileTable.getTableHeader()); } } @@ -2675,8 +2678,8 @@ public class FilePanelTab extends JPanel { String newStatus; if (markedCount > 0) { - newStatus = String.format(" Selected: %d files, %d directories (%s)", - fileCount, dirCount, FileItem.formatSize(totalSize)); + newStatus = " Selected: %d files, %d directories (%s)".formatted( + fileCount, dirCount, FileItem.formatSize(totalSize)); } else { int selectedRow = fileTable.getSelectedRow(); if (selectedRow >= 0) { @@ -2690,20 +2693,20 @@ public class FilePanelTab extends JPanel { if (item != null && !item.getName().equals("..")) { if (item.isDirectory()) { // Always display directory names in square brackets - newStatus = String.format(" [%s] | %s", - item.getName(), - item.getFormattedDate()); + newStatus = " [%s] | %s".formatted( + item.getName(), + item.getFormattedDate()); } else { - newStatus = String.format(" %s | %s | %s", - item.getName(), - FileItem.formatSize(item.getSize()), - item.getFormattedDate()); + newStatus = " %s | %s | %s".formatted( + item.getName(), + FileItem.formatSize(item.getSize()), + item.getFormattedDate()); } } else { - newStatus = String.format(" Items: %d", tableModel.items.size()); + newStatus = " Items: %d".formatted(tableModel.items.size()); } } else { - newStatus = String.format(" Items: %d", tableModel.items.size()); + newStatus = " Items: %d".formatted(tableModel.items.size()); } } @@ -2930,8 +2933,7 @@ public class FilePanelTab extends JPanel { boolean isSelected, boolean hasFocus, int row, int column) { java.awt.Component c = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); - if (c instanceof javax.swing.JLabel) { - javax.swing.JLabel lbl = (javax.swing.JLabel) c; + if (c instanceof javax.swing.JLabel lbl) { String txt = value != null ? value.toString() : ""; if (sortColumn == column && fileTable.getTableHeader().isVisible()) { String arrow = sortAscending ? " ▲" : " ▼"; diff --git a/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java b/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java index cd8c23c..b267663 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java +++ b/src/main/java/cz/kamma/kfmanager/ui/MainWindow.java @@ -312,8 +312,7 @@ public class MainWindow extends JFrame { // Handle the editor component (usually a JTextField) Component editorComp = commandLine.getEditor().getEditorComponent(); - if (editorComp instanceof JTextField) { - JTextField tf = (JTextField) editorComp; + if (editorComp instanceof JTextField tf) { tf.setFocusTraversalKeysEnabled(false); tf.putClientProperty("JTextField.selectAllOnFocus", Boolean.FALSE); tf.addActionListener(e -> executeCommand(tf.getText())); @@ -432,7 +431,7 @@ public class MainWindow extends JFrame { try { List files = (List) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); if (files != null && !files.isEmpty()) { - showAddToolbarShortcutDialog(files.get(0)); + showAddToolbarShortcutDialog(files.getFirst()); } return true; } catch (Exception e) { @@ -1014,8 +1013,7 @@ public class MainWindow extends JFrame { // Update command line colors if (commandLine != null) { Component ed = commandLine.getEditor().getEditorComponent(); - if (ed instanceof JTextField) { - JTextField tf = (JTextField) ed; + if (ed instanceof JTextField tf) { tf.setBackground(bg); boolean dark = isDark(bg); tf.setForeground(dark ? Color.WHITE : Color.BLACK); @@ -1037,8 +1035,7 @@ public class MainWindow extends JFrame { // Apply selection color to command line editor for selection if (commandLine != null) { Component ed = commandLine.getEditor().getEditorComponent(); - if (ed instanceof JTextField) { - JTextField tf = (JTextField) ed; + if (ed instanceof JTextField tf) { tf.setSelectionColor(sel); Color fieldBg = tf.getBackground(); boolean darkField = isDark(fieldBg); @@ -1082,8 +1079,7 @@ public class MainWindow extends JFrame { c instanceof JTabbedPane || c instanceof JButton || c instanceof JSplitPane || c instanceof JList || c instanceof JComboBox || c instanceof JTable) { c.setBackground(bg); - if (c instanceof JTable) { - JTable t = (JTable) c; + if (c instanceof JTable t) { if (t.getTableHeader() != null) { t.getTableHeader().setBackground(bg); t.getTableHeader().setForeground(dark ? Color.WHITE : Color.BLACK); @@ -1094,8 +1090,7 @@ public class MainWindow extends JFrame { c instanceof JButton || c instanceof JComboBox || c instanceof JList) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -1103,11 +1098,9 @@ public class MainWindow extends JFrame { tc.setSelectionColor(selColor); } } - if (c instanceof JComboBox) { - JComboBox cb = (JComboBox) c; + if (c instanceof JComboBox cb) { Component ed = cb.getEditor().getEditorComponent(); - if (ed instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) ed; + if (ed instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -1116,8 +1109,8 @@ public class MainWindow extends JFrame { } } } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -1611,13 +1604,13 @@ public class MainWindow extends JFrame { FilePanel sourcePanel = activePanel; int result = showConfirmWithBackground( - String.format("Copy %d items to:\n%s", selectedItems.size(), targetDir.getAbsolutePath()), + "Copy %d items to:\n%s".formatted(selectedItems.size(), targetDir.getAbsolutePath()), "Copy"); if (result == 0 || result == 1) { boolean background = (result == 1); if (background) { - addOperationToQueue("Copy", String.format("Copy %d items to %s", selectedItems.size(), targetDir.getName()), + addOperationToQueue("Copy", "Copy %d items to %s".formatted(selectedItems.size(), targetDir.getName()), (cb) -> FileOperations.copy(selectedItems, targetDir, cb), () -> sourcePanel.unselectAll(), targetPanel); } else { performFileOperation((callback) -> { @@ -1649,13 +1642,13 @@ public class MainWindow extends JFrame { File targetDir = targetPanel.getCurrentDirectory(); int result = showConfirmWithBackground( - String.format("Move %d items to:\n%s", selectedItems.size(), targetDir.getAbsolutePath()), + "Move %d items to:\n%s".formatted(selectedItems.size(), targetDir.getAbsolutePath()), "Move"); if (result == 0 || result == 1) { boolean background = (result == 1); if (background) { - addOperationToQueue("Move", String.format("Move %d items to %s", selectedItems.size(), targetDir.getName()), + addOperationToQueue("Move", "Move %d items to %s".formatted(selectedItems.size(), targetDir.getName()), (cb) -> FileOperations.move(selectedItems, targetDir, cb), activePanel, targetPanel); } else { performFileOperation((callback) -> { @@ -1736,7 +1729,7 @@ public class MainWindow extends JFrame { if (result == 0 || result == 1) { boolean background = (result == 1); if (background) { - addOperationToQueue("Delete", String.format("Delete %d items", selectedItems.size()), + addOperationToQueue("Delete", "Delete %d items".formatted(selectedItems.size()), (cb) -> FileOperations.delete(selectedItems, cb), activePanel); } else { performFileOperation((callback) -> { @@ -1776,7 +1769,7 @@ public class MainWindow extends JFrame { String defaultName; if (selectedItems.size() == 1) { - defaultName = selectedItems.get(0).getName(); + defaultName = selectedItems.getFirst().getName(); } else { defaultName = activePanel.getCurrentDirectory().getName(); if (defaultName == null || defaultName.isEmpty() || defaultName.equals("/") || defaultName.endsWith(":")) { @@ -1822,13 +1815,13 @@ public class MainWindow extends JFrame { final File finalTargetZip = targetZip; final FilePanel sourcePanel = activePanel; int result = showConfirmWithBackground( - String.format("Zip %d items to:\n%s", selectedItems.size(), targetZip.getAbsolutePath()), + "Zip %d items to:\n%s".formatted(selectedItems.size(), targetZip.getAbsolutePath()), "Zip"); if (result == 0 || result == 1) { boolean background = (result == 1); if (background) { - addOperationToQueue("Zip", String.format("Zip %d items to %s", selectedItems.size(), finalTargetZip.getName()), + addOperationToQueue("Zip", "Zip %d items to %s".formatted(selectedItems.size(), finalTargetZip.getName()), (cb) -> FileOperations.zip(selectedItems, finalTargetZip, cb), () -> sourcePanel.unselectAll(), targetPanel); } else { performFileOperation((callback) -> { @@ -1854,7 +1847,7 @@ public class MainWindow extends JFrame { return; } - File archiveFile = selectedItems.get(0).getFile(); + File archiveFile = selectedItems.getFirst().getFile(); if (!FileOperations.isArchiveFile(archiveFile)) { JOptionPane.showMessageDialog(this, "Selected file is not a supported archive", @@ -1869,13 +1862,13 @@ public class MainWindow extends JFrame { final FilePanel sourcePanel = activePanel; int result = showConfirmWithBackground( - String.format("Extract %s to:\n%s", archiveFile.getName(), targetDir.getAbsolutePath()), + "Extract %s to:\n%s".formatted(archiveFile.getName(), targetDir.getAbsolutePath()), "Extract archive"); if (result == 0 || result == 1) { boolean background = (result == 1); if (background) { - addOperationToQueue("Extract", String.format("Extract %s to %s", archiveFile.getName(), targetDir.getName()), + addOperationToQueue("Extract", "Extract %s to %s".formatted(archiveFile.getName(), targetDir.getName()), (cb) -> FileOperations.extractArchive(archiveFile, targetDir, cb), () -> sourcePanel.unselectAll(), targetPanel); } else { performFileOperation((callback) -> { @@ -1908,7 +1901,7 @@ public class MainWindow extends JFrame { requestFocusInActivePanel(); return; } - FileItem item = selectedItems.get(0); + FileItem item = selectedItems.getFirst(); String newName = JOptionPane.showInputDialog(this, "New name:", item.getName()); @@ -2101,7 +2094,7 @@ public class MainWindow extends JFrame { return; } - FileItem item = selectedItems.get(0); + FileItem item = selectedItems.getFirst(); if (item.isDirectory() || item.getName().equals("..")) { return; } @@ -2129,7 +2122,7 @@ public class MainWindow extends JFrame { return; } - FileItem item = selectedItems.get(0); + FileItem item = selectedItems.getFirst(); if (item.isDirectory() || item.getName().equals("..")) { return; } @@ -2177,14 +2170,14 @@ public class MainWindow extends JFrame { File rightFile = null; if (leftSelection.size() == 1) { - leftFile = leftSelection.get(0).getFile(); + leftFile = leftSelection.getFirst().getFile(); } else if (leftSelection.size() > 1) { JOptionPane.showMessageDialog(this, "Please select only one file in the left panel.", "Compare Files", JOptionPane.WARNING_MESSAGE); return; } if (rightSelection.size() == 1) { - rightFile = rightSelection.get(0).getFile(); + rightFile = rightSelection.getFirst().getFile(); } else if (rightSelection.size() > 1) { JOptionPane.showMessageDialog(this, "Please select only one file in the right panel.", "Compare Files", JOptionPane.WARNING_MESSAGE); return; @@ -2424,8 +2417,8 @@ public class MainWindow extends JFrame { // Clear after execution and return focus Component editorComp = commandLine.getEditor().getEditorComponent(); - if (editorComp instanceof JTextField) { - ((JTextField) editorComp).setText(""); + if (editorComp instanceof JTextField field) { + field.setText(""); } else { commandLine.setSelectedItem(""); } @@ -2540,23 +2533,26 @@ public class MainWindow extends JFrame { */ private void showAboutDialog() { JOptionPane.showMessageDialog(this, - "KF File Manager 1.0\n\n" + - "Two-panel file manager\n" + - "Java 11\n\n" + - "Keyboard shortcuts:\n" + - "F5 - Copy\n" + - "Alt+F5 - Zip\n" + - "Alt+F9 - Unzip\n" + - "F6 - Move\n" + - "F7 - New directory\n" + - "F8 - Delete\n" + - "F9 - Open terminal\n" + - "Shift+F6 - Rename\n" + - "TAB - Switch panel\n" + - "Ctrl+F - Search\n" + - "Alt+O - Settings\n" + - "Enter - Open directory\n" + - "Backspace - Parent directory", + """ + KF File Manager 1.0 + + Two-panel file manager + Java 11 + + Keyboard shortcuts: + F5 - Copy + Alt+F5 - Zip + Alt+F9 - Unzip + F6 - Move + F7 - New directory + F8 - Delete + F9 - Open terminal + Shift+F6 - Rename + TAB - Switch panel + Ctrl+F - Search + Alt+O - Settings + Enter - Open directory + Backspace - Parent directory""", "About", JOptionPane.INFORMATION_MESSAGE); requestFocusInActivePanel(); @@ -2608,11 +2604,19 @@ public class MainWindow extends JFrame { try { SwingUtilities.invokeAndWait(() -> { SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); - String message = String.format( - "File already exists: %s\n\n" + - "Source file:\n Size: %s\n Modified: %s\n\n" + - "Existing file:\n Size: %s\n Modified: %s\n\n" + - "Overwrite?", + String message = ( + """ + File already exists: %s + + Source file: + Size: %s + Modified: %s + + Existing file: + Size: %s + Modified: %s + + Overwrite?""").formatted( destination.getName(), FileItem.formatSize(source.length()), sdf.format(new Date(source.lastModified())), @@ -2652,9 +2656,11 @@ public class MainWindow extends JFrame { final FileOperations.SymlinkResponse[] result = new FileOperations.SymlinkResponse[1]; try { SwingUtilities.invokeAndWait(() -> { - String message = String.format( - "Symbolic link encountered: %s\n\n" + - "What do you want to do?", + String message = ( + """ + Symbolic link encountered: %s + + What do you want to do?""").formatted( symlink.getAbsolutePath() ); diff --git a/src/main/java/cz/kamma/kfmanager/ui/OperationQueueDialog.java b/src/main/java/cz/kamma/kfmanager/ui/OperationQueueDialog.java index c54495b..d4999f9 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/OperationQueueDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/OperationQueueDialog.java @@ -171,8 +171,7 @@ public class OperationQueueDialog extends JDialog { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - if (value instanceof QueuedTask) { - QueuedTask task = (QueuedTask) value; + if (value instanceof QueuedTask task) { if (task.getStatus() == FileOperationQueue.OperationStatus.FAILED) { progressBar.setIndeterminate(false); progressBar.setValue(0); diff --git a/src/main/java/cz/kamma/kfmanager/ui/ProgressDialog.java b/src/main/java/cz/kamma/kfmanager/ui/ProgressDialog.java index 64ed8eb..62c9aaf 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/ProgressDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/ProgressDialog.java @@ -184,7 +184,7 @@ public class ProgressDialog extends JDialog { if (bytes < 1024) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(1024)); char pre = "KMGTPE".charAt(exp - 1); - return String.format("%.1f %cB", bytes / Math.pow(1024, exp), pre); + return "%.1f %cB".formatted(bytes / Math.pow(1024, exp), pre); } private void checkState() { diff --git a/src/main/java/cz/kamma/kfmanager/ui/PropertiesDialog.java b/src/main/java/cz/kamma/kfmanager/ui/PropertiesDialog.java index e28214b..da6162b 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/PropertiesDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/PropertiesDialog.java @@ -264,7 +264,7 @@ public class PropertiesDialog extends JDialog { if (permChecks[2][1].isSelected()) octal += 0002; if (permChecks[2][2].isSelected()) octal += 0001; - octalField.setText(String.format("%03o", octal)); + octalField.setText("%03o".formatted(octal)); StringBuilder sb = new StringBuilder(); sb.append(file.isDirectory() ? 'd' : '-'); diff --git a/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java b/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java index a054597..725c19c 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/SearchDialog.java @@ -80,8 +80,7 @@ public class SearchDialog extends JDialog { c instanceof JTabbedPane || c instanceof JButton || c instanceof JSplitPane || c instanceof JList || c instanceof JComboBox || c instanceof JTable) { c.setBackground(bg); - if (c instanceof JTable) { - JTable t = (JTable) c; + if (c instanceof JTable t) { if (t.getTableHeader() != null) { t.getTableHeader().setBackground(bg); t.getTableHeader().setForeground(dark ? Color.WHITE : Color.BLACK); @@ -92,8 +91,7 @@ public class SearchDialog extends JDialog { c instanceof JButton || c instanceof JComboBox || c instanceof JList) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -101,11 +99,9 @@ public class SearchDialog extends JDialog { tc.setSelectionColor(selColor); } } - if (c instanceof JComboBox) { - JComboBox cb = (JComboBox) c; + if (c instanceof JComboBox cb) { Component ed = cb.getEditor().getEditorComponent(); - if (ed instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) ed; + if (ed instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -114,8 +110,8 @@ public class SearchDialog extends JDialog { } } } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -309,8 +305,7 @@ public class SearchDialog extends JDialog { // Bind Enter on the combo editor component so selecting an item from the // popup does not automatically trigger search until user confirms. java.awt.Component editorComp = patternCombo.getEditor().getEditorComponent(); - if (editorComp instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) editorComp; + if (editorComp instanceof javax.swing.text.JTextComponent tc) { tc.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "confirmSearch"); tc.getActionMap().put("confirmSearch", new AbstractAction() { @Override @@ -335,8 +330,7 @@ public class SearchDialog extends JDialog { // Same explicit-Enter behavior for content text pattern combo try { java.awt.Component contentEditorComp = contentPatternCombo.getEditor().getEditorComponent(); - if (contentEditorComp instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc2 = (javax.swing.text.JTextComponent) contentEditorComp; + if (contentEditorComp instanceof javax.swing.text.JTextComponent tc2) { tc2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "confirmSearchContent"); tc2.getActionMap().put("confirmSearchContent", new AbstractAction() { @Override @@ -365,8 +359,8 @@ public class SearchDialog extends JDialog { java.awt.Component ed = patternCombo.getEditor().getEditorComponent(); if (ed != null) { ed.requestFocusInWindow(); - if (ed instanceof javax.swing.text.JTextComponent) { - ((javax.swing.text.JTextComponent) ed).selectAll(); + if (ed instanceof javax.swing.text.JTextComponent component) { + component.selectAll(); } } } catch (Exception ignore) {} @@ -388,8 +382,8 @@ public class SearchDialog extends JDialog { java.awt.Component ed = contentPatternCombo.getEditor().getEditorComponent(); if (ed != null) { ed.requestFocusInWindow(); - if (ed instanceof javax.swing.text.JTextComponent) { - ((javax.swing.text.JTextComponent) ed).selectAll(); + if (ed instanceof javax.swing.text.JTextComponent component) { + component.selectAll(); } } } catch (Exception ignore) {} @@ -445,9 +439,9 @@ public class SearchDialog extends JDialog { if (cur != null && !cur.isEmpty()) { java.util.List hist = new java.util.ArrayList<>(config.getSearchHistory()); hist.remove(cur); - hist.add(0, cur); + hist.addFirst(cur); int max = 20; - while (hist.size() > max) hist.remove(hist.size() - 1); + while (hist.size() > max) hist.removeLast(); config.saveSearchHistory(hist); } } catch (Exception ignore) {} @@ -458,9 +452,9 @@ public class SearchDialog extends JDialog { if (ccur != null && !ccur.isEmpty()) { java.util.List chist = new java.util.ArrayList<>(config.getContentSearchHistory()); chist.remove(ccur); - chist.add(0, ccur); + chist.addFirst(ccur); int maxc = 20; - while (chist.size() > maxc) chist.remove(chist.size() - 1); + while (chist.size() > maxc) chist.removeLast(); config.saveContentSearchHistory(chist); } } catch (Exception ignore) {} @@ -516,9 +510,9 @@ public class SearchDialog extends JDialog { if (!namePat.isEmpty()) { java.util.List hist = new java.util.ArrayList<>(config.getSearchHistory()); hist.remove(namePat); - hist.add(0, namePat); + hist.addFirst(namePat); int max = 20; - while (hist.size() > max) hist.remove(hist.size() - 1); + while (hist.size() > max) hist.removeLast(); config.saveSearchHistory(hist); // update combo model @@ -531,9 +525,9 @@ public class SearchDialog extends JDialog { if (isContentSearch && !contentPat.isEmpty()) { java.util.List chist = new java.util.ArrayList<>(config.getContentSearchHistory()); chist.remove(contentPat); - chist.add(0, contentPat); + chist.addFirst(contentPat); int max = 20; - while (chist.size() > max) chist.remove(chist.size() - 1); + while (chist.size() > max) chist.removeLast(); config.saveContentSearchHistory(chist); // update content combo model @@ -655,8 +649,7 @@ public class SearchDialog extends JDialog { try { // Open location inside the application: show parent directory in the focused panel and select the file java.awt.Window w = SwingUtilities.getWindowAncestor(this); - if (w instanceof MainWindow) { - MainWindow mw = (MainWindow) w; + if (w instanceof MainWindow mw) { String fullPath = item.getPath(); File file = item.getFile(); diff --git a/src/main/java/cz/kamma/kfmanager/ui/SettingsDialog.java b/src/main/java/cz/kamma/kfmanager/ui/SettingsDialog.java index d6b4c1e..859bf24 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/SettingsDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/SettingsDialog.java @@ -321,7 +321,7 @@ public class SettingsDialog extends JDialog { for (Component c : container.getComponents()) { // Skip specialized buttons that manage their own background colors - if (c instanceof JComponent && Boolean.TRUE.equals(((JComponent) c).getClientProperty("isColorButton"))) { + if (c instanceof JComponent component && Boolean.TRUE.equals(component.getClientProperty("isColorButton"))) { continue; } @@ -329,8 +329,7 @@ public class SettingsDialog extends JDialog { c instanceof JTabbedPane || c instanceof JButton || c instanceof JSplitPane || c instanceof JList || c instanceof JComboBox || c instanceof JTable) { c.setBackground(bg); - if (c instanceof JTable) { - JTable t = (JTable) c; + if (c instanceof JTable t) { if (t.getTableHeader() != null) { t.getTableHeader().setBackground(bg); t.getTableHeader().setForeground(dark ? Color.WHITE : Color.BLACK); @@ -341,8 +340,7 @@ public class SettingsDialog extends JDialog { c instanceof JButton || c instanceof JComboBox || c instanceof JList) { c.setForeground(dark ? Color.WHITE : Color.BLACK); } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -350,11 +348,9 @@ public class SettingsDialog extends JDialog { tc.setSelectionColor(selColor); } } - if (c instanceof JComboBox) { - JComboBox cb = (JComboBox) c; + if (c instanceof JComboBox cb) { Component ed = cb.getEditor().getEditorComponent(); - if (ed instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) ed; + if (ed instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -363,8 +359,8 @@ public class SettingsDialog extends JDialog { } } } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -692,7 +688,7 @@ public class SettingsDialog extends JDialog { try { // parse strings like "name:asc" if (crit.size() > 0) { - String[] parts = crit.get(0).split(":"); + String[] parts = crit.getFirst().split(":"); if (parts.length >= 1) primaryField.setSelectedItem(capitalize(parts[0])); if (parts.length == 2 && parts[1].equalsIgnoreCase("desc")) primaryOrder.setSelectedItem("Descending"); } @@ -953,6 +949,6 @@ public class SettingsDialog extends JDialog { private String getFontDescription(Font f) { if (f == null) return "(default)"; - return String.format("%s %dpt", f.getName(), f.getSize()); + return "%s %dpt".formatted(f.getName(), f.getSize()); } } diff --git a/src/main/java/cz/kamma/kfmanager/ui/SyncDirectoriesDialog.java b/src/main/java/cz/kamma/kfmanager/ui/SyncDirectoriesDialog.java index fcbab12..7206050 100644 --- a/src/main/java/cz/kamma/kfmanager/ui/SyncDirectoriesDialog.java +++ b/src/main/java/cz/kamma/kfmanager/ui/SyncDirectoriesDialog.java @@ -285,8 +285,7 @@ public class SyncDirectoriesDialog extends JDialog { if (!(c instanceof JToggleButton)) { c.setBackground(bg); } - if (c instanceof JTable) { - JTable t = (JTable) c; + if (c instanceof JTable t) { if (t.getTableHeader() != null) { t.getTableHeader().setBackground(bg); t.getTableHeader().setForeground(dark ? Color.WHITE : Color.BLACK); @@ -299,8 +298,7 @@ public class SyncDirectoriesDialog extends JDialog { c.setForeground(dark ? Color.WHITE : Color.BLACK); } } - if (c instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) c; + if (c instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -308,11 +306,9 @@ public class SyncDirectoriesDialog extends JDialog { tc.setSelectionColor(selColor); } } - if (c instanceof JComboBox) { - JComboBox cb = (JComboBox) c; + if (c instanceof JComboBox cb) { Component ed = cb.getEditor().getEditorComponent(); - if (ed instanceof javax.swing.text.JTextComponent) { - javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) ed; + if (ed instanceof javax.swing.text.JTextComponent tc) { tc.setBackground(bg); tc.setForeground(dark ? Color.WHITE : Color.BLACK); tc.setCaretColor(dark ? Color.WHITE : Color.BLACK); @@ -321,8 +317,8 @@ public class SyncDirectoriesDialog extends JDialog { } } } - if (c instanceof Container) { - updateComponentBackground((Container) c, bg); + if (c instanceof Container container1) { + updateComponentBackground(container1, bg); } } } @@ -440,7 +436,7 @@ public class SyncDirectoriesDialog extends JDialog { else if ("<-".equals(e.relation) && showRightOnlyBtn.isSelected()) filtered.add(e); } tableModel.setResults(filtered); - statusLabel.setText(String.format("Showing %d of %d items.", filtered.size(), allEntries.size())); + statusLabel.setText("Showing %d of %d items.".formatted(filtered.size(), allEntries.size())); } private void updateRelation(SyncEntry entry) { @@ -541,7 +537,7 @@ public class SyncDirectoriesDialog extends JDialog { private String formatSize(long size) { if (size < 0) return ""; - return String.format("%,d", size); + return "%,d".formatted(size); } private String formatDate(long time) { @@ -586,8 +582,8 @@ public class SyncDirectoriesDialog extends JDialog { setHorizontalAlignment(LEFT); } - if (c instanceof JComponent) { - ((JComponent) c).setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, dark ? new Color(60, 60, 60) : new Color(220, 220, 220))); + if (c instanceof JComponent component) { + component.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, dark ? new Color(60, 60, 60) : new Color(220, 220, 220))); } return c;