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