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.gui.util.ComponentUtil;
023import icy.resource.icon.IcyIcon;
024import icy.util.StringUtil;
025
026import java.awt.Dimension;
027import java.awt.Image;
028
029import javax.swing.Action;
030import javax.swing.Icon;
031import javax.swing.JToggleButton;
032import javax.swing.SwingConstants;
033
034/**
035 * @author Stephane
036 */
037public class IcyToggleButton extends JToggleButton
038{
039    /**
040     * 
041     */
042    private static final long serialVersionUID = 568831905293026365L;
043
044    private boolean flat;
045
046    /**
047     * Create a toggle button with specified action
048     */
049    public IcyToggleButton(IcyAbstractAction action)
050    {
051        super(action);
052
053        flat = false;
054        init();
055    }
056
057    /**
058     * @deprecated User {@link #IcyToggleButton(IcyAbstractAction)} instead.
059     */
060    @Deprecated
061    public IcyToggleButton(icy.common.IcyAbstractAction action)
062    {
063        super(action);
064
065        flat = false;
066        init();
067    }
068
069    /**
070     * Create a toggle button with specified text and icon
071     */
072    public IcyToggleButton(String text, IcyIcon icon)
073    {
074        super(text, icon);
075
076        flat = false;
077        init();
078    }
079
080    /**
081     * Create a toggle button with specified text and icon
082     */
083    public IcyToggleButton(IcyIcon icon)
084    {
085        this(null, icon);
086    }
087
088    /**
089     * @deprecated Use {@link #IcyToggleButton(String, IcyIcon)} instead.
090     */
091    @Deprecated
092    public IcyToggleButton(String text, Image iconImage, int iconSize)
093    {
094        this(text, new IcyIcon(iconImage, iconSize));
095    }
096
097    /**
098     * @deprecated Use {@link #IcyToggleButton(String, IcyIcon)} instead.
099     */
100    @Deprecated
101    public IcyToggleButton(String text, Image iconImage)
102    {
103        this(text, iconImage, IcyIcon.DEFAULT_SIZE);
104    }
105
106    /**
107     * @deprecated Use {@link #IcyToggleButton(IcyIcon)} instead.
108     */
109    @Deprecated
110    public IcyToggleButton(Image iconImage, int iconSize)
111    {
112        this(null, iconImage, iconSize);
113    }
114
115    /**
116     * @deprecated Use {@link #IcyToggleButton(IcyIcon)} instead.
117     */
118    @Deprecated
119    public IcyToggleButton(Image iconImage)
120    {
121        this(null, iconImage, IcyIcon.DEFAULT_SIZE);
122    }
123
124    /**
125     * @deprecated Use {@link #IcyToggleButton(String, IcyIcon)} instead.
126     */
127    @Deprecated
128    public IcyToggleButton(String text, String iconName, int iconSize)
129    {
130        this(text, new IcyIcon(iconName, iconSize));
131    }
132
133    /**
134     * @deprecated Use {@link #IcyToggleButton(String, IcyIcon)} instead.
135     */
136    @Deprecated
137    public IcyToggleButton(String text, String iconName)
138    {
139        this(text, iconName, IcyIcon.DEFAULT_SIZE);
140    }
141
142    /**
143     * @deprecated Use {@link #IcyToggleButton(IcyIcon)} instead.
144     */
145    @Deprecated
146    public IcyToggleButton(String iconName, int iconSize)
147    {
148        this(null, iconName, iconSize);
149    }
150
151    /**
152     * @deprecated Use {@link #IcyToggleButton(IcyIcon)} instead.
153     */
154    @Deprecated
155    public IcyToggleButton(String iconName)
156    {
157        this(iconName, IcyIcon.DEFAULT_SIZE);
158    }
159
160    private void init()
161    {
162        setHorizontalAlignment(SwingConstants.CENTER);
163        setVerticalAlignment(SwingConstants.CENTER);
164
165        if (flat)
166        {
167            setBorderPainted(false);
168            setFocusPainted(false);
169            setFocusable(false);
170        }
171
172        // manual change notify
173        updateSize();
174    }
175
176    @Override
177    public void setAction(Action a)
178    {
179        super.setAction(a);
180
181        // override tooltip set from action
182        IcyAbstractAction.setToolTipTextFromAction(this, a);
183    }
184
185    /**
186     * Return the icon as IcyIcon
187     */
188    public IcyIcon getIcyIcon()
189    {
190        final Icon icon = getIcon();
191
192        if (icon instanceof IcyIcon)
193            return (IcyIcon) icon;
194
195        return null;
196    }
197
198    /**
199     * Return the selected icon as IcyIcon
200     */
201    public IcyIcon getSelectedIcyIcon()
202    {
203        final Icon icon = getSelectedIcon();
204
205        if (icon instanceof IcyIcon)
206            return (IcyIcon) icon;
207
208        return null;
209    }
210
211    /**
212     * @return the flat
213     */
214    public boolean isFlat()
215    {
216        return flat;
217    }
218
219    /**
220     * @param flat
221     *        the flat to set
222     */
223    public void setFlat(boolean flat)
224    {
225        if (this.flat != flat)
226        {
227            this.flat = flat;
228
229            setBorderPainted(!flat);
230            setFocusPainted(!flat);
231            setFocusable(!flat);
232
233            updateSize();
234        }
235    }
236
237    /**
238     * @return the icon name
239     */
240    public String getIconName()
241    {
242        final IcyIcon icon = getIcyIcon();
243
244        if (icon != null)
245            return icon.getName();
246
247        return null;
248    }
249
250    /**
251     * @param iconName
252     *        the iconName to set
253     */
254    public void setIconName(String iconName)
255    {
256        final IcyIcon icon = getIcyIcon();
257
258        if (icon != null)
259        {
260            icon.setName(iconName);
261            updateSize();
262        }
263    }
264
265    /**
266     * @return the icon name
267     */
268    public String getSelectedIconName()
269    {
270        final IcyIcon icon = getSelectedIcyIcon();
271
272        if (icon != null)
273            return icon.getName();
274
275        return null;
276    }
277
278    /**
279     * @param iconName
280     *        the iconName to set
281     */
282    public void setSelectedIconName(String iconName)
283    {
284        final IcyIcon icon = getSelectedIcyIcon();
285
286        if (icon != null)
287        {
288            icon.setName(iconName);
289            updateSize();
290        }
291    }
292
293    /**
294     * @param iconImage
295     *        the iconImage to set
296     */
297    public void setIconImage(Image iconImage)
298    {
299        final IcyIcon icon = getIcyIcon();
300
301        if (icon != null)
302        {
303            icon.setImage(iconImage);
304            updateSize();
305        }
306    }
307
308    /**
309     * @param iconImage
310     *        the iconImage to set
311     */
312    public void setSelectedIconImage(Image iconImage)
313    {
314        final IcyIcon icon = getSelectedIcyIcon();
315
316        if (icon != null)
317        {
318            icon.setImage(iconImage);
319            updateSize();
320        }
321    }
322
323    /**
324     * @return the icon size
325     */
326    public int getIconSize()
327    {
328        final IcyIcon icon = getIcyIcon();
329
330        if (icon != null)
331            return icon.getSize();
332
333        return -1;
334    }
335
336    /**
337     * @param iconSize
338     *        the iconSize to set
339     */
340    public void setIconSize(int iconSize)
341    {
342        final IcyIcon icon = getIcyIcon();
343
344        if (icon != null)
345        {
346            icon.setSize(iconSize);
347            updateSize();
348        }
349    }
350
351    @Override
352    public void setText(String text)
353    {
354        super.setText(text);
355
356        updateSize();
357    }
358
359    public void updateSize()
360    {
361        final IcyIcon icon = getIcyIcon();
362        boolean noText = StringUtil.isEmpty(getText());
363        noText |= (getAction() != null) && getHideActionText();
364
365        // adjust size to icon size if no text
366        if (flat && (icon != null) && noText)
367        {
368            final Dimension dim = icon.getDimension();
369            dim.height += 2;
370            dim.width += 2;
371            ComponentUtil.setFixedSize(this, dim);
372        }
373    }
374
375    @Override
376    protected void actionPropertyChanged(Action action, String propertyName)
377    {
378        // override tooltip set from action
379        if ((propertyName == Action.LONG_DESCRIPTION) || (propertyName == Action.SHORT_DESCRIPTION))
380            IcyAbstractAction.setToolTipTextFromAction(this, action);
381        else
382            super.actionPropertyChanged(action, propertyName);
383    }
384}