TabPaneFactory.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.TabPane
22 
23 /**
24  @author Andres Almiray
25  */
26 class TabPaneFactory 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 DELEGATE_PROPERTY_CLOSEABLE = '_delegateProperty:closeable'
32     public static final String DEFAULT_DELEGATE_PROPERTY_CLOSEABLE = 'closeable'
33     public static final String DELEGATE_PROPERTY_CORNER = '_delegateProperty:corner'
34     public static final String DEFAULT_DELEGATE_PROPERTY_CORNER = 'corner'
35     public static final String CONTEXT_DATA_KEY = 'TabPaneFactoryData'
36 
37     TabPaneFactory() {
38         super(TabPane)
39     }
40 
41     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
42         def newChild = super.newInstance(builder, name, value, attributes)
43         builder.context.tabPaneFactoryClosure = FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
44             if (builder.current == newChildinspectChild(cBuilder, cNode, cAttributes)
45         }
46         builder.addAttributeDelegate(builder.context.tabPaneFactoryClosure)
47         builder.context[DELEGATE_PROPERTY_ICON= attributes.remove('icon') ?: DEFAULT_DELEGATE_PROPERTY_ICON
48         builder.context[DELEGATE_PROPERTY_LABEL= attributes.remove('label') ?: DEFAULT_DELEGATE_PROPERTY_LABEL
49         builder.context[DELEGATE_PROPERTY_CLOSEABLE= attributes.remove('closeable') ?: DEFAULT_DELEGATE_PROPERTY_CLOSEABLE
50         builder.context[DELEGATE_PROPERTY_CORNER= attributes.remove('corner') ?: DEFAULT_DELEGATE_PROPERTY_CORNER
51 
52         return newChild
53     }
54 
55     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
56         if (!(node instanceof Component)) return
57         def icon = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_ICON?: DEFAULT_DELEGATE_PROPERTY_ICON)
58         def label = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_LABEL?: DEFAULT_DELEGATE_PROPERTY_LABEL)
59         def closeable = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_CLOSEABLE?: DEFAULT_DELEGATE_PROPERTY_CLOSEABLE)
60         def isCorner = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_CORNER?: DEFAULT_DELEGATE_PROPERTY_CORNER)
61         def tabPaneContext = builder.context.get(CONTEXT_DATA_KEY?: [:]
62         if (tabPaneContext.isEmpty()) {
63             builder.context.put(CONTEXT_DATA_KEY, tabPaneContext)
64         }
65         tabPaneContext.put(node, [icon, label, closeable, isCorner])
66     }
67 
68     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
69         if (child instanceof Component) {
70             def settings = builder.context[CONTEXT_DATA_KEY]?.get(child?: [null, null, null, false]
71             if (settings[3]) {
72                 parent.corner = child
73             else {
74                 parent.tabs.add(child)
75                 if (settings[0]) TabPane.setIcon(child, settings[0])
76                 if (settings[1]) TabPane.setLabel(child, settings[1])
77                 if (settings[2!= nullTabPane.setCloseable(child, settings[2])
78             }
79         else {
80             super.setChild(builder, parent, child)
81         }
82     }
83 
84     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
85         super.onNodeCompleted(builder, parent, node)
86         builder.removeAttributeDelegate(builder.context.tabPaneFactoryClosure)
87     }
88 }