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.Accordion
21 import org.apache.pivot.wtk.Component
22
23 /**
24 * @author Andres Almiray
25 */
26 class AccordionFactory extends ViewportFactory {
27 public static final String DELEGATE_PROPERTY_ICON = '_delegateProperty:icon'
28 public static final String DEFAULT_DELEGATE_PROPERTY_ICON = 'icon'
29 public static final String DELEGATE_PROPERTY_LABEL = '_delegateProperty:label'
30 public static final String DEFAULT_DELEGATE_PROPERTY_LABEL = 'label'
31 public static final String CONTEXT_DATA_KEY = 'AccordionFactoryData'
32
33 AccordionFactory() {
34 super(Accordion)
35 }
36
37 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
38 def newChild = super.newInstance(builder, name, value, attributes)
39 builder.context.accordionFactoryClosure = { FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
40 if (builder.current == newChild) inspectChild(cBuilder, cNode, cAttributes)
41 }
42 builder.addAttributeDelegate(builder.context.accordionFactoryClosure)
43 builder.context[DELEGATE_PROPERTY_ICON] = attributes.remove('icon') ?: DEFAULT_DELEGATE_PROPERTY_ICON
44 builder.context[DELEGATE_PROPERTY_LABEL] = attributes.remove('label') ?: DEFAULT_DELEGATE_PROPERTY_LABEL
45
46 return newChild
47 }
48
49 static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
50 if (!(node instanceof Component)) return
51 def icon = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_ICON) ?: DEFAULT_DELEGATE_PROPERTY_ICON)
52 def label = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_LABEL) ?: DEFAULT_DELEGATE_PROPERTY_LABEL)
53 def accordionContext = builder.context.get(CONTEXT_DATA_KEY) ?: [:]
54 if (accordionContext.isEmpty()) {
55 builder.context.put(CONTEXT_DATA_KEY, accordionContext)
56 }
57 accordionContext.put(node, [icon, label])
58 }
59
60 void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
61 if (child instanceof Component) {
62 def settings = builder.context[CONTEXT_DATA_KEY]?.get(child) ?: [null, null]
63 parent.panels.add(child)
64 if (settings[0]) Accordion.setIcon(child, settings[0])
65 if (settings[1]) Accordion.setLabel(child, settings[1])
66 } else {
67 super.setChild(builder, parent, child)
68 }
69 }
70
71 void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
72 super.onNodeCompleted(builder, parent, node)
73 builder.removeAttributeDelegate(builder.context.accordionFactoryClosure)
74 }
75 }
|