MenuFactory.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.javafx.factory
17 
18 import griffon.javafx.support.JavaFXAction
19 import groovyx.javafx.factory.AbstractNodeFactory
20 import javafx.scene.control.ButtonBase
21 import javafx.scene.control.Menu
22 import javafx.scene.control.MenuBar
23 import javafx.scene.control.MenuButton
24 import javafx.scene.control.MenuItem
25 import javafx.scene.control.SplitMenuButton
26 
27 import static griffon.builder.javafx.factory.ActionFactory.applyAction
28 import static griffon.builder.javafx.factory.ActionFactory.extractActionParams
29 
30 /**
31  *
32  @author jimclarke
33  */
34 class MenuFactory extends AbstractNodeFactory {
35     MenuFactory(Class beanClass) {
36         super(beanClass);
37     }
38 
39     Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributesthrows InstantiationException, IllegalAccessException {
40         JavaFXAction action = null
41         Map actionParams = [:]
42         if (value instanceof JavaFXAction) {
43             action = value
44             value = null
45             actionParams = extractActionParams(attributes)
46         }
47 
48         Object menu = super.newInstance(builder, name, value, attributes)
49 
50         if (menu instanceof ButtonBase && action) {
51             applyAction(menu, action, actionParams)
52         }
53 
54         if (value instanceof CharSequence) {
55             switch (menu) {
56                 case MenuButton:
57                 case SplitMenuButton:
58                     menu.text = value.toString()
59                     break;
60             }
61 
62         }
63         menu
64     }
65 
66     void setChild(FactoryBuilderSupport builder, Object parent, Object child) {
67         if (parent instanceof MenuBar && child instanceof Menu) {
68             parent.menus.add(child);
69         else if (child instanceof MenuItem) {
70             parent.items.add(child);
71         else {
72             super.setChild(builder, parent, child);
73         }
74     }
75 }