RollupFactory.groovy
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.builder.pivot.factory
19 
20 import org.apache.pivot.wtk.Component
21 import org.apache.pivot.wtk.Rollup
22 
23 /**
24  @author Andres Almiray
25  */
26 class RollupFactory extends ComponentFactory {
27     public static final String DELEGATE_PROPERTY_HEADER = '_delegateProperty:header'
28     public static final String DEFAULT_DELEGATE_PROPERTY_HEADER = 'header'
29     public static final String CONTEXT_DATA_KEY = 'RollupFactoryData'
30 
31     RollupFactory() {
32         super(Rollup)
33     }
34 
35     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
36         def newChild = super.newInstance(builder, name, value, attributes)
37         builder.context.rollupFactoryClosure = FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
38             if (builder.current == newChildinspectChild(cBuilder, cNode, cAttributes)
39         }
40         builder.addAttributeDelegate(builder.context.rollupFactoryClosure)
41         builder.context[DELEGATE_PROPERTY_HEADER= attributes.remove('header') ?: DEFAULT_DELEGATE_PROPERTY_HEADER
42 
43         return newChild
44     }
45 
46     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
47         if (!(node instanceof Component)) return
48         def header = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_HEADER?: DEFAULT_DELEGATE_PROPERTY_HEADER)
49         def rollupContext = builder.context.get(CONTEXT_DATA_KEY?: [:]
50         if (rollupContext.isEmpty()) {
51             builder.context.put(CONTEXT_DATA_KEY, rollupContext)
52         }
53         rollupContext.put(node, [header])
54     }
55 
56     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
57         if (child instanceof Component) {
58             def settings = builder.context[CONTEXT_DATA_KEY]?.get(child?: [null]
59             if (settings[0]) parent.header = child
60             else parent.content = child
61         else {
62             super.setChild(builder, parent, child)
63         }
64     }
65 
66     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
67         super.onNodeCompleted(builder, parent, node)
68         builder.removeAttributeDelegate(builder.context.rollupFactoryClosure)
69     }
70 }