From: bkuker Date: Sun, 24 Jun 2012 19:22:07 +0000 (+0000) Subject: Include OSX UI elements. X-Git-Tag: upstream/12.09^2~156 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=63504b5b5450f1ebc504551fbd8c89db36ecd4aa;p=debian%2Fopenrocket Include OSX UI elements. "OrangeExtensions" jar provides stubs for compilation and linking on other OSs. git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@803 180e2498-e6e9-4542-8430-84ac67f01cd8 --- diff --git a/core/.classpath b/core/.classpath index ac3ceedf..8689c002 100644 --- a/core/.classpath +++ b/core/.classpath @@ -30,5 +30,6 @@ + diff --git a/core/ChangeLog b/core/ChangeLog index 5f605187..10d880de 100644 --- a/core/ChangeLog +++ b/core/ChangeLog @@ -1,3 +1,9 @@ +2012-06-24 Bill Kuker + + * OSX UI Elements: Screen menu bar, Application name, Dock Icon, Quit, About & Preference + handlers. Stubs for the "Apple Java Extensions" to allow other platforms to compile provided + by https://github.com/ymasory/OrangeExtensions. + 2012-06-05 Doug Pedrick * Most recently used design files added to File menu. diff --git a/core/build.xml b/core/build.xml index 3b9a2377..b4ee250f 100644 --- a/core/build.xml +++ b/core/build.xml @@ -76,7 +76,7 @@ - + diff --git a/core/lib/OrangeExtensions-1.2.jar b/core/lib/OrangeExtensions-1.2.jar new file mode 100644 index 00000000..637d3969 Binary files /dev/null and b/core/lib/OrangeExtensions-1.2.jar differ diff --git a/core/src/net/sf/openrocket/startup/OSXStartup.java b/core/src/net/sf/openrocket/startup/OSXStartup.java new file mode 100644 index 00000000..ad14186b --- /dev/null +++ b/core/src/net/sf/openrocket/startup/OSXStartup.java @@ -0,0 +1,115 @@ +package net.sf.openrocket.startup; + +import java.awt.Image; +import java.awt.Toolkit; + +import net.sf.openrocket.arch.SystemInfo; +import net.sf.openrocket.arch.SystemInfo.Platform; +import net.sf.openrocket.gui.dialogs.AboutDialog; +import net.sf.openrocket.gui.dialogs.preferences.PreferencesDialog; +import net.sf.openrocket.gui.main.BasicFrame; +import net.sf.openrocket.logging.LogHelper; + +import com.apple.eawt.AboutHandler; +import com.apple.eawt.PreferencesHandler; +import com.apple.eawt.QuitHandler; +import com.apple.eawt.QuitResponse; +import com.apple.eawt.AppEvent.AboutEvent; +import com.apple.eawt.AppEvent.PreferencesEvent; +import com.apple.eawt.AppEvent.QuitEvent; + +/** + * Static code for initialization of OSX UI Elements: Menu, Icon, Name and + * Application menu handlers. + * + * @author Bill Kuker + * + */ +final class OSXStartup { + private static final LogHelper log = Application.getLogger(); + + // The name in the app menu + private static final String APP_NAME = "OpenRocket"; + + // The image resource to use for the Dock Icon + private static final String ICON_RSRC = "/pix/icon/icon-256.png"; + + /** + * The handler for the Quit item in the OSX app menu + */ + private static final QuitHandler qh = new QuitHandler() { + @Override + public void handleQuitRequestWith(final QuitEvent e, final QuitResponse r) { + BasicFrame.quitAction(); + // if we get here the user canceled + r.cancelQuit(); + } + }; + + /** + * The handler for the About item in the OSX app menu + */ + private static final AboutHandler ah = new AboutHandler() { + @Override + public void handleAbout(final AboutEvent a) { + new AboutDialog(null).setVisible(true); + } + }; + + /** + * The handler for the Preferences item in the OSX app menu + */ + private static final PreferencesHandler ph = new PreferencesHandler() { + @Override + public void handlePreferences(final PreferencesEvent p) { + PreferencesDialog.showPreferences(null); + } + }; + + /** + * Sets up the Application's Icon, Name, Menu and some menu item handlers + * for Apple OSX. This method needs to be called before other AWT or Swing + * things happen, or parts will fail to work. + * + * This function should fail gracefully if the OS is wrong. + */ + static void setupOSX() { + if (SystemInfo.getPlatform() != Platform.MAC_OS) { + log.warn("Attempting to set up OSX UI on non-MAC_OS"); + } + log.debug("Setting up OSX UI Elements"); + try { + // Put the menu bar at the top of the screen + System.setProperty("apple.laf.useScreenMenuBar", "true"); + // Set the name in the menu + System.setProperty("com.apple.mrj.application.apple.menu.about.name", APP_NAME); + + // This line must come AFTER the above properties are set, otherwise + // the name will not appear + final com.apple.eawt.Application osxApp = com.apple.eawt.Application.getApplication(); + + if (osxApp == null) { + // Application is null: Something is wrong, give up on OSX + // setup. + throw new NullPointerException("com.apple.eawt.Application.getApplication() returned NULL. " + + "Aborting OSX UI Setup."); + } + + // Set handlers + osxApp.setQuitHandler(qh); + osxApp.setAboutHandler(ah); + osxApp.setPreferencesHandler(ph); + + // Set the dock icon to the largest icon + final Image dockIcon = Toolkit.getDefaultToolkit().getImage( + Startup2.class.getResource(ICON_RSRC)); + osxApp.setDockIconImage(dockIcon); + + } catch (final Throwable t) { + // None of the preceding is critical to the app, + // so at worst case log an error and continue + log.warn("Error setting up OSX UI:", t); + } + } + +} diff --git a/core/src/net/sf/openrocket/startup/Startup2.java b/core/src/net/sf/openrocket/startup/Startup2.java index 3ab137a9..85500a36 100644 --- a/core/src/net/sf/openrocket/startup/Startup2.java +++ b/core/src/net/sf/openrocket/startup/Startup2.java @@ -9,6 +9,8 @@ import javax.swing.SwingUtilities; import javax.swing.Timer; import javax.swing.ToolTipManager; +import net.sf.openrocket.arch.SystemInfo; +import net.sf.openrocket.arch.SystemInfo.Platform; import net.sf.openrocket.communication.UpdateInfo; import net.sf.openrocket.communication.UpdateInfoRetriever; import net.sf.openrocket.database.ComponentPresetDatabase; @@ -52,6 +54,11 @@ public class Startup2 { VersionHelper.checkVersion(); VersionHelper.checkOpenJDK(); + // If running on a MAC set up OSX UI Elements. + if ( SystemInfo.getPlatform() == Platform.MAC_OS ){ + OSXStartup.setupOSX(); + } + // Run the actual startup method in the EDT since it can use progress dialogs etc. log.info("Moving startup to EDT"); SwingUtilities.invokeAndWait(new Runnable() {