You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/07/17 18:00:41 UTC

svn commit: r1836123 - in /pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger: PDFDebugger.java ui/OSXAdapter.java

Author: tilman
Date: Tue Jul 17 18:00:40 2018
New Revision: 1836123

URL: http://svn.apache.org/viewvc?rev=1836123&view=rev
Log:
PDFBOX-4013: support MacOS features on jdk9, by Emmeran Seehuber

Modified:
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/OSXAdapter.java

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java?rev=1836123&r1=1836122&r2=1836123&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java Tue Jul 17 18:00:40 2018
@@ -45,7 +45,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Properties;
 import java.util.Set;
-import java.util.StringTokenizer;
 import javax.imageio.spi.IIORegistry;
 
 import org.apache.commons.logging.Log;
@@ -450,10 +449,21 @@ public class PDFDebugger extends JFrame
             }
         });
 
+        initGlobalEventHandlers();
+
+    }
+
+    /**
+     * Initialise application global event handlers.
+     * Protected to allow subclasses to override this method if they
+     * don't want the global event handler overridden.
+     */
+    @SuppressWarnings("WeakerAccess")
+    protected void initGlobalEventHandlers()
+    {
         // Mac OS X file open/quit handler
-        if (IS_MAC_OS && !isMinJdk9())
+        if (IS_MAC_OS)
         {
-            //TODO this needs to be rewritten for JDK9, see PDFBOX-4013
             try
             {
                 Method osxOpenFiles = getClass().getDeclaredMethod("osxOpenFiles", String.class);
@@ -565,11 +575,8 @@ public class PDFDebugger extends JFrame
             }
         });
 
-        if (!IS_MAC_OS)
-        {
-            fileMenu.addSeparator();
-            fileMenu.add(printMenuItem);
-        }
+        fileMenu.addSeparator();
+        fileMenu.add(printMenuItem);
 
         JMenuItem exitMenuItem = new JMenuItem("Exit");
         exitMenuItem.setAccelerator(KeyStroke.getKeyStroke("alt F4"));
@@ -728,7 +735,7 @@ public class PDFDebugger extends JFrame
                 openDialog.setVisible(true);
                 if (openDialog.getFile() != null)
                 {
-                    readPDFFile(openDialog.getFile(), "");
+                    readPDFFile(new File(openDialog.getDirectory(),openDialog.getFile()), "");
                 }
             }
             else
@@ -1173,7 +1180,7 @@ public class PDFDebugger extends JFrame
         return data;
     }
     
-    private void exitMenuItemActionPerformed(ActionEvent evt)
+    private void exitMenuItemActionPerformed(ActionEvent ignored)
     {
         if( document != null )
         {
@@ -1191,6 +1198,16 @@ public class PDFDebugger extends JFrame
                 throw new RuntimeException(e);
             }
         }
+        performApplicationExit();
+    }
+
+    /**
+     * Exit the application after the window is closed. This is protected to
+     * let subclasses override the behavior.
+     */
+    @SuppressWarnings("WeakerAccess")
+    protected void performApplicationExit()
+    {
         System.exit(0);
     }
 
@@ -1247,23 +1264,7 @@ public class PDFDebugger extends JFrame
      */
     private void exitForm(WindowEvent evt)
     {
-        if( document != null )
-        {
-            try
-            {
-                document.close();
-                if (!currentFilePath.startsWith("http"))
-                {
-                    recentFiles.addFile(currentFilePath);
-                }
-                recentFiles.close();
-            }
-            catch( IOException e )
-            {
-                throw new RuntimeException(e);
-            }
-        }
-        System.exit(0);
+        exitMenuItemActionPerformed(null);
     }
     
     private void readPDFFile(String filePath, String password) throws IOException
@@ -1494,26 +1495,4 @@ public class PDFDebugger extends JFrame
         }
         return null;
     }
