KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ActionElement.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.Map JavaDoc;
12
13 import javax.swing.Action JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
16 import org.jdesktop.swing.Application;
17 import org.jdesktop.swing.actions.ActionManager;
18 import org.jdesktop.jdnc.markup.Attributes;
19 import org.jdesktop.jdnc.markup.Namespace;
20 import org.jdesktop.jdnc.markup.RealizationUtils;
21 import org.jdesktop.jdnc.markup.attr.ActionAttributes;
22 import org.jdesktop.jdnc.markup.attr.NullAttribute;
23 import net.openmarkup.AttributeHandler;
24 import net.openmarkup.ElementType;
25
26 /**
27  *
28  * @author Ramesh Gupta
29  * @author Mark Davidson
30  */

31 public class ActionElement
32     extends ElementProxy {
33     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
34
35     public ActionElement(Element JavaDoc element, ElementType elementType) {
36         super(element, elementType);
37     }
38
39     protected Object JavaDoc instantiate() {
40         Action JavaDoc action;
41         String JavaDoc id = this.getAttributeNSOptional(Namespace.JDNC,
42                                                 Attributes.ACTION_REF);
43         if (id.length() > 0) {
44             // Look in defs,
45
action = (Action JavaDoc) RealizationUtils.getReferencedObject(this, id);
46             if (action == null) {
47                 throw new RuntimeException JavaDoc(id + " action not found");
48             }
49         }
50         else {
51             action = (Action JavaDoc)super.instantiate();
52
53             // RG: XML namespace prefix required for id attribute
54
id = this.getAttribute("xml:id");
55             if (id.equals("")) {
56                 // This cannot be empty
57
id = (String JavaDoc) action.getValue(Action.NAME);
58             }
59
60             /*TODO: Should peek ahead to see if the <exec> element exists
61               to create a bound action
62               NodeList list = getElementsByTagname("exec");
63               if (list.getLength != 0) {
64                   action = new BoundAction();
65                      } else {
66                   action = new TargetableAction();
67                      }
68              */

69
70             ActionManager manager = Application.getInstance().getActionManager();
71             action.putValue(Action.ACTION_COMMAND_KEY, id);
72             manager.addAction(action);
73         }
74         return action;
75     }
76
77     protected void applyAttributesAfter() {
78         super.applyAttributesAfter();
79         applyAttribute(Namespace.JDNC, Attributes.DELEGATE);
80         //applyAttribute(Namespace.JDNC, Attributes.ID);
81
applyAttribute(Namespace.JDNC, Attributes.IS_TOGGLE);
82         applyAttribute(Namespace.JDNC, Attributes.GROUP);
83         applyAttribute(Namespace.JDNC, Attributes.TITLE);
84         applyAttribute(Namespace.JDNC, Attributes.MNEMONIC);
85         applyAttribute(Namespace.JDNC, Attributes.ICON);
86         applyAttribute(Namespace.JDNC, Attributes.SMALL_ICON);
87         applyAttribute(Namespace.JDNC, Attributes.ACCELERATOR);
88         applyAttribute(Namespace.JDNC, Attributes.DESCRIPTION);
89     }
90
91     protected Map JavaDoc registerAttributeHandlers() {
92         Map JavaDoc handlerMap = super.registerAttributeHandlers();
93         if (handlerMap != null) {
94             handlerMap.put(Namespace.JDNC + ":" + Attributes.DELEGATE,
95                            delegateHandler);
96             //handlerMap.put(Namespace.JDNC + ":" + Attributes.ID, idHandler);
97
handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_TOGGLE,
98                            toggleHandler);
99             handlerMap.put(Namespace.JDNC + ":" + Attributes.GROUP,
100                            groupHandler);
101             handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE,
102                            titleHandler);
103             handlerMap.put(Namespace.JDNC + ":" + Attributes.MNEMONIC,
104                            mnemonicHandler);
105             handlerMap.put(Namespace.JDNC + ":" + Attributes.ICON, iconHandler);
106             handlerMap.put(Namespace.JDNC + ":" + Attributes.SMALL_ICON,
107                            smallIconHandler);
108             handlerMap.put(Namespace.JDNC + ":" + Attributes.ACCELERATOR,
109                            acceleratorHandler);
110             handlerMap.put(Namespace.JDNC + ":" + Attributes.DESCRIPTION,
111                            descriptionHandler);
112         // id and actionRef are attributes which are not handled
113
// in appliers
114
handlerMap.put(Namespace.JDNC + ":" + Attributes.ID,
115                            NullAttribute.idHandler);
116             handlerMap.put(Namespace.JDNC + ":" + Attributes.ACTION_REF,
117                            actionRefHandler);
118         
119         }
120
121         return handlerMap;
122     }
123
124     protected Map JavaDoc getAttributeHandlerMap() {
125         return attrMap;
126     }
127
128     private static final AttributeHandler delegateHandler =
129         new AttributeHandler(Namespace.JDNC, Attributes.DELEGATE,
130                              ActionAttributes.delegateApplier);
131     private static final AttributeHandler actionRefHandler =
132         new AttributeHandler(Namespace.JDNC, Attributes.ACTION_REF,
133                              NullAttribute.nullApplier);
134     private static final AttributeHandler toggleHandler =
135         new AttributeHandler(Namespace.JDNC, Attributes.IS_TOGGLE,
136                              ActionAttributes.isToggleApplier);
137     private static final AttributeHandler groupHandler =
138         new AttributeHandler(Namespace.JDNC, Attributes.GROUP,
139                              ActionAttributes.groupApplier);
140     private static final AttributeHandler titleHandler =
141         new AttributeHandler(Namespace.JDNC, Attributes.TITLE,
142                              ActionAttributes.titleApplier);
143     private static final AttributeHandler mnemonicHandler =
144         new AttributeHandler(Namespace.JDNC, Attributes.MNEMONIC,
145                              ActionAttributes.mnemonicApplier);
146     private static final AttributeHandler iconHandler =
147         new AttributeHandler(Namespace.JDNC, Attributes.ICON,
148                              ActionAttributes.iconApplier);
149     private static final AttributeHandler smallIconHandler =
150         new AttributeHandler(Namespace.JDNC, Attributes.SMALL_ICON,
151                              ActionAttributes.smallIconApplier);
152     private static final AttributeHandler acceleratorHandler =
153         new AttributeHandler(Namespace.JDNC, Attributes.ACCELERATOR,
154                              ActionAttributes.acceleratorApplier);
155     private static final AttributeHandler descriptionHandler =
156         new AttributeHandler(Namespace.JDNC, Attributes.DESCRIPTION,
157                              ActionAttributes.descriptionApplier);
158 }
159
Popular Tags