AliasedMetaComponentFactory.groovy
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.factory
17 
18 /**
19  * Enables MVC groups to be used as component nodes
20  *
21  @author Andres Almiray
22  @author Alexander Klein
23  */
24 @SuppressWarnings("rawtypes")
25 class AliasedMetaComponentFactory extends MetaComponentFactory {
26     final String mvcType
27     final Map<String, Object> attributes = [:]
28     final boolean leaf
29 
30     AliasedMetaComponentFactory(String mvcType, boolean leaf = true, Map<String, Object> attributes = [:]) {
31         this.leaf = leaf
32         this.mvcType = mvcType
33         this.attributes.putAll(attributes ?: [:])
34     }
35 
36     @Override
37     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
38         return super.newInstance(builder, name, value, attributes + [value: value])
39     }
40 
41     @Override
42     protected Map resolveAttributes(Map attributes) {
43         Map defaultMvcArgs = this.attributes.mvcArgs ?: [:]
44         Map mvcArgs = attributes.mvcArgs ?: [:]
45         Map attrs = [:]
46         attrs.putAll(this.attributes)
47         attrs.putAll(attributes)
48         Map args = [:]
49         args.putAll(defaultMvcArgs)
50         args.putAll(mvcArgs)
51         attrs.mvcArgs = args
52         return attrs
53     }
54 }