CoreBuilderCustomizer.java
01 /*
02  * Copyright 2008-2017 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 griffon.builder.core;
17 
18 import griffon.builder.core.factory.MetaComponentFactory;
19 import griffon.core.threading.UIThreadManager;
20 import groovy.lang.Closure;
21 import groovy.util.Factory;
22 import groovy.util.FactoryBuilderSupport;
23 import org.codehaus.griffon.runtime.groovy.view.AbstractBuilderCustomizer;
24 import org.codehaus.groovy.runtime.MethodClosure;
25 
26 import javax.annotation.Nonnull;
27 import javax.annotation.PostConstruct;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import java.util.Arrays;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 
35 /**
36  @author Andres Almiray
37  */
38 @Named("core")
39 public class CoreBuilderCustomizer extends AbstractBuilderCustomizer {
40     private static final String KEY_ROOT_NODE_NAME = "ROOT_NODE_NAME";
41     @Inject
42     private UIThreadManager uiThreadManager;
43 
44     public CoreBuilderCustomizer() {
45         Map<String, Factory> factories = new LinkedHashMap<>();
46         factories.put("metaComponent"new MetaComponentFactory());
47         setFactories(factories);
48     }
49 
50     @PostConstruct
51     private void setup() {
52         Map<String, Closure> methods = new LinkedHashMap<>();
53         methods.put("runInsideUISync"new MethodClosure(uiThreadManager, "runInsideUISync"));
54         methods.put("runInsideUIAsync"new MethodClosure(uiThreadManager, "runInsideUIAsync"));
55         methods.put("runOutsideUI"new MethodClosure(uiThreadManager, "runOutsideUI"));
56         methods.put("runFuture"new MethodClosure(uiThreadManager, "runFuture"));
57         methods.put("isUIThread"new MethodClosure(uiThreadManager, "isUIThread"));
58         setMethods(methods);
59     }
60 
61     @Nonnull
62     @Override
63     public List<Closure> getPreInstantiateDelegates() {
64         return Arrays.<Closure>asList(new MethodClosure(this, "rootNodePreInstantiateDelegate"));
65     }
66 
67     @Nonnull
68     @Override
69     public List<Closure> getPostNodeCompletionDelegates() {
70         return Arrays.<Closure>asList(new MethodClosure(this, "rootNodePostNodeCompletionDelegate"));
71     }
72 
73     protected void rootNodePreInstantiateDelegate(FactoryBuilderSupport builder, Map attributes, Object value) {
74         String name = String.valueOf(builder.getContext().get(FactoryBuilderSupport.CURRENT_NAME));
75         if (!builder.hasVariable(KEY_ROOT_NODE_NAME)) {
76             builder.setVariable(KEY_ROOT_NODE_NAME, name);
77         }
78     }
79 
80     protected void rootNodePostNodeCompletionDelegate(FactoryBuilderSupport builder, Object parent, Object node) {
81         String name = String.valueOf(builder.getContext().get(FactoryBuilderSupport.CURRENT_NAME));
82         if (builder.getVariable(KEY_ROOT_NODE_NAME).equals(name&& builder.hasVariable("mvcId")) {
83             String mvcId = String.valueOf(builder.getVariable("mvcId"));
84             builder.getVariables().put(mvcId + "-rootNode", node);
85         }
86     }
87 }