Component scaling support
[debian/openrocket] / src / net / sf / openrocket / util / Icons.java
1 package net.sf.openrocket.util;
2
3 import java.net.URL;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.swing.Icon;
9 import javax.swing.ImageIcon;
10
11 import net.sf.openrocket.document.Simulation;
12 import net.sf.openrocket.gui.main.ExceptionHandler;
13 import net.sf.openrocket.logging.LogHelper;
14 import net.sf.openrocket.startup.Application;
15
16
17 public class Icons {
18         private static final LogHelper log = Application.getLogger();
19         
20         static {
21                 log.debug("Starting to load icons");
22         }
23         
24         /**
25          * Icons used for showing the status of a simulation (up to date, out of date, etc).
26          */
27         public static final Map<Simulation.Status, Icon> SIMULATION_STATUS_ICON_MAP;
28         static {
29                 HashMap<Simulation.Status, Icon> map = new HashMap<Simulation.Status, Icon>();
30                 map.put(Simulation.Status.NOT_SIMULATED, loadImageIcon("pix/spheres/gray-16x16.png", "Not simulated"));
31                 map.put(Simulation.Status.UPTODATE, loadImageIcon("pix/spheres/green-16x16.png", "Up to date"));
32                 map.put(Simulation.Status.LOADED, loadImageIcon("pix/spheres/yellow-16x16.png", "Loaded from file"));
33                 map.put(Simulation.Status.OUTDATED, loadImageIcon("pix/spheres/red-16x16.png", "Out-of-date"));
34                 map.put(Simulation.Status.EXTERNAL, loadImageIcon("pix/spheres/blue-16x16.png", "Imported data"));
35                 SIMULATION_STATUS_ICON_MAP = Collections.unmodifiableMap(map);
36         }
37         
38         public static final Icon SIMULATION_LISTENER_OK;
39         public static final Icon SIMULATION_LISTENER_ERROR;
40         static {
41                 SIMULATION_LISTENER_OK = SIMULATION_STATUS_ICON_MAP.get(Simulation.Status.UPTODATE);
42                 SIMULATION_LISTENER_ERROR = SIMULATION_STATUS_ICON_MAP.get(Simulation.Status.OUTDATED);
43         }
44         
45
46         public static final Icon FILE_NEW = loadImageIcon("pix/icons/document-new.png", "New document");
47         public static final Icon FILE_OPEN = loadImageIcon("pix/icons/document-open.png", "Open document");
48         public static final Icon FILE_OPEN_EXAMPLE = loadImageIcon("pix/icons/document-open-example.png", "Open example document");
49         public static final Icon FILE_SAVE = loadImageIcon("pix/icons/document-save.png", "Save document");
50         public static final Icon FILE_SAVE_AS = loadImageIcon("pix/icons/document-save-as.png", "Save document as");
51         public static final Icon FILE_PRINT = loadImageIcon("pix/icons/document-print.png", "Print document");
52         public static final Icon FILE_CLOSE = loadImageIcon("pix/icons/document-close.png", "Close document");
53         public static final Icon FILE_QUIT = loadImageIcon("pix/icons/application-exit.png", "Quit OpenRocket");
54         
55         public static final Icon EDIT_UNDO = loadImageIcon("pix/icons/edit-undo.png", "Undo");
56         public static final Icon EDIT_REDO = loadImageIcon("pix/icons/edit-redo.png", "Redo");
57         public static final Icon EDIT_CUT = loadImageIcon("pix/icons/edit-cut.png", "Cut");
58         public static final Icon EDIT_COPY = loadImageIcon("pix/icons/edit-copy.png", "Copy");
59         public static final Icon EDIT_PASTE = loadImageIcon("pix/icons/edit-paste.png", "Paste");
60         public static final Icon EDIT_DELETE = loadImageIcon("pix/icons/edit-delete.png", "Delete");
61         
62         public static final Icon ZOOM_IN = loadImageIcon("pix/icons/zoom-in.png", "Zoom in");
63         public static final Icon ZOOM_OUT = loadImageIcon("pix/icons/zoom-out.png", "Zoom out");
64         
65         public static final Icon PREFERENCES = loadImageIcon("pix/icons/preferences.png", "Preferences");
66         
67         public static final Icon DELETE = loadImageIcon("pix/icons/delete.png", "Delete");
68         
69         static {
70                 log.debug("Icons loaded");
71         }
72         
73         /**
74          * Load an ImageIcon from the specified file.  The file is obtained as a system
75          * resource from the normal classpath.  If the file cannot be loaded a bug dialog
76          * is opened and <code>null</code> is returned.
77          * 
78          * @param file  the file to load.
79          * @param name  the description of the icon.
80          * @return              the ImageIcon, or null if could not be loaded (after the user closes the dialog)
81          */
82         public static ImageIcon loadImageIcon(String file, String name) {
83                 if (System.getProperty("openrocket.unittest") != null) {
84                         return new ImageIcon();
85                 }
86                 
87                 URL url = ClassLoader.getSystemResource(file);
88                 if (url == null) {
89                         ExceptionHandler.handleErrorCondition("Image file " + file + " not found, ignoring.");
90                         return null;
91                 }
92                 return new ImageIcon(url, name);
93         }
94 }