This commit is contained in:
xfarrow 2023-08-10 10:00:52 +02:00
parent 7be8f4a2e1
commit 4325c59a40
5 changed files with 55 additions and 15 deletions

View File

@ -5,8 +5,8 @@ import com.jcraft.jsch.SftpProgressMonitor;
// Documentation: https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/SftpProgressMonitor.html // Documentation: https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/SftpProgressMonitor.html
public class GuifySftpProgressMonitor implements SftpProgressMonitor { public class GuifySftpProgressMonitor implements SftpProgressMonitor {
TransferProgress transferProgress = null; private TransferProgress transferProgress = null;
@Override @Override
public boolean count(long bytes) { public boolean count(long bytes) {

View File

@ -1,3 +1,5 @@
// In loving memory of L.B. and E.B.
package code; package code;
import java.awt.EventQueue; import java.awt.EventQueue;
@ -19,7 +21,4 @@ public class Main {
} }
}); });
} }
} }
// In loving memory of L.B. and E.B.

View File

@ -8,6 +8,8 @@ import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Vector; import java.util.Vector;
import java.util.function.Consumer;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import com.jcraft.jsch.*; import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry; 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 * Uploads a file from the local machine to the remote host. Executed
* asynchronously. * asynchronously.
*/ */
public static void uploadFile(File fileToUpload, String remoteDirectory) { public static void uploadFile(File fileToUpload, String remoteDirectory, Consumer<String> uploadCompletedEvent) {
// We execute the lengthy and time-consuming operation on a different // We execute the lengthy and time-consuming operation on a different
// thread instead of the Event Dispatch Thread. // thread instead of the Event Dispatch Thread.
// We use SwingWorker so any GUI changes requested by this thread will // We use SwingWorker so any GUI changes requested by this thread will
@ -190,6 +192,9 @@ public class SshEngine {
fileToUpload.getName()); fileToUpload.getName());
channelSftp.put(fileToUpload.getAbsolutePath(), remotePath, channelSftp.put(fileToUpload.getAbsolutePath(), remotePath,
new GuifySftpProgressMonitor()); new GuifySftpProgressMonitor());
// Fire the upload completed event
uploadCompletedEvent.accept(remoteDirectory);
System.out.println("File: " + fileToUpload.getAbsolutePath() System.out.println("File: " + fileToUpload.getAbsolutePath()
+ " uploaded to remote path: " + remotePath); + " uploaded to remote path: " + remotePath);
} catch (SftpException sftpex) { } catch (SftpException sftpex) {
@ -214,7 +219,7 @@ public class SshEngine {
* will be uploaded in * will be uploaded in
*/ */
public static void uploadDirectoriesRecursively(File directory, public static void uploadDirectoriesRecursively(File directory,
String remoteDirectory) { String remoteDirectory, Consumer<String> uploadCompletedEvent) {
// We execute the lengthy and time-consuming operation on a different // We execute the lengthy and time-consuming operation on a different
// thread instead of the Event Dispatch Thread. // thread instead of the Event Dispatch Thread.
// We use SwingWorker so any GUI changes requested by this thread will // We use SwingWorker so any GUI changes requested by this thread will
@ -228,6 +233,9 @@ public class SshEngine {
channelSftp.connect(); channelSftp.connect();
uploadDirectoriesRecursively_aux(channelSftp, directory, uploadDirectoriesRecursively_aux(channelSftp, directory,
remoteDirectory); remoteDirectory);
// Fire the upload completed event
uploadCompletedEvent.accept(remoteDirectory);
} catch (SftpException sftpex) { } catch (SftpException sftpex) {
// TODO maybe no permissions // TODO maybe no permissions
} catch (Exception e) { } catch (Exception e) {

View File

@ -15,6 +15,9 @@ import code.SshEngine;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import javax.swing.SwingUtilities;
public class DesktopController { 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<String> uploadCompletedEvent = s -> this
.uploadCompletedEvent(s);
for (File file : selectedFiles) { for (File file : selectedFiles) {
SshEngine.uploadFile(file, this.getCurrentWorkingDirectory()); SshEngine.uploadFile(file, this.getCurrentWorkingDirectory(),
uploadCompletedEvent);
} }
for (File directory : selectedDirectories) { for (File directory : selectedDirectories) {
SshEngine.uploadDirectoriesRecursively(directory, SshEngine.uploadDirectoriesRecursively(directory,
this.getCurrentWorkingDirectory()); this.getCurrentWorkingDirectory(), uploadCompletedEvent);
} }
} }
@ -449,6 +459,34 @@ public class DesktopController {
return title.toString(); 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 ========== * ========== END Other ==========
*/ */

View File

@ -165,16 +165,13 @@ public class Desktop extends JFrame implements IDesktopFrame {
((DesktopController) controller) ((DesktopController) controller)
.uploadToRemoteServer(droppedFileArray); .uploadToRemoteServer(droppedFileArray);
drawComponentsForDirectory(
((DesktopController) controller)
.getCurrentWorkingDirectory());
} catch (UnsupportedFlavorException | IOException e) { } catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
dtde.dropComplete(true); 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) { && fileChooser.getSelectedFiles().length > 0) {
controller.uploadToRemoteServer( controller.uploadToRemoteServer(
fileChooser.getSelectedFiles()); fileChooser.getSelectedFiles());
drawComponentsForDirectory(
controller.getCurrentWorkingDirectory());
} }
} }