create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / main / ComponentIcons.java
1 package net.sf.openrocket.gui.main;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.HashMap;
7
8 import javax.imageio.ImageIO;
9 import javax.swing.Icon;
10 import javax.swing.ImageIcon;
11
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;
30
31
32 public class ComponentIcons {
33         private static final Translator trans = Application.getTranslator();
34         
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";
38         
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>();
45         
46         static {
47                 //// Nose cone
48                 load("nosecone", trans.get("ComponentIcons.Nosecone"), NoseCone.class);
49                 //// Body tube
50                 load("bodytube", trans.get("ComponentIcons.Bodytube"), BodyTube.class);
51                 //// Transition
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);
57                 //// Freeform fin set
58                 load("freeformfin", trans.get("ComponentIcons.Freeformfinset"), FreeformFinSet.class);
59                 //// Launch lug
60                 load("launchlug", trans.get("ComponentIcons.Launchlug"), LaunchLug.class);
61                 //// Inner tube
62                 load("innertube", trans.get("ComponentIcons.Innertube"), InnerTube.class);
63                 //// Tube coupler
64                 load("tubecoupler", trans.get("ComponentIcons.Tubecoupler"), TubeCoupler.class);
65                 //// Centering ring
66                 load("centeringring", trans.get("ComponentIcons.Centeringring"), CenteringRing.class);
67                 //// Bulk head
68                 load("bulkhead", trans.get("ComponentIcons.Bulkhead"), Bulkhead.class);
69                 //// Engine block
70                 load("engineblock", trans.get("ComponentIcons.Engineblock"), EngineBlock.class);
71                 //// Parachute
72                 load("parachute", trans.get("ComponentIcons.Parachute"), Parachute.class);
73                 //// Streamer
74                 load("streamer", trans.get("ComponentIcons.Streamer"), Streamer.class);
75                 //// Shock cord
76                 load("shockcord", trans.get("ComponentIcons.Shockcord"), ShockCord.class);
77                 //// Mass component
78                 load("mass", trans.get("ComponentIcons.Masscomponent"), MassComponent.class);
79         }
80         
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);
84                 
85                 ImageIcon[] icons = loadLarge(ICON_DIRECTORY + filename + LARGE_SUFFIX, name);
86                 LARGE_ICONS.put(componentClass, icons[0]);
87                 DISABLED_ICONS.put(componentClass, icons[1]);
88         }
89         
90         
91         /**
92          * Return the small icon for a component type.
93          * 
94          * @param c             the component class.
95          * @return              the icon, or <code>null</code> if none available.
96          */
97         public static Icon getSmallIcon(Class<?> c) {
98                 return SMALL_ICONS.get(c);
99         }
100         
101         /**
102          * Return the large icon for a component type.
103          * 
104          * @param c             the component class.
105          * @return              the icon, or <code>null</code> if none available.
106          */
107         public static Icon getLargeIcon(Class<?> c) {
108                 return LARGE_ICONS.get(c);
109         }
110         
111         /**
112          * Return the large disabled icon for a component type.
113          * 
114          * @param c             the component class.
115          * @return              the icon, or <code>null</code> if none available.
116          */
117         public static Icon getLargeDisabledIcon(Class<?> c) {
118                 return DISABLED_ICONS.get(c);
119         }
120         
121         
122
123
124         private static ImageIcon loadSmall(String file, String desc) {
125                 URL url = ClassLoader.getSystemResource(file);
126                 if (url == null) {
127                         Application.getExceptionHandler().handleErrorCondition("ERROR:  Couldn't find file: " + file);
128                         return null;
129                 }
130                 return new ImageIcon(url, desc);
131         }
132         
133         
134         private static ImageIcon[] loadLarge(String file, String desc) {
135                 ImageIcon[] icons = new ImageIcon[2];
136                 
137                 URL url = ClassLoader.getSystemResource(file);
138                 if (url != null) {
139                         BufferedImage bi, bi2;
140                         try {
141                                 bi = ImageIO.read(url);
142                                 bi2 = ImageIO.read(url); //  How the fsck can one duplicate a BufferedImage???
143                         } catch (IOException e) {
144                                 Application.getExceptionHandler().handleErrorCondition("ERROR:  Couldn't read file: " + file, e);
145                                 return new ImageIcon[] { null, null };
146                         }
147                         
148                         icons[0] = new ImageIcon(bi, desc);
149                         
150                         // Create disabled icon
151                         if (false) { // Fade using alpha 
152                         
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;
157                                         
158                                         //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
159                                 }
160                                 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
161                                 
162                         } else { // Raster alpha
163                         
164                                 for (int x = 0; x < bi.getWidth(); x++) {
165                                         for (int y = 0; y < bi.getHeight(); y++) {
166                                                 if ((x + y) % 2 == 0) {
167                                                         bi2.setRGB(x, y, 0);
168                                                 }
169                                         }
170                                 }
171                                 
172                         }
173                         
174                         //// (disabled)
175                         icons[1] = new ImageIcon(bi2, desc + " " + trans.get("ComponentIcons.disabled"));
176                         
177                         return icons;
178                 } else {
179                         Application.getExceptionHandler().handleErrorCondition("ERROR:  Couldn't find file: " + file);
180                         return new ImageIcon[] { null, null };
181                 }
182         }
183 }