Skip to content

Commit

Permalink
Show now count of files to copy via DnD
Browse files Browse the repository at this point in the history
  • Loading branch information
cleme authored and cleme committed Oct 9, 2023
1 parent d6b1ea5 commit 72b088c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
15 changes: 5 additions & 10 deletions PhotoSlide/src/main/java/org/photoslide/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,10 @@ public static Alert setDefaultButton(Alert alert, ButtonType defBtn) {
return alert;
}

public static void copyDir(String src, String dest, boolean overwrite) throws IOException {
Files.walk(Paths.get(src)).forEach(a -> {
Path b = Paths.get(dest, a.toString().substring(src.length()));
try {
if (!a.toString().equals(src)) {
Files.copy(a, b, overwrite ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{});
}
} catch (IOException e) {
}
});
public static long fileCount(Path dir) throws IOException {
return Files.walk(dir)
.parallel()
.filter(p -> !p.toFile().isDirectory())
.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
Expand Down Expand Up @@ -214,16 +215,39 @@ private void setupDropTarget(DragEvent t) {
if (alert.getResult() == ButtonType.OK) {
File dropFile = db.getFiles().get(0);
List<File> fileItem = db.getFiles();
AtomicLong count=new AtomicLong(0);
if (dropFile.isDirectory()) {
try {
count.set(Utility.fileCount(dropFile.toPath()));
} catch (IOException ex) {
Logger.getLogger(CollectionsController.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
count.set(fileItem.size());
}
Task<Boolean> task = new Task<>() {
@Override
protected Boolean call() throws Exception {
String targetStr = cbRootFolder.getSelectionModel().getSelectedItem() + File.separator + dropFile.toPath().getFileName().toString();
updateTitle("Copy mediafiles to " + Path.of(cbRootFolder.getSelectionModel().getSelectedItem()).getFileName().toString());
AtomicInteger iatom = new AtomicInteger(1);
if (dropFile.isDirectory()) {
Path targetPath = Path.of(targetStr + "_" + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE));
updateMessage("Copy to target dir...");
Files.createDirectory(targetPath);
Utility.copyDir(dropFile.getAbsolutePath(), targetPath.toString(), true);
String src = dropFile.getAbsolutePath();
String dest = targetPath.toString();
Files.walk(Paths.get(src)).forEach(a -> {
Path b = Paths.get(dest, a.toString().substring(src.length()));
try {
if (!a.toString().equals(src)) {
Files.copy(a, b, true ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{});
updateMessage(iatom.get() + " / " + count.get());
iatom.addAndGet(1);
}
} catch (IOException e) {
}
});
} else {
//Create Task
String targetDir = cbRootFolder.getSelectionModel().getSelectedItem() + File.separator + LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE);
Expand All @@ -232,6 +256,8 @@ protected Boolean call() throws Exception {
try {
updateMessage("Copy " + t.getName());
Files.copy(t.toPath(), Path.of(targetDir + File.separator + t.getName()), StandardCopyOption.REPLACE_EXISTING);
updateMessage(iatom.get() + " / " + count.get());
iatom.addAndGet(1);
} catch (IOException ex) {
Logger.getLogger(CollectionsController.class.getName()).log(Level.SEVERE, null, ex);
}
Expand All @@ -240,6 +266,12 @@ protected Boolean call() throws Exception {
return true;
}
};
task.setOnScheduled((tk) -> {
mainController.getStatusLabelLeft().setVisible(true);
mainController.getProgressPane().setVisible(true);
mainController.getProgressbarLabel().setText(count + " files found - Loading...");
mainController.getStatusLabelRight().setVisible(true);
});
task.setOnFailed((fail) -> {
Alert alertError = new Alert(AlertType.ERROR, "Error during import of media files", ButtonType.OK);
alertError.getDialogPane().getStylesheets().add(
Expand Down

0 comments on commit 72b088c

Please sign in to comment.