"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy" (3 Sep 2021, 8059 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "MacOSXMenuBar.groovy": 3.0.9_vs_4.0.0-rc-1.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.console.ui.view
   20 
   21 import groovy.transform.Field
   22 import org.codehaus.groovy.vmplugin.VMPluginFactory
   23 
   24 // install handlers for JDK9+
   25 final String JDK9PLUS_SCRIPT = """
   26 import java.awt.Desktop
   27 def handler = Desktop.getDesktop()
   28 handler.setAboutHandler(controller.&showAbout)
   29 handler.setQuitHandler(controller.&exitDesktop)
   30 handler.setPreferencesHandler(controller.&preferences)
   31 handler
   32 """
   33 
   34 // install handlers for JDK <= 8, if the "macOS Runtime Support for Java" (MRJ) is present
   35 final String MRJ_SCRIPT = """
   36 package groovy.console.ui
   37 
   38 import com.apple.mrj.*
   39 
   40 class ConsoleMacOsSupport implements MRJQuitHandler, MRJAboutHandler, MRJPrefsHandler {
   41 
   42     def quitHandler
   43     def aboutHandler
   44     def prefHandler
   45 
   46     public void handleAbout() {
   47         aboutHandler()
   48     }
   49 
   50     public void handleQuit() {
   51         quitHandler()
   52     }
   53 
   54 
   55     public void handlePrefs() throws IllegalStateException {
   56         prefHandler()
   57     }
   58 }
   59 
   60 def handler = new ConsoleMacOsSupport(quitHandler:controller.&exit, aboutHandler:controller.&showAbout, prefHandler:controller.&preferences)
   61 MRJApplicationUtils.registerAboutHandler(handler)
   62 MRJApplicationUtils.registerQuitHandler(handler)
   63 MRJApplicationUtils.registerPrefsHandler(handler)
   64 
   65 return handler
   66 """
   67 
   68 // install handlers for JDK <= 8, if the "Apple AWT Extension" (EAWT) is present
   69 final String EAWT_SCRIPT = """
   70 package groovy.console.ui
   71 
   72 import com.apple.eawt.*
   73 import com.apple.eawt.AppEvent.*
   74 
   75 class ConsoleMacOsSupport implements QuitHandler, AboutHandler, PreferencesHandler {
   76 
   77     def quitHandler
   78     def aboutHandler
   79     def prefHandler
   80 
   81     @Override
   82     public void handleAbout(AboutEvent ev) {
   83         aboutHandler()
   84     }
   85 
   86     @Override
   87     public void handleQuitRequestWith(QuitEvent ev, QuitResponse resp) {
   88         // Console.exit() returns false, if the user chose to stay around
   89         if (quitHandler()) {
   90             resp.performQuit()
   91         } else {
   92             resp.cancelQuit()
   93         }
   94     }
   95 
   96     @Override
   97     public void handlePreferences(PreferencesEvent ev) {
   98         prefHandler()
   99     }
  100 }
  101 
  102 def handler = new ConsoleMacOsSupport(quitHandler:controller.&exit, aboutHandler:controller.&showAbout, prefHandler:controller.&preferences)
  103 def application = Application.getApplication()
  104 application.setQuitHandler(handler)
  105 application.setAboutHandler(handler)
  106 application.setPreferencesHandler(handler)
  107 
  108 return handler
  109 """
  110 
  111 def jdk9plus = VMPluginFactory.getPlugin().getVersion() > 8
  112 // JDK <= 8 only
  113 def macOsRuntimeForJavaPresent = classExists('com.apple.mrj.MRJApplicationUtils')
  114         && classExists('com.apple.mrj.MRJQuitHandler')
  115         && classExists('com.apple.mrj.MRJAboutHandler')
  116         && classExists('com.apple.mrj.MRJPrefsHandler')
  117 // JDK <= 8 only
  118 def appleAwtExtensionPresent = classExists('com.apple.eawt.Application')
  119         && classExists('com.apple.eawt.QuitHandler')
  120         && classExists('com.apple.eawt.AboutHandler')
  121         && classExists('com.apple.eawt.PreferencesHandler')
  122 // TODO Desktop handlers are supposed to work cross platform, should we do version check at a higher layer
  123 // TODO there is also an open files handler, should we also be using that?
  124 try {
  125     // select handler version
  126     def scriptSource = jdk9plus ? JDK9PLUS_SCRIPT :
  127             macOsRuntimeForJavaPresent ? MRJ_SCRIPT :
  128             appleAwtExtensionPresent ? EAWT_SCRIPT :
  129             null
  130     @Field
  131     static boolean handlersInstalled = false
  132 
  133     if (scriptSource) {
  134         if (!handlersInstalled) {
  135             build(scriptSource, new GroovyClassLoader(MacOSXMenuBar.class.classLoader))
  136             handlersInstalled = true
  137         }
  138         // else just skip handler installation and continue with the rest of the script
  139     } else {
  140         build(BasicMenuBar)
  141         return
  142     }
  143 } catch (Exception se) {
  144     // usually an AccessControlException, sometimes applets and JNLP won't let
  145     // you access MRJ classes.
  146     // However, in any exceptional case back out and use the BasicMenuBar
  147     se.printStackTrace()
  148     build(BasicMenuBar)
  149     return
  150 }
  151 
  152 menuBar {
  153     menu(text: 'File', mnemonic: 'F') {
  154         menuItem(newFileAction, icon:null)
  155         menuItem(newWindowAction, icon:null)
  156         menuItem(openAction, icon:null)
  157         separator()
  158         menuItem(saveAction, icon:null)
  159         menuItem(saveAsAction, icon:null)
  160         separator()
  161         menuItem(printAction, icon:null)
  162     }
  163 
  164     menu(text: 'Edit', mnemonic: 'E') {
  165         menuItem(undoAction, icon:null)
  166         menuItem(redoAction, icon:null)
  167         separator()
  168         menuItem(cutAction, icon:null)
  169         menuItem(copyAction, icon:null)
  170         menuItem(pasteAction, icon:null)
  171         separator()
  172         menuItem(findAction, icon:null)
  173         menuItem(findNextAction, icon:null)
  174         menuItem(findPreviousAction, icon:null)
  175         menuItem(replaceAction, icon:null)
  176         separator()
  177         menuItem(selectAllAction, icon:null)
  178     separator()
  179     menuItem(commentAction, icon:null)
  180         menuItem(selectBlockAction, icon:null)
  181     }
  182 
  183     menu(text: 'View', mnemonic: 'V') {
  184         menuItem(clearOutputAction, icon:null)
  185         separator()
  186         menuItem(largerFontAction, icon:null)
  187         menuItem(smallerFontAction, icon:null)
  188         separator()
  189         checkBoxMenuItem(captureStdOutAction, selected: controller.captureStdOut)
  190         checkBoxMenuItem(captureStdErrAction, selected: controller.captureStdErr)
  191         checkBoxMenuItem(fullStackTracesAction, selected: controller.fullStackTraces)
  192         checkBoxMenuItem(showScriptInOutputAction, selected: controller.showScriptInOutput)
  193         checkBoxMenuItem(visualizeScriptResultsAction, selected: controller.visualizeScriptResults)
  194         checkBoxMenuItem(showToolbarAction, selected: controller.showToolbar)
  195         checkBoxMenuItem(detachedOutputAction, selected: controller.detachedOutput)
  196         checkBoxMenuItem(autoClearOutputAction, selected: controller.autoClearOutput)
  197         checkBoxMenuItem(orientationVerticalAction, selected: controller.orientationVertical)
  198     }
  199 
  200     menu(text: 'History', mnemonic: 'I') {
  201         menuItem(historyPrevAction, icon:null)
  202         menuItem(historyNextAction, icon:null)
  203     }
  204 
  205     menu(text: 'Script', mnemonic: 'S') {
  206         menuItem(runAction, icon:null)
  207         checkBoxMenuItem(loopModeAction, selected: controller.loopMode)
  208         checkBoxMenuItem(saveOnRunAction, selected: controller.saveOnRun)
  209         menuItem(runSelectionAction, icon:null)
  210         checkBoxMenuItem(threadInterruptAction, selected: controller.threadInterrupt)
  211         menuItem(interruptAction, icon:null)
  212         menuItem(compileAction, icon:null)
  213         separator()
  214         menuItem(addClasspathJar)
  215         menuItem(addClasspathDir)
  216         menuItem(listClasspath)
  217         menuItem(clearClassloader)
  218         separator()
  219         menuItem(inspectLastAction, icon:null)
  220         menuItem(inspectVariablesAction, icon:null)
  221         menuItem(inspectAstAction, icon:null)
  222         menuItem(inspectCstAction, icon:null)
  223         menuItem(inspectTokensAction, icon:null)
  224     }
  225 }
  226 
  227 static classExists(String className) {
  228     try {
  229         MacOSXMenuBar.class.classLoader.loadClass(className)
  230         true
  231     } catch (ClassNotFoundException ignored) {
  232         false
  233     }
  234 }