MetaComponentFactory.groovy
01 /*
02  * Copyright 2009-2013 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 
17 package org.codehaus.griffon.runtime.builder.factory
18 
19 import griffon.core.MVCGroup
20 import griffon.util.ApplicationHolder
21 
22 /**
23  * Enables MVC groups to be used as component nodes
24  *
25  @author Andres Almiray
26  */
27 class MetaComponentFactory extends AbstractFactory {
28     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
29         String mvcType = ''
30         if (value != null && value instanceof CharSequence) {
31             mvcType = value.toString()
32         else {
33             throw new IllegalArgumentException("In $name value must be an MVC group type")
34         }
35 
36         String mvcName = attributes.remove('mvcName')
37         mvcName = attributes.remove('mvcId')
38 
39         MVCGroup mvcGroup = ApplicationHolder.application.buildMVCGroup(mvcType, mvcName, attributes)
40         def root = mvcGroup.getScriptResult('view')
41         builder.context.root = root
42         builder.context.mvcGroup = mvcGroup
43         root
44     }
45 
46     boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
47         false
48     }
49 
50     boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure childContent) {
51         def root = builder.context.root
52         builder = builder.context.mvcGroup.builder
53         Closure handleChildContent = builder.getVariables().get('handleChildContent')
54         if (handleChildContent != null) {
55             handleChildContent(childContent)
56         else {
57             builder.container(root, childContent)
58         }
59         false
60     }
61 
62     boolean isHandlesNodeChildren() {
63         false
64     }
65 }