ButtonGroupFactory.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.Button
19 import org.apache.pivot.wtk.ButtonGroup
20 import org.apache.pivot.wtk.RadioButton
21 
22 /**
23  * Copied from SwingBuilder to avoid dependency on Swing module.
24  *
25  @author Danno Ferrin
26  @author Andres Almiray
27  */
28 class ButtonGroupFactory extends BeanFactory {
29     public static final String DELEGATE_PROPERTY_BUTTON_GROUP = "_delegateProperty:buttonGroup";
30     public static final String DEFAULT_DELEGATE_PROPERTY_BUTTON_GROUP = "buttonGroup";
31 
32     public ButtonGroupFactory() {
33         super(ButtonGroup, false)
34     }
35 
36     public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) {
37         builder.context[DELEGATE_PROPERTY_BUTTON_GROUP= attributes.remove("buttonGroupProperty"?: DEFAULT_DELEGATE_PROPERTY_BUTTON_GROUP
38         return super.newInstance(builder, name, value, attributes)
39     }
40 
41     public static buttonGroupAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes) {
42         def buttonGroupAttr = builder?.context?.getAt(DELEGATE_PROPERTY_BUTTON_GROUP?: DEFAULT_DELEGATE_PROPERTY_BUTTON_GROUP
43         if (attributes.containsKey(buttonGroupAttr)) {
44             def o = attributes.get(buttonGroupAttr)
45             if ((instanceof ButtonGroup&& (node instanceof Button)) {
46                 if (!(node instanceof RadioButton)) node.toggleButton = true
47                 node.buttonGroup = o
48                 attributes.remove(buttonGroupAttr)
49             }
50         }
51     }
52 }