Revision: 5944 http://sourceforge.net/p/jump-pilot/code/5944 Author: edso Date: 2018-09-25 13:47:47 +0000 (Tue, 25 Sep 2018) Log Message: ----------- finetuning macos integration
Modified Paths: -------------- core/trunk/src/com/vividsolutions/jump/workbench/ui/WorkbenchFrame.java core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/OptionsPlugIn.java Modified: core/trunk/src/com/vividsolutions/jump/workbench/ui/WorkbenchFrame.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/workbench/ui/WorkbenchFrame.java 2018-09-25 13:03:23 UTC (rev 5943) +++ core/trunk/src/com/vividsolutions/jump/workbench/ui/WorkbenchFrame.java 2018-09-25 13:47:47 UTC (rev 5944) @@ -2325,116 +2325,136 @@ getTaskListeners().remove(l); } - private class AppleHandler implements InvocationHandler { - public void register() { - // import com.apple.eawt.AboutHandler; - // import com.apple.eawt.AppEvent.AboutEvent; - // import com.apple.eawt.AppEvent.QuitEvent; - // import com.apple.eawt.Application; - // import com.apple.eawt.QuitHandler; - // import com.apple.eawt.QuitResponse; + private class AppleHandler implements InvocationHandler { + public void register() { + // import com.apple.eawt.AboutHandler; + // import com.apple.eawt.AppEvent.AboutEvent; + // import com.apple.eawt.AppEvent.QuitEvent; + // import com.apple.eawt.Application; + // import com.apple.eawt.QuitHandler; + // import com.apple.eawt.QuitResponse; + + // com.apple.eawt.Application app = + // com.apple.eawt.Application.getApplication(); + // app.setQuitHandler(new com.apple.eawt.QuitHandler() { + // public void handleQuitRequestWith( + // com.apple.eawt.AppEvent.QuitEvent e, + // com.apple.eawt.QuitResponse resp) { + // closeApplication(); + // // still here?, must have been cancelled + // resp.cancelQuit(); + // } + // }); + // app.setAboutHandler(new com.apple.eawt.AboutHandler() { + // public void handleAbout(com.apple.eawt.AppEvent.AboutEvent e) { + // AboutDialog.instance(getContext()).setVisible(true); + // } + // }); + // app.removePreferencesMenuItem(); + + // using reflection to avoid macos specific classes being required for + // compiling on non macos platforms + try { + Class<?> desktopClass = null; + Object desktopObject = null; - // com.apple.eawt.Application app = - // com.apple.eawt.Application.getApplication(); - // app.setQuitHandler(new com.apple.eawt.QuitHandler() { - // public void handleQuitRequestWith( - // com.apple.eawt.AppEvent.QuitEvent e, - // com.apple.eawt.QuitResponse resp) { - // closeApplication(); - // // still here?, must have been cancelled - // resp.cancelQuit(); - // } - // }); - // app.setAboutHandler(new com.apple.eawt.AboutHandler() { - // public void handleAbout(com.apple.eawt.AppEvent.AboutEvent e) { - // AboutDialog.instance(getContext()).setVisible(true); - // } - // }); - // app.removePreferencesMenuItem(); - - // using reflection to avoid macos specific classes being required for - // compiling on non macos platforms - Class<?> applicationClass = findClass("Application"); - if (applicationClass == null) { - Logger.error("Couldn't find apple java extension application class. Skip registering handlers."); - return; + // try new java9+ way + desktopClass = findClass("Desktop", new String[] { "java.awt" }); + if (desktopClass != null) { + desktopObject = desktopClass.getMethod("getDesktop").invoke(null); + } + // try old java8- apple java extensions way + desktopClass = findClass("Application", new String[] { "com.apple.eawt" }); + if (desktopClass != null) { + desktopObject = desktopClass.getDeclaredMethod("getApplication").invoke(null); + } + + // give up now + if (desktopClass == null) { + Logger.error("Couldn't find apple desktop class. Skip registering desktop handlers."); + return; + } + + Class<?> quitHandlerClass = findClass("QuitHandler"); + Class<?> aboutHandlerClass = findClass("AboutHandler"); + Class<?> openFilesHandlerClass = findClass("OpenFilesHandler"); + Class<?> preferencesHandlerClass = findClass("PreferencesHandler"); + + // fetch instance of app + // Object application = applicationClass.getConstructor((Class[]) + // null).newInstance((Object[]) null); + // Object application = + // applicationClass.getDeclaredMethod("getApplication").invoke(null); + + Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), + new Class<?>[] { quitHandlerClass, aboutHandlerClass, openFilesHandlerClass, preferencesHandlerClass }, + this); + + if (quitHandlerClass != null) + desktopClass.getDeclaredMethod("setQuitHandler", quitHandlerClass).invoke(desktopObject, proxy); + if (aboutHandlerClass != null) + desktopClass.getDeclaredMethod("setAboutHandler", aboutHandlerClass).invoke(desktopObject, proxy); + if (openFilesHandlerClass != null) + desktopClass.getDeclaredMethod("setOpenFileHandler", openFilesHandlerClass).invoke(desktopObject, proxy); + if (preferencesHandlerClass != null) + desktopClass.getDeclaredMethod("setPreferencesHandler", preferencesHandlerClass).invoke(desktopObject, proxy); + } catch ( /* InstantiationException | */ IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { + Logger.error(e); + } } - - Class<?> quitHandlerClass = findClass("QuitHandler"); - Class<?> aboutHandlerClass = findClass("AboutHandler"); - Class<?> openFilesHandlerClass = findClass("OpenFilesHandler"); - Class<?> preferencesHandlerClass = findClass("PreferencesHandler"); - - try { - // fetch instance of app - //Object application = applicationClass.getConstructor((Class[]) null).newInstance((Object[]) null); - Object application = applicationClass.getDeclaredMethod("getApplication").invoke(null); - - Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), - new Class<?>[] { quitHandlerClass, aboutHandlerClass, openFilesHandlerClass, preferencesHandlerClass }, - this); - - if (quitHandlerClass != null) - applicationClass.getDeclaredMethod("setQuitHandler", quitHandlerClass).invoke(application, proxy); - if (aboutHandlerClass != null) - applicationClass.getDeclaredMethod("setAboutHandler", aboutHandlerClass).invoke(application, proxy); - if (openFilesHandlerClass != null) - applicationClass.getDeclaredMethod("setOpenFileHandler", openFilesHandlerClass).invoke(application, proxy); - if (preferencesHandlerClass != null) - applicationClass.getDeclaredMethod("setPreferencesHandler", preferencesHandlerClass).invoke(application, - proxy); - } catch ( /*InstantiationException |*/ IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e) { - Logger.error(e); + + String[] packageNames = new String[] { "java.awt.desktop", "com.apple.eawt" }; + + private Class findClass(String className) { + return findClass(className, packageNames); } - } - - String[] packageNames = new String[]{"java.awt.desktop","com.apple.eawt"}; - - private Class findClass(String className) { - // since java9 apple java extensions moved into "java.awt.desktop" - for (String packageName : packageNames) { - Logger.debug("Looking for apple handler '"+ className +"' .."); - String fullClassName = packageName + "." + className; - try { - Logger.debug("Try '"+ fullClassName +"' .."); - return Class.forName(fullClassName); - } catch (ClassNotFoundException e) { - Logger.debug("class not avail '"+ fullClassName +"'"); - continue; + + private Class findClass(String className, String[] packageNameList) { + // since java9 apple java extensions moved into package "java.awt.desktop" + for (String packageName : packageNames) { + Logger.debug("Looking for apple handler '" + className + "' .."); + String fullClassName = packageName + "." + className; + try { + Logger.debug("Try '" + fullClassName + "' .."); + return Class.forName(fullClassName); + } catch (ClassNotFoundException e) { + Logger.debug("class not avail '" + fullClassName + "'"); + continue; + } } + return null; } - return null; - } - - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - if ("openFiles".equals(method.getName())) { - // TODO: implement -// if (args[0] != null) { -// Object files = args[0].getClass().getMethod("getFiles").invoke(args[0]); -// if (files instanceof List) { -// OpenAction openAction = new OpenAction(kseFrame); -// for (File file : (List<File>) files) { -// openAction.openKeyStore(file); -// } -// } -// } - } else if ("handleQuitRequestWith".equals(method.getName())) { - closeApplication(); - // If we have returned from the above call the user has cancelled - if (args[1] != null) { - args[1].getClass().getDeclaredMethod("cancelQuit").invoke(args[1]); - } - } else if ("handleAbout".equals(method.getName())) { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + if ("openFiles".equals(method.getName())) { + // TODO: implement + // if (args[0] != null) { + // Object files = + // args[0].getClass().getMethod("getFiles").invoke(args[0]); + // if (files instanceof List) { + // OpenAction openAction = new OpenAction(kseFrame); + // for (File file : (List<File>) files) { + // openAction.openKeyStore(file); + // } + // } + // } + } else if ("handleQuitRequestWith".equals(method.getName())) { + closeApplication(); + // If we have returned from the above call the user has cancelled + if (args[1] != null) { + args[1].getClass().getDeclaredMethod("cancelQuit").invoke(args[1]); + } + } else if ("handleAbout".equals(method.getName())) { AboutDialog.instance(getContext()).setVisible(true); - } else if ("handlePreferences".equals(method.getName())) { + } else if ("handlePreferences".equals(method.getName())) { OptionsPlugIn.execute(); + } + return null; } - return null; + } - - } // run a plugin internally, used for the statusbar private boolean executePlugin(PlugIn plugin) { Modified: core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/OptionsPlugIn.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/OptionsPlugIn.java 2018-09-25 13:03:23 UTC (rev 5943) +++ core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/OptionsPlugIn.java 2018-09-25 13:47:47 UTC (rev 5944) @@ -107,20 +107,21 @@ } // static execute method for usage in apple handler - public static boolean execute(){ - if (instance!=null) - return false; - - OptionsPlugIn p = new OptionsPlugIn(); - try { - PlugInContext pc = JUMPWorkbench.getInstance().getContext().createPlugInContext(); - p.initialize(pc); - return p.execute(pc); - } catch (Exception e) { - JUMPWorkbench.getInstance().getFrame().handleThrowable(e); + public static boolean execute() throws Exception { + PlugInContext pc = JUMPWorkbench.getInstance().getContext().createPlugInContext(); + + if (instance == null) { + OptionsPlugIn p = new OptionsPlugIn(); + try { + p.initialize(pc); + instance = p; + } catch (Exception e) { + JUMPWorkbench.getInstance().getFrame().handleThrowable(e); + return false; + } } - - return false; + + return instance.execute(pc); } public Icon getIcon(int height) { _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel