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.gui.frame.IcyExternalFrame;
022import icy.gui.frame.IcyFrame;
023import icy.gui.frame.IcyInternalFrame;
024import icy.gui.main.MainFrame;
025import icy.gui.main.TaskFrameManager;
026import icy.main.Icy;
027import icy.system.thread.ThreadUtil;
028
029import java.awt.BorderLayout;
030import java.awt.Dimension;
031
032import javax.swing.BorderFactory;
033import javax.swing.JPanel;
034
035/**
036 * Use it to create a Task Window on the border like the loader (Thread Safe)<br>
037 * 
038 * @author fab & Stephane
039 */
040public abstract class TaskFrame extends IcyFrame
041{
042    protected JPanel mainPanel;
043    private boolean remove;
044
045    /**
046     * 
047     */
048    public TaskFrame()
049    {
050        this("Task in progress", false, false, false, false);
051    }
052
053    /**
054     * @param title
055     */
056    public TaskFrame(String title)
057    {
058        this(title, false, false, false, false);
059    }
060
061    /**
062     * @param title
063     * @param resizable
064     */
065    public TaskFrame(String title, boolean resizable)
066    {
067        this(title, resizable, false, false, false);
068    }
069
070    /**
071     * @param title
072     * @param resizable
073     * @param closable
074     */
075    public TaskFrame(String title, boolean resizable, boolean closable)
076    {
077        this(title, resizable, closable, false, false);
078    }
079
080    /**
081     * @param title
082     * @param resizable
083     * @param closable
084     * @param maximizable
085     */
086    public TaskFrame(String title, boolean resizable, boolean closable, boolean maximizable)
087    {
088        this(title, resizable, closable, maximizable, false);
089    }
090
091    /**
092     * @param title
093     * @param resizable
094     * @param closable
095     * @param maximizable
096     * @param iconifiable
097     */
098    public TaskFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
099    {
100        super(title, resizable, closable, maximizable, iconifiable, true);
101
102        remove = false;
103
104        ThreadUtil.invokeLater(new Runnable()
105        {
106            @Override
107            public void run()
108            {
109                final IcyInternalFrame iFrame = getIcyInternalFrame();
110                final IcyExternalFrame eFrame = getIcyExternalFrame();
111
112                mainPanel = new JPanel();
113                mainPanel.setBorder(BorderFactory.createTitledBorder(""));
114
115                // no border on frame
116                iFrame.setBorder(BorderFactory.createEmptyBorder());
117                // no focusable
118                iFrame.setFocusable(false);
119                eFrame.setFocusable(false);
120                eFrame.setFocusableWindowState(false);
121                // no title bar
122                iFrame.setTitleBarVisible(false);
123                eFrame.setTitleBarVisible(false);
124
125                // set maximum size of task frame
126                Dimension maxDim = new Dimension(800, 600);
127
128                final MainFrame mf = Icy.getMainInterface().getMainFrame();
129                if (mf != null)
130                {
131                    final Dimension desktopSize = mf.getDesktopSize();
132                    if (desktopSize != null)
133                        maxDim = desktopSize;
134                }
135                iFrame.setMaximumSize(maxDim);
136                eFrame.setMaximumSize(maxDim);
137
138                if (isInternalized())
139                {
140                    iFrame.setLayout(new BorderLayout());
141                    iFrame.add(mainPanel, BorderLayout.CENTER);
142                }
143                else
144                {
145                    eFrame.setLayout(new BorderLayout());
146                    eFrame.add(mainPanel, BorderLayout.CENTER);
147                }
148
149                // add to the task manager if a GUI is present
150                final TaskFrameManager tfm = Icy.getMainInterface().getTaskWindowManager();
151
152                if (tfm != null)
153                    tfm.addTaskWindow(TaskFrame.this);
154            }
155        });
156    }
157
158    @Override
159    public void stateChanged()
160    {
161        super.stateChanged();
162
163        // re pack the frame
164        pack();
165    }
166
167    @Override
168    public void close()
169    {
170        // prevent direct close here
171        remove = true;
172    }
173
174    /**
175     * Used by the TaskWindowManager to close the TaskFrame.<br>
176     * (internal use only)
177     */
178    public void internalClose()
179    {
180        super.close();
181    }
182
183    /**
184     * Used by the TaskWindowManager to close the TaskFrame.<br>
185     * (internal use only)
186     */
187    public boolean canRemove()
188    {
189        return remove;
190    }
191
192    /**
193     * @deprecated Use {@link #canRemove()} instead
194     */
195    @Deprecated
196    public boolean isCanBeRemoved()
197    {
198        return canRemove();
199    }
200}