SwingActionManager.java
001 /*
002  * Copyright 2008-2014 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package org.codehaus.griffon.runtime.swing.controller;
017 
018 import griffon.core.GriffonApplication;
019 import griffon.core.artifact.GriffonController;
020 import griffon.core.controller.Action;
021 import org.codehaus.griffon.runtime.core.controller.AbstractActionManager;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 
025 import javax.annotation.Nonnull;
026 import javax.inject.Inject;
027 
028 import static griffon.util.GriffonApplicationUtils.isMacOSX;
029 import static griffon.util.GriffonNameUtils.isBlank;
030 import static griffon.util.TypeUtils.castToBoolean;
031 
032 /**
033  @author Andres Almiray
034  @since 2.0.0
035  */
036 public class SwingActionManager extends AbstractActionManager {
037     private static final Logger LOG = LoggerFactory.getLogger(SwingActionManager.class);
038 
039     @Inject
040     public SwingActionManager(@Nonnull GriffonApplication application) {
041         super(application);
042     }
043 
044     @Override
045     protected void doConfigureAction(@Nonnull Action action, @Nonnull GriffonController controller, @Nonnull String normalizeNamed, @Nonnull String keyPrefix) {
046         SwingGriffonControllerAction swingAction = (SwingGriffonControllerActionaction;
047 
048         String rsAccelerator = msg(keyPrefix, normalizeNamed, "accelerator""");
049         if (!isBlank(rsAccelerator)) {
050             //noinspection ConstantConditions
051             if (!isMacOSX() && rsAccelerator.contains("meta"&& !rsAccelerator.contains("ctrl")) {
052                 rsAccelerator = rsAccelerator.replace("meta""ctrl");
053             }
054             if (LOG.isTraceEnabled()) {
055                 LOG.trace(keyPrefix + normalizeNamed + ".accelerator = " + rsAccelerator);
056             }
057             swingAction.setAccelerator(rsAccelerator);
058         }
059 
060         String rsCommand = msg(keyPrefix, normalizeNamed, "command""");
061         if (!isBlank(rsCommand)) {
062             if (LOG.isTraceEnabled()) {
063                 LOG.trace(keyPrefix + normalizeNamed + ".command = " + rsCommand);
064             }
065             swingAction.setCommand(rsCommand);
066         }
067 
068         String rsShortDescription = msg(keyPrefix, normalizeNamed, "short_description""");
069         if (!isBlank(rsShortDescription)) {
070             if (LOG.isTraceEnabled()) {
071                 LOG.trace(keyPrefix + normalizeNamed + ".short_description = " + rsShortDescription);
072             }
073             swingAction.setShortDescription(rsShortDescription);
074         }
075 
076         String rsLongDescription = msg(keyPrefix, normalizeNamed, "long_description""");
077         if (!isBlank(rsLongDescription)) {
078             if (LOG.isTraceEnabled()) {
079                 LOG.trace(keyPrefix + normalizeNamed + ".long_description = " + rsLongDescription);
080             }
081             swingAction.setLongDescription(rsLongDescription);
082         }
083 
084         String rsMnemonic = msg(keyPrefix, normalizeNamed, "mnemonic""");
085         if (!isBlank(rsMnemonic)) {
086             if (LOG.isTraceEnabled()) {
087                 LOG.trace(keyPrefix + normalizeNamed + ".mnemonic = " + rsMnemonic);
088             }
089             swingAction.setMnemonic(rsMnemonic);
090         }
091 
092         String rsSmallIcon = msg(keyPrefix, normalizeNamed, "small_icon""");
093         if (!isBlank(rsSmallIcon)) {
094             if (LOG.isTraceEnabled()) {
095                 LOG.trace(keyPrefix + normalizeNamed + ".small_icon = " + rsSmallIcon);
096             }
097             swingAction.setSmallIcon(rsSmallIcon);
098         }
099 
100         String rsLargeIcon = msg(keyPrefix, normalizeNamed, "large_icon""");
101         if (!isBlank(rsLargeIcon)) {
102             if (LOG.isTraceEnabled()) {
103                 LOG.trace(keyPrefix + normalizeNamed + ".large_icon = " + rsLargeIcon);
104             }
105             swingAction.setLargeIcon(rsLargeIcon);
106         }
107 
108         String rsEnabled = msg(keyPrefix, normalizeNamed, "enabled""true");
109         if (!isBlank(rsEnabled)) {
110             if (LOG.isTraceEnabled()) {
111                 LOG.trace(keyPrefix + normalizeNamed + ".enabled = " + rsEnabled);
112             }
113             swingAction.setEnabled(castToBoolean(rsEnabled));
114         }
115 
116         String rsSelected = msg(keyPrefix, normalizeNamed, "selected""false");
117         if (!isBlank(rsSelected)) {
118             if (LOG.isTraceEnabled()) {
119                 LOG.trace(keyPrefix + normalizeNamed + ".selected = " + rsSelected);
120             }
121             swingAction.setSelected(castToBoolean(rsSelected));
122         }
123     }
124 
125     @Nonnull
126     @Override
127     protected Action createControllerAction(@Nonnull GriffonController controller, @Nonnull String actionName) {
128         return new SwingGriffonControllerAction(getUiThreadManager(), this, controller, actionName);
129     }
130 }