KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: SplitPaneElement.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.awt.Component JavaDoc;
11
12 import java.util.Hashtable JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import javax.swing.JComponent JavaDoc;
17 import javax.swing.JSplitPane JavaDoc;
18
19 import net.openmarkup.AttributeHandler;
20 import net.openmarkup.ElementAssimilator;
21 import net.openmarkup.ElementHandler;
22 import net.openmarkup.ElementType;
23 import net.openmarkup.Realizable;
24
25 import org.w3c.dom.Element JavaDoc;
26
27 import org.jdesktop.jdnc.markup.Attributes;
28 import org.jdesktop.jdnc.markup.ElementTypes;
29 import org.jdesktop.jdnc.markup.Namespace;
30 import org.jdesktop.jdnc.markup.attr.SplitPaneAttributes;
31
32 /**
33  *
34  * @author Amy Fowler
35  */

36 public class SplitPaneElement extends ComponentElement {
37
38     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
39     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
40
41     public SplitPaneElement(Element JavaDoc element, ElementType elementType) {
42         super(element, elementType);
43     }
44
45     public ElementHandler getElementHandler(String JavaDoc namespaceURI, String JavaDoc elementName) {
46         Map JavaDoc handlerMap = getElementHandlerMap();
47         return handlerMap == null ? null : (ElementHandler) handlerMap.get(namespaceURI + ":" + elementName);
48     }
49
50     public AttributeHandler getAttributeHandler(String JavaDoc namespaceURI, String JavaDoc attrName) {
51         Map JavaDoc handlerMap = getAttributeHandlerMap();
52         return handlerMap == null ? null : (AttributeHandler) attrMap.get(namespaceURI + ":" + attrName);
53     }
54
55     protected Map JavaDoc getAttributeHandlerMap() {
56         return attrMap;
57     }
58
59     protected Map JavaDoc getElementHandlerMap() {
60         return elementMap;
61     }
62
63     protected void applyAttributesAfter() {
64         super.applyAttributesAfter();
65         applyAttribute(Namespace.JDNC, Attributes.ORIENTATION);
66     }
67
68     protected Map JavaDoc registerAttributeHandlers() {
69          Map JavaDoc handlerMap = super.registerAttributeHandlers();
70          if (handlerMap != null) {
71              handlerMap.put(Namespace.JDNC + ":" + Attributes.ORIENTATION,
72                            orientationHandler);
73          }
74          return handlerMap;
75     }
76
77     protected Map JavaDoc registerElementHandlers() {
78         Map JavaDoc handlerMap = super.registerElementHandlers();
79         if (handlerMap != null) {
80             handlerMap.put(Namespace.JDNC + ":" +
81                            ElementTypes.EDITOR.getLocalName(),
82                            editorElementHandler);
83             handlerMap.put(Namespace.JDNC + ":" +
84                            ElementTypes.FORM.getLocalName(),
85                            formElementHandler);
86             handlerMap.put(Namespace.JDNC + ":" +
87                            ElementTypes.TABLE.getLocalName(),
88                            tableElementHandler);
89             handlerMap.put(Namespace.JDNC + ":" +
90                            ElementTypes.TREE_TABLE.getLocalName(),
91                            treeTableElementHandler);
92         }
93         return handlerMap;
94     }
95
96     public static final ElementAssimilator componentAssimilator = new ElementAssimilator() {
97         public void assimilate(Realizable parent, Realizable child) {
98             JSplitPane JavaDoc splitPane = (JSplitPane JavaDoc)parent.getObject();
99             Component JavaDoc component = (Component JavaDoc)child.getObject();
100             int orientation = splitPane.getOrientation();
101             if (orientation == JSplitPane.VERTICAL_SPLIT) {
102                 // splitpane adds silly button children by default, so we
103
// use a client property to track when we add children ourselves
104
if (splitPane.getClientProperty("first") == null) {
105                     splitPane.setTopComponent(component);
106                     splitPane.putClientProperty("first", "true");
107                 } else {
108                     if (splitPane.getClientProperty("second") == null) {
109                         splitPane.setBottomComponent(component);
110                         splitPane.putClientProperty("second", "true");
111                     } else {
112                         /**@todo aim: need better error handling */
113                         System.out.println("splitpane can only have two children");
114                     }
115                 }
116             } else { /* HORIZONTAL_SPLIT */
117                 if (splitPane.getClientProperty("first") == null) {
118                     splitPane.setLeftComponent(component);
119                     splitPane.putClientProperty("first", "true");
120                 } else {
121                     if (splitPane.getClientProperty("second") == null) {
122                         splitPane.setRightComponent(component);
123                         splitPane.putClientProperty("second", "true");
124                     } else {
125                         /**@todo aim: need better error handling */
126                         System.out.println("splitpane can only have two children");
127                     }
128                 }
129             }
130         }
131     };
132
133     // For typecasting entries in a Vector when converted to an array
134

135     protected static final AttributeHandler orientationHandler =
136         new AttributeHandler(Namespace.JDNC, Attributes.ORIENTATION, SplitPaneAttributes.orientationApplier);
137
138     protected static final ElementHandler editorElementHandler =
139         new ElementHandler(ElementTypes.EDITOR,
140                            SplitPaneElement.componentAssimilator);
141
142     protected static final ElementHandler formElementHandler =
143         new ElementHandler(ElementTypes.FORM,
144                            SplitPaneElement.componentAssimilator);
145
146     protected static final ElementHandler tableElementHandler =
147         new ElementHandler(ElementTypes.TABLE,
148                            SplitPaneElement.componentAssimilator);
149
150     protected static final ElementHandler treeTableElementHandler =
151         new ElementHandler(ElementTypes.TREE_TABLE,
152                            SplitPaneElement.componentAssimilator);
153
154
155 }
156
Popular Tags