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.beans.PropertyChangeEvent;
026import java.beans.PropertyChangeListener;
027
028import javax.swing.Icon;
029
030import org.pushingpixels.flamingo.api.common.JCommandToggleMenuButton;
031
032/**
033 * @author Stephane
034 */
035public class IcyCommandToggleMenuButton extends JCommandToggleMenuButton
036{
037    /**
038     * 
039     */
040    private static final long serialVersionUID = -7391297214095914082L;
041
042    /**
043     * internals
044     */
045    private boolean internalEnabled;
046    private IcyAbstractAction action;
047    private final PropertyChangeListener actionPropertyChangeListener;
048
049    public IcyCommandToggleMenuButton(String title, IcyIcon icon)
050    {
051        super(title, icon);
052
053        action = null;
054        internalEnabled = isEnabled();
055
056        actionPropertyChangeListener = new PropertyChangeListener()
057        {
058            @Override
059            public void propertyChange(PropertyChangeEvent evt)
060            {
061                if (StringUtil.equals("enabled", evt.getPropertyName()))
062                    refreshEnabled();
063            }
064        };
065    }
066
067    /**
068     * @deprecated Use {@link #IcyCommandToggleMenuButton(String, IcyIcon)} instead.
069     */
070    @Deprecated
071    public IcyCommandToggleMenuButton(String title, String iconName)
072    {
073        this(title, new IcyIcon(iconName));
074    }
075
076    public IcyCommandToggleMenuButton(IcyIcon icon)
077    {
078        this(null, icon);
079    }
080
081    public IcyCommandToggleMenuButton(String title)
082    {
083        this(title, (IcyIcon) null);
084    }
085
086    public IcyCommandToggleMenuButton(IcyAbstractAction action)
087    {
088        this(null, (IcyIcon) null);
089
090        setAction(action);
091    }
092
093    /**
094     * @deprecated User {@link #IcyCommandToggleMenuButton(IcyAbstractAction)} instead.
095     */
096    @Deprecated
097    public IcyCommandToggleMenuButton(icy.common.IcyAbstractAction action)
098    {
099        this(null, (IcyIcon) null);
100
101        setAction(action);
102    }
103
104    public IcyCommandToggleMenuButton()
105    {
106        this(null, (IcyIcon) null);
107    }
108
109    /**
110     * Return the icon as IcyIcon
111     */
112    public IcyIcon getIcyIcon()
113    {
114        final Icon icon = getIcon();
115
116        if (icon instanceof IcyIcon)
117            return (IcyIcon) icon;
118
119        return null;
120    }
121
122    /**
123     * @return the icon name
124     */
125    public String getIconName()
126    {
127        final IcyIcon icon = getIcyIcon();
128
129        if (icon != null)
130            return icon.getName();
131
132        return null;
133    }
134
135    /**
136     * @param iconName
137     *        the icon name to set
138     */
139    public void setIconName(String iconName)
140    {
141        final IcyIcon icon = getIcyIcon();
142
143        if (icon != null)
144            icon.setName(iconName);
145    }
146
147    @Override
148    public void setEnabled(boolean b)
149    {
150        internalEnabled = b;
151        refreshEnabled();
152    }
153
154    protected void refreshEnabled()
155    {
156        super.setEnabled(internalEnabled && ((action == null) || action.isEnabled()));
157    }
158
159    /**
160     * Sets the {@link IcyAbstractAction} attached to this button.
161     */
162    public void setAction(IcyAbstractAction value)
163    {
164        if (action != value)
165        {
166            // remove listener from previous action
167            if (action != null)
168                action.removePropertyChangeListener(actionPropertyChangeListener);
169
170            action = value;
171
172            setText(action.getName());
173
174            final IcyIcon icon = action.getIcon();
175
176            if (icon != null)
177                setIcon(new IcyIcon(icon));
178            else
179                setIcon(null);
180
181            if (value != null)
182            {
183                // set tooltip
184                setActionRichTooltip(action.getRichToolTip());
185
186                // add listeners
187                addActionListener(value);
188                value.addPropertyChangeListener(actionPropertyChangeListener);
189            }
190
191            refreshEnabled();
192        }
193    }
194
195    /**
196     * Returns the {@link IcyAbstractAction} attached to this button.
197     */
198    public IcyAbstractAction getAction()
199    {
200        return action;
201    }
202}