1 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 ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Vector ; 22 23 import javax.swing.Action ; 24 import javax.swing.JComponent ; 25 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 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 48 public class FormElement extends ComponentElement { 49 private static final Map attrMap = new Hashtable (); 50 private static final Map elementMap = new Hashtable (); 51 52 public static void bindToForm(JNForm form, Object dataModel, String fieldName) throws BindException{ 54 55 } 56 57 public FormElement(Element element, ElementType elementType) { 58 super(element, elementType); 59 } 60 61 protected Map getAttributeHandlerMap() { 62 return attrMap; 63 } 64 65 protected Map getElementHandlerMap() { 66 return elementMap; 67 } 68 69 protected Object instantiate() { 70 return new JNForm(); 71 } 72 73 protected void applyAttributesBefore() { 74 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 registerAttributeHandlers() { 85 Map 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 registerElementHandlers() { 98 Map 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 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 ("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 children = getChildNodes(); 135 String formComponentName = ElementTypes.COMPONENTS.getLocalName(); 136 for(int i = 0;i < children.getLength(); i++) { 137 Node node = children.item(i); 138 if (node.getNodeType() == Node.ELEMENT_NODE) { 139 Realizable child = ElementProxy.getRealizable((Element )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 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 list = (List ) child.getObject(); 162 for (int i = 0; i < list.size(); i++) { 163 Object binding[] = (Object []) list.get(i); 164 createComponentBinding(form, binding); 165 } 166 } 167 168 private void createComponentBinding(JNForm form, Object binding[]) { 169 Object dataModel = binding[0]; 170 String fieldName = (String )binding[1]; 171 if (dataModel == null) { 172 dataModel = form.getClientProperty("formData"); 174 } 175 if (dataModel == null) { 176 throw new RuntimeException ("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); } 188 } 189 catch (BindException e) { 190 throw new RuntimeException ("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 |