/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.jar.Pack200;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import javax.swing.SwingConstants;
import org.apache.log4j.Logger;
import org.jdesktop.swingworker.SwingWorker;

/**
 * An {@link InfiniteGlassPane} that displays a cancel button.
 * @author chm
 */
public class InterruptibleInfiniteGlassPane extends InfiniteGlassPane {
    private static final Logger logger = Logger.getLogger(InterruptibleInfiniteGlassPane.class);
    private SwingWorker threadToInterrupt;
    protected JButton pbCancel;
    protected JLabel lblStop;
    
    /** Creates a new instance of InterruptibleInfiniteGlassPane */
    public InterruptibleInfiniteGlassPane() {
        super();
        setAlignmentX(0.5f);
        setAlignmentY(0.5f);
    }
    protected void initComponents() {
        setLayout(new OverlayLayout(this));
        lblStop = new JLabel("Arręt en cours...");
        lblStop.setForeground(Color.RED);
        lblStop.setAlignmentX(0.5f);
        lblStop.setAlignmentY(0.5f);
        lblStop.setHorizontalAlignment(SwingConstants.CENTER);
        JPanel p1 = new JPanel(new BorderLayout());
        p1.setOpaque(false);
        p1.setSize(150,80);
        Dimension dim = new Dimension(150,80);
        p1.setPreferredSize(dim);
        p1.setMinimumSize(dim);
        p1.setMaximumSize(dim);
        p1.add(lblStop,BorderLayout.SOUTH);
        add(p1);

        pbCancel=new JButton("Annuler");
        pbCancel.setAlignmentX(0.5f);
        pbCancel.setAlignmentY(0.5f);
        add(pbCancel);
        rotor=new JRotatePanel();
        rotor.setAlignmentX(0.5f);
        rotor.setAlignmentY(0.5f);
        add(rotor);
        setVisible(false);
        addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent e) {
                        rotor.setSize(getSize());
                        pbCancel.setLocation(3,(int)(getSize().getHeight()-pbCancel.getHeight()-3));
                }
        });
        pbCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblStop.setVisible(true);
                if(threadToInterrupt!=null) threadToInterrupt.cancel(true);
            }
        });
    }
    @Override
    public void setSize(Dimension d) {
        super.setSize(d);
        rotor.setSize(d);
    }
    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);
        rotor.setSize(width,height);
    }
    public void setVisible(SwingWorker sw) {
        threadToInterrupt=sw;
        lblStop.setVisible(false);
        super.setVisible(true);
        rotor.setSize(getSize());
        rotor.setVisible(true);
        pbCancel.setVisible(true);
    }
    @Override
    public void setVisible(boolean aFlag) {
        if(aFlag && threadToInterrupt==null) throw new UnsupportedOperationException("You must use setVisible(SwingWorker) to show this glassPane");
        super.setVisible(aFlag);
        if(!aFlag) lblStop.setVisible(false);
        rotor.setSize(getSize());
        rotor.setVisible(aFlag);
        pbCancel.setVisible(aFlag);
    }
    @Override
    protected void paintComponent(Graphics g) {
        rotor.paintBackground(g);
        rotor.paintComponent(g);
    }

    public void setThreadToInterrupt(SwingWorker threadToInterrupt) {
        this.threadToInterrupt = threadToInterrupt;
    }
    
}
