LanternaActionManager.java
01 /*
02  * Copyright 2008-2017 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.lanterna.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 import java.beans.PropertyChangeEvent;
28 import java.beans.PropertyChangeListener;
29 
30 import static griffon.util.GriffonNameUtils.getNaturalName;
31 import static griffon.util.GriffonNameUtils.isBlank;
32 
33 /**
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public class LanternaActionManager extends AbstractActionManager {
38     private static final Logger LOG = LoggerFactory.getLogger(LanternaActionManager.class);
39 
40     private static final String DOT = ".";
41     private static final String EQUALS = " = ";
42     private static final String KEY_NAME = "name";
43 
44     @Inject
45     public LanternaActionManager(@Nonnull GriffonApplication application) {
46         super(application);
47     }
48 
49     @Nonnull
50     @Override
51     protected Action createControllerAction(@Nonnull GriffonController controller, @Nonnull String actionName) {
52         return new LanternaGriffonControllerAction(getUiThreadManager(), this, controller, actionName);
53     }
54 
55     @Override
56     protected void doConfigureAction(@Nonnull final Action action, @Nonnull final GriffonController controller, @Nonnull final String normalizeNamed, @Nonnull final String keyPrefix) {
57         controller.getApplication().addPropertyChangeListener(GriffonApplication.PROPERTY_LOCALE, new PropertyChangeListener() {
58             @Override
59             public void propertyChange(PropertyChangeEvent evt) {
60                 configureAction((LanternaGriffonControllerActionaction, controller, normalizeNamed, keyPrefix);
61             }
62         });
63         configureAction((LanternaGriffonControllerActionaction, controller, normalizeNamed, keyPrefix);
64     }
65 
66     protected void configureAction(@Nonnull LanternaGriffonControllerAction action, @Nonnull GriffonController controller, @Nonnull String normalizeNamed, @Nonnull String keyPrefix) {
67         resolveName(action, controller, normalizeNamed, keyPrefix);
68     }
69 
70     protected void resolveName(@Nonnull LanternaGriffonControllerAction action, @Nonnull GriffonController controller, @Nonnull String normalizeNamed, @Nonnull String keyPrefix) {
71         String rsActionName = msg(keyPrefix, normalizeNamed, KEY_NAME, getNaturalName(normalizeNamed));
72         if (!isBlank(rsActionName)) {
73             trace(keyPrefix + normalizeNamed, KEY_NAME, rsActionName);
74             action.setName(rsActionName);
75         }
76     }
77 
78     protected void trace(@Nonnull String actionKey, @Nonnull String key, @Nonnull String value) {
79         if (LOG.isTraceEnabled()) {
80             LOG.trace(actionKey + DOT + key + EQUALS + value);
81         }
82     }
83 }