DefaultMacOSXPlatformHandler.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core;
19 
20 import com.apple.mrj.MRJAboutHandler;
21 import com.apple.mrj.MRJApplicationUtils;
22 import com.apple.mrj.MRJPrefsHandler;
23 import com.apple.mrj.MRJQuitHandler;
24 import griffon.core.GriffonApplication;
25 
26 import javax.annotation.Nonnull;
27 
28 import static griffon.util.GriffonNameUtils.capitalize;
29 import static java.util.Arrays.asList;
30 
31 /**
32  * Handles Linux integration.
33  *
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public class DefaultMacOSXPlatformHandler extends DefaultPlatformHandler {
38     @Override
39     public void handle(@Nonnull GriffonApplication application) {
40         super.handle(application);
41 
42         // use unified menu bar
43         System.setProperty("apple.laf.useScreenMenuBar""true");
44 
45         // set menu bar title
46         String title = application.getConfiguration().getAsString("application.title""Griffon");
47         System.setProperty("com.apple.mrj.application.apple.menu.about.name", capitalize(title));
48 
49 
50         boolean skipAbout = application.getConfiguration().getAsBoolean("osx.noabout"false);
51         boolean skipPrefs = application.getConfiguration().getAsBoolean("osx.noprefs"false);
52         boolean skipQuit = application.getConfiguration().getAsBoolean("osx.noquit"false);
53 
54         GriffonMacOSXSupport handler = new GriffonMacOSXSupport(application, skipQuit);
55         if (!skipAboutMRJApplicationUtils.registerAboutHandler(handler);
56         if (!skipPrefsMRJApplicationUtils.registerPrefsHandler(handler);
57         MRJApplicationUtils.registerQuitHandler(handler);
58     }
59 
60     private static class GriffonMacOSXSupport implements MRJAboutHandler, MRJQuitHandler, MRJPrefsHandler {
61         private final GriffonApplication application;
62         private final boolean noquit;
63 
64         private GriffonMacOSXSupport(@Nonnull GriffonApplication application, boolean noquit) {
65             this.application = application;
66             this.noquit = noquit;
67         }
68 
69         @Override
70         public void handleAbout() {
71             application.getEventRouter().publishEvent("OSXAbout", asList(application));
72         }
73 
74         @Override
75         public void handlePrefs() throws IllegalStateException {
76             application.getEventRouter().publishEvent("OSXPrefs", asList(application));
77         }
78 
79         @Override
80         public void handleQuit() {
81             if (noquit) {
82                 application.getEventRouter().publishEvent("OSXQuit", asList(application));
83             else {
84                 application.shutdown();
85             }
86         }
87     }
88 }