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.groovy;
17
18 import griffon.core.GriffonApplication;
19 import griffon.core.artifact.GriffonController;
20 import griffon.core.controller.Action;
21 import griffon.core.controller.ActionManager;
22 import griffon.core.mvc.MVCGroup;
23 import griffon.core.mvc.MVCGroupConfiguration;
24 import groovy.util.FactoryBuilderSupport;
25 import org.codehaus.griffon.runtime.core.addon.AbstractGriffonAddon;
26
27 import javax.annotation.Nonnull;
28 import javax.inject.Named;
29 import java.util.Map;
30
31 /**
32 * @author Andres Almiray
33 * @since 2.0.0
34 */
35 @Named("groovy")
36 public class GroovyAddon extends AbstractGriffonAddon {
37 private GriffonApplication application;
38
39 public void init(@Nonnull GriffonApplication application) {
40 this.application = application;
41 }
42
43 public void onInitializeMVCGroup(@Nonnull MVCGroupConfiguration configuration, @Nonnull MVCGroup group) {
44 GriffonController controller = group.getController();
45 if (controller == null) return;
46 FactoryBuilderSupport builder = (FactoryBuilderSupport) group.getMember("builder");
47 if (builder == null) return;
48 Map<String, Action> actions = application.getActionManager().actionsFor(controller);
49 for (Map.Entry<String, Action> action : actions.entrySet()) {
50 String actionKey = application.getActionManager().normalizeName(action.getKey()) + ActionManager.ACTION;
51 getLog().trace("Adding action {} to {}:{}:builder", actionKey, configuration.getMvcType(), group.getMvcId());
52 builder.setVariable(actionKey, action.getValue().getToolkitAction());
53 }
54 }
55 }
|