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 public static Icon getSmallIcon(Class<?> c) {
93 return SMALL_ICONS.get(c);
95 public static Icon getLargeIcon(Class<?> c) {
96 return LARGE_ICONS.get(c);
98 public static Icon getLargeDisabledIcon(Class<?> c) {
99 return DISABLED_ICONS.get(c);
105 private static ImageIcon loadSmall(String file, String desc) {
106 URL url = ClassLoader.getSystemResource(file);
108 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
111 return new ImageIcon(url, desc);
115 private static ImageIcon[] loadLarge(String file, String desc) {
116 ImageIcon[] icons = new ImageIcon[2];
118 URL url = ClassLoader.getSystemResource(file);
120 BufferedImage bi,bi2;
122 bi = ImageIO.read(url);
123 bi2 = ImageIO.read(url); // How the fsck can one duplicate a BufferedImage???
124 } catch (IOException e) {
125 ExceptionHandler.handleErrorCondition("ERROR: Couldn't read file: "+file, e);
126 return new ImageIcon[]{null,null};
129 icons[0] = new ImageIcon(bi,desc);
131 // Create disabled icon
132 if (false) { // Fade using alpha
134 int rgb[] = bi2.getRGB(0,0,bi2.getWidth(),bi2.getHeight(),null,0,bi2.getWidth());
135 for (int i=0; i<rgb.length; i++) {
136 final int alpha = (rgb[i]>>24)&0xFF;
137 rgb[i] = (rgb[i]&0xFFFFFF) | (alpha/3)<<24;
139 //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
141 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
143 } else { // Raster alpha
145 for (int x=0; x < bi.getWidth(); x++) {
146 for (int y=0; y < bi.getHeight(); y++) {
156 icons[1] = new ImageIcon(bi2,desc + " " +trans.get("ComponentIcons.disabled"));
160 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
161 return new ImageIcon[]{null,null};