diff --git a/src/main/java/info/loenwind/compare/ImgWindow.java b/src/main/java/info/loenwind/compare/ImgWindow.java index 1fe9fb8..01e01a0 100644 --- a/src/main/java/info/loenwind/compare/ImgWindow.java +++ b/src/main/java/info/loenwind/compare/ImgWindow.java @@ -10,6 +10,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Collections; @@ -33,6 +34,7 @@ import javax.swing.border.LineBorder; import javax.swing.table.DefaultTableModel; +import info.loenwind.compare.tools.FileFinder; import info.loenwind.compare.tools.Settings; public class ImgWindow extends JFrame { @@ -577,6 +579,15 @@ private void trash(File file) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "The file " + name + " could not be moved to " + target + ". Message: " + ex.getLocalizedMessage(), "Error", JOptionPane.WARNING_MESSAGE); + return; + } + // also move related files + for (Path path : new FileFinder(file.toPath()).getMatches()) { + try { + Files.move(path, new File(settings.getTrashFolder(), path.getFileName().toString()).toPath()); + } catch (IOException e) { + e.printStackTrace(); + } } } } diff --git a/src/main/java/info/loenwind/compare/tools/FileFinder.java b/src/main/java/info/loenwind/compare/tools/FileFinder.java new file mode 100644 index 0000000..7679341 --- /dev/null +++ b/src/main/java/info/loenwind/compare/tools/FileFinder.java @@ -0,0 +1,74 @@ +package info.loenwind.compare.tools; + +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.List; + +import static java.nio.file.FileVisitResult.CONTINUE; + +public class FileFinder extends SimpleFileVisitor { + + private final PathMatcher matcher; + private final String ignore; + private final List matches = new ArrayList<>(); + + @SuppressWarnings("resource") + public FileFinder(Path startingDir, String pattern) { + matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); + ignore = ""; + try { + Files.walkFileTree(startingDir, this); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @SuppressWarnings("resource") + public FileFinder(Path startingFile) { + Path fileName = startingFile.getFileName(); + Path startingDir = startingFile.getParent(); + ignore = fileName.toString(); + String pattern = ignore.replaceFirst("\\.[^.]+$", ".*"); + matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); + try { + Files.walkFileTree(startingDir, this); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void find(Path file) { + Path name = file.getFileName(); + if (name != null && matcher.matches(name) && !ignore.equals(name.toString())) { + matches.add(file); + } + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + find(file); + return CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + return CONTINUE; + } + + public List getMatches() { + return matches; + } + +} \ No newline at end of file diff --git a/src/test/java/info/loenwind/compare/tools/FileFinderTest.java b/src/test/java/info/loenwind/compare/tools/FileFinderTest.java new file mode 100644 index 0000000..2c6958f --- /dev/null +++ b/src/test/java/info/loenwind/compare/tools/FileFinderTest.java @@ -0,0 +1,21 @@ +package info.loenwind.compare.tools; + +import java.nio.file.Paths; +import java.util.Collections; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FileFinderTest { + + @Test + public final void testFileFinderPath() { + FileFinder finder = new FileFinder(Paths.get("src/test/resources", "test.txt")); + assertEquals("2 matches", 2, finder.getMatches().size()); + Collections.sort(finder.getMatches()); + assertEquals("Found xml neighbour", Paths.get("src/test/resources", "test.xml"), finder.getMatches().get(0)); + assertEquals("Found yml neighbor", Paths.get("src/test/resources", "test.yaml"), finder.getMatches().get(1)); + } + +} diff --git a/src/test/resources/test.txt b/src/test/resources/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/test.xml b/src/test/resources/test.xml new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/test.yaml b/src/test/resources/test.yaml new file mode 100644 index 0000000..e69de29