CoreBuilderCustomizer.java
001 /*
002  * Copyright 2008-2017 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.builder.core;
017 
018 import griffon.builder.core.factory.MetaComponentFactory;
019 import griffon.core.threading.UIThreadManager;
020 import groovy.lang.Closure;
021 import groovy.util.Factory;
022 import groovy.util.FactoryBuilderSupport;
023 import org.codehaus.griffon.runtime.groovy.view.AbstractBuilderCustomizer;
024 import org.codehaus.groovy.runtime.MethodClosure;
025 
026 import javax.annotation.Nonnull;
027 import javax.annotation.PostConstruct;
028 import javax.inject.Inject;
029 import javax.inject.Named;
030 import java.util.Arrays;
031 import java.util.LinkedHashMap;
032 import java.util.List;
033 import java.util.Map;
034 
035 import static griffon.util.TypeUtils.castToBoolean;
036 
037 /**
038  @author Andres Almiray
039  */
040 @Named("core")
041 public class CoreBuilderCustomizer extends AbstractBuilderCustomizer {
042     private static final String KEY_ROOT_NODE_NAME = "ROOT_NODE_NAME";
043     private static final String MVC_ID = "mvcId";
044     private static final String ROOT_NODE_IDENTIFIED_BY_ATTRIBUTE = "rootNodeIdentifiedByAttribute";
045 
046     @Inject
047     private UIThreadManager uiThreadManager;
048 
049     public CoreBuilderCustomizer() {
050         Map<String, Factory> factories = new LinkedHashMap<>();
051         factories.put("metaComponent"new MetaComponentFactory());
052         setFactories(factories);
053     }
054 
055     @PostConstruct
056     private void setup() {
057         Map<String, Closure> methods = new LinkedHashMap<>();
058         methods.put("runInsideUISync"new MethodClosure(uiThreadManager, "runInsideUISync"));
059         methods.put("runInsideUIAsync"new MethodClosure(uiThreadManager, "runInsideUIAsync"));
060         methods.put("runOutsideUI"new MethodClosure(uiThreadManager, "runOutsideUI"));
061         methods.put("runOutsideUIAsync"new MethodClosure(uiThreadManager, "runOutsideUIAsync"));
062         methods.put("runFuture"new MethodClosure(uiThreadManager, "runFuture"));
063         methods.put("isUIThread"new MethodClosure(uiThreadManager, "isUIThread"));
064         setMethods(methods);
065     }
066 
067     @Nonnull
068     @Override
069     public List<Closure> getAttributeDelegates() {
070         return Arrays.<Closure>asList(new MethodClosure(this, "rootAttributeDelegate"));
071     }
072 
073     @Nonnull
074     @Override
075     public List<Closure> getPreInstantiateDelegates() {
076         return Arrays.<Closure>asList(new MethodClosure(this, "rootNodePreInstantiateDelegate"));
077     }
078 
079     @Nonnull
080     @Override
081     public List<Closure> getPostNodeCompletionDelegates() {
082         return Arrays.<Closure>asList(new MethodClosure(this, "rootNodePostNodeCompletionDelegate"));
083     }
084 
085     protected void rootAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes) {
086         Object isRootNode = attributes.remove("rootNode");
087         if (isRootNode != null && castToBoolean(isRootNode)) {
088             String mvcId = String.valueOf(builder.getVariable(MVC_ID));
089             builder.getVariables().put(ROOT_NODE_IDENTIFIED_BY_ATTRIBUTE, true);
090             builder.getVariables().put(mvcId + "-rootNode", node);
091         }
092     }
093 
094     protected void rootNodePreInstantiateDelegate(FactoryBuilderSupport builder, Map attributes, Object value) {
095         String name = String.valueOf(builder.getContext().get(FactoryBuilderSupport.CURRENT_NAME));
096         if (!builder.hasVariable(KEY_ROOT_NODE_NAME)) {
097             builder.setVariable(KEY_ROOT_NODE_NAME, name);
098         }
099     }
100 
101     protected void rootNodePostNodeCompletionDelegate(FactoryBuilderSupport builder, Object parent, Object node) {
102         Object hasRootNode = builder.getVariables().get(ROOT_NODE_IDENTIFIED_BY_ATTRIBUTE);
103         if (hasRootNode != null && castToBoolean(hasRootNode)) {
104             return;
105         }
106 
107         String name = String.valueOf(builder.getContext().get(FactoryBuilderSupport.CURRENT_NAME));
108         if (builder.getVariable(KEY_ROOT_NODE_NAME).equals(name&& builder.hasVariable(MVC_ID)) {
109             String mvcId = String.valueOf(builder.getVariable(MVC_ID));
110             builder.getVariables().put(mvcId + "-rootNode", node);
111         }
112     }
113 }