/*
* Copyright (C) 2014 GG-Net GmbH - Oliver Günther
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.ggnet.dwoss.progress;
import eu.ggnet.saft.api.progress.IMonitor;
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.*;
/**
*
* @author oliver.guenther
*/
public class JDialogMonitor extends JDialog implements IMonitor {
private BoundedRangeModel model;
/** Creates new form JFrameMonitor */
public JDialogMonitor(Window parent) {
super(parent);
initComponents();
setLocationRelativeTo(parent);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
progressBar = new javax.swing.JProgressBar();
messageLabel = new javax.swing.JLabel();
cancelButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
messageLabel.setText(" ");
cancelButton.setText("Abbrechen");
cancelButton.setEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(messageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton))
.addComponent(progressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(messageLabel)
.addComponent(cancelButton))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
@SuppressWarnings("SleepWhileInLoop")
public static void main(String args[]) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JDialogMonitor monitor = new JDialogMonitor(null);
SubMonitor m = SubMonitor.convert(monitor, "TestMonitor", 100);
SubMonitor c1 = m.newChild(30);
c1.setWorkRemaining(100);
c1.start();
c1.message("tu so also, ob ich lade");
Thread.sleep(10000);
for (int i = 0; i < 100; i++) {
c1.worked(1, "Worked C1: " + i);
Thread.sleep(50);
}
c1.finish();
SubMonitor c2 = SubMonitor.convert(m.newChild(30), 10);
c2.start();
for (int i = 0; i <= 10; i++) {
c2.worked(1, "Worked C2: " + i);
Thread.sleep(50);
}
c2.finish();
m.start();
for (int i = 0; i < 40; i++) {
m.worked(1, "Worked: " + i);
Thread.sleep(50);
}
m.finish();
System.exit(0);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JLabel messageLabel;
private javax.swing.JProgressBar progressBar;
// End of variables declaration//GEN-END:variables
@Override
public IMonitor title(String title) {
setTitle(title);
return this;
}
@Override
public IMonitor start() {
this.model = new DefaultBoundedRangeModel(0, 1, 0, 100);
progressBar.setModel(model);
progressBar.setIndeterminate(true);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JDialogMonitor.this.setVisible(true);
}
});
return this;
}
@Override
public IMonitor worked(int i) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
if ( progressBar.isIndeterminate() ) progressBar.setIndeterminate(false);
}
});
model.setValue(model.getValue() + i);
return this;
}
@Override
public IMonitor message(final String message) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
messageLabel.setText(message);
}
});
return this;
}
@Override
public IMonitor worked(int i, String string) {
worked(i);
message(string);
return this;
}
@Override
public IMonitor finish() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
if ( progressBar.isIndeterminate() ) progressBar.setIndeterminate(false);
JDialogMonitor.this.setVisible(false);
}
});
model.setValue(model.getMaximum());
return this;
}
@Override
public int getAbsolutRemainingTicks() {
return model.getMaximum() - model.getValue();
}
}