/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * 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/>.
 *
 * CopyProgressDialog.java
 *
 * Created on 27.6.2008, 14:08
 */

package org.wandora.utils;

/**
 *
 * @author  anttirt
 */
public class AbortableProgressDialog extends javax.swing.JDialog {
    
    private static final long serialVersionUID = 1L;
    
    
    private Runnable abortProc;
    private Abortable.Status status;
    private double ratio;
    
    /** Creates new form AbortableProgressDialog */
    public AbortableProgressDialog(java.awt.Frame parent, boolean modal, Runnable abortProc, String title) {
        super(parent, modal);
        
        this.abortProc = abortProc;
        status = Abortable.Status.InProgress;
        ratio = 0.0;
        initComponents();
        this.setTitle(title);
        this.setSize(320, 160);
        jProgressBar1.setMaximum(1000);
    }
    
    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        wandoraButton1 = new org.wandora.application.gui.simple.SimpleButton();
        msgLabel = new org.wandora.application.gui.simple.SimpleLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        closeBtn = new org.wandora.application.gui.simple.SimpleButton();

        wandoraButton1.setText("wandoraButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        msgLabel.setText("File copy progress:");
        msgLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
        getContentPane().add(msgLabel, gridBagConstraints);

        jProgressBar1.setMinimumSize(new java.awt.Dimension(300, 16));
        jProgressBar1.setPreferredSize(new java.awt.Dimension(300, 16));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10);
        getContentPane().add(jProgressBar1, gridBagConstraints);

        closeBtn.setText("Abort");
        closeBtn.setMaximumSize(new java.awt.Dimension(70, 23));
        closeBtn.setMinimumSize(new java.awt.Dimension(70, 23));
        closeBtn.setPreferredSize(new java.awt.Dimension(70, 23));
        closeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 7, 10);
        getContentPane().add(closeBtn, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void progress(double ratio, Abortable.Status status, String message) {
        this.ratio = ratio;
        this.status = status;
        msgLabel.setText("<html><body><p>" + message + "</p></body></html>");
        jProgressBar1.setValue((int)(ratio * 1000));
        
        switch(status) {
            case InProgress:
                break;
            case Success:
                closeBtn.setText("OK");
                break;
            case Failure:
                closeBtn.setText("OK");
                break;
        }
    }
    
    private void closeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeBtnActionPerformed
        switch(status) {
            case InProgress:
                if(abortProc != null)
                    abortProc.run();
                break;
            case Success:
                break;
            case Failure:
                break;
        }
        
        this.setVisible(false);
}//GEN-LAST:event_closeBtnActionPerformed
    
    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) {
        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                OperationProgressDialog dialog = new OperationProgressDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
        */
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.wandora.application.gui.simple.SimpleButton closeBtn;
    private javax.swing.JProgressBar jProgressBar1;
    private org.wandora.application.gui.simple.SimpleLabel msgLabel;
    private org.wandora.application.gui.simple.SimpleButton wandoraButton1;
    // End of variables declaration//GEN-END:variables
    
}
