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;
020
021import java.awt.BorderLayout;
022import java.awt.Dimension;
023import java.awt.Font;
024import java.awt.event.ActionEvent;
025import java.awt.event.ActionListener;
026
027import javax.swing.BoxLayout;
028import javax.swing.JButton;
029import javax.swing.JFrame;
030import javax.swing.JLabel;
031import javax.swing.JPanel;
032import javax.swing.JRootPane;
033import javax.swing.SwingConstants;
034import javax.swing.Timer;
035import javax.swing.border.BevelBorder;
036
037import icy.main.Icy;
038
039public class ExitFrame extends JFrame
040{
041    /**
042     * 
043     */
044    private static final long serialVersionUID = 5980838429118602985L;
045
046    private JPanel buttonPanel;
047
048    boolean forced;
049    Timer timer;
050    private JPanel forceQuitPanel;
051
052    /**
053     * Create the frame.
054     */
055    public ExitFrame(int forceDelay)
056    {
057        super();
058
059        forced = false;
060        timer = new Timer(forceDelay, new ActionListener()
061        {
062            @Override
063            public void actionPerformed(ActionEvent e)
064            {
065                displayForcePanel();
066            }
067        });
068
069        initialize();
070
071        // default
072        forceQuitPanel.setVisible(false);
073        buttonPanel.setVisible(false);
074
075        if (forceDelay > 0)
076            timer.start();
077        else if (forceDelay == 0)
078            displayForcePanel();
079
080        getRootPane().setWindowDecorationStyle(JRootPane.NONE);
081
082        // pack, center and display
083        pack();
084        setLocationRelativeTo(Icy.getMainInterface().getMainFrame());
085        setAlwaysOnTop(true);
086        setVisible(true);
087    }
088
089    public ExitFrame()
090    {
091        this(-1);
092    }
093
094    public void displayForcePanel()
095    {
096        forceQuitPanel.setVisible(true);
097        buttonPanel.setVisible(true);
098
099        // pack and center
100        pack();
101        setLocationRelativeTo(Icy.getMainInterface().getMainFrame());
102    }
103
104    public boolean isForced()
105    {
106        return forced;
107    }
108
109    void initialize()
110    {
111        setTitle("Exit");
112        setSize(new Dimension(400, 140));
113
114        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
115
116        JPanel panel_1 = new JPanel();
117        panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
118        getContentPane().add(panel_1, BorderLayout.CENTER);
119        panel_1.setLayout(new BorderLayout(0, 0));
120
121        buttonPanel = new JPanel();
122        panel_1.add(buttonPanel, BorderLayout.SOUTH);
123
124        JButton button = new JButton("Force Quit");
125        button.addActionListener(new ActionListener()
126        {
127            @Override
128            public void actionPerformed(ActionEvent e)
129            {
130                forced = true;
131                // close frame
132                dispose();
133            }
134        });
135        button.setFont(new Font("Tahoma", Font.BOLD, 14));
136        buttonPanel.add(button);
137
138        JPanel panel = new JPanel();
139        panel_1.add(panel, BorderLayout.CENTER);
140        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
141
142        JPanel panel_2 = new JPanel();
143        panel.add(panel_2);
144
145        JLabel label = new JLabel("Please wait while exiting...");
146        panel_2.add(label);
147        label.setHorizontalAlignment(SwingConstants.CENTER);
148        label.setFont(new Font("Tahoma", Font.BOLD, 16));
149
150        forceQuitPanel = new JPanel();
151        panel.add(forceQuitPanel);
152
153        JLabel forceQuitLabel = new JLabel("Click on 'Force Quit' to kill remaining tasks and exit.");
154        forceQuitPanel.add(forceQuitLabel);
155        forceQuitLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
156        forceQuitLabel.setHorizontalAlignment(SwingConstants.CENTER);
157    }
158}