public class ProgressWindow extends JDialog {
/** unique ID for serialization */
private static final long serialVersionUID = 255284650387076779L;
/** Showing the progress */
private JProgressBar progress;
/** Label showing additional information */
private JLabel jlStatus;
/** Box showing buttons */
private Box buttonBox, progressBarBox, labelBox;
/**
* Member implicating if file correction is on hold
*/
private boolean paused = false;
/**
* Member implicating if file correction must be cancelled
*/
private boolean running = true;
/**
* Status before pause is stored here
*/
private String lastStatus;
/**
* Progress window
*
* @param owner {@link JFrame}
* @param title {@link String}
*/
public ProgressWindow(JFrame owner, String title) {
super(owner, title, false);
//Init GUI
setSize(500, 140);
setLocationRelativeTo(owner);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
//Current file
jlStatus = new JLabel();
labelBox = Box.createHorizontalBox();
labelBox.add(jlStatus, Box.CENTER_ALIGNMENT);
add(labelBox);
//Progress bar
progress = new JProgressBar();
progress.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
progress.setValue(0);
progress.setStringPainted(true);
progressBarBox = Box.createHorizontalBox();
progressBarBox.add(progress, Box.CENTER_ALIGNMENT);
add(progressBarBox);
//Buttons
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
running = false;
}
});
final JButton pause = new JButton("Pause");
pause.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Pause")) {
pause.setText("Continue");
setPaused(true);
} else { // Continue
pause.setText("Pause");
setPaused(false);
}
}
});
buttonBox = Box.createHorizontalBox();
buttonBox.add(pause, Box.RIGHT_ALIGNMENT);
buttonBox.add(cancel, Box.LEFT_ALIGNMENT);
add(buttonBox);
setVisible(true);
}
/**
* Shows that action has been finished
*/
public void showFinish() {
//Show finish
buttonBox.removeAll();
JButton finish = new JButton("Finished");
finish.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ProgressWindow.this.dispose();
}
});
buttonBox.add(finish);
//Force refresh
setSize(500, 141);
}
/**
* Set value for progress bar
* @param value 0-100
*/
public void showProgress(final int value) {
try {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progress.setValue(value);
}
});
} catch (Exception e) {
// do nothing
}
}
/**
* Calculates the progress in a range from 0-100
* @param current current value
* @param all total
*/
public void showProgress(int current, int all) {
showProgress((int) ((double) (current + 1) / all * 100d));
}
/**
* Set status
* @param status String
*/
public void setStatus(final String status) {
try {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jlStatus.setText(status);
}
});
} catch (Exception e) {
// do nothing
}
}
/**
* Pause the current thread with busy waiting
*/
public void doPauseIfNeeded() {
//Paused?
while (paused) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
//do nothing
}
}
}
/**
* Getter for paused
* @return true if paused
*/
public boolean isPaused() {
return paused;
}
/**
* Setter for paused
* @param paused true if paused
*/
public void setPaused(boolean paused) {
if(paused) {
lastStatus = jlStatus.getText();
setStatus("Paused");
} else {
setStatus(lastStatus);
}
this.paused = paused;
}
/**
* Getter for running
* @return false if must be canceled
*/
public boolean isRunning() {
return running;
}
/**
* Setter for running
* @param running true, if running
*/
public void setRunning(boolean running) {
this.running = running;
}
}