-    
-    private static boolean isMinJdk9()
-    {
-        // strategy from lucene-solr/lucene/core/src/java/org/apache/lucene/util/Constants.java
-        String version = System.getProperty("java.specification.version");
-        final StringTokenizer st = new StringTokenizer(version, ".");
-        try
-        {
-            int major = Integer.parseInt(st.nextToken());
-            int minor = 0;
-            if (st.hasMoreTokens())
-            {
-                minor = Integer.parseInt(st.nextToken());
-            }
-            return major > 1 || (major == 1 && minor >= 9);
-        }
-        catch (NumberFormatException nfe)
-        {
-            // maybe some new numbering scheme in the 22nd century
-            return true;
-        }
-    }
 }

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/OSXAdapter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/OSXAdapter.java?rev=1836123&r1=1836122&r2=1836123&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/OSXAdapter.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/OSXAdapter.java Tue Jul 17 18:00:40 2018
@@ -18,7 +18,7 @@
 
 /* 
  * This file includes code under the following terms:
- * 			
+ *
  *  Version: 2.0
  *  
  *  Disclaimer: IMPORTANT:  This Apple software is supplied to you by 
@@ -64,10 +64,14 @@
 
 package org.apache.pdfbox.debugger.ui;
 
+import java.awt.Desktop;
+import java.io.File;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.util.StringTokenizer;
+import java.util.List;
 
 /**
  * Hooks existing preferences/about/quit functionality from an
@@ -87,10 +91,67 @@ public class OSXAdapter implements Invoc
     protected String proxySignature;
 
     static Object macOSXApplication;
+    
+    private static boolean isMinJdk9()
+    {
+        // strategy from lucene-solr/lucene/core/src/java/org/apache/lucene/util/Constants.java
+        String version = System.getProperty("java.specification.version");
+        final StringTokenizer st = new StringTokenizer(version, ".");
+        try
+        {
+            int major = Integer.parseInt(st.nextToken());
+            int minor = 0;
+            if (st.hasMoreTokens())
+            {
+                minor = Integer.parseInt(st.nextToken());
+            }
+            return major > 1 || (major == 1 && minor >= 9);
+        }
+        catch (NumberFormatException nfe)
+        {
+            // maybe some new numbering scheme in the 22nd century
+            return true;
+        }
+    }
 
     // Pass this method an Object and Method equipped to perform application shutdown logic
     // The method passed should return a boolean stating whether or not the quit should occur
-    public static void setQuitHandler(Object target, Method quitHandler) {
+    public static void setQuitHandler(final Object target, final Method quitHandler)
+    {
+        if (isMinJdk9())
+        {
+            try
+            {
+                Desktop desktopObject = Desktop.getDesktop();
+                Class<?> filesHandlerClass = Class.forName("java.awt.desktop.QuitHandler");
+                final Method setQuitHandlerMethod = desktopObject.getClass().getMethod("setQuitHandler", filesHandlerClass);
+                Object osxAdapterProxy = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(),
+                        new Class[]
+                        {
+                            filesHandlerClass
+                        }, new InvocationHandler()
+                {
+                    @Override
+                    public Object invoke(Object proxy, Method method, Object[] args)
+                            throws Throwable
+                    {
+                        if (!method.getName().equals("handleQuitRequestWith"))
+                        {
+                            return null;
+                        }
+                        // We just call our own quit handler
+                        quitHandler.invoke(target);
+                        return null;
+                    }
+                });
+                setQuitHandlerMethod.invoke(desktopObject, osxAdapterProxy);
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+            return;
+        }
         setHandler(new OSXAdapter("handleQuit", target, quitHandler));
     }
 
@@ -133,7 +194,53 @@ public class OSXAdapter implements Invoc
     // Pass this method an Object and a Method equipped to handle document events from the Finder
     // Documents are registered with the Finder via the CFBundleDocumentTypes dictionary in the 
     // application bundle's Info.plist
-    public static void setFileHandler(Object target, Method fileHandler) {
+    public static void setFileHandler(Object target, Method fileHandler)
+    {
+        if (isMinJdk9())
+        {
+            try
+            {
+                Desktop desktopObject = Desktop.getDesktop();
+                Class<?> filesHandlerClass = Class.forName("java.awt.desktop.OpenFilesHandler");
+                Method setOpenFileHandlerMethod = desktopObject.getClass().getMethod("setOpenFileHandler", filesHandlerClass);
+                Object osxAdapterProxy = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(),
+                        new Class[]
+                        {
+                            filesHandlerClass
+                        }, new OSXAdapter("openFiles", target, fileHandler)
+                {
+                    // Override OSXAdapter.callTarget to send information on the
+                    // file to be opened
+                    public boolean callTarget(Object openFilesEvent)
+                    {
+                        if (openFilesEvent != null)
+                        {
+                            try
+                            {
+                                Method getFilesMethod = openFilesEvent.getClass().getDeclaredMethod("getFiles",
+                                        (Class[]) null);
+                                @SuppressWarnings("unchecked")
+                                List<File> files = (List<File>) getFilesMethod.invoke(openFilesEvent,
+                                        (Object[]) null);
+                                this.targetMethod.invoke(this.targetObject, files.get(0).getAbsolutePath());
+                            }
+                            catch (Exception ex)
+                            {
+                                throw new RuntimeException(ex);
+                            }
+                        }
+                        return true;
+                    }
+                });
+                setOpenFileHandlerMethod.invoke(desktopObject, osxAdapterProxy);
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+            return;
+        }
+        /* JDK <= 1.8, using Apple classes */
         setHandler(new OSXAdapter("handleOpenFile", target, fileHandler) {
             // Override OSXAdapter.callTarget to send information on the
             // file to be opened