| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.iconbundle.impl; |
| 25 | |
|
| 26 | |
import java.io.File; |
| 27 | |
import java.io.InputStream; |
| 28 | |
import java.io.IOException; |
| 29 | |
|
| 30 | |
import java.awt.Color; |
| 31 | |
import java.awt.Image; |
| 32 | |
import java.awt.Component; |
| 33 | |
import java.awt.Graphics2D; |
| 34 | |
import java.awt.RenderingHints; |
| 35 | |
|
| 36 | |
import java.awt.image.BufferedImage; |
| 37 | |
|
| 38 | |
import java.awt.geom.Rectangle2D; |
| 39 | |
import java.awt.geom.AffineTransform; |
| 40 | |
|
| 41 | |
import java.net.URL; |
| 42 | |
|
| 43 | |
import javax.imageio.ImageIO; |
| 44 | |
|
| 45 | |
import javax.imageio.stream.ImageInputStream; |
| 46 | |
|
| 47 | |
import javax.swing.UIManager; |
| 48 | |
|
| 49 | |
import org.dishevelled.iconbundle.IconSize; |
| 50 | |
import org.dishevelled.iconbundle.IconState; |
| 51 | |
import org.dishevelled.iconbundle.IconBundle; |
| 52 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public final class PNGIconBundle |
| 60 | |
implements IconBundle |
| 61 | |
{ |
| 62 | |
|
| 63 | |
private final String baseImageName; |
| 64 | |
|
| 65 | |
|
| 66 | |
private final BufferedImage baseImage; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public PNGIconBundle(final File file) |
| 76 | 14 | { |
| 77 | 14 | if (file == null) |
| 78 | |
{ |
| 79 | 1 | throw new IllegalArgumentException("file must not be null"); |
| 80 | |
} |
| 81 | |
|
| 82 | 13 | baseImageName = null; |
| 83 | |
try |
| 84 | |
{ |
| 85 | 13 | baseImage = ImageIO.read(file); |
| 86 | |
} |
| 87 | 1 | catch (Exception e) |
| 88 | |
{ |
| 89 | 1 | throw new IllegalArgumentException("could not create base image; " + e.getMessage()); |
| 90 | 12 | } |
| 91 | 12 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public PNGIconBundle(final InputStream inputStream) |
| 100 | 2 | { |
| 101 | 2 | if (inputStream == null) |
| 102 | |
{ |
| 103 | 1 | throw new IllegalArgumentException("inputStream must not be null"); |
| 104 | |
} |
| 105 | |
|
| 106 | 1 | baseImageName = null; |
| 107 | |
try |
| 108 | |
{ |
| 109 | 1 | baseImage = ImageIO.read(inputStream); |
| 110 | |
} |
| 111 | 0 | catch (Exception e) |
| 112 | |
{ |
| 113 | 0 | throw new IllegalArgumentException("could not create base image; " + e.getMessage()); |
| 114 | 1 | } |
| 115 | 1 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public PNGIconBundle(final ImageInputStream imageInputStream) |
| 124 | 2 | { |
| 125 | 2 | if (imageInputStream == null) |
| 126 | |
{ |
| 127 | 1 | throw new IllegalArgumentException("imageInputStream must not be null"); |
| 128 | |
} |
| 129 | |
|
| 130 | 1 | baseImageName = null; |
| 131 | |
try |
| 132 | |
{ |
| 133 | 1 | baseImage = ImageIO.read(imageInputStream); |
| 134 | |
} |
| 135 | 0 | catch (Exception e) |
| 136 | |
{ |
| 137 | 0 | throw new IllegalArgumentException("could not create base image; " + e.getMessage()); |
| 138 | 1 | } |
| 139 | 1 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public PNGIconBundle(final URL url) |
| 148 | 3 | { |
| 149 | 3 | if (url == null) |
| 150 | |
{ |
| 151 | 1 | throw new IllegalArgumentException("url must not be null"); |
| 152 | |
} |
| 153 | |
|
| 154 | 2 | baseImageName = null; |
| 155 | |
try |
| 156 | |
{ |
| 157 | 2 | baseImage = ImageIO.read(url); |
| 158 | |
} |
| 159 | 1 | catch (Exception e) |
| 160 | |
{ |
| 161 | 1 | throw new IllegalArgumentException("could not create base image; " + e.getMessage()); |
| 162 | 1 | } |
| 163 | 1 | } |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public PNGIconBundle(final String baseImageName) |
| 173 | 3 | { |
| 174 | 3 | if (baseImageName == null) |
| 175 | |
{ |
| 176 | 1 | throw new IllegalArgumentException("baseImageName must not be null"); |
| 177 | |
} |
| 178 | 2 | baseImage = null; |
| 179 | 2 | this.baseImageName = baseImageName; |
| 180 | 2 | } |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public Image getImage(final Component component, |
| 185 | |
final IconTextDirection direction, |
| 186 | |
final IconState state, |
| 187 | |
final IconSize size) |
| 188 | |
{ |
| 189 | 2166 | if (direction == null) |
| 190 | |
{ |
| 191 | 1 | throw new IllegalArgumentException("direction must not be null"); |
| 192 | |
} |
| 193 | 2165 | if (state == null) |
| 194 | |
{ |
| 195 | 1 | throw new IllegalArgumentException("state must not be null"); |
| 196 | |
} |
| 197 | 2164 | if (size == null) |
| 198 | |
{ |
| 199 | 1 | throw new IllegalArgumentException("size must not be null"); |
| 200 | |
} |
| 201 | |
|
| 202 | 2163 | BufferedImage image = null; |
| 203 | 2163 | if (baseImage == null) |
| 204 | |
{ |
| 205 | |
try |
| 206 | |
{ |
| 207 | 7 | URL url = getClass().getResource(baseImageName + size.toString() + ".png"); |
| 208 | 7 | image = (url == null) ? null : ImageIO.read(url); |
| 209 | |
} |
| 210 | 0 | catch (IOException e) |
| 211 | |
{ |
| 212 | |
|
| 213 | 7 | } |
| 214 | |
} |
| 215 | |
else |
| 216 | |
{ |
| 217 | 2156 | int h = size.getHeight(); |
| 218 | 2156 | int w = size.getWidth(); |
| 219 | |
|
| 220 | 2156 | image = new BufferedImage(w + 1, h + 1, BufferedImage.TYPE_INT_ARGB); |
| 221 | 2156 | Graphics2D g = image.createGraphics(); |
| 222 | |
|
| 223 | 2156 | g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
| 224 | 2156 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 225 | 2156 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
| 226 | |
|
| 227 | 2156 | Rectangle2D bounds = new Rectangle2D.Double(0.0d, 0.0d, |
| 228 | |
(double) baseImage.getWidth(), |
| 229 | |
(double) baseImage.getHeight()); |
| 230 | |
|
| 231 | 2156 | double d = 1.0d; |
| 232 | 2156 | if (bounds.getWidth() >= bounds.getHeight()) |
| 233 | |
{ |
| 234 | 2156 | d = w / bounds.getWidth(); |
| 235 | |
} |
| 236 | |
else |
| 237 | |
{ |
| 238 | 0 | d = h / bounds.getHeight(); |
| 239 | |
} |
| 240 | |
|
| 241 | 2156 | AffineTransform at = new AffineTransform(); |
| 242 | |
|
| 243 | |
|
| 244 | 2156 | at.translate(w / 2.0d, h / 2.0d); |
| 245 | |
|
| 246 | 2156 | at.scale(d, d); |
| 247 | |
|
| 248 | 2156 | at.translate(-1 * bounds.getCenterX(), -1 * bounds.getCenterY()); |
| 249 | |
|
| 250 | 2156 | g.drawRenderedImage(baseImage, at); |
| 251 | 2156 | g.dispose(); |
| 252 | |
} |
| 253 | |
|
| 254 | 2163 | if (IconState.ACTIVE == state) |
| 255 | |
{ |
| 256 | 308 | return IconBundleUtils.makeActive(image); |
| 257 | |
} |
| 258 | 1855 | else if (IconState.MOUSEOVER == state) |
| 259 | |
{ |
| 260 | 308 | return IconBundleUtils.makeMouseover(image); |
| 261 | |
} |
| 262 | 1547 | else if (IconState.SELECTED == state) |
| 263 | |
{ |
| 264 | 308 | Color selectionColor = UIManager.getColor("List.selectionBackground"); |
| 265 | 308 | return IconBundleUtils.makeSelected(image, selectionColor); |
| 266 | |
} |
| 267 | 1239 | else if (IconState.SELECTED_MOUSEOVER == state) |
| 268 | |
{ |
| 269 | 308 | Color selectionColor = UIManager.getColor("List.selectionBackground"); |
| 270 | 308 | return IconBundleUtils.makeSelectedMouseover(image, selectionColor); |
| 271 | |
} |
| 272 | 931 | else if (IconState.DRAGGING == state) |
| 273 | |
{ |
| 274 | 308 | return IconBundleUtils.makeDragging(image); |
| 275 | |
} |
| 276 | 623 | else if (IconState.DISABLED == state) |
| 277 | |
{ |
| 278 | 308 | return IconBundleUtils.makeDisabled(image); |
| 279 | |
} |
| 280 | |
else |
| 281 | |
{ |
| 282 | 315 | return image; |
| 283 | |
} |
| 284 | |
} |
| 285 | |
} |