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.resource.icon;
020
021import icy.gui.util.LookAndFeelUtil;
022import icy.image.ImageUtil;
023import icy.resource.ResourceUtil;
024import icy.util.StringUtil;
025
026import java.awt.Color;
027import java.awt.Component;
028import java.awt.Dimension;
029import java.awt.Graphics;
030import java.awt.Image;
031import java.awt.image.BufferedImage;
032
033import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
034
035/**
036 * Icy Icon class.<br>
037 * This class is used to display alpha mask or classic image icon.<br>
038 * When the image is alpha type (default) default fill color is taken from the current
039 * look and feel scheme but it can also be defined manually.
040 * 
041 * @author Stephane
042 */
043public class IcyIcon implements ResizableIcon
044{
045    public static final int DEFAULT_SIZE = 20;
046
047    protected Image image;
048    protected String name;
049    protected Color color;
050    protected boolean alpha;
051    protected final Dimension dim;
052
053    private BufferedImage cache;
054    private BufferedImage alphaImage;
055
056    private IcyIcon(String name, Image image, int size, boolean alpha)
057    {
058        super();
059
060        this.image = image;
061        this.name = name;
062        color = null;
063        this.alpha = alpha;
064
065        dim = new Dimension(size, size);
066
067        updateImage();
068    }
069
070    public IcyIcon(String name, int size, boolean alpha)
071    {
072        this(name, null, size, alpha);
073    }
074
075    public IcyIcon(String name, int size)
076    {
077        this(name, null, size, true);
078    }
079
080    public IcyIcon(String name, boolean alpha)
081    {
082        this(name, null, DEFAULT_SIZE, alpha);
083    }
084
085    public IcyIcon(String name)
086    {
087        this(name, null, DEFAULT_SIZE, true);
088    }
089
090    public IcyIcon(Image image, int size, boolean alpha)
091    {
092        this(null, image, size, alpha);
093    }
094
095    public IcyIcon(Image image, int size)
096    {
097        this(null, image, size, true);
098    }
099
100    public IcyIcon(Image image, boolean alpha)
101    {
102        this(null, image, DEFAULT_SIZE, alpha);
103    }
104
105    public IcyIcon(Image image)
106    {
107        this(null, image, DEFAULT_SIZE, true);
108    }
109
110    public IcyIcon(IcyIcon icon)
111    {
112        this(icon.getName(), icon.getImage(), icon.getIconWidth(), icon.getAlpha());
113    }
114
115    public int getSize()
116    {
117        return dim.width;
118    }
119
120    public void setSize(int size)
121    {
122        setDimension(new Dimension(size, size));
123    }
124
125    /**
126     * @return the iconName
127     */
128    public String getName()
129    {
130        return name;
131    }
132
133    /**
134     * @param value
135     *        the name to set
136     */
137    public void setName(String value)
138    {
139        if (name != value)
140        {
141            // can't set image and name at same time
142            name = value;
143            image = null;
144
145            updateImage();
146        }
147    }
148
149    /**
150     * @return the image
151     */
152    public Image getImage()
153    {
154        return image;
155    }
156
157    /**
158     * @param value
159     *        the image to set
160     */
161    public void setImage(Image value)
162    {
163        if (image != value)
164        {
165            // can't set image and name at same time
166            image = value;
167            name = null;
168
169            updateImage();
170        }
171    }
172
173    /**
174     * @return the fill color (null if we use the default LAF color).
175     */
176    public Color getColor()
177    {
178        return color;
179    }
180
181    /**
182     * @param color
183     *        the fill color to set (null = default LAF color)
184     */
185    public void setColor(Color color)
186    {
187        if (this.color != color)
188        {
189            this.color = color;
190            update();
191        }
192    }
193
194    /**
195     * @deprecated Use {@link #getAlpha()} instead
196     */
197    @Deprecated
198    public boolean isAlpha()
199    {
200        return getAlpha();
201    }
202
203    /**
204     * @return the alpha
205     */
206    public boolean getAlpha()
207    {
208        return alpha;
209    }
210
211    /**
212     * @param alpha
213     *        use alpha or color image depending
214     */
215    public void setAlpha(boolean alpha)
216    {
217        if (this.alpha != alpha)
218        {
219            this.alpha = alpha;
220            updateImage();
221        }
222    }
223
224    private void updateImage()
225    {
226        if (!StringUtil.isEmpty(name))
227        {
228            if (alpha)
229                image = ResourceUtil.getAlphaIconAsImage(name);
230            else
231                image = ResourceUtil.getColorIconAsImage(name);
232        }
233
234        update();
235    }
236
237    private void update()
238    {
239        if (image != null)
240        {
241            cache = ImageUtil.scaleQuality(image, dim.width, dim.height);
242            alphaImage = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
243        }
244        else
245        {
246            cache = null;
247            alphaImage = null;
248        }
249    }
250
251    public Dimension getDimension()
252    {
253        return new Dimension(dim);
254    }
255
256    @Override
257    public void setDimension(Dimension newDim)
258    {
259        // dimension changed
260        if (!dim.equals(newDim))
261        {
262            dim.setSize(newDim);
263            update();
264        }
265    }
266
267    @Override
268    public void paintIcon(Component c, Graphics g, int x, int y)
269    {
270        if (alpha)
271        {
272            // alpha image cache defined ?
273            if (alphaImage != null)
274            {
275                if (color == null)
276                    LookAndFeelUtil.paintForegroundImageFromAlphaImage(c, cache, alphaImage);
277                else
278                    ImageUtil.paintColorImageFromAlphaImage(cache, alphaImage, color);
279            }
280
281            g.drawImage(alphaImage, x, y, null);
282        }
283        else
284            g.drawImage(cache, x, y, null);
285    }
286
287    @Override
288    public int getIconWidth()
289    {
290        return dim.width;
291    }
292
293    @Override
294    public int getIconHeight()
295    {
296        return dim.height;
297    }
298}