/***
    Copyright 2017 by Sean Luke
    Licensed under the Apache License version 2.0
*/

package edisyn;

import java.lang.reflect.*;

// Code for handling MacOS X specific About Menus.  Maybe also we'll handle Preferences later.
//
// Largely cribbed from https://stackoverflow.com/questions/7256230/in-order-to-macify-a-java-app-to-catch-the-about-event-do-i-have-to-implement
//
// I used the reflection version so it compiles cleanly on linux and windows as well.

public class Mac
    {
    public static void setup(Synth synth)
        {
        if (System.getProperty("os.name").contains("Mac")) 
            {
            try 
                {
                Object app = Class.forName("com.apple.eawt.Application").getMethod("getApplication").invoke(null);

                Object al = Proxy.newProxyInstance(
                    Class.forName("com.apple.eawt.AboutHandler").getClassLoader(),
                    new Class[]{Class.forName("com.apple.eawt.AboutHandler")},
                    new AboutListener(synth));

                app.getClass().getMethod("setAboutHandler", Class.forName("com.apple.eawt.AboutHandler")).invoke(app, al);
                }
            catch (Exception e) 
                {
                //fail quietly
                }
            }       
        }
    }
        
class AboutListener implements InvocationHandler 
    {
    Synth synth;
    public AboutListener(Synth synth)
        {
        this.synth = synth;
        }
                
    public Object invoke(Object proxy, Method method, Object[] args) 
        {
        synth.doAbout();
        return null;
        }
    }

@@ -1148,16 +1148,388 @@
 pp, al);
+%0A%0A             %09al = Proxy.newProxyInstance(%0A                    Class.forName(%22com.apple.eawt.QuitHandler%22).getClassLoader(),%0A                    new Class%5B%5D%7BClass.forName(%22com.apple.eawt.QuitHandler%22)%7D,%0A                    new QuitListener(synth));%0A%0A                app.getClass().getMethod(%22setQuitHandler%22, Class.forName(%22com.apple.eawt.QuitHandler%22)).invoke(app, al);
 %0A       
@@ -1987,24 +1987,340 @@
 n null;%0A        %7D%0A    %7D%0A
+%0Aclass QuitListener implements InvocationHandler %0A    %7B%0A    Synth synth;%0A    public QuitListener(Synth synth)%0A        %7B%0A        this.synth = synth;%0A        %7D%0A                %0A    public Object invoke(Object proxy, Method method, Object%5B%5D args) %0A        %7B%0A        synth.doQuit();%0A        return null;%0A        %7D%0A    %7D%0A

Quit Handler