1 24 package org.riotfamily.forms; 25 26 import java.io.PrintWriter ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.riotfamily.forms.request.FormRequest; 31 32 33 36 public abstract class AbstractElement implements Element { 37 38 protected Log log = LogFactory.getLog(getClass()); 39 40 private Form form; 41 42 private FormContext formContext; 43 44 private Element parent; 45 46 private String label; 47 48 private String hint; 49 50 private String id; 51 52 private String styleClass; 53 54 private boolean required; 55 56 private boolean enabled = true; 57 58 public String getId() { 59 return id; 60 } 61 62 public void setId(String id) { 63 this.id = id; 64 } 65 66 public String getStyleClass() { 67 return styleClass; 68 } 69 70 public void setStyleClass(String styleClass) { 71 this.styleClass = styleClass; 72 } 73 74 public Form getForm() { 75 return form; 76 } 77 78 public final void setForm(Form form) { 79 this.form = form; 80 afterFormSet(); 81 } 82 83 protected void afterFormSet() { 84 } 85 86 public FormContext getFormContext() { 87 return formContext; 88 } 89 90 public final void setFormContext(FormContext formContext) { 91 this.formContext = formContext; 92 afterFormContextSet(); 93 } 94 95 protected void afterFormContextSet() { 96 } 97 98 public Element getParent() { 99 return parent; 100 } 101 102 public void setParent(Element parent) { 103 this.parent = parent; 104 } 105 106 public String getLabel() { 107 return label; 108 } 109 110 public void setLabel(String label) { 111 this.label = label; 112 } 113 114 public String getHint() { 115 return hint; 116 } 117 118 public void setHint(String hint) { 119 this.hint = hint; 120 } 121 122 public void focus() { 123 if (form == null) { 124 throw new IllegalStateException ( 125 "Element must be registered before calling focus()"); 126 } 127 form.requestFocus(this); 128 } 129 130 public final void render() { 131 if (form == null) { 132 throw new IllegalStateException ("A form must be set!"); 133 } 134 PrintWriter writer = getFormContext().getWriter(); 135 if (writer == null) { 136 throw new IllegalStateException ("A writer must be set!"); 137 } 138 render(writer); 139 } 140 141 public void render(PrintWriter writer) { 142 renderInternal(writer); 143 form.elementRendered(this); 144 } 145 146 protected abstract void renderInternal(PrintWriter writer); 147 148 152 public void processRequest(FormRequest request) { 153 } 154 155 159 public final boolean isEnabled() { 160 if (enabled && (parent != null)) { 161 return parent.isEnabled(); 162 } 163 return enabled; 164 } 165 166 171 public final void setEnabled(boolean enabled) { 172 this.enabled = enabled; 173 if (getFormListener() != null) { 174 getFormListener().elementEnabled(this); 175 } 176 } 177 178 182 public boolean isRequired() { 183 return required; 184 } 185 186 189 public void setRequired(boolean required) { 190 this.required = required; 191 } 192 193 public boolean isCompositeElement() { 194 return false; 195 } 196 197 protected FormListener getFormListener() { 198 if (form != null) { 199 return form.getFormListener(); 200 } 201 return null; 202 } 203 } 204 | Popular Tags |