1 package net.sf.openrocket.gui.main;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
6 import java.util.HashMap;
8 import javax.imageio.ImageIO;
9 import javax.swing.Icon;
10 import javax.swing.ImageIcon;
12 import net.sf.openrocket.l10n.Translator;
13 import net.sf.openrocket.rocketcomponent.BodyTube;
14 import net.sf.openrocket.rocketcomponent.Bulkhead;
15 import net.sf.openrocket.rocketcomponent.CenteringRing;
16 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
17 import net.sf.openrocket.rocketcomponent.EngineBlock;
18 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
19 import net.sf.openrocket.rocketcomponent.InnerTube;
20 import net.sf.openrocket.rocketcomponent.LaunchLug;
21 import net.sf.openrocket.rocketcomponent.MassComponent;
22 import net.sf.openrocket.rocketcomponent.NoseCone;
23 import net.sf.openrocket.rocketcomponent.Parachute;
24 import net.sf.openrocket.rocketcomponent.ShockCord;
25 import net.sf.openrocket.rocketcomponent.Streamer;
26 import net.sf.openrocket.rocketcomponent.Transition;
27 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
28 import net.sf.openrocket.rocketcomponent.TubeCoupler;
29 import net.sf.openrocket.startup.Application;
32 public class ComponentIcons {
33 private static final Translator trans = Application.getTranslator();
35 private static final String ICON_DIRECTORY = "pix/componenticons/";
36 private static final String SMALL_SUFFIX = "-small.png";
37 private static final String LARGE_SUFFIX = "-large.png";
39 private static final HashMap<Class<?>, ImageIcon> SMALL_ICONS =
40 new HashMap<Class<?>, ImageIcon>();
41 private static final HashMap<Class<?>, ImageIcon> LARGE_ICONS =
42 new HashMap<Class<?>, ImageIcon>();
43 private static final HashMap<Class<?>, ImageIcon> DISABLED_ICONS =
44 new HashMap<Class<?>, ImageIcon>();
48 load("nosecone", trans.get("ComponentIcons.Nosecone"), NoseCone.class);
50 load("bodytube", trans.get("ComponentIcons.Bodytube"), BodyTube.class);
52 load("transition", trans.get("ComponentIcons.Transition"), Transition.class);
53 //// Trapezoidal fin set
54 load("trapezoidfin", trans.get("ComponentIcons.Trapezoidalfinset"), TrapezoidFinSet.class);
55 //// Elliptical fin set
56 load("ellipticalfin", trans.get("ComponentIcons.Ellipticalfinset"), EllipticalFinSet.class);
58 load("freeformfin", trans.get("ComponentIcons.Freeformfinset"), FreeformFinSet.class);
60 load("launchlug", trans.get("ComponentIcons.Launchlug"), LaunchLug.class);
62 load("innertube", trans.get("ComponentIcons.Innertube"), InnerTube.class);
64 load("tubecoupler", trans.get("ComponentIcons.Tubecoupler"), TubeCoupler.class);
66 load("centeringring", trans.get("ComponentIcons.Centeringring"), CenteringRing.class);
68 load("bulkhead", trans.get("ComponentIcons.Bulkhead"), Bulkhead.class);
70 load("engineblock", trans.get("ComponentIcons.Engineblock"), EngineBlock.class);
72 load("parachute", trans.get("ComponentIcons.Parachute"), Parachute.class);
74 load("streamer", trans.get("ComponentIcons.Streamer"), Streamer.class);
76 load("shockcord", trans.get("ComponentIcons.Shockcord"), ShockCord.class);
78 load("mass", trans.get("ComponentIcons.Masscomponent"), MassComponent.class);
81 private static void load(String filename, String name, Class<?> componentClass) {
82 ImageIcon icon = loadSmall(ICON_DIRECTORY + filename + SMALL_SUFFIX, name);
83 SMALL_ICONS.put(componentClass, icon);
85 ImageIcon[] icons = loadLarge(ICON_DIRECTORY + filename + LARGE_SUFFIX, name);
86 LARGE_ICONS.put(componentClass, icons[0]);
87 DISABLED_ICONS.put(componentClass, icons[1]);
92 * Return the small icon for a component type.
94 * @param c the component class.
95 * @return the icon, or <code>null</code> if none available.
97 public static Icon getSmallIcon(Class<?> c) {
98 return SMALL_ICONS.get(c);
102 * Return the large icon for a component type.
104 * @param c the component class.
105 * @return the icon, or <code>null</code> if none available.
107 public static Icon getLargeIcon(Class<?> c) {
108 return LARGE_ICONS.get(c);
112 * Return the large disabled icon for a component type.
114 * @param c the component class.
115 * @return the icon, or <code>null</code> if none available.
117 public static Icon getLargeDisabledIcon(Class<?> c) {
118 return DISABLED_ICONS.get(c);
124 private static ImageIcon loadSmall(String file, String desc) {
125 URL url = ClassLoader.getSystemResource(file);
127 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
130 return new ImageIcon(url, desc);
134 private static ImageIcon[] loadLarge(String file, String desc) {
135 ImageIcon[] icons = new ImageIcon[2];
137 URL url = ClassLoader.getSystemResource(file);
139 BufferedImage bi, bi2;
141 bi = ImageIO.read(url);
142 bi2 = ImageIO.read(url); // How the fsck can one duplicate a BufferedImage???
143 } catch (IOException e) {
144 ExceptionHandler.handleErrorCondition("ERROR: Couldn't read file: " + file, e);
145 return new ImageIcon[] { null, null };
148 icons[0] = new ImageIcon(bi, desc);
150 // Create disabled icon
151 if (false) { // Fade using alpha
153 int rgb[] = bi2.getRGB(0, 0, bi2.getWidth(), bi2.getHeight(), null, 0, bi2.getWidth());
154 for (int i = 0; i < rgb.length; i++) {
155 final int alpha = (rgb[i] >> 24) & 0xFF;
156 rgb[i] = (rgb[i] & 0xFFFFFF) | (alpha / 3) << 24;
158 //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
160 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
162 } else { // Raster alpha
164 for (int x = 0; x < bi.getWidth(); x++) {
165 for (int y = 0; y < bi.getHeight(); y++) {
166 if ((x + y) % 2 == 0) {
175 icons[1] = new ImageIcon(bi2, desc + " " + trans.get("ComponentIcons.disabled"));
179 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
180 return new ImageIcon[] { null, null };