DefaultMacOSXPlatformHandler.java
01 /*
02  * Copyright 2009-2013 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.codehaus.griffon.runtime.util;
18 
19 import griffon.core.GriffonApplication;
20 import griffon.util.ApplicationClassLoader;
21 import griffon.util.PlatformHandler;
22 import groovy.lang.Binding;
23 import groovy.lang.GroovyShell;
24 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
25 
26 import java.net.URL;
27 
28 import static griffon.util.ConfigUtils.getConfigValueAsBoolean;
29 import static griffon.util.ConfigUtils.getConfigValueAsString;
30 import static griffon.util.GriffonNameUtils.capitalize;
31 
32 /**
33  * Handles OSX' menubar, about, quit and preferences menus.
34  *
35  @author Andres Almiray
36  @since 0.9.3
37  */
38 public class DefaultMacOSXPlatformHandler implements PlatformHandler {
39     private static Object macOSXHandler = null;
40 
41     public void handle(GriffonApplication app) {
42         // use unified menu bar
43         System.setProperty("apple.laf.useScreenMenuBar""true");
44 
45         // set menu bar title
46         String title = getConfigValueAsString(app.getConfig()"application.title""Griffon");
47         System.setProperty("com.apple.mrj.application.apple.menu.about.name", capitalize(title));
48         // we may want to have a more specific option like application.shortTitle, that is used first
49 
50         // exit handler
51         if (macOSXHandler == null) {
52             try {
53                 Binding bindings = new Binding();
54                 bindings.setVariable("app", app);
55                 bindings.setVariable("skipAbout", getConfigValueAsBoolean(app.getConfig()"osx.noabout"false));
56                 bindings.setVariable("skipPrefs", getConfigValueAsBoolean(app.getConfig()"osx.noprefs"false));
57                 bindings.setVariable("skipQuit", getConfigValueAsBoolean(app.getConfig()"osx.noquit"false));
58 
59                 GroovyShell shell = new GroovyShell(ApplicationClassLoader.get(), bindings);
60                 String resourceName = "META-INF/" + DefaultMacOSXPlatformHandler.class.getName() ".txt";
61                 URL scriptUrl = ApplicationClassLoader.get().getResource(resourceName);
62                 macOSXHandler = shell.evaluate(DefaultGroovyMethods.getText(scriptUrl));
63             catch (Throwable t) {
64                 t.printStackTrace(System.out);
65             }
66         }
67     }
68 }