1 package net.sf.openrocket.startup;
4 import java.awt.Toolkit;
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;
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;
22 * Static code for initialization of OSX UI Elements: Menu, Icon, Name and
23 * Application menu handlers.
25 * @author Bill Kuker <bkuker@billkuker.com>
28 final class OSXStartup {
29 private static final LogHelper log = Application.getLogger();
31 // The name in the app menu
32 private static final String APP_NAME = "OpenRocket";
34 // The image resource to use for the Dock Icon
35 private static final String ICON_RSRC = "/pix/icon/icon-256.png";
38 * The handler for the Quit item in the OSX app menu
40 private static final QuitHandler qh = new QuitHandler() {
42 public void handleQuitRequestWith(final QuitEvent e, final QuitResponse r) {
43 BasicFrame.quitAction();
44 // if we get here the user canceled
50 * The handler for the About item in the OSX app menu
52 private static final AboutHandler ah = new AboutHandler() {
54 public void handleAbout(final AboutEvent a) {
55 new AboutDialog(null).setVisible(true);
60 * The handler for the Preferences item in the OSX app menu
62 private static final PreferencesHandler ph = new PreferencesHandler() {
64 public void handlePreferences(final PreferencesEvent p) {
65 PreferencesDialog.showPreferences(null);
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.
74 * This function should fail gracefully if the OS is wrong.
76 static void setupOSX() {
77 if (SystemInfo.getPlatform() != Platform.MAC_OS) {
78 log.warn("Attempting to set up OSX UI on non-MAC_OS");
80 log.debug("Setting up OSX UI Elements");
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);
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();
92 // Application is null: Something is wrong, give up on OSX
94 throw new NullPointerException("com.apple.eawt.Application.getApplication() returned NULL. "
95 + "Aborting OSX UI Setup.");
99 osxApp.setQuitHandler(qh);
100 osxApp.setAboutHandler(ah);
101 osxApp.setPreferencesHandler(ph);
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);
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);