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