Skip to content

Commit

Permalink
Added moving related files
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLoenwind committed Sep 24, 2022
1 parent c0027e6 commit f79f118
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/info/loenwind/compare/ImgWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/info/loenwind/compare/tools/FileFinder.java
Original file line number Diff line number Diff line change
@@ -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<Path> {

private final PathMatcher matcher;
private final String ignore;
private final List<Path> 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<Path> getMatches() {
return matches;
}

}
21 changes: 21 additions & 0 deletions src/test/java/info/loenwind/compare/tools/FileFinderTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
Empty file added src/test/resources/test.txt
Empty file.
Empty file added src/test/resources/test.xml
Empty file.
Empty file added src/test/resources/test.yaml
Empty file.

0 comments on commit f79f118

Please sign in to comment.