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;
026import java.util.Timer;
027import java.util.TimerTask;
028
029import javax.swing.JButton;
030import javax.swing.JLabel;
031
032public class AnnounceFrame extends TaskFrame implements ActionListener
033{
034    JButton button;
035    JLabel label;
036    Timer timer;
037    Runnable action;
038
039    /**
040     * Show an announcement with specified message.
041     * 
042     * @param message
043     *        message to display in announcement
044     */
045    public AnnounceFrame(final String message)
046    {
047        this(message, "Ok", null, 0);
048    }
049
050    /**
051     * Show an announcement with specified parameters.
052     * 
053     * @param message
054     *        message to display in announcement
055     * @param btnAction
056     *        action on button click
057     */
058    public AnnounceFrame(final String message, final Runnable btnAction)
059    {
060        this(message, "Ok", btnAction, 0);
061    }
062
063    /**
064     * Show an announcement with specified parameters
065     * 
066     * @param message
067     *        message to display in announcement
068     * @param liveTime
069     *        life time in second (0 = infinite)
070     */
071    public AnnounceFrame(final String message, final int liveTime)
072    {
073        this(message, "Ok", null, liveTime);
074    }
075
076    /**
077     * Show an announcement with specified parameters
078     * 
079     * @param message
080     *        message to display in announcement
081     * @param btnAction
082     *        action on button click
083     * @param liveTime
084     *        life time in second (0 = infinite)
085     */
086
087    public AnnounceFrame(final String message, final Runnable btnAction, final int liveTime)
088    {
089        this(message, "Ok", btnAction, liveTime);
090    }
091
092    /**
093     * Show an announcement with specified parameters
094     * 
095     * @param message
096     *        message to display in announcement
097     * @param buttonText
098     *        button text
099     * @param btnAction
100     *        action on button click
101     * @param liveTime
102     *        life time in second (0 = infinite)
103     */
104    public AnnounceFrame(final String message, final String buttonText, final Runnable btnAction, final int liveTime)
105    {
106        super("");
107
108        if (liveTime != 0)
109        {
110            timer = new Timer("Announce timer");
111            timer.schedule(new TimerTask()
112            {
113                @Override
114                public void run()
115                {
116                    // close frame (EDT safe)
117                    close();
118                }
119            }, liveTime * 1000);
120        }
121
122        ThreadUtil.invokeLater(new Runnable()
123        {
124            @Override
125            public void run()
126            {
127                button = new JButton();
128                label = new JLabel();
129                action = btnAction;
130
131                label.setText("   " + message + "   ");
132                button.setText(buttonText);
133                button.setFocusable(false);
134                button.addActionListener(AnnounceFrame.this);
135
136                mainPanel.setLayout(new BorderLayout());
137
138                mainPanel.add(label, BorderLayout.CENTER);
139                mainPanel.add(button, BorderLayout.EAST);
140
141                pack();
142            }
143        });
144    }
145
146    /**
147     * @return the action
148     */
149    public Runnable getAction()
150    {
151        return action;
152    }
153
154    /**
155     * @param action
156     *        the action to set
157     */
158    public void setAction(Runnable action)
159    {
160        this.action = action;
161    }
162
163    @Override
164    public void internalClose()
165    {
166        // stop timer
167        if (timer != null)
168            timer.cancel();
169
170        super.internalClose();
171    }
172
173    @Override
174    public void actionPerformed(ActionEvent e)
175    {
176        if (e.getSource() == button)
177        {
178            // execute action
179            if (action != null)
180                action.run();
181        }
182
183        // close frame on both action (timer or button)
184        close();
185    }
186
187}