You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/05/20 04:46:42 UTC

[groovy] branch GROOVY_3_0_X updated (72e2868 -> fd0af75)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 72e2868  GROOVY-10097: Better propagation of InterruptedException (additional cases) (port to 3_0_X)
     new e824348  fix GROOVY-10077
     new fd0af75  replace nested class in MacOSXMenuBar with simple @Field annotation

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../main/groovy/groovy/console/ui/Console.groovy   |   5 +
 .../groovy/console/ui/view/MacOSXMenuBar.groovy    | 111 ++++++++++++++++++---
 2 files changed, 102 insertions(+), 14 deletions(-)

[groovy] 01/02: fix GROOVY-10077

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit e82434897690c800c6a684e30198451c872aa715
Author: Harald Fassler <ha...@gmail.com>
AuthorDate: Sun May 9 02:41:46 2021 +0200

    fix GROOVY-10077
    
    Signed-off-by: Harald Fassler <ha...@gmail.com>
---
 .../main/groovy/groovy/console/ui/Console.groovy   |   5 +
 .../groovy/console/ui/view/MacOSXMenuBar.groovy    | 127 ++++++++++++++++++---
 2 files changed, 118 insertions(+), 14 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index 3ccf41f..c15fcb8 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -549,6 +549,11 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener, Fo
     }
 
     void appendStacktrace(text) {
+        // prevent NPE when outputArea is missing, i.e. there is currently no window present
+        // TODO the text should not be swallowed (options: postpone output, open new window, log file, terminal, ...)
+        if (outputArea == null) {
+            return
+        }
         def doc = outputArea.styledDocument
 
         // split lines by new line separator
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
index 58a78a7..0b53b76 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
@@ -20,20 +20,40 @@ package groovy.console.ui.view
 
 import org.codehaus.groovy.vmplugin.VMPluginFactory
 
-def handler = false
-def jdk9plus = VMPluginFactory.getPlugin().getVersion() > 8
-// TODO Desktop handlers are supposed to work cross platform, should we do version check at a higher layer
-// TODO there is also an open files handler, should we also be using that?
-if (!handler) {
-    try {
-        handler = build(jdk9plus ? """
+import java.util.concurrent.atomic.AtomicBoolean
+
+/**
+ * Allow to install menu event handlers only once.<br /><br />
+ * (Add static state, which is not directly possible in script files.)
+ */
+class Static {
+
+    private Static() {}
+
+    private static final AtomicBoolean handlersInstalled = new AtomicBoolean(false)
+
+    /**
+     * Test, if the menu handlers have been already installed.
+     * @return false when this method is called the first time, true on subsequent calls.
+     */
+    static boolean installHandlersOnce() {
+        return !handlersInstalled.get()
+                && handlersInstalled.compareAndSet(false, true)
+    }
+}
+
+// install handlers for JDK9+
+final String JDK9PLUS_SCRIPT = """
 import java.awt.Desktop
 def handler = Desktop.getDesktop()
 handler.setAboutHandler(controller.&showAbout)
 handler.setQuitHandler(controller.&exitDesktop)
 handler.setPreferencesHandler(controller.&preferences)
 handler
-""" : """
+"""
+
+// install handlers for JDK <= 8, if the "macOS Runtime Support for Java" (MRJ) is present
+final String MRJ_SCRIPT = """
 package groovy.console.ui
 
 import com.apple.mrj.*
@@ -64,15 +84,85 @@ MRJApplicationUtils.registerQuitHandler(handler)
 MRJApplicationUtils.registerPrefsHandler(handler)
 
 return handler
-""", new GroovyClassLoader(this.class.classLoader))
-    } catch (Exception se) {
-        // usually an AccessControlException, sometimes applets and JNLP won't let
-        // you access MRJ classes.
-        // However, in any exceptional case back out and use the BasicMenuBar
-        se.printStackTrace()
+"""
+
+// install handlers for JDK <= 8, if the "Apple AWT Extension" (EAWT) is present
+final String EAWT_SCRIPT = """
+package groovy.console.ui
+
+import com.apple.eawt.*
+import com.apple.eawt.AppEvent.*
+
+class ConsoleMacOsSupport implements QuitHandler, AboutHandler, PreferencesHandler {
+
+    def quitHandler
+    def aboutHandler
+    def prefHandler
+
+    @Override
+    public void handleAbout(AboutEvent ev) {
+        aboutHandler()
+    }
+
+    @Override
+    public void handleQuitRequestWith(QuitEvent ev, QuitResponse resp) {
+        // Console.exit() returns false, if the user chose to stay around
+        if (quitHandler()) {
+            resp.performQuit()
+        } else {
+            resp.cancelQuit()
+        }
+    }
+
+    @Override
+    public void handlePreferences(PreferencesEvent ev) {
+        prefHandler()
+    }
+}
+
+def handler = new ConsoleMacOsSupport(quitHandler:controller.&exit, aboutHandler:controller.&showAbout, prefHandler:controller.&preferences)
+def application = Application.getApplication()
+application.setQuitHandler(handler)
+application.setAboutHandler(handler)
+application.setPreferencesHandler(handler)
+
+return handler
+"""
+
+def jdk9plus = VMPluginFactory.getPlugin().getVersion() > 8
+// JDK <= 8 only
+def macOsRuntimeForJavaPresent = classExists('com.apple.mrj.MRJApplicationUtils')
+        && classExists('com.apple.mrj.MRJQuitHandler')
+        && classExists('com.apple.mrj.MRJAboutHandler')
+        && classExists('com.apple.mrj.MRJPrefsHandler')
+// JDK <= 8 only
+def appleAwtExtensionPresent = classExists('com.apple.eawt.Application')
+        && classExists('com.apple.eawt.QuitHandler')
+        && classExists('com.apple.eawt.AboutHandler')
+        && classExists('com.apple.eawt.PreferencesHandler')
+// TODO Desktop handlers are supposed to work cross platform, should we do version check at a higher layer
+// TODO there is also an open files handler, should we also be using that?
+try {
+    // select handler version
+    def scriptSource = jdk9plus ? JDK9PLUS_SCRIPT :
+            macOsRuntimeForJavaPresent ? MRJ_SCRIPT :
+            appleAwtExtensionPresent ? EAWT_SCRIPT :
+            null
+    if (scriptSource) {
+        if (Static.installHandlersOnce()) {
+            build(scriptSource, new GroovyClassLoader(MacOSXMenuBar.class.classLoader))
+        }
+    } else {
         build(BasicMenuBar)
         return
     }
+} catch (Exception se) {
+    // usually an AccessControlException, sometimes applets and JNLP won't let
+    // you access MRJ classes.
+    // However, in any exceptional case back out and use the BasicMenuBar
+    se.printStackTrace()
+    build(BasicMenuBar)
+    return
 }
 
 menuBar {
@@ -149,3 +239,12 @@ menuBar {
         menuItem(inspectTokensAction, icon:null)
     }
 }
+
+static classExists(String className) {
+    try {
+        MacOSXMenuBar.class.classLoader.loadClass(className)
+        true
+    } catch (ClassNotFoundException ignored) {
+        false
+    }
+}

[groovy] 02/02: replace nested class in MacOSXMenuBar with simple @Field annotation

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit fd0af757a0b1cd68e3933aa53871c62eeeb62f4f
Author: Harald Fassler <ha...@gmail.com>
AuthorDate: Sun May 9 18:51:52 2021 +0200

    replace nested class in MacOSXMenuBar with simple @Field annotation
    
    Signed-off-by: Harald Fassler <ha...@gmail.com>
---
 .../groovy/console/ui/view/MacOSXMenuBar.groovy    | 30 +++++-----------------
 1 file changed, 7 insertions(+), 23 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
index 0b53b76..b23dd7c 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
@@ -18,30 +18,9 @@
  */
 package groovy.console.ui.view
 
+import groovy.transform.Field
 import org.codehaus.groovy.vmplugin.VMPluginFactory
 
-import java.util.concurrent.atomic.AtomicBoolean
-
-/**
- * Allow to install menu event handlers only once.<br /><br />
- * (Add static state, which is not directly possible in script files.)
- */
-class Static {
-
-    private Static() {}
-
-    private static final AtomicBoolean handlersInstalled = new AtomicBoolean(false)
-
-    /**
-     * Test, if the menu handlers have been already installed.
-     * @return false when this method is called the first time, true on subsequent calls.
-     */
-    static boolean installHandlersOnce() {
-        return !handlersInstalled.get()
-                && handlersInstalled.compareAndSet(false, true)
-    }
-}
-
 // install handlers for JDK9+
 final String JDK9PLUS_SCRIPT = """
 import java.awt.Desktop
@@ -148,10 +127,15 @@ try {
             macOsRuntimeForJavaPresent ? MRJ_SCRIPT :
             appleAwtExtensionPresent ? EAWT_SCRIPT :
             null
+    @Field
+    static boolean handlersInstalled = false
+
     if (scriptSource) {
-        if (Static.installHandlersOnce()) {
+        if (!handlersInstalled) {
             build(scriptSource, new GroovyClassLoader(MacOSXMenuBar.class.classLoader))
+            handlersInstalled = true
         }
+        // else just skip handler installation and continue with the rest of the script
     } else {
         build(BasicMenuBar)
         return