TablePaneFactory.groovy
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.builder.pivot.factory
019 
020 import org.apache.pivot.wtk.Component
021 import org.apache.pivot.wtk.TablePane
022 
023 /**
024  @author Andres Almiray
025  */
026 class TablePaneFactory extends ComponentFactory {
027     public static final String DELEGATE_PROPERTY_COLUMN_SPAN = '_delegateProperty:columnSpan'
028     public static final String DEFAULT_DELEGATE_PROPERTY_COLUMN_SPAN = 'columnSpan'
029     public static final String DELEGATE_PROPERTY_ROW_SPAN = '_delegateProperty:rowSpan'
030     public static final String DEFAULT_DELEGATE_PROPERTY_ROW_SPAN = 'rowSpan'
031     public static final String CONTEXT_DATA_KEY = 'TablePaneFactoryData'
032 
033     TablePaneFactory() {
034         super(TablePane)
035     }
036 
037 /*
038     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
039         def newChild = super.newInstance(builder, name, value, attributes)
040         builder.context.tablePaneFactoryClosure = { FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
041             if (builder.current == newChild) inspectChild(cBuilder, cNode, cAttributes)
042         }
043         builder.addAttributeDelegate(builder.context.tablePaneFactoryClosure)
044         builder.context[DELEGATE_PROPERTY_COLUMN_SPAN] = attributes.remove('columnSpan') ?: DEFAULT_DELEGATE_PROPERTY_COLUMN_SPAN
045         builder.context[DELEGATE_PROPERTY_ROW_SPAN] = attributes.remove('rowSpan') ?: DEFAULT_DELEGATE_PROPERTY_ROW_SPAN
046 
047         return newChild
048     }
049 
050 
051     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
052         if(!(node instanceof Component)) return
053         def columnSpan = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_COLUMN_SPAN) ?: DEFAULT_DELEGATE_PROPERTY_COLUMN_SPAN)
054         def rowSpan = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_ROW_SPAN) ?: DEFAULT_DELEGATE_PROPERTY_ROW_SPAN)
055         def tablePaneContext = builder.context.get(CONTEXT_DATA_KEY) ?: [:]
056         if (tablePaneContext.isEmpty()) {
057             builder.context.put(CONTEXT_DATA_KEY, tablePaneContext)
058         }
059         tablePaneContext.put(node, [columnSpan, rowSpan])
060     }
061 
062     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
063         if(child instanceof Component) {
064             def settings = builder.context[CONTEXT_DATA_KEY]?.get(child) ?: [null, null]
065             parent.panels.add(child)
066             if(settings[0] != null) TablePane.setColumnSpan(child, settings[0])
067             if(settings[1] != null) TablePane.setRowSpan(child, settings[1])
068         } else {
069             super.setChild(builder, parent, child)
070         }
071     }
072 
073     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
074         super.onNodeCompleted(builder, parent, node)
075         builder.removeAttributeDelegate(builder.context.tablePaneFactoryClosure)
076     }
077 */
078 
079     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
080         if (child instanceof TablePane.Columnparent.columns.add(child)
081         if (child instanceof TablePane.Rowparent.rows.add(child)
082     }
083 }
084 
085 /**
086  @author Andres Almiray
087  */
088 class TablePaneColumnFactory extends ComponentFactory {
089     TablePaneColumnFactory() {
090         super(TablePane.Column)
091     }
092 
093     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
094         if (child instanceof Componentparent.add(child)
095     }
096 }
097 
098 /**
099  @author Andres Almiray
100  */
101 class TablePaneRowFactory extends ComponentFactory {
102     TablePaneRowFactory() {
103         super(TablePane.Row)
104     }
105 
106     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
107         if (child instanceof Componentparent.add(child)
108     }
109 }