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.lanterna.factory
17
18 import com.googlecode.lanterna.gui.component.Panel
19
20 /**
21 * @author Andres Almiray
22 */
23 class LayoutFactory extends BeanFactory {
24 private Map contextProps
25 static final String DELEGATE_PROPERTY_CONSTRAINT = '_delegateProperty:Constraint'
26 static final String DEFAULT_DELEGATE_PROPERTY_CONSTRAINT = 'constraints'
27
28 LayoutFactory(Class klass) {
29 super(klass, true)
30 }
31
32 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
33 builder.context[DELEGATE_PROPERTY_CONSTRAINT] = attributes.remove('constraintsProperty') ?: DEFAULT_DELEGATE_PROPERTY_CONSTRAINT
34 Object o = super.newInstance(builder, name, value, attributes)
35 addLayoutProperties(builder.context)
36 return o
37 }
38
39 protected void addLayoutProperties(Map context, Class layoutClass) {
40 if (contextProps == null) {
41 contextProps = [:]
42 layoutClass.fields.each {
43 String name = it.name
44 if (name.toUpperCase() == name) {
45 contextProps[name] = layoutClass."$name"
46 }
47 }
48 }
49
50 context.putAll(contextProps)
51 }
52
53 protected void addLayoutProperties(Map context) {
54 addLayoutProperties(context, beanClass)
55 }
56
57 void setParent(FactoryBuilderSupport builder, Object parent, Object child) {
58 if (parent instanceof Panel) {
59 parent.layoutManager = child
60 }
61 }
62
63 static constraintsAttributeDelegate(builder, node, attributes) {
64 def constraintsAttr = builder?.context?.getAt(DELEGATE_PROPERTY_CONSTRAINT) ?: DEFAULT_DELEGATE_PROPERTY_CONSTRAINT
65 if (attributes.containsKey(constraintsAttr)) {
66 builder.context.constraints = attributes.remove(constraintsAttr)
67 }
68 }
69 }
|