01 /*
02 * Copyright 2008-2015 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.pivot.factory
17
18 import org.apache.pivot.wtk.Component
19 import org.apache.pivot.wtk.Rollup
20
21 /**
22 * @author Andres Almiray
23 */
24 class RollupFactory extends ComponentFactory {
25 public static final String DELEGATE_PROPERTY_HEADER = "_delegateProperty:header"
26 public static final String DEFAULT_DELEGATE_PROPERTY_HEADER = "header"
27 public static final String CONTEXT_DATA_KEY = "RollupFactoryData"
28
29 RollupFactory() {
30 super(Rollup)
31 }
32
33 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
34 def newChild = super.newInstance(builder, name, value, attributes)
35 builder.context.rollupFactoryClosure = { FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
36 if (builder.current == newChild) inspectChild(cBuilder, cNode, cAttributes)
37 }
38 builder.addAttributeDelegate(builder.context.rollupFactoryClosure)
39 builder.context[DELEGATE_PROPERTY_HEADER] = attributes.remove("header") ?: DEFAULT_DELEGATE_PROPERTY_HEADER
40
41 return newChild
42 }
43
44 static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
45 if (!(node instanceof Component)) return
46 def header = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_HEADER) ?: DEFAULT_DELEGATE_PROPERTY_HEADER)
47 def rollupContext = builder.context.get(CONTEXT_DATA_KEY) ?: [:]
48 if (rollupContext.isEmpty()) {
49 builder.context.put(CONTEXT_DATA_KEY, rollupContext)
50 }
51 rollupContext.put(node, [header])
52 }
53
54 void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
55 if (child instanceof Component) {
56 def settings = builder.context[CONTEXT_DATA_KEY]?.get(child) ?: [null]
57 if (settings[0]) parent.header = child
58 else parent.content = child
59 } else {
60 super.setChild(builder, parent, child)
61 }
62 }
63
64 public void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
65 super.onNodeCompleted(builder, parent, node)
66 builder.removeAttributeDelegate(builder.context.rollupFactoryClosure)
67 }
68 }
|