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