ScrollPaneFactory.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.ScrollPane
22 
23 /**
24  @author Andres Almiray
25  */
26 class ScrollPaneFactory extends ViewportFactory {
27     public static final String DELEGATE_PROPERTY_COLUMN_HEADER = '_delegateProperty:columnHeader'
28     public static final String DEFAULT_DELEGATE_PROPERTY_COLUMN_HEADER = 'columnHeader'
29     public static final String DELEGATE_PROPERTY_ROW_HEADER = '_delegateProperty:rowHeader'
30     public static final String DEFAULT_DELEGATE_PROPERTY_ROW_HEADER = 'rowHeader'
31     public static final String DELEGATE_PROPERTY_CORNER = '_delegateProperty:corner'
32     public static final String DEFAULT_DELEGATE_PROPERTY_CORNER = 'corner'
33     public static final String CONTEXT_DATA_KEY = 'ScrollPaneFactoryData'
34 
35     ScrollPaneFactory() {
36         super(ScrollPane)
37     }
38 
39     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
40         def newChild = super.newInstance(builder, name, value, attributes)
41         builder.context.scrollPaneFactoryClosure = FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
42             if (builder.current == newChildinspectChild(cBuilder, cNode, cAttributes)
43         }
44         builder.addAttributeDelegate(builder.context.scrollPaneFactoryClosure)
45         builder.context[DELEGATE_PROPERTY_COLUMN_HEADER= attributes.remove('columnHeader') ?: DEFAULT_DELEGATE_PROPERTY_COLUMN_HEADER
46         builder.context[DELEGATE_PROPERTY_ROW_HEADER= attributes.remove('rowHeader') ?: DEFAULT_DELEGATE_PROPERTY_ROW_HEADER
47         builder.context[DELEGATE_PROPERTY_CORNER= attributes.remove('corner') ?: DEFAULT_DELEGATE_PROPERTY_CORNER
48 
49         return newChild
50     }
51 
52     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
53         if (!(node instanceof Component)) return
54         def isColumnHeader = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_COLUMN_HEADER?: DEFAULT_DELEGATE_PROPERTY_COLUMN_HEADER)
55         def isRowHeader = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_ROW_HEADER?: DEFAULT_DELEGATE_PROPERTY_ROW_HEADER)
56         def isCorner = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_CORNER?: DEFAULT_DELEGATE_PROPERTY_CORNER)
57         def scrollPaneContext = builder.context.get(CONTEXT_DATA_KEY?: [:]
58         if (scrollPaneContext.isEmpty()) {
59             builder.context.put(CONTEXT_DATA_KEY, scrollPaneContext)
60         }
61         scrollPaneContext.put(node, [isColumnHeader, isRowHeader, isCorner])
62     }
63 
64     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
65         if (child instanceof Component) {
66             def settings = builder.context[CONTEXT_DATA_KEY]?.get(child?: [false, false, false]
67             if (settings[0]) parent.columnHeader = child
68             else if (settings[1]) parent.rowHeader = child
69             else if (settings[2]) parent.corner = child
70             else parent.view = child
71         else {
72             super.setChild(builder, parent, child)
73         }
74     }
75 
76     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
77         super.onNodeCompleted(builder, parent, node)
78         builder.removeAttributeDelegate(builder.context.scrollPaneFactoryClosure)
79     }
80 }