I18 changes
[debian/openrocket] / 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         public static Icon getSmallIcon(Class<?> c) {
93                 return SMALL_ICONS.get(c);
94         }
95         public static Icon getLargeIcon(Class<?> c) {
96                 return LARGE_ICONS.get(c);
97         }
98         public static Icon getLargeDisabledIcon(Class<?> c) {
99                 return DISABLED_ICONS.get(c);
100         }
101         
102         
103         
104         
105         private static ImageIcon loadSmall(String file, String desc) {
106                 URL url = ClassLoader.getSystemResource(file);
107                 if (url==null) {
108                 ExceptionHandler.handleErrorCondition("ERROR:  Couldn't find file: " + file);
109                         return null;
110                 }
111                 return new ImageIcon(url, desc);
112         }
113         
114         
115         private static ImageIcon[] loadLarge(String file, String desc) {
116                 ImageIcon[] icons = new ImageIcon[2];
117                 
118                 URL url = ClassLoader.getSystemResource(file);
119             if (url != null) {
120                 BufferedImage bi,bi2;
121                 try {
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};
127                         }
128                         
129                         icons[0] = new ImageIcon(bi,desc);
130                         
131                         // Create disabled icon
132                         if (false) {   // Fade using alpha 
133                                 
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;
138                                         
139                                         //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
140                                 }
141                                 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
142
143                         } else {   // Raster alpha
144
145                                 for (int x=0; x < bi.getWidth(); x++) {
146                                         for (int y=0; y < bi.getHeight(); y++) {
147                                                 if ((x+y)%2 == 0) {
148                                                         bi2.setRGB(x, y, 0);
149                                                 }
150                                         }
151                                 }
152                                 
153                         }
154                         
155                         //// (disabled)
156                         icons[1] = new ImageIcon(bi2,desc + " " +trans.get("ComponentIcons.disabled"));
157                 
158                 return icons;
159             } else {
160                 ExceptionHandler.handleErrorCondition("ERROR:  Couldn't find file: " + file);
161                 return new ImageIcon[]{null,null};
162             }
163         }
164 }