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