JavaFXActionManager.java
01 /*
02  * Copyright 2008-2014 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 package org.codehaus.griffon.runtime.javafx.controller;
17 
18 import griffon.core.GriffonApplication;
19 import griffon.core.artifact.GriffonController;
20 import griffon.core.controller.Action;
21 import org.codehaus.griffon.runtime.core.controller.AbstractActionManager;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 
25 import javax.annotation.Nonnull;
26 import javax.inject.Inject;
27 
28 import static griffon.util.GriffonApplicationUtils.isMacOSX;
29 import static griffon.util.GriffonNameUtils.isBlank;
30 import static griffon.util.TypeUtils.castToBoolean;
31 
32 /**
33  @author Andres Almiray
34  @since 2.0.0
35  */
36 public class JavaFXActionManager extends AbstractActionManager {
37     private static final Logger LOG = LoggerFactory.getLogger(JavaFXActionManager.class);
38 
39     @Inject
40     public JavaFXActionManager(@Nonnull GriffonApplication application) {
41         super(application);
42     }
43 
44     @Nonnull
45     @Override
46     protected Action createControllerAction(@Nonnull GriffonController controller, @Nonnull String actionName) {
47         return new JavaFXGriffonControllerAction(getUiThreadManager(), this, controller, actionName);
48     }
49 
50     @Override
51     protected void doConfigureAction(@Nonnull Action action, @Nonnull GriffonController controller, @Nonnull String normalizeNamed, @Nonnull String keyPrefix) {
52         JavaFXGriffonControllerAction javafxAction = (JavaFXGriffonControllerActionaction;
53 
54         String rsAccelerator = msg(keyPrefix, normalizeNamed, "accelerator""");
55         if (!isBlank(rsAccelerator)) {
56             //noinspection ConstantConditions
57             if (!isMacOSX() && rsAccelerator.contains("meta"&& !rsAccelerator.contains("ctrl")) {
58                 rsAccelerator = rsAccelerator.replace("meta""ctrl");
59             }
60             if (LOG.isTraceEnabled()) {
61                 LOG.trace(keyPrefix + normalizeNamed + ".accelerator = " + rsAccelerator);
62             }
63             javafxAction.setAccelerator(rsAccelerator);
64         }
65 
66         String rsDescription = msg(keyPrefix, normalizeNamed, "description""");
67         if (!isBlank(rsDescription)) {
68             if (LOG.isTraceEnabled()) {
69                 LOG.trace(keyPrefix + normalizeNamed + "._description = " + rsDescription);
70             }
71             javafxAction.setDescription(rsDescription);
72         }
73 
74         String rsIcon = msg(keyPrefix, normalizeNamed, "icon""");
75         if (!isBlank(rsIcon)) {
76             if (LOG.isTraceEnabled()) {
77                 LOG.trace(keyPrefix + normalizeNamed + ".icon = " + rsIcon);
78             }
79             javafxAction.setIcon(rsIcon);
80         }
81 
82         String rsEnabled = msg(keyPrefix, normalizeNamed, "enabled""true");
83         if (!isBlank(rsEnabled)) {
84             if (LOG.isTraceEnabled()) {
85                 LOG.trace(keyPrefix + normalizeNamed + ".enabled = " + rsEnabled);
86             }
87             javafxAction.setEnabled(castToBoolean(rsEnabled));
88         }
89 
90         String rsSelected = msg(keyPrefix, normalizeNamed, "selected""false");
91         if (!isBlank(rsSelected)) {
92             if (LOG.isTraceEnabled()) {
93                 LOG.trace(keyPrefix + normalizeNamed + ".selected = " + rsSelected);
94             }
95             javafxAction.setSelected(castToBoolean(rsSelected));
96         }
97     }
98 }