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.component.button;
020
021import icy.action.IcyAbstractAction;
022import icy.resource.icon.IcyIcon;
023import icy.util.StringUtil;
024
025import java.awt.Image;
026import java.beans.PropertyChangeEvent;
027import java.beans.PropertyChangeListener;
028
029import javax.swing.Icon;
030
031import org.pushingpixels.flamingo.api.common.JCommandButton;
032import org.pushingpixels.flamingo.api.common.RichTooltip;
033import org.pushingpixels.flamingo.api.common.popup.PopupPanelCallback;
034
035/**
036 * @author Stephane
037 */
038public class IcyCommandButton extends JCommandButton
039{
040    /**
041     * 
042     */
043    private static final long serialVersionUID = 6430339971361017326L;
044
045    /**
046     * internals
047     */
048    private boolean internalEnabled;
049    private IcyAbstractAction action;
050    private final PropertyChangeListener actionPropertyChangeListener;
051
052    public IcyCommandButton(String title, IcyIcon icon)
053    {
054        super(title, icon);
055
056        action = null;
057        internalEnabled = isEnabled();
058
059        actionPropertyChangeListener = new PropertyChangeListener()
060        {
061            @Override
062            public void propertyChange(PropertyChangeEvent evt)
063            {
064                if (StringUtil.equals("enabled", evt.getPropertyName()))
065                    refreshEnabled();
066            }
067        };
068    }
069
070    /**
071     * @deprecated Use {@link #IcyCommandButton(String, IcyIcon)} instead.
072     */
073    @Deprecated
074    public IcyCommandButton(String title, Image icon)
075    {
076        this(title, new IcyIcon(icon));
077    }
078
079    /**
080     * @deprecated Use {@link #IcyCommandButton(String, IcyIcon)} instead.
081     */
082    @Deprecated
083    public IcyCommandButton(String title, String iconName)
084    {
085        this(title, new IcyIcon(iconName));
086    }
087
088    public IcyCommandButton(IcyIcon icon)
089    {
090        this(null, icon);
091    }
092
093    public IcyCommandButton(String title)
094    {
095        this(title, (IcyIcon) null);
096    }
097
098    public IcyCommandButton(IcyAbstractAction action)
099    {
100        this(null, (IcyIcon) null);
101
102        setAction(action);
103    }
104
105    /**
106     * @deprecated User {@link #IcyCommandButton(IcyAbstractAction)} instead.
107     */
108    @Deprecated
109    public IcyCommandButton(icy.common.IcyAbstractAction action)
110    {
111        this(null, (IcyIcon) null);
112
113        setAction(action);
114    }
115
116    public IcyCommandButton()
117    {
118        this(null, (IcyIcon) null);
119    }
120
121    /**
122     * Return the icon as IcyIcon
123     */
124    public IcyIcon getIcyIcon()
125    {
126        final Icon icon = getIcon();
127
128        if (icon instanceof IcyIcon)
129            return (IcyIcon) icon;
130
131        return null;
132    }
133
134    /**
135     * @return the icon name
136     */
137    public String getIconName()
138    {
139        final IcyIcon icon = getIcyIcon();
140
141        if (icon != null)
142            return icon.getName();
143
144        return null;
145    }
146
147    /**
148     * @param iconName
149     *        the icon name to set
150     */
151    public void setIconName(String iconName)
152    {
153        final IcyIcon icon = getIcyIcon();
154
155        if (icon != null)
156            icon.setName(iconName);
157    }
158
159    @Override
160    public void setEnabled(boolean b)
161    {
162        internalEnabled = b;
163        refreshEnabled();
164    }
165
166    protected void refreshEnabled()
167    {
168        super.setEnabled(internalEnabled && ((action == null) || action.isEnabled()));
169    }
170
171    /**
172     * Returns the {@link IcyAbstractAction} attached to this button.
173     */
174    public IcyAbstractAction getAction()
175    {
176        return action;
177    }
178
179    /**
180     * Sets the {@link IcyAbstractAction} attached to this button.
181     */
182    public void setAction(IcyAbstractAction value)
183    {
184        if (action != value)
185        {
186            // remove listener from previous action
187            if (action != null)
188            {
189                removeActionListener(action);
190                action.removePropertyChangeListener(actionPropertyChangeListener);
191            }
192
193            action = value;
194            setCommandButtonKind(CommandButtonKind.ACTION_ONLY);
195
196            if (value != null)
197            {
198                // set text
199                setText(action.getName());
200
201                // set icon
202                final IcyIcon icon = action.getIcon();
203
204                if (icon != null)
205                    setIcon(new IcyIcon(icon));
206                else
207                    setIcon(null);
208
209                // set tooltip
210                setActionRichTooltip(action.getRichToolTip());
211
212                // add listeners
213                addActionListener(value);
214                value.addPropertyChangeListener(actionPropertyChangeListener);
215            }
216
217            refreshEnabled();
218        }
219    }
220
221    /**
222     * Sets the button in Popup mode with specified Popup panel callback and tool tip
223     */
224    public void setPopup(PopupPanelCallback cb, RichTooltip toolTip)
225    {
226        // remove action
227        setAction(null);
228
229        // then set popup
230        setCommandButtonKind(CommandButtonKind.POPUP_ONLY);
231
232        if (cb != null)
233            setPopupCallback(cb);
234        if (toolTip != null)
235            setPopupRichTooltip(toolTip);
236
237        refreshEnabled();
238    }
239}