diff --git a/Guify/src/code/GuifySftpProgressMonitor.java b/Guify/src/code/GuifySftpProgressMonitor.java index d65e81a..75f8b19 100644 --- a/Guify/src/code/GuifySftpProgressMonitor.java +++ b/Guify/src/code/GuifySftpProgressMonitor.java @@ -5,8 +5,8 @@ import com.jcraft.jsch.SftpProgressMonitor; // Documentation: https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/SftpProgressMonitor.html public class GuifySftpProgressMonitor implements SftpProgressMonitor { - TransferProgress transferProgress = null; - + private TransferProgress transferProgress = null; + @Override public boolean count(long bytes) { diff --git a/Guify/src/code/Main.java b/Guify/src/code/Main.java index 62e4983..84376c7 100644 --- a/Guify/src/code/Main.java +++ b/Guify/src/code/Main.java @@ -1,3 +1,5 @@ +// In loving memory of L.B. and E.B. + package code; import java.awt.EventQueue; @@ -19,7 +21,4 @@ public class Main { } }); } - } - -// In loving memory of L.B. and E.B. diff --git a/Guify/src/code/SshEngine.java b/Guify/src/code/SshEngine.java index 15b255a..ef12161 100644 --- a/Guify/src/code/SshEngine.java +++ b/Guify/src/code/SshEngine.java @@ -8,6 +8,8 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Properties; import java.util.Vector; +import java.util.function.Consumer; + import org.apache.commons.io.IOUtils; import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; @@ -173,7 +175,7 @@ public class SshEngine { * Uploads a file from the local machine to the remote host. Executed * asynchronously. */ - public static void uploadFile(File fileToUpload, String remoteDirectory) { + public static void uploadFile(File fileToUpload, String remoteDirectory, Consumer uploadCompletedEvent) { // We execute the lengthy and time-consuming operation on a different // thread instead of the Event Dispatch Thread. // We use SwingWorker so any GUI changes requested by this thread will @@ -190,6 +192,9 @@ public class SshEngine { fileToUpload.getName()); channelSftp.put(fileToUpload.getAbsolutePath(), remotePath, new GuifySftpProgressMonitor()); + + // Fire the upload completed event + uploadCompletedEvent.accept(remoteDirectory); System.out.println("File: " + fileToUpload.getAbsolutePath() + " uploaded to remote path: " + remotePath); } catch (SftpException sftpex) { @@ -214,7 +219,7 @@ public class SshEngine { * will be uploaded in */ public static void uploadDirectoriesRecursively(File directory, - String remoteDirectory) { + String remoteDirectory, Consumer uploadCompletedEvent) { // We execute the lengthy and time-consuming operation on a different // thread instead of the Event Dispatch Thread. // We use SwingWorker so any GUI changes requested by this thread will @@ -228,6 +233,9 @@ public class SshEngine { channelSftp.connect(); uploadDirectoriesRecursively_aux(channelSftp, directory, remoteDirectory); + + // Fire the upload completed event + uploadCompletedEvent.accept(remoteDirectory); } catch (SftpException sftpex) { // TODO maybe no permissions } catch (Exception e) { diff --git a/Guify/src/controllers/DesktopController.java b/Guify/src/controllers/DesktopController.java index ee811a0..3b22a0e 100644 --- a/Guify/src/controllers/DesktopController.java +++ b/Guify/src/controllers/DesktopController.java @@ -15,6 +15,9 @@ import code.SshEngine; import java.io.File; import java.util.*; import java.util.List; +import java.util.function.Consumer; + +import javax.swing.SwingUtilities; public class DesktopController { /* @@ -159,13 +162,20 @@ public class DesktopController { } } + // Once the upload has completed, fire this event (draw the desktop). + // The parameter is the remote path in which the file(s) are uploaded + // into + Consumer uploadCompletedEvent = s -> this + .uploadCompletedEvent(s); + for (File file : selectedFiles) { - SshEngine.uploadFile(file, this.getCurrentWorkingDirectory()); + SshEngine.uploadFile(file, this.getCurrentWorkingDirectory(), + uploadCompletedEvent); } for (File directory : selectedDirectories) { SshEngine.uploadDirectoriesRecursively(directory, - this.getCurrentWorkingDirectory()); + this.getCurrentWorkingDirectory(), uploadCompletedEvent); } } @@ -449,6 +459,34 @@ public class DesktopController { return title.toString(); } + /** + * Event that gets fired once + */ + public void uploadCompletedEvent(String path) { + + if (!path.equals(getCurrentWorkingDirectory())) { + // Redraw the desktop only if necessary + return; + } + + // This method gets called from outside the Event + // Dispatch Thread and we know it, but we write this condition + // because you never know when this method may be helpful in + // the future, forgetting to check this condition. + if (!SwingUtilities.isEventDispatchThread()) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + uploadCompletedEvent(path); + } + }); + return; + } + + // Draw the UI again + frame.drawComponentsForDirectory(path); + } + /* * ========== END Other ========== */ diff --git a/Guify/src/views/Desktop.java b/Guify/src/views/Desktop.java index 6ba408a..72029c9 100644 --- a/Guify/src/views/Desktop.java +++ b/Guify/src/views/Desktop.java @@ -165,16 +165,13 @@ public class Desktop extends JFrame implements IDesktopFrame { ((DesktopController) controller) .uploadToRemoteServer(droppedFileArray); - drawComponentsForDirectory( - ((DesktopController) controller) - .getCurrentWorkingDirectory()); } catch (UnsupportedFlavorException | IOException e) { e.printStackTrace(); } } dtde.dropComplete(true); - desktopPanel.setBorder(null); + desktopPanel.setBorder(null); // remove the blue line } }); } @@ -959,8 +956,6 @@ public class Desktop extends JFrame implements IDesktopFrame { && fileChooser.getSelectedFiles().length > 0) { controller.uploadToRemoteServer( fileChooser.getSelectedFiles()); - drawComponentsForDirectory( - controller.getCurrentWorkingDirectory()); } }