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