FormFactory.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.Form
20 import org.apache.pivot.wtk.MessageType
21 
22 /**
23  @author Andres Almiray
24  */
25 class FormFlagFactory extends PivotBeanFactory {
26     FormFlagFactory() {
27         super(Form.Flag)
28     }
29 
30     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
31         new Form.Flag(
32                 attributes.remove('type') ?: MessageType.ERROR,
33                 attributes.remove('message') ?: '<empty>'
34         )
35     }
36 }
37 
38 /**
39  @author Andres Almiray
40  */
41 class FormSectionFactory extends ContainerFactory {
42     public static final String DELEGATE_PROPERTY_FORM_LABEL = "_delegateProperty:formLabel"
43     public static final String DEFAULT_DELEGATE_PROPERTY_FORM_LABEL = "formLabel"
44 //    public static final String DELEGATE_PROPERTY_FORM_FLAG = "_delegateProperty:formFlag"
45 //    public static final String DEFAULT_DELEGATE_PROPERTY_FORM_FLAG = "formFlag"
46     public static final String SECTIONS_CONTEXT_DATA_KEY = "FormSections"
47 
48     FormSectionFactory() {
49         super(Form.Section)
50     }
51 
52     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
53         def newChild = super.newInstance(builder, name, value, attributes)
54         builder.context.formSectionFactoryClosure = FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
55             if (builder.current == newChildinspectChild(cBuilder, cNode, cAttributes)
56         }
57         builder.addAttributeDelegate(builder.context.formSectionFactoryClosure)
58         builder.context[DELEGATE_PROPERTY_FORM_LABEL= attributes.remove("formLabel"?: DEFAULT_DELEGATE_PROPERTY_FORM_LABEL
59 //        builder.context[DELEGATE_PROPERTY_FORM_FLAG] = attributes.remove("formFlag") ?: DEFAULT_DELEGATE_PROPERTY_FORM_FLAG
60         builder.context.get(SECTIONS_CONTEXT_DATA_KEY, [])
61 
62         return newChild
63     }
64 
65 
66     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
67         if (!(node instanceof Component)) return
68         def formLabel = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_FORM_LABEL?: DEFAULT_DELEGATE_PROPERTY_FORM_LABEL)
69 //        def formFlag = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_FORM_FLAG) ?: DEFAULT_DELEGATE_PROPERTY_FORM_FLAG)
70         if (builder?.parentContext) {
71             builder.parentContext[SECTIONS_CONTEXT_DATA_KEY<< [component: node, formLabel: formLabel/*, formFlag: formFlag*/]
72         }
73     }
74 
75     void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
76         if (parent instanceof Formparent.sections.add(node)
77         else super.setParent(builder, parent, node)
78     }
79 
80     public void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
81         def sectionData = builder.context.remove(SECTIONS_CONTEXT_DATA_KEY)
82         sectionData?.each chunk ->
83             if (chunk?.formLabelForm.setLabel(chunk.component, chunk.formLabel)
84         }
85         super.onNodeCompleted(builder, parent, node)
86         builder.removeAttributeDelegate(builder.context.formSectionFactoryClosure)
87     }
88 }