001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.builder.pivot.factory
019
020 import org.apache.pivot.wtk.Frame
021 import org.apache.pivot.wtk.Menu
022 import org.apache.pivot.wtk.MenuBar
023 import org.apache.pivot.wtk.MenuButton
024 import org.apache.pivot.wtk.MenuPopup
025
026 /**
027 * @author Andres Almiray
028 */
029 class MenuFactory extends ComponentFactory {
030 MenuFactory() {
031 super(Menu)
032 }
033
034 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
035 if (value instanceof GString) value = value as String
036 if (FactoryBuilderSupport.checkValueIsTypeNotString(value, name, beanClass)) {
037 return value
038 }
039 if (value && !attributes.containsKey('section')) attributes.section = value
040 if (!attributes.section) attributes.section = 'Menu' + System.currentTimeMillis()
041 builder.context.sectionName = attributes.section
042 beanClass.newInstance()
043 }
044
045 boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
046 def section = new Menu.Section()
047 section.name = builder.context.section
048 node.sections.add(section)
049 return super.onHandleNodeAttributes(builder, node, attributes)
050 }
051
052 void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
053 switch (parent?.getClass()) {
054 case MenuButton:
055 case MenuPopup:
056 case MenuBar.Item:
057 parent.setMenu(node)
058 break
059 case Menu:
060 parent.sections.add(node.sections.remove(node.getSection(builder.context.sectionName)))
061 break
062 }
063 }
064 }
065
066 /**
067 * @author Andres Almiray
068 */
069 class MenuItemFactory extends ComponentFactory {
070 MenuItemFactory() {
071 super(Menu.Item, false)
072 }
073
074 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
075 if (value instanceof GString) value = value as String
076 if (FactoryBuilderSupport.checkValueIsTypeNotString(value, name, beanClass)) {
077 return value
078 }
079 if (value && !attributes.containsKey('buttonData')) attributes.buttonData = value
080 beanClass.newInstance()
081 }
082
083 boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map attributes) {
084 def section = new Menu.Section()
085 section.name = builder.context.section
086 node.sections.add(section)
087 return super.onHandleNodeAttributes(builder, node, attributes)
088 }
089
090 void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
091 if (!(parent instanceof Menu)) return
092 parent.sections[parent.sections.length - 1].add(node)
093 }
094 }
095
096 /**
097 * @author Andres Almiray
098 */
099 class MenuBarFactory extends ComponentFactory {
100 MenuBarFactory() {
101 super(MenuBar, false)
102 }
103
104 void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
105 if (!(parent instanceof Frame)) return
106 parent.setMenuBar(node)
107 }
108 }
109
110 /**
111 * @author Andres Almiray
112 */
113 class MenuBarItemFactory extends ComponentFactory {
114 MenuBarItemFactory() {
115 super(MenuBar.Item, false)
116 }
117
118 void setParent(FactoryBuilderSupport builder, Object parent, Object node) {
119 if (!(parent instanceof MenuBar)) return
120 parent.items.add(node)
121 }
122 }
|