Skip to content

Commit 0fd02f4

Browse files
committed
Added moving excluded files into a "trash" folder
1 parent 0096e57 commit 0fd02f4

File tree

2 files changed

+96
-17
lines changed

2 files changed

+96
-17
lines changed

src/main/java/info/loenwind/compare/AppWindow.java

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323
import javax.swing.Box;
2424
import javax.swing.JButton;
25+
import javax.swing.JCheckBox;
2526
import javax.swing.JFileChooser;
2627
import javax.swing.JFrame;
2728
import javax.swing.JLabel;
29+
import javax.swing.JOptionPane;
2830
import javax.swing.JTextField;
2931
import javax.swing.UIManager;
3032
import javax.swing.UnsupportedLookAndFeelException;
@@ -47,6 +49,9 @@ public class AppWindow {
4749
private Component horizontalStrut_1;
4850
private Component verticalStrut;
4951
private Component horizontalStrut_2;
52+
private JCheckBox checkMove;
53+
private JTextField textMove;
54+
private JButton buttonMove;
5055

5156
public static void run() {
5257
EventQueue.invokeLater(new Runnable() {
@@ -77,9 +82,9 @@ private void initialize() {
7782
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
7883
GridBagLayout gridBagLayout = new GridBagLayout();
7984
gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
80-
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
85+
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
8186
gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE };
82-
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
87+
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
8388
frame.getContentPane().setLayout(gridBagLayout);
8489

8590
verticalStrut = Box.createVerticalStrut(20);
@@ -179,7 +184,7 @@ public void actionPerformed(ActionEvent e) {
179184
}
180185
});
181186
GridBagConstraints gbc_buttonSelect = new GridBagConstraints();
182-
gbc_buttonSelect.insets = new Insets(0, 0, 5, 0);
187+
gbc_buttonSelect.insets = new Insets(0, 0, 5, 5);
183188
gbc_buttonSelect.gridx = 5;
184189
gbc_buttonSelect.gridy = 1;
185190
frame.getContentPane().add(buttonSelect, gbc_buttonSelect);
@@ -253,7 +258,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
253258
labelScan = new JLabel("Please select a folder");
254259
GridBagConstraints gbc_labelScan = new GridBagConstraints();
255260
gbc_labelScan.gridwidth = 2;
256-
gbc_labelScan.insets = new Insets(0, 0, 5, 0);
261+
gbc_labelScan.insets = new Insets(0, 0, 5, 5);
257262
gbc_labelScan.gridx = 4;
258263
gbc_labelScan.gridy = 3;
259264
frame.getContentPane().add(labelScan, gbc_labelScan);
@@ -265,14 +270,6 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
265270
gbc_lblNewLabel_3.gridy = 5;
266271
frame.getContentPane().add(lblNewLabel_3, gbc_lblNewLabel_3);
267272

268-
JButton buttonExit = new JButton("Exit");
269-
buttonExit.addActionListener(new ActionListener() {
270-
@Override
271-
public void actionPerformed(ActionEvent e) {
272-
frame.dispose();
273-
}
274-
});
275-
276273
buttonCompare = new JButton("Compare images");
277274
buttonCompare.setEnabled(false);
278275
buttonCompare.addActionListener(new ActionListener() {
@@ -288,6 +285,14 @@ public void windowClosing(WindowEvent e1) {
288285
}
289286
});
290287
imgframe.setFiles(files);
288+
if (checkMove.isSelected()) {
289+
File file = new File(textMove.getText());
290+
if (file.isDirectory()) {
291+
imgframe.setExclusionFolder(file);
292+
} else {
293+
JOptionPane.showMessageDialog(frame, file + " is not a valid folder. Moving excluded files is disabled.", "Error", JOptionPane.WARNING_MESSAGE);
294+
}
295+
}
291296
imgframe.setVisible(true);
292297
if ((frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) != 0) {
293298
imgframe.setExtendedState(imgframe.getExtendedState() | JFrame.MAXIMIZED_BOTH);
@@ -303,14 +308,58 @@ public void windowClosing(WindowEvent e1) {
303308
labelCompare = new JLabel("Please scan for images");
304309
GridBagConstraints gbc_labelCompare = new GridBagConstraints();
305310
gbc_labelCompare.gridwidth = 2;
306-
gbc_labelCompare.insets = new Insets(0, 0, 5, 0);
311+
gbc_labelCompare.insets = new Insets(0, 0, 5, 5);
307312
gbc_labelCompare.gridx = 4;
308313
gbc_labelCompare.gridy = 5;
309314
frame.getContentPane().add(labelCompare, gbc_labelCompare);
310-
GridBagConstraints gbc_buttonExit = new GridBagConstraints();
311-
gbc_buttonExit.gridx = 5;
312-
gbc_buttonExit.gridy = 7;
313-
frame.getContentPane().add(buttonExit, gbc_buttonExit);
315+
316+
checkMove = new JCheckBox("Enable moving excluded files to this folder:");
317+
GridBagConstraints gbc_checkMove = new GridBagConstraints();
318+
gbc_checkMove.anchor = GridBagConstraints.NORTHWEST;
319+
gbc_checkMove.gridwidth = 2;
320+
gbc_checkMove.insets = new Insets(0, 0, 5, 5);
321+
gbc_checkMove.gridx = 3;
322+
gbc_checkMove.gridy = 7;
323+
frame.getContentPane().add(checkMove, gbc_checkMove);
324+
325+
textMove = new JTextField();
326+
GridBagConstraints gbc_textMove = new GridBagConstraints();
327+
gbc_textMove.insets = new Insets(0, 0, 0, 5);
328+
gbc_textMove.fill = GridBagConstraints.HORIZONTAL;
329+
gbc_textMove.gridx = 4;
330+
gbc_textMove.gridy = 8;
331+
frame.getContentPane().add(textMove, gbc_textMove);
332+
textMove.setColumns(10);
333+
334+
buttonMove = new JButton("Select...");
335+
buttonMove.addActionListener(new ActionListener() {
336+
@Override
337+
public void actionPerformed(ActionEvent e) {
338+
JFileChooser chooser = new JFileChooser();
339+
if (textMove.getText().isEmpty()) {
340+
chooser.setCurrentDirectory(new File("."));
341+
} else {
342+
chooser.setCurrentDirectory(new File(textMove.getText()));
343+
}
344+
chooser.setDialogTitle("Select folder for excluded images");
345+
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
346+
chooser.setAcceptAllFileFilterUsed(false);
347+
if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
348+
try {
349+
textMove.setText(chooser.getSelectedFile().getCanonicalPath().toString());
350+
} catch (IOException e1) {
351+
e1.printStackTrace();
352+
textMove.setText(e1.getLocalizedMessage());
353+
checkMove.setSelected(false);
354+
}
355+
}
356+
}
357+
});
358+
GridBagConstraints gbc_buttonMove = new GridBagConstraints();
359+
gbc_buttonMove.insets = new Insets(0, 0, 0, 5);
360+
gbc_buttonMove.gridx = 5;
361+
gbc_buttonMove.gridy = 8;
362+
frame.getContentPane().add(buttonMove, gbc_buttonMove);
314363
}
315364

316365
}

src/main/java/info/loenwind/compare/ImgWindow.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.awt.event.ActionEvent;
99
import java.awt.event.ActionListener;
1010
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
1113
import java.util.ArrayList;
1214
import java.util.Collections;
1315
import java.util.HashMap;
@@ -20,6 +22,7 @@
2022
import javax.swing.JButton;
2123
import javax.swing.JFrame;
2224
import javax.swing.JLabel;
25+
import javax.swing.JOptionPane;
2326
import javax.swing.JPanel;
2427
import javax.swing.SwingConstants;
2528
import javax.swing.border.EmptyBorder;
@@ -44,6 +47,8 @@ public class ImgWindow extends JFrame {
4447
private JLabel textRemaining;
4548
private JLabel textFully;
4649

50+
private File trash = null;
51+
4752
private final List<File> allFiles = new ArrayList<>();
4853
private final List<File> excludedFiles = new ArrayList<>();
4954
private final List<File> currentRound = new ArrayList<>();
@@ -280,6 +285,7 @@ public void actionPerformed(ActionEvent e) {
280285
likes.remove(pair.getA());
281286
nextRound.remove(pair.getA());
282287
excludedFiles.add(pair.getA());
288+
trash(pair.getA());
283289
nextPair(pair.getB(), true);
284290
}
285291
});
@@ -339,6 +345,7 @@ public void actionPerformed(ActionEvent e) {
339345
likes.remove(pair.getB());
340346
nextRound.remove(pair.getB());
341347
excludedFiles.add(pair.getB());
348+
trash(pair.getB());
342349
nextPair(pair.getA(), false);
343350
}
344351
});
@@ -534,4 +541,27 @@ private String getResultString() {
534541
}
535542
return b.toString();
536543
}
544+
545+
public void setExclusionFolder(File file) {
546+
trash = file;
547+
}
548+
549+
private void trash(File file) {
550+
if (trash != null) {
551+
String name = file.getName();
552+
File target = new File(trash, name);
553+
while (target.exists()) {
554+
target = new File(trash, (rollingPostfix++) + "_" + name);
555+
}
556+
try {
557+
Files.move(file.toPath(), target.toPath());
558+
} catch (IOException ex) {
559+
ex.printStackTrace();
560+
JOptionPane.showMessageDialog(this, "The file " + name + " could not be moved to " + target + ". Message: " + ex.getLocalizedMessage(), "Error",
561+
JOptionPane.WARNING_MESSAGE);
562+
}
563+
}
564+
}
565+
566+
private int rollingPostfix = 0;
537567
}

0 commit comments

Comments
 (0)