DefaultMacOSXPlatformHandler.java
01 /*
02  * Copyright 2009-2012 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.PlatformHandler;
21 import groovy.lang.Binding;
22 import groovy.lang.GroovyShell;
23 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
24 
25 import java.net.URL;
26 
27 import static griffon.util.ConfigUtils.getConfigValueAsBoolean;
28 import static griffon.util.ConfigUtils.getConfigValueAsString;
29 import static griffon.util.GriffonNameUtils.capitalize;
30 
31 /**
32  * Handles OSX' menubar, about, quit and preferences menus.
33  *
34  @author Andres Almiray
35  @since 0.9.3
36  */
37 public class DefaultMacOSXPlatformHandler implements PlatformHandler {
38     private static Object macOSXHandler = null;
39 
40     public void handle(GriffonApplication app) {
41         // use unified menu bar
42         System.setProperty("apple.laf.useScreenMenuBar""true");
43 
44         // set menu bar title
45         String title = getConfigValueAsString(app.getConfig()"application.title""Griffon");
46         System.setProperty("com.apple.mrj.application.apple.menu.about.name", capitalize(title));
47         // we may want to have a more specific option like application.shortTitle, that is used first
48 
49         // exit handler
50         if (macOSXHandler == null) {
51             try {
52                 Binding bindings = new Binding();
53                 bindings.setVariable("app", app);
54                 bindings.setVariable("skipAbout", getConfigValueAsBoolean(app.getConfig()"osx.noabout"false));
55                 bindings.setVariable("skipPrefs", getConfigValueAsBoolean(app.getConfig()"osx.noprefs"false));
56                 bindings.setVariable("skipQuit", getConfigValueAsBoolean(app.getConfig()"osx.noquit"false));
57 
58                 GroovyShell shell = new GroovyShell(DefaultMacOSXPlatformHandler.class.getClassLoader(), bindings);
59                 String resourceName = "META-INF/" + DefaultMacOSXPlatformHandler.class.getName() ".txt";
60                 URL scriptUrl = Thread.currentThread().getContextClassLoader().getResource(resourceName);
61                 macOSXHandler = shell.evaluate(DefaultGroovyMethods.getText(scriptUrl));
62             catch (Throwable t) {
63                 t.printStackTrace(System.out);
64             }
65         }
66     }
67 }