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 */ 019 020package icy.resource.icon; 021 022import icy.image.ImageUtil; 023import icy.resource.ResourceUtil; 024 025import java.awt.Component; 026import java.awt.Dimension; 027import java.awt.Graphics; 028import java.awt.Image; 029 030import javax.swing.ImageIcon; 031 032import org.pushingpixels.flamingo.api.common.icon.ResizableIcon; 033 034/** 035 * @author fab & stephane 036 */ 037public class BasicResizableIcon implements ResizableIcon 038{ 039 protected static final int DEFAULT_ICONSIZE = 48; 040 041 protected ImageIcon icon; 042 protected final Image image; 043 protected Dimension dim; 044 045 public BasicResizableIcon(Image image) 046 { 047 super(); 048 049 this.image = image; 050 051 if (image != null) 052 { 053 // be sure image data are ready 054 ImageUtil.waitImageReady(image); 055 dim = new Dimension(image.getWidth(null), image.getHeight(null)); 056 } 057 else 058 dim = new Dimension(); 059 060 buildIcon(dim.width); 061 } 062 063 public BasicResizableIcon(ImageIcon srcIcon) 064 { 065 this(srcIcon.getImage()); 066 } 067 068 public BasicResizableIcon() 069 { 070 this((Image) null); 071 } 072 073 protected void buildIcon(int size) 074 { 075 if (image != null) 076 icon = ResourceUtil.getImageIcon(image, size); 077 else 078 icon = null; 079 } 080 081 @Override 082 public void setDimension(Dimension dim) 083 { 084 this.dim = dim; 085 086 if (icon != null) 087 { 088 final int size = (int) dim.getWidth(); 089 090 if (size != icon.getIconWidth()) 091 buildIcon(size); 092 } 093 } 094 095 @Override 096 public int getIconHeight() 097 { 098 if (icon != null) 099 return icon.getIconHeight(); 100 101 return dim.height; 102 } 103 104 @Override 105 public int getIconWidth() 106 { 107 if (icon != null) 108 return icon.getIconWidth(); 109 110 return dim.width; 111 } 112 113 @Override 114 public void paintIcon(Component c, Graphics g, int x, int y) 115 { 116 if (icon != null) 117 icon.paintIcon(c, g, x, y); 118 else 119 { 120 g.drawLine(x, y, x + dim.width, y + dim.height); 121 g.drawLine(x + dim.width, y, x, y + dim.height); 122 } 123 } 124}