1 7 8 package org.jdesktop.jdnc.markup.elem; 9 10 import java.util.Hashtable ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Vector ; 14 15 import javax.swing.AbstractButton ; 16 import javax.swing.Action ; 17 import javax.swing.JComponent ; 18 import javax.swing.JMenu ; 19 import javax.swing.JSeparator ; 20 21 import org.w3c.dom.Element ; 22 import org.jdesktop.swing.Application; 23 import org.jdesktop.swing.actions.ActionContainerFactory; 24 import org.jdesktop.swing.actions.ActionManager; 25 import org.jdesktop.jdnc.markup.Attributes; 26 import org.jdesktop.jdnc.markup.ElementTypes; 27 import org.jdesktop.jdnc.markup.Namespace; 28 import org.jdesktop.jdnc.markup.RealizationUtils; 29 import org.jdesktop.jdnc.markup.attr.MenuAttributes; 30 import org.jdesktop.jdnc.markup.attr.NullAttribute; 31 import net.openmarkup.AttributeHandler; 32 import net.openmarkup.ElementAssimilator; 33 import net.openmarkup.ElementHandler; 34 import net.openmarkup.ElementType; 35 import net.openmarkup.Realizable; 36 37 public class MenuElement extends ElementProxy { 38 private static final Map attrMap = new Hashtable (); 39 private static final Map elementMap = new Hashtable (); 40 41 public MenuElement(Element element, ElementType elementType) { 42 super(element, elementType); 43 } 44 45 protected Object instantiate() { 46 Action action = null; 47 String id = this.getAttributeNSOptional(Namespace.JDNC, 48 Attributes.ACTION_REF); 49 if (id.length() > 0) { 50 action = (Action ) RealizationUtils.getReferencedObject(this, id); 52 if (action == null) { 53 throw new RuntimeException (id + " action not found"); 54 } 55 } 56 57 JComponent menu = (JComponent )super.instantiate(); 58 if (action != null && menu instanceof AbstractButton ) { 59 ( (AbstractButton ) menu).setAction(action); 61 } 62 return menu; 63 } 64 65 protected Map getElementHandlerMap() { 66 return elementMap; 67 } 68 69 protected Map getAttributeHandlerMap() { 70 return attrMap; 71 } 72 73 protected Map registerAttributeHandlers() { 74 Map handlerMap = super.registerAttributeHandlers(); 75 if (handlerMap != null) { 76 handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE, 77 titleHandler); 78 handlerMap.put(Namespace.JDNC + ":" + Attributes.MNEMONIC, 79 mnemonicHandler); 80 handlerMap.put(Namespace.JDNC + ":" + Attributes.DESCRIPTION, 81 descriptionHandler); 82 83 handlerMap.put(Namespace.JDNC + ":" + Attributes.ACTION_REF, 85 actionRefHandler); 86 } 87 return handlerMap; 88 } 89 90 protected Map registerElementHandlers() { 91 Map handlerMap = super.registerElementHandlers(); 92 if (handlerMap != null) { 93 handlerMap.put(Namespace.JDNC + ":" + 94 ElementTypes.MENU.getLocalName(), 95 menuElementHandler); 96 handlerMap.put(Namespace.JDNC + ":" + 97 ElementTypes.SEPARATOR.getLocalName(), 98 separatorElementHandler); 99 handlerMap.put(Namespace.JDNC + ":" + 100 ElementTypes.ACTION.getLocalName(), 101 actionElementHandler); 102 } 103 return handlerMap; 104 } 105 106 protected void applyAttributesAfter() { 107 super.applyAttributesAfter(); 108 applyAttribute(Namespace.JDNC, Attributes.TITLE); 109 applyAttribute(Namespace.JDNC, Attributes.MNEMONIC); 110 applyAttribute(Namespace.JDNC, Attributes.DESCRIPTION); 111 } 112 113 public static final ElementAssimilator menuAssimilator = new 114 ElementAssimilator() { 115 public void assimilate(Realizable parent, Realizable child) { 116 JComponent parentMenu = (JComponent ) parent.getObject(); 117 JMenu menu = (JMenu ) child.getObject(); 118 parentMenu.add(menu); 119 } 120 }; 121 122 public static final ElementAssimilator separatorAssimilator = new 123 ElementAssimilator() { 124 public void assimilate(Realizable parent, Realizable child) { 125 JComponent menu = (JComponent ) parent.getObject(); 126 JSeparator separator = (JSeparator ) child.getObject(); 127 menu.add(separator); 128 } 129 }; 130 131 public static final ElementAssimilator actionAssimilator = new 132 ElementAssimilator() { 133 public void assimilate(Realizable parent, Realizable child) { 134 JComponent menu = (JComponent ) parent.getObject(); 135 136 ActionManager manager = Application.getInstance().getActionManager(); 137 ActionContainerFactory factory = manager.getFactory(); 138 139 menu.add(factory.createMenuItem( (Action ) child.getObject(), menu)); 140 } 141 }; 142 143 public static final ElementAssimilator groupAssimilator = new 144 ElementAssimilator() { 145 public void assimilate(Realizable parent, Realizable child) { 146 JComponent menu = (JComponent ) parent.getObject(); 147 Vector vector = (Vector ) child.getObject(); 148 149 ActionManager manager = Application.getInstance().getActionManager(); 150 ActionContainerFactory factory = manager.getFactory(); 151 152 Iterator iter = vector.iterator(); 153 while (iter.hasNext()) { 154 menu.add(factory.createMenuItem( (Action ) iter.next(), menu)); 155 } 156 } 157 }; 158 159 private static final AttributeHandler actionRefHandler = 160 new AttributeHandler(Namespace.JDNC, Attributes.ACTION_REF, 161 NullAttribute.nullApplier); 162 private static final AttributeHandler titleHandler = 163 new AttributeHandler(Namespace.JDNC, Attributes.TITLE, 164 MenuAttributes.titleApplier); 165 private static final AttributeHandler mnemonicHandler = 166 new AttributeHandler(Namespace.JDNC, Attributes.MNEMONIC, 167 MenuAttributes.mnemonicApplier); 168 private static final AttributeHandler descriptionHandler = 169 new AttributeHandler(Namespace.JDNC, Attributes.DESCRIPTION, 170 MenuAttributes.descriptionApplier); 171 172 protected static final ElementHandler menuElementHandler = 173 new ElementHandler(ElementTypes.MENU, MenuElement.menuAssimilator); 174 protected static final ElementHandler separatorElementHandler = 175 new ElementHandler(ElementTypes.SEPARATOR,MenuElement.separatorAssimilator); 176 protected static final ElementHandler actionElementHandler = 177 new ElementHandler(ElementTypes.ACTION, MenuElement.actionAssimilator); 178 } 179 | Popular Tags |