KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FormElement.java,v 1.2 2005/01/27 13:31:59 kleopatra 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 org.jdesktop.swing.data.DataModel;
11 import org.jdesktop.swing.data.DefaultDataModel;
12 import org.jdesktop.swing.data.DefaultTableModelExt;
13
14 import org.jdesktop.swing.binding.BindException;
15
16 import org.jdesktop.jdnc.JNForm;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 import net.openmarkup.AttributeHandler;
31 import net.openmarkup.ElementAssimilator;
32 import net.openmarkup.ElementHandler;
33 import net.openmarkup.ElementType;
34 import net.openmarkup.Realizable;
35 import net.openmarkup.AssimilatorException;
36
37 import org.jdesktop.jdnc.markup.Attributes;
38 import org.jdesktop.jdnc.markup.ElementTypes;
39 import org.jdesktop.jdnc.markup.Namespace;
40 import org.jdesktop.jdnc.markup.attr.BaseAttribute;
41 import org.jdesktop.jdnc.markup.attr.ComponentAttributes;
42 import org.jdesktop.jdnc.markup.attr.FormAttributes;
43
44 /**
45  *
46  * @author Amy Fowler
47  */

48 public class FormElement extends ComponentElement {
49     private static final Map JavaDoc attrMap = new Hashtable JavaDoc();
50     private static final Map JavaDoc elementMap = new Hashtable JavaDoc();
51
52     // utility method for executing a bind operation
53
public static void bindToForm(JNForm form, Object JavaDoc dataModel, String JavaDoc fieldName) throws BindException{
54
55     }
56
57     public FormElement(Element JavaDoc element, ElementType elementType) {
58         super(element, elementType);
59     }
60
61     protected Map JavaDoc getAttributeHandlerMap() {
62         return attrMap;
63     }
64
65     protected Map JavaDoc getElementHandlerMap() {
66         return elementMap;
67     }
68
69     protected Object JavaDoc instantiate() {
70         return new JNForm();
71     }
72
73     protected void applyAttributesBefore() {
74         // must be applied before any components/bindings are assimilated
75
applyAttribute(Namespace.JDNC, Attributes.DATA);
76     }
77
78     protected void applyAttributesAfter() {
79         super.applyAttributesAfter();
80         applyAttribute(Namespace.JDNC, Attributes.PREFERRED_SIZE);
81         applyAttribute(Namespace.JDNC, Attributes.TRACKS);
82     }
83
84     protected Map JavaDoc registerAttributeHandlers() {
85         Map JavaDoc handlerMap = super.registerAttributeHandlers();
86         if (handlerMap != null) {
87             handlerMap.put(Namespace.JDNC + ":" + Attributes.PREFERRED_SIZE,
88                            preferredSizeHandler);
89             handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA,
90                            dataHandler);
91             handlerMap.put(Namespace.JDNC + ":" + Attributes.TRACKS,
92                            tracksHandler);
93         }
94         return handlerMap;
95     }
96
97     protected Map JavaDoc registerElementHandlers() {
98         Map JavaDoc handlerMap = super.registerElementHandlers();
99         if (handlerMap != null) {
100             elementMap.put(Namespace.JDNC + ":" + ElementTypes.FORM_DATA.getLocalName(),
101                            formDataElementHandler);
102             elementMap.put(Namespace.JDNC + ":" + ElementTypes.DATA.getLocalName(),
103                            dataElementHandler);
104             elementMap.put(Namespace.JDNC + ":" + ElementTypes.COMPONENTS.getLocalName(),
105                            componentsElementHandler);
106         }
107         return handlerMap;
108     }
109
110     public void handleBinding(JNForm form, Object JavaDoc dataModel) {
111         if (!hasComponentElements()) {
112             try {
113                 if (dataModel instanceof DefaultTableModelExt) {
114                     form.bind((DefaultTableModelExt)dataModel);
115                 }
116                 else if (dataModel instanceof DataModel) {
117                     form.bind((DataModel)dataModel);
118                 }
119                 else {
120                     form.bind(dataModel);
121                 }
122             }
123             catch (BindException e) {
124                 throw new RuntimeException JavaDoc("could not bind form to datamodel", e);
125             }
126         }
127         else {
128             form.putClientProperty("formData", dataModel);
129         }
130     }
131
132
133     public boolean hasComponentElements() {
134         NodeList JavaDoc children = getChildNodes();
135         String JavaDoc formComponentName = ElementTypes.COMPONENTS.getLocalName();
136         for(int i = 0;i < children.getLength(); i++) {
137             Node JavaDoc node = children.item(i);
138             if (node.getNodeType() == Node.ELEMENT_NODE) {
139                 Realizable child = ElementProxy.getRealizable((Element JavaDoc)node);
140                 if (child.getLocalName().equals(formComponentName)) {
141                     return true;
142                 }
143             }
144         }
145         return false;
146    }
147
148     public static final ElementAssimilator formDataAssimilator = new
149         ElementAssimilator() {
150         public void assimilate(Realizable parent, Realizable child) {
151             JNForm form = (JNForm)parent.getObject();
152             Object JavaDoc dataModel = child.getObject();
153             ((FormElement)parent).handleBinding(form, dataModel);
154         }
155     };
156
157     public static final ElementAssimilator componentsAssimilator = new
158         ElementAssimilator() {
159         public void assimilate(Realizable parent, Realizable child) {
160             JNForm form = (JNForm)parent.getObject();
161             List JavaDoc list = (List JavaDoc) child.getObject();
162             for (int i = 0; i < list.size(); i++) {
163                 Object JavaDoc binding[] = (Object JavaDoc[]) list.get(i);
164                 createComponentBinding(form, binding);
165             }
166         }
167
168         private void createComponentBinding(JNForm form, Object JavaDoc binding[]) {
169             Object JavaDoc dataModel = binding[0];
170             String JavaDoc fieldName = (String JavaDoc)binding[1];
171             if (dataModel == null) {
172                 // try to get data model stored on JNForm
173
dataModel = form.getClientProperty("formData");
174             }
175             if (dataModel == null) {
176                 throw new RuntimeException JavaDoc("either component or form must specify data attribute");
177             }
178             try {
179                 if (dataModel instanceof DefaultTableModelExt) {
180                     form.bind( (DefaultTableModelExt) dataModel, fieldName);
181                 }
182                 else if (dataModel instanceof DataModel) {
183                     form.bind( (DataModel) dataModel, fieldName);
184                 }
185                 else {
186                     form.bind(dataModel, fieldName); // JavaBean
187
}
188             }
189             catch (BindException e) {
190                 throw new RuntimeException JavaDoc("unable to create binding for \""+ fieldName +
191                                            "\"", e);
192             }
193         }
194     };
195
196
197     protected static final AttributeHandler dataHandler =
198         new AttributeHandler(Namespace.JDNC, Attributes.DATA, FormAttributes.dataApplier);
199
200     protected static final AttributeHandler preferredSizeHandler =
201         new AttributeHandler(Namespace.JDNC, Attributes.PREFERRED_SIZE, ComponentAttributes.preferredSizeApplier);
202
203     protected static final AttributeHandler tracksHandler =
204         new AttributeHandler(Namespace.JDNC, Attributes.TRACKS, FormAttributes.tracksApplier);
205
206     protected static final ElementHandler formDataElementHandler =
207         new ElementHandler(ElementTypes.FORM_DATA, FormElement.formDataAssimilator);
208
209     protected static final ElementHandler dataElementHandler =
210         new ElementHandler(ElementTypes.DATA, FormElement.formDataAssimilator);
211
212     protected static final ElementHandler componentsElementHandler =
213         new ElementHandler(ElementTypes.COMPONENTS, FormElement.componentsAssimilator);
214
215 }
216
Popular Tags