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