Skip to content

Commit

Permalink
Merge pull request #32 from brianmcmichael/progress
Browse files Browse the repository at this point in the history
Progress tracking
  • Loading branch information
liry committed Feb 7, 2016
2 parents 1902878 + c1eb93f commit 8010e82
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/brianmcmichael/sagu/SAGU.java
Original file line number Diff line number Diff line change
Expand Up @@ -803,15 +803,16 @@ protected Object doInBackground() throws Exception {
+ uploadFileBatch.length + ")"
+ " Uploading: " + thisFile);

UploadResult result = atm.upload(vaultName, description, uploadFileBatch[i]);
UploadResult result = atm.upload(null, vaultName, description, uploadFileBatch[i],
new OneFileProgressListener(uw, uploadFileBatch[i].length()));

uw.addToLog("Done: " + thisFile + "\n");
uw.addToFinishedFiles(thisFile + "\n");

uploadedSize += uploadFileBatch[i].length();

int percentage = (int) (((double) uploadedSize / totalSize) * 100);

uw.updateProgress(percentage);
uw.updateAllFilesProgress(percentage);

final LogWriter logWriter;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors
*
* This program is free software licensed under GNU General Public License
* found in the LICENSE file in the root directory of this source tree.
*/

package com.brianmcmichael.sagu.ui;

import com.amazonaws.event.ProgressEvent;
import com.amazonaws.event.ProgressTracker;

/**
* Listener for upload progress reporting of one file (currently being uploaded).
*/
public class OneFileProgressListener extends ProgressTracker {

private final UploadWindow uploadWindow;
private final long totalBytesToTransfer;

public OneFileProgressListener(final UploadWindow uploadWindow, final long totalBytesToTransfer) {
this.uploadWindow = uploadWindow;
this.totalBytesToTransfer = totalBytesToTransfer;
}

@Override
public void progressChanged(final ProgressEvent progressEvent) {
super.progressChanged(progressEvent);
final int percents = getPercents(getProgress().getRequestBytesTransferred());
uploadWindow.updateOneFileProgress(percents);
}

int getPercents(final double bytesTransferred) {
return totalBytesToTransfer <= 0
? 0
: (int) ((bytesTransferred / (double) totalBytesToTransfer) * 100.0);
}
}
111 changes: 90 additions & 21 deletions src/main/java/com/brianmcmichael/sagu/ui/UploadWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,109 @@
import javax.swing.*;
import java.awt.*;

/**
* Dialog window for upload progress displaying.
*/
public class UploadWindow extends JFrame {

private static final long serialVersionUID = 1L;

private JTextArea uploadText = new JTextArea();
private static final long serialVersionUID = 1L;

private JScrollPane uploadScroll = new JScrollPane(uploadText);
private JProgressBar totalProgressBar = new JProgressBar(0, 100);
private final JTextArea finishedFilesArea = new JTextArea();
private final JProgressBar allFilesProgressBar = new JProgressBar(0, 100);
private final JProgressBar oneFileProgressBar = new JProgressBar(0, 100);

/**
* Initializes and displays the Upload Window.
*/
public UploadWindow() {
setTitle("Uploading");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JProgressBar dumJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);
dumJProgressBar.setIndeterminate(true);
add(dumJProgressBar, BorderLayout.NORTH);
add(uploadScroll, BorderLayout.CENTER);
add(totalProgressBar, BorderLayout.SOUTH);
setSize(500, 400);
uploadText.setEditable(false);
setLocationRelativeTo(null);

initUI();

setVisible(true);
}

public void addToLog(String text) {
uploadText.append(text);
/**
* Adds file (path string) to the list of finished files.
*
* @param filePath file path string to be added
*/
public void addToFinishedFiles(final String filePath) {
finishedFilesArea.append(filePath);
}

/**
* Updates total (all files) progress.
*
* @param percentage progress percentage to be displayed
*/
public void updateAllFilesProgress(final int percentage) {
SwingUtilities.invokeLater(() -> allFilesProgressBar.setValue(percentage));
}

/**
* Updates one (current) file progress.
*
* @param percentage progress percentage to be displayed
*/
public void updateOneFileProgress(final int percentage) {
SwingUtilities.invokeLater(() -> oneFileProgressBar.setValue(percentage));
}

public void updateProgress(final int percentage) {
SwingUtilities.invokeLater(new Runnable() {
private void initUI() {
setTitle("Uploading");
setLayout(new GridBagLayout());

final GridBagConstraints constraints = new GridBagConstraints();
constraints.weightx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(5, 5, 5, 5);
final JLabel oneFileLabel = new JLabel();
oneFileLabel.setText("Current file:");
add(oneFileLabel, constraints);

constraints.weightx = 10;
constraints.gridx = 1;
add(oneFileProgressBar, constraints);

constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 1;
final JLabel allFilesLabel = new JLabel();
allFilesLabel.setText("All files:");
add(allFilesLabel, constraints);

constraints.weightx = 10;
constraints.gridx = 1;
add(allFilesProgressBar, constraints);

constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.insets = new Insets(0, 0, 0, 0);
final JSeparator separator = new JSeparator();
add(separator, constraints);

constraints.insets = new Insets(5, 5, 0, 5);
constraints.gridy = 3;
final JLabel finishedFilesLabel = new JLabel();
finishedFilesLabel.setText("Finished files:");
add(finishedFilesLabel, constraints);

constraints.insets = new Insets(5, 5, 5, 5);
constraints.weightx = 1;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 2;
add(new JScrollPane(finishedFilesArea), constraints);

@Override
public void run() {
totalProgressBar.setValue(percentage);
}
});
finishedFilesArea.setEditable(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors
*
* This program is free software licensed under GNU General Public License
* found in the LICENSE file in the root directory of this source tree.
*/

package com.brianmcmichael.sagu.ui;

import org.testng.annotations.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.*;

public class OneFileProgressListenerTest {

@Test
public void getPercentsShouldReturnZeroWhenTransferredZero() throws Exception {
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000);
assertThat(listener.getPercents(0), is(0));
}

@Test
public void getPercentsShouldReturnFiftyWhenTransferredHalf() throws Exception {
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000);
assertThat(listener.getPercents(500), is(50));
}

@Test
public void getPercentsShouldReturnHundredWhenEverythingTransferred() throws Exception {
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 1000);
assertThat(listener.getPercents(1000), is(100));
}

@Test
public void getPercentsShouldReturnZeroWhenTotalZero() throws Exception {
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), 0);
assertThat(listener.getPercents(0), is(0));
}

@Test
public void getPercentsShouldReturnZeroWhenTotalNegative() throws Exception {
final OneFileProgressListener listener = new OneFileProgressListener(mock(UploadWindow.class), -1);
assertThat(listener.getPercents(0), is(0));
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/brianmcmichael/sagu/ui/UploadWindowTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Simple Amazon Glacier Uploader - GUI client for Amazon Glacier
* Copyright (C) 2012-2015 Brian L. McMichael, Libor Rysavy and other contributors
*
* This program is free software licensed under GNU General Public License
* found in the LICENSE file in the root directory of this source tree.
*/

package com.brianmcmichael.sagu.ui;

import org.testng.annotations.Test;

import static org.testng.Assert.*;

public class UploadWindowTest {

// just for manual UI testing
@Test(enabled = false)
public void testUI() throws Exception {
new UploadWindow();
Thread.sleep(10000);
}
}

0 comments on commit 8010e82

Please sign in to comment.