| 
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.groovy.mvc;
 17
 18 import griffon.core.artifact.GriffonViewClass;
 19 import griffon.core.mvc.MVCGroup;
 20 import griffon.core.mvc.MVCGroupConfiguration;
 21 import griffon.core.mvc.MVCGroupManager;
 22 import groovy.lang.Script;
 23 import groovy.util.FactoryBuilderSupport;
 24 import org.codehaus.griffon.runtime.core.mvc.DefaultMVCGroup;
 25
 26 import javax.annotation.Nonnull;
 27 import javax.annotation.Nullable;
 28 import java.util.LinkedHashMap;
 29 import java.util.Map;
 30
 31 /**
 32  * @author Andres Almiray
 33  */
 34 public class GroovyAwareMVCGroup extends DefaultMVCGroup {
 35     public static final String BUILDER = "builder";
 36     protected final Map<String, Object> scriptResults = new LinkedHashMap<>();
 37
 38     public GroovyAwareMVCGroup(@Nonnull MVCGroupManager mvcGroupManager, @Nonnull MVCGroupConfiguration configuration, @Nullable String mvcId, @Nonnull Map<String, Object> members, @Nullable MVCGroup parentGroup) {
 39         super(mvcGroupManager, configuration, mvcId, members, parentGroup);
 40     }
 41
 42     public FactoryBuilderSupport getBuilder() {
 43         return (FactoryBuilderSupport) getMember(BUILDER);
 44     }
 45
 46     public Object getScriptResult(String name) {
 47         return scriptResults.get(name);
 48     }
 49
 50     public void buildScriptMember(final String name) {
 51         Object member = getMember(name);
 52         if (!(member instanceof Script)) return;
 53         final Script script = (Script) member;
 54
 55         // special case: view gets executed in the UI thread always
 56         if (GriffonViewClass.TYPE.equals(name)) {
 57             getMvcGroupManager().getApplication().getUIThreadManager().runInsideUISync(new Runnable() {
 58                 @Override
 59                 public void run() {
 60                     scriptResults.put(name, getBuilder().build(script));
 61                 }
 62             });
 63         } else {
 64             scriptResults.put(name, getBuilder().build(script));
 65         }
 66     }
 67 }
 |