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.dialog;
020
021import icy.gui.util.ComponentUtil;
022import icy.main.Icy;
023import icy.resource.ResourceUtil;
024
025import java.awt.BorderLayout;
026import java.awt.Frame;
027import java.awt.GridBagConstraints;
028import java.awt.GridBagLayout;
029import java.awt.Insets;
030import java.awt.Rectangle;
031import java.awt.event.ActionEvent;
032import java.awt.event.ActionListener;
033import java.awt.event.WindowAdapter;
034import java.awt.event.WindowEvent;
035
036import javax.swing.BorderFactory;
037import javax.swing.JButton;
038import javax.swing.JComponent;
039import javax.swing.JDialog;
040import javax.swing.JPanel;
041
042/**
043 * @author Stephane
044 */
045public class ActionDialog extends JDialog implements ActionListener
046{
047    /**
048     * 
049     */
050    private static final long serialVersionUID = -8071873763517931268L;
051
052    protected static final String OK_CMD = "ok";
053    protected static final String CANCEL_CMD = "cancel";
054
055    protected JPanel mainPanel;
056    protected JPanel buttonPanel;
057
058    JButton okBtn;
059    JButton cancelBtn;
060
061    private ActionListener okAction;
062    private boolean closeAfterAction;
063    boolean opened;
064    boolean canceled;
065    boolean closed;
066
067    public ActionDialog(String title, JComponent component, Frame owner)
068    {
069        super(owner, title, true);
070
071        // init GUI
072        initialize(component);
073
074        // set action
075        okBtn.setActionCommand(OK_CMD);
076        cancelBtn.setActionCommand(CANCEL_CMD);
077        okBtn.addActionListener(this);
078        cancelBtn.addActionListener(this);
079
080        addWindowListener(new WindowAdapter()
081        {
082            @Override
083            public void windowOpened(WindowEvent e)
084            {
085                opened = true;
086            }
087
088            @Override
089            public void windowClosed(WindowEvent e)
090            {
091                closed = true;
092
093                onClosed();
094            }
095        });
096
097        okAction = null;
098        closeAfterAction = true;
099        opened = false;
100        canceled = true;
101        closed = false;
102    }
103
104    public ActionDialog(String title, JComponent component)
105    {
106        this(title, component, Icy.getMainInterface().getMainFrame());
107    }
108
109    /**
110     * @wbp.parser.constructor
111     */
112    public ActionDialog(String title)
113    {
114        this(title, null, Icy.getMainInterface().getMainFrame());
115    }
116
117    /**
118     * @deprecated Use {@link #ActionDialog(String, JComponent, Frame)} instead
119     */
120    @Deprecated
121    public ActionDialog(Frame owner, String title)
122    {
123        this(title, null, owner);
124    }
125
126    private void initialize(JComponent component)
127    {
128        setIconImages(ResourceUtil.getIcyIconImages());
129        // so we always pass in the closed event
130        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
131
132        // GUI
133        if (component instanceof JPanel)
134            mainPanel = (JPanel) component;
135        else
136        {
137            mainPanel = new JPanel();
138            mainPanel.setLayout(new BorderLayout());
139
140            if (component != null)
141                mainPanel.add(component, BorderLayout.CENTER);
142        }
143
144        buttonPanel = new JPanel();
145        buttonPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
146        GridBagLayout gbl_buttonPanel = new GridBagLayout();
147        gbl_buttonPanel.columnWidths = new int[] {0, 0, 45, 65, 0, 0, 0};
148        gbl_buttonPanel.rowHeights = new int[] {23, 0};
149        gbl_buttonPanel.columnWeights = new double[] {0.0, 0.0, 1.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
150        gbl_buttonPanel.rowWeights = new double[] {0.0, Double.MIN_VALUE};
151        buttonPanel.setLayout(gbl_buttonPanel);
152
153        okBtn = new JButton("Ok");
154        GridBagConstraints gbc_okBtn = new GridBagConstraints();
155        gbc_okBtn.anchor = GridBagConstraints.EAST;
156        gbc_okBtn.insets = new Insets(0, 0, 0, 5);
157        gbc_okBtn.gridx = 2;
158        gbc_okBtn.gridy = 0;
159        buttonPanel.add(okBtn, gbc_okBtn);
160        cancelBtn = new JButton("Cancel");
161        GridBagConstraints gbc_cancelBtn = new GridBagConstraints();
162        gbc_cancelBtn.insets = new Insets(0, 0, 0, 5);
163        gbc_cancelBtn.anchor = GridBagConstraints.WEST;
164        gbc_cancelBtn.gridx = 3;
165        gbc_cancelBtn.gridy = 0;
166        buttonPanel.add(cancelBtn, gbc_cancelBtn);
167
168        getContentPane().add(mainPanel, BorderLayout.CENTER);
169        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
170    }
171
172    /**
173     * Easy "onclose" process
174     */
175    protected void onClosed()
176    {
177        //
178    }
179
180    public boolean isClosed()
181    {
182        return closed;
183    }
184
185    public boolean isCanceled()
186    {
187        return opened && canceled;
188    }
189
190    /**
191     * @return the closeAfterAction
192     */
193    public boolean isCloseAfterAction()
194    {
195        return closeAfterAction;
196    }
197
198    /**
199     * @param closeAfterAction
200     *        the closeAfterAction to set
201     */
202    public void setCloseAfterAction(boolean closeAfterAction)
203    {
204        this.closeAfterAction = closeAfterAction;
205    }
206
207    /**
208     * @return the okAction
209     */
210    public ActionListener getOkAction()
211    {
212        return okAction;
213    }
214
215    /**
216     * @param okAction
217     *        the okAction to set
218     */
219    public void setOkAction(ActionListener okAction)
220    {
221        this.okAction = okAction;
222    }
223
224    /**
225     * @return the mainPanel
226     */
227    public JPanel getMainPanel()
228    {
229        return mainPanel;
230    }
231
232    /**
233     * @return the buttonPanel
234     */
235    public JPanel getButtonPanel()
236    {
237        return buttonPanel;
238    }
239
240    /**
241     * @return the okBtn
242     */
243    public JButton getOkBtn()
244    {
245        return okBtn;
246    }
247
248    /**
249     * @return the cancelBtn
250     */
251    public JButton getCancelBtn()
252    {
253        return cancelBtn;
254    }
255
256    @Override
257    public void actionPerformed(ActionEvent e)
258    {
259        final String cmd = e.getActionCommand();
260
261        if (CANCEL_CMD.equals(cmd))
262            dispose();
263        else if (OK_CMD.equals(cmd))
264        {
265            canceled = false;
266            // do action here
267            if (okAction != null)
268                okAction.actionPerformed(e);
269            // close if wanted
270            if (closeAfterAction)
271                dispose();
272        }
273    }
274
275    @Override
276    public void reshape(int x, int y, int width, int height)
277    {
278        final Rectangle r = new Rectangle(x, y, width, height);
279
280        // prevent to go completely off screen
281        ComponentUtil.fixPosition(this, r);
282
283        super.reshape(r.x, r.y, r.width, r.height);
284    }
285}