| 
01 /*02  * Copyright 2008-2015 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.pivot.controller;
 17
 18 import griffon.core.GriffonApplication;
 19 import griffon.core.artifact.GriffonController;
 20 import griffon.core.controller.Action;
 21 import griffon.exceptions.InstanceMethodInvocationException;
 22 import org.apache.pivot.wtk.Component;
 23 import org.codehaus.griffon.runtime.core.controller.AbstractActionManager;
 24 import org.slf4j.Logger;
 25 import org.slf4j.LoggerFactory;
 26
 27 import javax.annotation.Nonnull;
 28 import javax.inject.Inject;
 29
 30 import static griffon.util.GriffonClassUtils.EMPTY_ARGS;
 31 import static griffon.util.GriffonClassUtils.invokeExactInstanceMethod;
 32 import static griffon.util.GriffonNameUtils.isBlank;
 33 import static griffon.util.TypeUtils.castToBoolean;
 34
 35 /**
 36  * @author Andres Almiray
 37  * @since 2.0.0
 38  */
 39 public class PivotActionManager extends AbstractActionManager {
 40     private static final Logger LOG = LoggerFactory.getLogger(PivotActionManager.class);
 41
 42     @Inject
 43     public PivotActionManager(@Nonnull GriffonApplication application) {
 44         super(application);
 45     }
 46
 47     @Nonnull
 48     @Override
 49     protected Action createControllerAction(@Nonnull GriffonController controller, @Nonnull String actionName) {
 50         return new PivotGriffonControllerAction(getUiThreadManager(), this, controller, actionName);
 51     }
 52
 53     @Override
 54     protected void doConfigureAction(@Nonnull Action action, @Nonnull GriffonController controller, @Nonnull String normalizeNamed, @Nonnull String keyPrefix) {
 55         PivotGriffonControllerAction swingAction = (PivotGriffonControllerAction) action;
 56
 57         String rsDescription = msg(keyPrefix, normalizeNamed, "description", "");
 58         if (!isBlank(rsDescription)) {
 59             if (LOG.isTraceEnabled()) {
 60                 LOG.trace(keyPrefix + normalizeNamed + ".description = " + rsDescription);
 61             }
 62             swingAction.setDescription(rsDescription);
 63         }
 64
 65         String rsEnabled = msg(keyPrefix, normalizeNamed, "enabled", "true");
 66         if (!isBlank(rsEnabled)) {
 67             if (LOG.isTraceEnabled()) {
 68                 LOG.trace(keyPrefix + normalizeNamed + ".enabled = " + rsEnabled);
 69             }
 70             swingAction.setEnabled(castToBoolean(rsEnabled));
 71         }
 72     }
 73
 74     @Override
 75     protected void doInvokeAction(@Nonnull GriffonController controller, @Nonnull String actionName, @Nonnull Object[] updatedArgs) {
 76         try {
 77             invokeExactInstanceMethod(controller, actionName, updatedArgs);
 78         } catch (InstanceMethodInvocationException imie) {
 79             if (imie.getCause() instanceof NoSuchMethodException) {
 80                 // try again but this time remove the 1st arg if it's
 81                 // descendant of org.apache.pivot.wtk.Component
 82                 if (updatedArgs.length == 1 && updatedArgs[0] != null && Component.class.isAssignableFrom(updatedArgs[0].getClass())) {
 83                     invokeExactInstanceMethod(controller, actionName, EMPTY_ARGS);
 84                 } else {
 85                     throw imie;
 86                 }
 87             } else {
 88                 throw imie;
 89             }
 90         }
 91     }
 92 }
 |