Improvements

1) The Queue does not get updated each time a chunk of data gets transferred, but only after a certain amount of time has passed (1/100th of a second)
2) Improved desktop's scroll behaviour
This commit is contained in:
xfarrow 2023-07-24 15:08:10 +02:00
parent b39d82a671
commit ad4d91708d
3 changed files with 30 additions and 40 deletions

View File

@ -18,8 +18,10 @@ public class QueueController implements Observer{
*/ */
private IQueueFrame frame; private IQueueFrame frame;
// Accessed only through the EDT, so no need to use ConcurrentHashMap
private HashMap<TransferProgress, Integer> indexHashMap = new HashMap<>(); private HashMap<TransferProgress, Integer> indexHashMap = new HashMap<>();
private long lastGuiExecutionTime = 0;
private static final long THROTTLE_TIME_MS = 10; // 10ms = 1/100th of second
/* /*
* ========== END Attributes ========== * ========== END Attributes ==========
*/ */
@ -47,17 +49,23 @@ public class QueueController implements Observer{
} }
} }
// Executed by different threads // Executed on different threads
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
TransferProgress transferProgress = (TransferProgress)arg; TransferProgress transferProgress = (TransferProgress)arg;
// Do not invoke the Event Dispatch Thread too frequently as
// it may impact the performance of low-end computer systems.
if(System.currentTimeMillis() - lastGuiExecutionTime >= THROTTLE_TIME_MS || transferProgress.getTransferStatus() == TransferProgress.END) {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
frame.manageTransferProgress(transferProgress); frame.manageTransferProgress(transferProgress);
lastGuiExecutionTime = System.currentTimeMillis();
} }
}); });
} }
}
public int computePercentage(TransferProgress transferProgress) { public int computePercentage(TransferProgress transferProgress) {
// Avoid division by zero // Avoid division by zero

View File

@ -98,6 +98,7 @@ public class Desktop extends JFrame implements IDesktopFrame {
JScrollPane scrollPane = new JScrollPane(); JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(156, 36, 1098, 634); scrollPane.setBounds(156, 36, 1098, 634);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setUnitIncrement(25);
getContentPane().add(scrollPane); getContentPane().add(scrollPane);
desktopPanel = new JPanel(); desktopPanel = new JPanel();

View File

@ -91,17 +91,10 @@ public class Queue extends JFrame implements IQueueFrame {
@Override @Override
public void manageTransferProgress(TransferProgress transferProgress) { public void manageTransferProgress(TransferProgress transferProgress) {
if(transferProgress.getTransferStatus() == TransferProgress.INIT) { // Remember that when QueueController calls frame.manageTransferProgress(transferProgress),
if(!controller.isTransferProgressInHashMap(transferProgress)) { // here transferProgress might have a different status (as it's updated by a different thread).
controller.putTableIndex(transferProgress, // We do not need a lock as we do not edit it, but just keep it in mind.
addRow(transferProgress.getSource(),
transferProgress.getDestination(),
transferProgress.getOperation() == SftpProgressMonitor.GET? "Download" : "Upload",
0));
}
}
else if(transferProgress.getTransferStatus() == TransferProgress.UPDATING) {
if(!controller.isTransferProgressInHashMap(transferProgress)) { if(!controller.isTransferProgressInHashMap(transferProgress)) {
controller.putTableIndex(transferProgress, controller.putTableIndex(transferProgress,
addRow(transferProgress.getSource(), addRow(transferProgress.getSource(),
@ -110,22 +103,10 @@ public class Queue extends JFrame implements IQueueFrame {
controller.computePercentage(transferProgress))); controller.computePercentage(transferProgress)));
} }
else { else {
if(transferProgress.getTransferStatus() != TransferProgress.INIT) {
updateRow(controller.getTableIndex(transferProgress), controller.computePercentage(transferProgress)); updateRow(controller.getTableIndex(transferProgress), controller.computePercentage(transferProgress));
} }
}
else if(transferProgress.getTransferStatus() == TransferProgress.END) {
if(!controller.isTransferProgressInHashMap(transferProgress)) {
controller.putTableIndex(transferProgress,
addRow(transferProgress.getSource(),
transferProgress.getDestination(),
transferProgress.getOperation() == SftpProgressMonitor.GET? "Download" : "Upload",
100));
}
else {
updateRow(controller.getTableIndex(transferProgress), 100);
}
}
} }
} }
}