Include OSX UI elements.
authorbkuker <bkuker@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sun, 24 Jun 2012 19:22:07 +0000 (19:22 +0000)
committerbkuker <bkuker@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sun, 24 Jun 2012 19:22:07 +0000 (19:22 +0000)
"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

core/.classpath
core/ChangeLog
core/build.xml
core/lib/OrangeExtensions-1.2.jar [new file with mode: 0644]
core/src/net/sf/openrocket/startup/OSXStartup.java [new file with mode: 0644]
core/src/net/sf/openrocket/startup/Startup2.java

index ac3ceedf2cd6f594273d6a6d55edbacec27cc9d7..8689c0026e87a672acafc9efe8e2cdf4ef918e2a 100644 (file)
@@ -30,5 +30,6 @@
        <classpathentry kind="lib" path="lib/exp4j-0.2.9.jar"/>
        <classpathentry kind="lib" path="lib/jogl/gluegen-rt.jar"/>
        <classpathentry kind="lib" path="lib/jogl/jogl.all.jar"/>
+       <classpathentry kind="lib" path="lib/OrangeExtensions-1.2.jar"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index 5f605187bb26ec8e796f3cb3ea0f45cd421d8b20..10d880de1684fd2c4ff190fb0d9f98d13cad076b 100644 (file)
@@ -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.
index 3b9a23777ed96577806d685464cc4e5c7f65ab21..b4ee250f1a4425a215b0f808c1a94498b0b707cf 100644 (file)
@@ -76,7 +76,7 @@
                                <attribute name="Rsrc-Main-Class" value="${main-class}" />
                                <attribute name="SplashScreen-Image" value="pix/splashscreen.png" />
                                <attribute name="Class-Path" value="." />
-                               <attribute name="Rsrc-Class-Path" value="./ main/${ant.project.name}-Core.jar lib/jfreechart-1.0.13.jar lib/jcommon-1.0.16.jar lib/gluegen-rt.jar lib/miglayout15-swing.jar lib/iText-5.0.2.jar lib/jogl.all.jar lib/opencsv-2.3.jar" />
+                               <attribute name="Rsrc-Class-Path" value="./ main/${ant.project.name}-Core.jar lib/jfreechart-1.0.13.jar lib/jcommon-1.0.16.jar lib/gluegen-rt.jar lib/miglayout15-swing.jar lib/iText-5.0.2.jar lib/jogl.all.jar lib/opencsv-2.3.jar lib/OrangeExtensions-1.2.jar" />
                        </manifest>
 
                        <!-- Unzip the Eclipse JIJ Loader -->
diff --git a/core/lib/OrangeExtensions-1.2.jar b/core/lib/OrangeExtensions-1.2.jar
new file mode 100644 (file)
index 0000000..637d396
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 (file)
index 0000000..ad14186
--- /dev/null
@@ -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 <bkuker@billkuker.com>
+ * 
+ */
+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);
+               }
+       }
+
+}
index 3ab137a90f3b54a0c1ba848006aec7409700a8c4..85500a361299d3f49668afd4c9c983791406d9a5 100644 (file)
@@ -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() {