1 15 package org.apache.tapestry.form; 16 17 import org.apache.tapestry.AbstractComponent; 18 import org.apache.tapestry.IForm; 19 import org.apache.tapestry.IMarkupWriter; 20 import org.apache.tapestry.IRequestCycle; 21 import org.apache.tapestry.PageRenderSupport; 22 import org.apache.tapestry.TapestryUtils; 23 import org.apache.tapestry.valid.IValidationDelegate; 24 25 33 public abstract class AbstractFormComponent extends AbstractComponent implements IFormComponent 34 { 35 private static final String SELECTED_ATTRIBUTE_NAME = "org.apache.tapestry.form.SelectedField"; 36 37 public abstract IForm getForm(); 38 39 public abstract void setForm(IForm form); 40 41 public abstract String getName(); 42 43 public abstract void setName(String name); 44 45 49 50 public abstract String getIdParameter(); 51 52 55 56 public abstract void setClientId(String id); 57 58 63 64 protected void renderIdAttribute(IMarkupWriter writer, IRequestCycle cycle) 65 { 66 String rawId = getIdParameter(); 67 68 if (rawId == null) 69 return; 70 71 String id = cycle.getUniqueId(rawId); 72 73 75 setClientId(id); 76 77 writer.attribute("id", id); 78 } 79 80 84 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 85 { 86 IForm form = TapestryUtils.getForm(cycle, this); 87 88 setForm(form); 89 90 if (form.wasPrerendered(writer, this)) 91 return; 92 93 IValidationDelegate delegate = form.getDelegate(); 94 95 delegate.setFormComponent(this); 96 97 setName(form); 98 99 if (form.isRewinding()) 100 { 101 if (!isDisabled()) 102 { 103 rewindFormComponent(writer, cycle); 104 } 105 } 106 else if (!cycle.isRewinding()) 107 { 108 renderFormComponent(writer, cycle); 109 110 if (delegate.isInError()) 111 { 112 select(cycle); 113 } 114 } 115 } 116 117 protected void select(IRequestCycle cycle) 118 { 119 if (cycle.getAttribute(SELECTED_ATTRIBUTE_NAME) == null) 120 { 121 PageRenderSupport pageRenderSupport = TapestryUtils.getOptionalPageRenderSupport(cycle); 122 123 if (pageRenderSupport != null) 124 { 125 String formName = getForm().getName(); 126 String fieldName = getName(); 127 128 String script = "focus(document." + formName + "." + fieldName + ")"; 129 130 pageRenderSupport.addInitializationScript(script); 131 132 134 cycle.setAttribute(SELECTED_ATTRIBUTE_NAME, Boolean.TRUE); 135 } 136 } 137 } 138 139 protected void renderDelegatePrefix(IMarkupWriter writer, IRequestCycle cycle) 140 { 141 getForm().getDelegate().writePrefix(writer, cycle, this, null); 142 } 143 144 protected void renderDelegateAttributes(IMarkupWriter writer, IRequestCycle cycle) 145 { 146 getForm().getDelegate().writeAttributes(writer, cycle, this, null); 147 } 148 149 protected void renderDelegateSuffix(IMarkupWriter writer, IRequestCycle cycle) 150 { 151 getForm().getDelegate().writeSuffix(writer, cycle, this, null); 152 } 153 154 protected void setName(IForm form) 155 { 156 form.getElementId(this); 157 } 158 159 protected abstract void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle); 160 161 protected abstract void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle); 162 } | Popular Tags |