create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / startup / OSXStartup.java
1 package net.sf.openrocket.startup;
2
3 import java.awt.Image;
4 import java.awt.Toolkit;
5
6 import net.sf.openrocket.arch.SystemInfo;
7 import net.sf.openrocket.arch.SystemInfo.Platform;
8 import net.sf.openrocket.gui.dialogs.AboutDialog;
9 import net.sf.openrocket.gui.dialogs.preferences.PreferencesDialog;
10 import net.sf.openrocket.gui.main.BasicFrame;
11 import net.sf.openrocket.logging.LogHelper;
12
13 import com.apple.eawt.AboutHandler;
14 import com.apple.eawt.PreferencesHandler;
15 import com.apple.eawt.QuitHandler;
16 import com.apple.eawt.QuitResponse;
17 import com.apple.eawt.AppEvent.AboutEvent;
18 import com.apple.eawt.AppEvent.PreferencesEvent;
19 import com.apple.eawt.AppEvent.QuitEvent;
20
21 /**
22  * Static code for initialization of OSX UI Elements: Menu, Icon, Name and
23  * Application menu handlers.
24  * 
25  * @author Bill Kuker <bkuker@billkuker.com>
26  * 
27  */
28 final class OSXStartup {
29         private static final LogHelper log = Application.getLogger();
30
31         // The name in the app menu
32         private static final String APP_NAME = "OpenRocket";
33         
34         // The image resource to use for the Dock Icon
35         private static final String ICON_RSRC = "/pix/icon/icon-256.png";
36         
37         /**
38          * The handler for the Quit item in the OSX app menu
39          */
40         private static final QuitHandler qh = new QuitHandler() {
41                 @Override
42                 public void handleQuitRequestWith(final QuitEvent e, final QuitResponse r) {
43                         BasicFrame.quitAction();
44                         // if we get here the user canceled
45                         r.cancelQuit();
46                 }
47         };
48
49         /**
50          * The handler for the About item in the OSX app menu
51          */
52         private static final AboutHandler ah = new AboutHandler() {
53                 @Override
54                 public void handleAbout(final AboutEvent a) {
55                         new AboutDialog(null).setVisible(true);
56                 }
57         };
58
59         /**
60          * The handler for the Preferences item in the OSX app menu
61          */
62         private static final PreferencesHandler ph = new PreferencesHandler() {
63                 @Override
64                 public void handlePreferences(final PreferencesEvent p) {
65                         PreferencesDialog.showPreferences(null);
66                 }
67         };
68
69         /**
70          * Sets up the Application's Icon, Name, Menu and some menu item handlers
71          * for Apple OSX. This method needs to be called before other AWT or Swing
72          * things happen, or parts will fail to work.
73          * 
74          * This function should fail gracefully if the OS is wrong.
75          */
76         static void setupOSX() {
77                 if (SystemInfo.getPlatform() != Platform.MAC_OS) {
78                         log.warn("Attempting to set up OSX UI on non-MAC_OS");
79                 }
80                 log.debug("Setting up OSX UI Elements");
81                 try {
82                         // Put the menu bar at the top of the screen
83                         System.setProperty("apple.laf.useScreenMenuBar", "true");
84                         // Set the name in the menu
85                         System.setProperty("com.apple.mrj.application.apple.menu.about.name", APP_NAME);
86
87                         // This line must come AFTER the above properties are set, otherwise
88                         // the name will not appear
89                         final com.apple.eawt.Application osxApp = com.apple.eawt.Application.getApplication();
90
91                         if (osxApp == null) {
92                                 // Application is null: Something is wrong, give up on OSX
93                                 // setup.
94                                 throw new NullPointerException("com.apple.eawt.Application.getApplication() returned NULL. "
95                                                 + "Aborting OSX UI Setup.");
96                         }
97                         
98                         // Set handlers
99                         osxApp.setQuitHandler(qh);
100                         osxApp.setAboutHandler(ah);
101                         osxApp.setPreferencesHandler(ph);
102
103                         // Set the dock icon to the largest icon
104                         final Image dockIcon = Toolkit.getDefaultToolkit().getImage(
105                                         Startup2.class.getResource(ICON_RSRC));
106                         osxApp.setDockIconImage(dockIcon);
107
108                 } catch (final Throwable t) {
109                         // None of the preceding is critical to the app,
110                         // so at worst case log an error and continue
111                         log.warn("Error setting up OSX UI:", t);
112                 }
113         }
114
115 }