Skip to content

Commit fbe6044

Browse files
committed
feat: Cleanup code
1 parent c410598 commit fbe6044

File tree

5 files changed

+82
-61
lines changed

5 files changed

+82
-61
lines changed

src/main/java/de/tjorven/MetadataListUI.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@ public class MetadataListUI extends JFrame {
2222
private final PageablePanel pageablePanel;
2323

2424
public static void main(String[] args) {
25-
SwingUtilities.invokeLater(() -> {
26-
FlatDarculaLaf.setup();
27-
28-
try {
29-
UIManager.setLookAndFeel(new FlatMTMaterialDeepOceanIJTheme());
30-
} catch (UnsupportedLookAndFeelException e) {
31-
logger.error("Error setting LAF", e);
32-
}
33-
34-
instance = new MetadataListUI();
35-
instance.setVisible(true);
36-
});
25+
SwingUtilities.invokeLater(MetadataListUI::initApp);
26+
}
27+
28+
private static void initApp() {
29+
FlatDarculaLaf.setup();
30+
31+
try {
32+
UIManager.setLookAndFeel(new FlatMTMaterialDeepOceanIJTheme());
33+
} catch (UnsupportedLookAndFeelException e) {
34+
logger.error("Error setting LAF", e);
35+
}
36+
37+
instance = new MetadataListUI();
38+
instance.setVisible(true);
3739
}
3840

3941
public MetadataListUI() {
4042
this.setTitle("File-Sorter");
4143
this.setSize(1200, 600);
44+
4245
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4346
this.setLayout(new BorderLayout(10, 10));
4447

src/main/java/de/tjorven/algorithm/FolderAnalyser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public Set<String> getFileNames() {
6969

7070
private Set<String> loadBlacklist() {
7171
Path path = Paths.get("blacklist.txt");
72-
if (!Files.exists(path)) return Collections.emptySet();
72+
if (!Files.exists(path)) {
73+
return Collections.emptySet();
74+
}
7375
try (Stream<String> lines = Files.lines(path)) {
7476
return lines.filter(line -> !line.isBlank()).collect(Collectors.toSet());
7577
} catch (IOException e) {

src/main/java/de/tjorven/algorithm/RecursiveSorter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public void runFilter(Path rootPath, List<String> selectedAttributes, List<Strin
2929
try (Stream<Path> stream = Files.list(rootPath)) {
3030
filesInFolder = stream.filter(Files::isRegularFile)
3131
.filter(path -> {
32-
if (allowedExtensions == null || allowedExtensions.isEmpty()) return true;
32+
if (allowedExtensions == null || allowedExtensions.isEmpty()) {
33+
return true;
34+
}
3335
String name = path.getFileName().toString().toLowerCase();
3436
return allowedExtensions.stream().anyMatch(ext -> name.endsWith(ext.toLowerCase()));
3537
})
@@ -38,7 +40,9 @@ public void runFilter(Path rootPath, List<String> selectedAttributes, List<Strin
3840

3941
for (Path filePath : filesInFolder) {
4042
Map<String, String> fileMeta = this.fileMetadataMap.get(filePath.getFileName().toString());
41-
if (fileMeta == null) continue;
43+
if (fileMeta == null) {
44+
continue;
45+
}
4246

4347
Path currentTargetDir = rootPath;
4448
for (String attr : selectedAttributes) {
@@ -47,7 +51,9 @@ public void runFilter(Path rootPath, List<String> selectedAttributes, List<Strin
4751
currentTargetDir = currentTargetDir.resolve(folderName);
4852
}
4953

50-
if (!Files.exists(currentTargetDir)) Files.createDirectories(currentTargetDir);
54+
if (!Files.exists(currentTargetDir)) {
55+
Files.createDirectories(currentTargetDir);
56+
}
5157

5258
Path targetFile = currentTargetDir.resolve(filePath.getFileName());
5359
Files.move(filePath, targetFile, StandardCopyOption.REPLACE_EXISTING);

src/main/java/de/tjorven/page/HomePage.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class HomePage extends JPanel {
2020
private final List<String> metadataDropdown = new ArrayList<>();
2121

2222
private final JLabel pathLabel;
23-
private final JButton selectBtn;
2423
private final JButton scanBtn;
2524
private final JButton nextBtn;
2625
private final JProgressBar progressBar;
@@ -33,8 +32,8 @@ public HomePage() {
3332
title.setFont(new Font("SansSerif", Font.BOLD, 24));
3433
title.setAlignmentX(Component.CENTER_ALIGNMENT);
3534

36-
this.selectBtn = new JButton("1. Select Folder");
37-
this.selectBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
35+
JButton selectBtn = new JButton("1. Select Folder");
36+
selectBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
3837

3938
this.pathLabel = new JLabel("No directory selected");
4039
this.pathLabel.setForeground(Color.GRAY);
@@ -54,13 +53,13 @@ public HomePage() {
5453
this.progressBar.setVisible(false);
5554
this.progressBar.setMaximumSize(new Dimension(300, 20));
5655

57-
this.selectBtn.addActionListener(e -> this.selectFolder());
56+
selectBtn.addActionListener(e -> this.selectFolder());
5857
this.scanBtn.addActionListener(e -> this.startAnalysis());
5958
this.nextBtn.addActionListener(e -> this.goToSortPage());
6059

6160
this.add(title);
6261
this.add(Box.createRigidArea(new Dimension(0, 30)));
63-
this.add(this.selectBtn);
62+
this.add(selectBtn);
6463
this.add(this.pathLabel);
6564
this.add(Box.createRigidArea(new Dimension(0, 10)));
6665
this.add(this.scanBtn);

src/main/java/de/tjorven/page/SortOptionsPage.java

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,44 +89,7 @@ private void init() {
8989
private void setupDragAndDrop() {
9090
this.levelsList.setDragEnabled(true);
9191
this.levelsList.setDropMode(DropMode.INSERT);
92-
this.levelsList.setTransferHandler(new TransferHandler() {
93-
private int index = -1;
94-
95-
@Override
96-
public int getSourceActions(JComponent c) {
97-
return MOVE;
98-
}
99-
100-
@Override
101-
protected Transferable createTransferable(JComponent c) {
102-
this.index = SortOptionsPage.this.levelsList.getSelectedIndex();
103-
return new StringSelection(SortOptionsPage.this.levelsList.getSelectedValue());
104-
}
105-
106-
@Override
107-
public boolean canImport(TransferSupport support) {
108-
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
109-
}
110-
111-
@Override
112-
public boolean importData(TransferSupport support) {
113-
if (!this.canImport(support)) return false;
114-
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
115-
int dropIndex = dl.getIndex();
116-
try {
117-
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
118-
if (this.index != -1) {
119-
SortOptionsPage.this.selectedLevelsModel.remove(this.index);
120-
if (dropIndex > this.index) dropIndex--;
121-
}
122-
SortOptionsPage.this.selectedLevelsModel.add(dropIndex, data);
123-
SortOptionsPage.this.updatePreview();
124-
return true;
125-
} catch (Exception e) {
126-
return false;
127-
}
128-
}
129-
});
92+
this.levelsList.setTransferHandler(new SortTransferHandler());
13093
}
13194

13295
private void updatePreview() {
@@ -150,7 +113,9 @@ private void updatePreview() {
150113
break;
151114
}
152115
}
153-
if (!matches) return;
116+
if (!matches) {
117+
return;
118+
}
154119
}
155120

156121
DefaultMutableTreeNode currentNode = this.rootNode;
@@ -171,7 +136,9 @@ private void updatePreview() {
171136
private DefaultMutableTreeNode getOrCreateChild(DefaultMutableTreeNode parent, String name) {
172137
for (int i = 0; i < parent.getChildCount(); i++) {
173138
DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
174-
if (child.getUserObject().equals(name)) return child;
139+
if (child.getUserObject().equals(name)) {
140+
return child;
141+
}
175142
}
176143
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
177144
parent.add(newNode);
@@ -257,4 +224,48 @@ private void handleRevert() {
257224
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
258225
}
259226
}
227+
228+
private class SortTransferHandler extends TransferHandler {
229+
private int index = -1;
230+
231+
@Override
232+
public int getSourceActions(JComponent c) {
233+
return MOVE;
234+
}
235+
236+
@Override
237+
protected Transferable createTransferable(JComponent c) {
238+
this.index = SortOptionsPage.this.levelsList.getSelectedIndex();
239+
return new StringSelection(SortOptionsPage.this.levelsList.getSelectedValue());
240+
}
241+
242+
@Override
243+
public boolean canImport(TransferSupport support) {
244+
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
245+
}
246+
247+
@Override
248+
public boolean importData(TransferSupport support) {
249+
if (!this.canImport(support)) {
250+
return false;
251+
}
252+
253+
JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
254+
int dropIndex = dl.getIndex();
255+
try {
256+
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
257+
if (this.index != -1) {
258+
SortOptionsPage.this.selectedLevelsModel.remove(this.index);
259+
if (dropIndex > this.index) {
260+
dropIndex--;
261+
}
262+
}
263+
SortOptionsPage.this.selectedLevelsModel.add(dropIndex, data);
264+
SortOptionsPage.this.updatePreview();
265+
return true;
266+
} catch (Exception e) {
267+
return false;
268+
}
269+
}
270+
}
260271
}

0 commit comments

Comments
 (0)