FormFactory.groovy
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.builder.pivot.factory
19 
20 import org.apache.pivot.wtk.Component
21 import org.apache.pivot.wtk.Form
22 import org.apache.pivot.wtk.MessageType
23 
24 /**
25  @author Andres Almiray
26  */
27 class FormFlagFactory extends PivotBeanFactory {
28     FormFlagFactory() {
29         super(Form.Flag)
30     }
31 
32     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
33         new Form.Flag(
34                 attributes.remove('type') ?: MessageType.ERROR,
35                 attributes.remove('message') ?: '<empty>'
36         )
37     }
38 }
39 
40 /**
41  @author Andres Almiray
42  */
43 class FormSectionFactory extends ContainerFactory {
44     public static final String DELEGATE_PROPERTY_FORM_LABEL = '_delegateProperty:formLabel'
45     public static final String DEFAULT_DELEGATE_PROPERTY_FORM_LABEL = 'formLabel'
46 //    public static final String DELEGATE_PROPERTY_FORM_FLAG = '_delegateProperty:formFlag'
47 //    public static final String DEFAULT_DELEGATE_PROPERTY_FORM_FLAG = 'formFlag'
48     public static final String SECTIONS_CONTEXT_DATA_KEY = 'FormSections'
49 
50     FormSectionFactory() {
51         super(Form.Section)
52     }
53 
54     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
55         def newChild = super.newInstance(builder, name, value, attributes)
56         builder.context.formSectionFactoryClosure = FactoryBuilderSupport cBuilder, Object cNode, Map cAttributes ->
57             if (builder.current == newChildinspectChild(cBuilder, cNode, cAttributes)
58         }
59         builder.addAttributeDelegate(builder.context.formSectionFactoryClosure)
60         builder.context[DELEGATE_PROPERTY_FORM_LABEL= attributes.remove('formLabel') ?: DEFAULT_DELEGATE_PROPERTY_FORM_LABEL
61 //        builder.context[DELEGATE_PROPERTY_FORM_FLAG] = attributes.remove('formFlag') ?: DEFAULT_DELEGATE_PROPERTY_FORM_FLAG
62         builder.context.get(SECTIONS_CONTEXT_DATA_KEY, [])
63 
64         return newChild
65     }
66 
67 
68     static void inspectChild(FactoryBuilderSupport builder, Object node, Map attributes) {
69         if (!(node instanceof Component)) return
70         def formLabel = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_FORM_LABEL?: DEFAULT_DELEGATE_PROPERTY_FORM_LABEL)
71 //        def formFlag = attributes.remove(builder?.parentContext?.getAt(DELEGATE_PROPERTY_FORM_FLAG) ?: DEFAULT_DELEGATE_PROPERTY_FORM_FLAG)
72         if (builder?.parentContext) {
73             builder.parentContext[SECTIONS_CONTEXT_DATA_KEY<< [component: node, formLabel: formLabel/*, formFlag: formFlag*/]
74         }
75     }
76 
77     void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
78         if (parent instanceof Formparent.sections.add(node)
79         else super.setParent(builder, parent, node)
80     }
81 
82     void onNodeCompleted(FactoryBuilderSupport builder, Object parent, Object node) {
83         def sectionData = builder.context.remove(SECTIONS_CONTEXT_DATA_KEY)
84         sectionData?.each chunk ->
85             if (chunk?.formLabelForm.setLabel(chunk.component, chunk.formLabel)
86         }
87         super.onNodeCompleted(builder, parent, node)
88         builder.removeAttributeDelegate(builder.context.formSectionFactoryClosure)
89     }
90 }