001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.frame.progress;
020
021import icy.system.thread.ThreadUtil;
022
023import java.awt.BorderLayout;
024import java.awt.event.ActionEvent;
025import java.awt.event.ActionListener;
026
027import javax.swing.JButton;
028
029/**
030 * @author stephane
031 */
032public class CancelableProgressFrame extends ProgressFrame implements ActionListener
033{
034    // GUI
035    JButton cancelBtn;
036
037    private boolean cancelRequested;
038
039    /**
040     * @param message
041     */
042    public CancelableProgressFrame(String message)
043    {
044        super(message);
045
046        cancelRequested = false;
047
048        ThreadUtil.invokeLater(new Runnable()
049        {
050            @Override
051            public void run()
052            {
053                cancelBtn = new JButton("Cancel");
054
055                cancelBtn.setFocusPainted(false);
056                cancelBtn.addActionListener(CancelableProgressFrame.this);
057
058                mainPanel.add(cancelBtn, BorderLayout.EAST);
059
060                pack();
061            }
062        });
063    }
064
065    @Override
066    public void onClosed()
067    {
068        super.onClosed();
069
070        // so we force cancel operation on application exit
071        cancelRequested = true;
072    }
073
074    /**
075     * Request cancel and close the frame
076     */
077    public void cancel()
078    {
079        cancelRequested = true;
080        close();
081    }
082
083    /**
084     * @return the cancelRequested
085     */
086    public boolean isCancelRequested()
087    {
088        return cancelRequested;
089    }
090
091    public void setCancelEnabled(boolean enabled)
092    {
093        if (cancelBtn != null)
094            cancelBtn.setEnabled(enabled);
095    }
096
097    @Override
098    public void actionPerformed(ActionEvent e)
099    {
100        // notify cancel requested
101        cancelRequested = true;
102        // close frame
103        close();
104    }
105
106    @Override
107    public boolean notifyProgress(double position, double length)
108    {
109        // cancel requested ?
110        if (isCancelRequested())
111            return false;
112
113        return super.notifyProgress(position, length);
114    }
115}