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.event.ActionEvent;
023import java.awt.event.ActionListener;
024
025import javax.swing.BorderFactory;
026import javax.swing.Box;
027import javax.swing.BoxLayout;
028import javax.swing.JButton;
029import javax.swing.JPanel;
030
031/**
032 * @author Stephane
033 */
034public class ActionFrame extends TitledFrame
035{
036    protected static final String OK_CMD = "ok";
037    protected static final String CANCEL_CMD = "cancel";
038
039    protected final JPanel buttonPanel;
040    protected final JButton okBtn;
041    protected final JButton cancelBtn;
042
043    protected ActionListener okAction;
044    protected boolean closeAfterAction;
045
046    public ActionFrame(String title)
047    {
048        this(title, false, false);
049    }
050
051    public ActionFrame(String title, boolean resizable)
052    {
053        this(title, resizable, false);
054    }
055
056    public ActionFrame(String title, boolean resizable, boolean iconifiable)
057    {
058        super(title, resizable, true, false, iconifiable);
059
060        buttonPanel = new JPanel();
061        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
062        buttonPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
063
064        okBtn = new JButton("Ok");
065        cancelBtn = new JButton("Cancel");
066
067        buttonPanel.add(Box.createHorizontalGlue());
068        buttonPanel.add(okBtn);
069        buttonPanel.add(Box.createHorizontalStrut(10));
070        buttonPanel.add(cancelBtn);
071        buttonPanel.add(Box.createHorizontalGlue());
072
073        add(buttonPanel, BorderLayout.SOUTH);
074
075        // OTHERS
076        okBtn.setActionCommand(OK_CMD);
077        cancelBtn.setActionCommand(CANCEL_CMD);
078
079        final ActionListener buttonActions = new ActionListener()
080        {
081            @Override
082            public void actionPerformed(ActionEvent e)
083            {
084                final String cmd = e.getActionCommand();
085
086                if (CANCEL_CMD.equals(cmd))
087                    close();
088                else if (OK_CMD.equals(cmd))
089                {
090                    // do action here
091                    if (okAction != null)
092                        okAction.actionPerformed(e);
093                    // close if wanted
094                    if (closeAfterAction)
095                        close();
096                }
097            }
098        };
099
100        okBtn.addActionListener(buttonActions);
101        cancelBtn.addActionListener(buttonActions);
102
103        okAction = null;
104        closeAfterAction = true;
105    }
106
107    /**
108     * @return the closeAfterAction
109     */
110    public boolean isCloseAfterAction()
111    {
112        return closeAfterAction;
113    }
114
115    /**
116     * @param closeAfterAction
117     *        the closeAfterAction to set
118     */
119    public void setCloseAfterAction(boolean closeAfterAction)
120    {
121        this.closeAfterAction = closeAfterAction;
122    }
123
124    /**
125     * @return the okAction
126     */
127    public ActionListener getOkAction()
128    {
129        return okAction;
130    }
131
132    /**
133     * @param okAction
134     *        the okAction to set
135     */
136    public void setOkAction(ActionListener okAction)
137    {
138        this.okAction = okAction;
139    }
140
141    /**
142     * @return the okBtn
143     */
144    public JButton getOkBtn()
145    {
146        return okBtn;
147    }
148
149    /**
150     * @return the cancelBtn
151     */
152    public JButton getCancelBtn()
153    {
154        return cancelBtn;
155    }
156
157}