KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RootPaneElement.java,v 1.2 2004/07/28 21:21:17 aim 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.awt.BorderLayout JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Dimension JavaDoc;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import javax.swing.Action JavaDoc;
22 import javax.swing.JComponent JavaDoc;
23 import javax.swing.JMenuBar JavaDoc;
24 import javax.swing.JRootPane JavaDoc;
25 import javax.swing.JSeparator JavaDoc;
26 import javax.swing.JToolBar JavaDoc;
27
28 import net.openmarkup.AttributeHandler;
29 import net.openmarkup.ElementAssimilator;
30 import net.openmarkup.ElementHandler;
31 import net.openmarkup.ElementType;
32 import net.openmarkup.Realizable;
33
34 import org.w3c.dom.Element JavaDoc;
35
36 import org.jdesktop.jdnc.markup.Attributes;
37 import org.jdesktop.jdnc.markup.ElementTypes;
38 import org.jdesktop.jdnc.markup.Namespace;
39 import org.jdesktop.jdnc.markup.attr.ComponentAttributes;
40 import org.jdesktop.jdnc.markup.attr.RootPaneAttributes;
41
42 import org.jdesktop.swing.JXStatusBar;
43 import org.jdesktop.swing.JXRootPane;
44
45 import org.jdesktop.swing.Application;
46 import org.jdesktop.swing.actions.ActionContainerFactory;
47 import org.jdesktop.swing.actions.ActionManager;
48
49 /**
50  *
51  * @author Amy Fowler
52  */

