updates for 0.9.4
[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.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.Bulkhead;
14 import net.sf.openrocket.rocketcomponent.CenteringRing;
15 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
16 import net.sf.openrocket.rocketcomponent.EngineBlock;
17 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
18 import net.sf.openrocket.rocketcomponent.InnerTube;
19 import net.sf.openrocket.rocketcomponent.LaunchLug;
20 import net.sf.openrocket.rocketcomponent.MassComponent;
21 import net.sf.openrocket.rocketcomponent.NoseCone;
22 import net.sf.openrocket.rocketcomponent.Parachute;
23 import net.sf.openrocket.rocketcomponent.ShockCord;
24 import net.sf.openrocket.rocketcomponent.Streamer;
25 import net.sf.openrocket.rocketcomponent.Transition;
26 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
27 import net.sf.openrocket.rocketcomponent.TubeCoupler;
28
29
30 public class ComponentIcons {
31         
32         private static final String ICON_DIRECTORY = "pix/componenticons/";
33         private static final String SMALL_SUFFIX = "-small.png";
34         private static final String LARGE_SUFFIX = "-large.png";
35         
36         private static final HashMap<Class<?>,ImageIcon> SMALL_ICONS = 
37                 new HashMap<Class<?>,ImageIcon>();
38         private static final HashMap<Class<?>,ImageIcon> LARGE_ICONS = 
39                 new HashMap<Class<?>,ImageIcon>();
40         private static final HashMap<Class<?>,ImageIcon> DISABLED_ICONS = 
41                 new HashMap<Class<?>,ImageIcon>();
42
43         static {
44                 load("nosecone", "Nose cone", NoseCone.class);
45                 load("bodytube", "Body tube", BodyTube.class);
46                 load("transition", "Transition", Transition.class);
47                 load("trapezoidfin", "Trapezoidal fin set", TrapezoidFinSet.class);
48                 load("ellipticalfin", "Elliptical fin set", EllipticalFinSet.class);
49                 load("freeformfin", "Freeform fin set", FreeformFinSet.class);
50                 load("launchlug", "Launch lug", LaunchLug.class);
51                 load("innertube", "Inner tube", InnerTube.class);
52                 load("tubecoupler", "Tube coupler", TubeCoupler.class);
53                 load("centeringring", "Centering ring", CenteringRing.class);
54                 load("bulkhead", "Bulk head", Bulkhead.class);
55                 load("engineblock", "Engine block", EngineBlock.class);
56                 load("parachute", "Parachute", Parachute.class);
57                 load("streamer", "Streamer", Streamer.class);
58                 load("shockcord", "Shock cord", ShockCord.class);
59                 load("mass", "Mass component", MassComponent.class);
60         }
61         
62         private static void load(String filename, String name, Class<?> componentClass) {
63                 ImageIcon icon = loadSmall(ICON_DIRECTORY + filename + SMALL_SUFFIX, name);
64                 SMALL_ICONS.put(componentClass, icon);
65                 
66                 ImageIcon[] icons = loadLarge(ICON_DIRECTORY + filename + LARGE_SUFFIX, name);
67                 LARGE_ICONS.put(componentClass, icons[0]);
68                 DISABLED_ICONS.put(componentClass, icons[1]);
69         }
70         
71         
72         
73         public static Icon getSmallIcon(Class<?> c) {
74                 return SMALL_ICONS.get(c);
75         }
76         public static Icon getLargeIcon(Class<?> c) {
77                 return LARGE_ICONS.get(c);
78         }
79         public static Icon getLargeDisabledIcon(Class<?> c) {
80                 return DISABLED_ICONS.get(c);
81         }
82         
83         
84         
85         
86         private static ImageIcon loadSmall(String file, String desc) {
87                 URL url = ClassLoader.getSystemResource(file);
88                 if (url==null) {
89                 ExceptionHandler.handleErrorCondition("ERROR:  Couldn't find file: " + file);
90                         return null;
91                 }
92                 return new ImageIcon(url, desc);
93         }
94         
95         
96         private static ImageIcon[] loadLarge(String file, String desc) {
97                 ImageIcon[] icons = new ImageIcon[2];
98                 
99                 URL url = ClassLoader.getSystemResource(file);
100             if (url != null) {
101                 BufferedImage bi,bi2;
102                 try {
103                                 bi = ImageIO.read(url);
104                                 bi2 = ImageIO.read(url);   //  How the fsck can one duplicate a BufferedImage???
105                         } catch (IOException e) {
106                                 ExceptionHandler.handleErrorCondition("ERROR:  Couldn't read file: "+file, e);
107                         return new ImageIcon[]{null,null};
108                         }
109                         
110                         icons[0] = new ImageIcon(bi,desc);
111                         
112                         // Create disabled icon
113                         if (false) {   // Fade using alpha 
114                                 
115                                 int rgb[] = bi2.getRGB(0,0,bi2.getWidth(),bi2.getHeight(),null,0,bi2.getWidth());
116                                 for (int i=0; i<rgb.length; i++) {
117                                         final int alpha = (rgb[i]>>24)&0xFF;
118                                         rgb[i] = (rgb[i]&0xFFFFFF) | (alpha/3)<<24;
119                                         
120                                         //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
121                                 }
122                                 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
123
124                         } else {   // Raster alpha
125
126                                 for (int x=0; x < bi.getWidth(); x++) {
127                                         for (int y=0; y < bi.getHeight(); y++) {
128                                                 if ((x+y)%2 == 0) {
129                                                         bi2.setRGB(x, y, 0);
130                                                 }
131                                         }
132                                 }
133                                 
134                         }
135                         
136                         icons[1] = new ImageIcon(bi2,desc + " (disabled)");
137                 
138                 return icons;
139             } else {
140                 ExceptionHandler.handleErrorCondition("ERROR:  Couldn't find file: " + file);
141                 return new ImageIcon[]{null,null};
142             }
143         }
144 }