KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > elem > MenuElement


1 /*
2  * $Id: MenuElement.java,v 1.1.1.1 2004/06/16 01:43:40 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.elem;
9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import javax.swing.AbstractButton JavaDoc;
16 import javax.swing.Action JavaDoc;
17 import javax.swing.JComponent JavaDoc;
18 import javax.swing.JMenu JavaDoc;
19 import javax.swing.JSeparator JavaDoc;
20
21 import org.w3c.dom.Element JavaDoc;
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 JavaDoc attrMap = new Hashtable JavaDoc();
39     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
40
41     public MenuElement(Element JavaDoc element, ElementType elementType) {
42         super(element, elementType);
43     }
44
45     protected Object JavaDoc instantiate() {
46         Action JavaDoc action = null;
47         String JavaDoc id = this.getAttributeNSOptional(Namespace.JDNC,
48                                                 Attributes.ACTION_REF);
49         if (id.length() > 0) {
50             // Get the action
51
action = (Action JavaDoc) RealizationUtils.getReferencedObject(this, id);
52             if (action == null) {
53                 throw new RuntimeException JavaDoc(id + " action not found");
54             }
55         }
56
57         JComponent JavaDoc menu = (JComponent JavaDoc)super.instantiate();
58         if (action != null && menu instanceof AbstractButton JavaDoc) {
59             // Create a menu item from the action
60
( (AbstractButton JavaDoc) menu).setAction(action);
61         }
62         return menu;
63     }
64
65     protected Map JavaDoc getElementHandlerMap() {
66         return elementMap;
67     }
68
69     protected Map JavaDoc getAttributeHandlerMap() {
70         return attrMap;
71     }
72
73     protected Map JavaDoc registerAttributeHandlers() {
74         Map JavaDoc 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         // Register null appliers. These attributes are handled elsewhere
84
handlerMap.put(Namespace.JDNC + ":" + Attributes.ACTION_REF,
85                            actionRefHandler);
86         }
87         return handlerMap;
88     }
89
90     protected Map JavaDoc registerElementHandlers() {
91         Map JavaDoc 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 JavaDoc parentMenu = (JComponent JavaDoc) parent.getObject();
117             JMenu JavaDoc menu = (JMenu JavaDoc) 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 JavaDoc menu = (JComponent JavaDoc) parent.getObject();
126             JSeparator JavaDoc separator = (JSeparator JavaDoc) 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 JavaDoc menu = (JComponent JavaDoc) parent.getObject();
135
136             ActionManager manager = Application.getInstance().getActionManager();
137             ActionContainerFactory factory = manager.getFactory();
138
139             menu.add(factory.createMenuItem( (Action JavaDoc) 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 JavaDoc menu = (JComponent JavaDoc) parent.getObject();
147             Vector JavaDoc vector = (Vector JavaDoc) child.getObject();
148
149             ActionManager manager = Application.getInstance().getActionManager();
150             ActionContainerFactory factory = manager.getFactory();
151
152             Iterator JavaDoc iter = vector.iterator();
153             while (iter.hasNext()) {
154                 menu.add(factory.createMenuItem( (Action JavaDoc) 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