53 public class RootPaneElement extends ComponentElement {
54
55     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
56     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
57
58     public RootPaneElement(Element JavaDoc element, ElementType elementType) {
59         super(element, elementType);
60     }
61
62     public ElementHandler getElementHandler(String JavaDoc namespaceURI, String JavaDoc elementName) {
63         Map JavaDoc handlerMap = getElementHandlerMap();
64         return handlerMap == null ? null : (ElementHandler) handlerMap.get(namespaceURI + ":" + elementName);
65     }
66
67     public AttributeHandler getAttributeHandler(String JavaDoc namespaceURI, String JavaDoc attrName) {
68         Map JavaDoc handlerMap = getAttributeHandlerMap();
69         return handlerMap == null ? null : (AttributeHandler) attrMap.get(namespaceURI + ":" + attrName);
70     }
71
72     protected Map JavaDoc getAttributeHandlerMap() {
73         return attrMap;
74     }
75
76     protected Map JavaDoc getElementHandlerMap() {
77         return elementMap;
78     }
79
80     protected void applyAttributesBefore() {
81         super.applyAttributesBefore();
82         applyAttribute(Namespace.JDNC, Attributes.APP);
83         applyAttribute(Namespace.JDNC, Attributes.SIZE); // WIDTH and HEIGHT
84
}
85
86     protected Map JavaDoc registerAttributeHandlers() {
87          Map JavaDoc handlerMap = super.registerAttributeHandlers();
88          if (handlerMap != null) {
89              handlerMap.put(Namespace.JDNC + ":" + Attributes.APP,
90                             appHandler);
91              handlerMap.put(Namespace.JDNC + ":" + Attributes.SIZE,
92                             sizeHandler); // WIDTH and HEIGHT
93
}
94          return handlerMap;
95     }
96
97     protected Map JavaDoc registerElementHandlers() {
98         Map JavaDoc handlerMap = super.registerElementHandlers();
99         if (handlerMap != null) {
100             handlerMap.put(Namespace.JDNC + ":" +
101                            ElementTypes.EDITOR.getLocalName(),
102                            editorElementHandler);
103             handlerMap.put(Namespace.JDNC + ":" +
104                            ElementTypes.FORM.getLocalName(),
105                            formElementHandler);
106             handlerMap.put(Namespace.JDNC + ":" +
107                            ElementTypes.SPLIT_PANE.getLocalName(),
108                            splitPaneElementHandler);
109             handlerMap.put(Namespace.JDNC + ":" +
110                            ElementTypes.TABBED_PANE.getLocalName(),
111                            tabbedPaneElementHandler);
112             handlerMap.put(Namespace.JDNC + ":" +
113                            ElementTypes.TABLE.getLocalName(),
114                            tableElementHandler);
115             handlerMap.put(Namespace.JDNC + ":" +
116                            ElementTypes.TREE_TABLE.getLocalName(),
117                            treeTableElementHandler);
118             handlerMap.put(Namespace.JDNC + ":" +
119                            ElementTypes.COMPONENT_TOOLBAR.getLocalName(),
120                            toolBarElementHandler);
121             handlerMap.put(Namespace.JDNC + ":" +
122                            ElementTypes.EDITOR.getLocalName(),
123                            editorElementHandler);
124             handlerMap.put(Namespace.JDNC + ":" +
125                            ElementTypes.MENUBAR.getLocalName(),
126                            menuBarElementHandler);
127             handlerMap.put(Namespace.JDNC + ":" +
128                            ElementTypes.STATUSBAR.getLocalName(),
129                            statusBarElementHandler);
130         }
131         return handlerMap;
132     }
133
134     public static final ElementAssimilator contentComponentAssimilator = new ElementAssimilator() {
135         public void assimilate(Realizable parent, Realizable child) {
136             JXRootPane rootPane = (JXRootPane)parent.getObject();
137             Component JavaDoc contentComponent = (Component JavaDoc)child.getObject();
138             rootPane.addComponent(contentComponent);
139         }
140     };
141
142     public static final ElementAssimilator menuBarAssimilator = new ElementAssimilator() {
143         public void assimilate(Realizable parent, Realizable child) {
144         JRootPane JavaDoc rootPane = (JRootPane JavaDoc)parent.getObject();
145         JMenuBar JavaDoc menuBar = (JMenuBar JavaDoc)child.getObject();
146         rootPane.setJMenuBar(menuBar);
147         }
148     };
149
150
151     /**
152      * Handle the toolbar element which is represented as a Vector.
153      * <p>
154      * XXX Note: this is pretty hacked. It appears that the toolbar should
155      * be broken out into a toolbar element. Like the MenuElement.
156      */

157     public static final ElementAssimilator toolBarAssimilator = new ElementAssimilator() {
158         public void assimilate(Realizable parent, Realizable child) {
159             JXRootPane rootPane = (JXRootPane)parent.getObject();
160
161         // Should convert the Action and Separator entries into id tags
162
// and null.
163
Vector JavaDoc entries = (Vector JavaDoc)child.getObject();
164         Iterator JavaDoc iter = entries.iterator();
165         List JavaDoc actionlist = new ArrayList JavaDoc();
166         while (iter.hasNext()) {
167         Object JavaDoc obj = iter.next();
168         if (obj instanceof Action JavaDoc) {
169             actionlist.add(((Action JavaDoc)obj).getValue(Action.ACTION_COMMAND_KEY));
170         } else if (obj instanceof JSeparator JavaDoc) {
171             actionlist.add(null);
172         } else if (obj instanceof Vector JavaDoc) {
173             // This is a group element which is also represented as a vector.
174
Vector JavaDoc group = (Vector JavaDoc)obj;
175             Iterator JavaDoc i2 = group.iterator();
176             while (i2.hasNext()) {
177             Action JavaDoc action = (Action JavaDoc)i2.next();
178             Object JavaDoc value = action.getValue(Action.ACTION_COMMAND_KEY);
179             actionlist.add(value);
180             }
181         }
182             }
183
184         ActionManager manager = Application.getInstance().getActionManager();
185         ActionContainerFactory factory = manager.getFactory();
186
187             JToolBar JavaDoc toolBar = factory.createToolBar(actionlist);
188             rootPane.setToolBar(toolBar);
189         }
190     };
191
192     public static final ElementAssimilator statusBarAssimilator = new ElementAssimilator() {
193         public void assimilate(Realizable parent, Realizable child) {
194         JXRootPane rootPane = (JXRootPane)parent.getObject();
195         JXStatusBar status = (JXStatusBar)child.getObject();
196         rootPane.setStatusBar(status);
197         }
198     };
199
200
201     // For typecasting entries in a Vector when converted to an array
202

203     protected static final AttributeHandler appHandler =
204         new AttributeHandler(Namespace.JDNC, Attributes.APP,
205                              RootPaneAttributes.appApplier);
206
207     protected static final AttributeHandler sizeHandler =
208         new AttributeHandler(Namespace.JDNC, Attributes.SIZE,
209                              ComponentAttributes.sizeApplier);
210
211     protected static final ElementHandler editorElementHandler =
212         new ElementHandler(ElementTypes.EDITOR,
213                            RootPaneElement.contentComponentAssimilator);
214
215     protected static final ElementHandler formElementHandler =
216         new ElementHandler(ElementTypes.FORM,
217                            RootPaneElement.contentComponentAssimilator);
218
219     protected static final ElementHandler splitPaneElementHandler =
220         new ElementHandler(ElementTypes.SPLIT_PANE,
221                            RootPaneElement.contentComponentAssimilator);
222
223     protected static final ElementHandler tabbedPaneElementHandler =
224         new ElementHandler(ElementTypes.TABBED_PANE,
225                            RootPaneElement.contentComponentAssimilator);
226
227     protected static final ElementHandler tableElementHandler =
228         new ElementHandler(ElementTypes.TABLE,
229                            RootPaneElement.contentComponentAssimilator);
230
231     protected static final ElementHandler treeTableElementHandler =
232         new ElementHandler(ElementTypes.TREE_TABLE,
233                            RootPaneElement.contentComponentAssimilator);
234
235     protected static final ElementHandler toolBarElementHandler =
236         new ElementHandler(ElementTypes.COMPONENT_TOOLBAR,
237                            RootPaneElement.toolBarAssimilator);
238
239     protected static final ElementHandler statusBarElementHandler =
240         new ElementHandler(ElementTypes.STATUSBAR,
241                            RootPaneElement.statusBarAssimilator);
242
243     protected static final ElementHandler menuBarElementHandler =
244         new ElementHandler(ElementTypes.MENUBAR,
245                            RootPaneElement.menuBarAssimilator);
246 }
247
Popular Tags