/*
 * DlgPatchProgress.java
 *
 * Created on 22 mars 2007, 11:43
 */

package fr.gouv.finances.cp.xemelios.data.ui;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @author  chm
 */
public class DlgPatchProgress extends javax.swing.JDialog implements PatchProgress {
    private long maxProgress, currentProgress;
    
    /** Creates new form DlgPatchProgress */
    public DlgPatchProgress() {
        super((JFrame)null, false);
        initComponents();
        setLocationRelativeTo(null);
    }

    public void startProgress(String message) {
        this.message.setText(message);
        startProgress();
        pack();
    }

    public void startProgress() {
        currentProgress = 0L;
    }

    public void setMaxProgress(long maxProgress) {
        this.maxProgress = maxProgress;
    }

    public void pushProgress() {
        currentProgress++;
        displayProgress();
    }

    public void endProgress() {
        currentProgress = maxProgress;
        displayProgress();
    }
    private void displayProgress() {
        final int progressValue = (int)((float)currentProgress/(float)maxProgress*100f);
        Runnable p = new Runnable() {
           public void run() {
               progress.setValue(progressValue);
               progress.paintImmediately(0,0,progress.getWidth(),progress.getHeight());
           } 
        } ;
        if(SwingUtilities.isEventDispatchThread()) {
            p.run();
        }
        else try { 
            SwingUtilities.invokeAndWait(p);
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }
    
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        message = new javax.swing.JLabel();
        progress = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Mise \u00e0 jour de la base de donn\u00e9es");
        setAlwaysOnTop(true);
        setResizable(false);

        progress.setStringPainted(true);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, progress, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, message, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(message, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 9, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(progress, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel message;
    private javax.swing.JProgressBar progress;
    // End of variables declaration//GEN-END:variables
    
}
