import java.util.Date;
public class ProgressFrame extends javax.swing.JFrame {
private Date lastChange = null;
private int value = 0;
private int lastValue = 0;
private int maximum;
public ProgressFrame(String title) {
initComponents();
setTitle(title);
progressBar.setStringPainted(false);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
pack();
setLocationRelativeTo(null);
}
public void setValue(int value)
{
progressBar.setValue(value);
this.value = value;
if (lastChange == null)
{
lastChange = new Date();
}
Date now = new Date();
long diff = now.getTime() - lastChange.getTime();
if (diff >= 1000)
{
int dx = (int) (((new Date()).getTime() - lastChange.getTime()));
float change = (float) (value - lastValue) / (float) dx;
etaLabel.setText(
formatAsETA((int) (((float) (maximum - value) / change) / 1000))
+ " remaining"
);
this.lastValue = value;
lastChange = now;
}
}
public void setMinimum(int min)
{
progressBar.setMinimum(min);
}
public void setMaximum(int max)
{
progressBar.setMaximum(max);
maximum = max;
}
public void setAction(String action)
{
currentActionLabel.setText(action);
}
public void setIndeterminate(boolean is)
{
progressBar.setIndeterminate(is);
if (is)
{
etaLabel.setText("");
}
else
{
etaLabel.setText(
"estimating..."
);
}
}
private static String formatAsETA(int nSeconds)
{
int minutes = 0;
while (nSeconds >= 60)
{
nSeconds -= 60;
minutes++;
}
if (minutes != 0)
{
return minutes + "m " + nSeconds + "s";
}
else
{
return nSeconds + "s";
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
currentActionLabel = new javax.swing.JLabel();
etaLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setResizable(false);
currentActionLabel.setText("-");
etaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
etaLabel.setText("estimating...");
progressBar.setToolTipText("");
progressBar.setValue(30);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(currentActionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
.addComponent(etaLabel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(currentActionLabel)
.addComponent(etaLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel currentActionLabel;
private javax.swing.JLabel etaLabel;
private javax.swing.JProgressBar progressBar;
// End of variables declaration//GEN-END:variables
}