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