1 18 package net.sf.uitags.tag.formGuide; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 26 import net.sf.uitags.tag.AbstractUiTag; 27 import net.sf.uitags.tagutil.ScopedIdGenerator; 28 import net.sf.uitags.util.Template; 29 import net.sf.uitags.util.UiString; 30 31 37 public class FormGuideTag extends AbstractUiTag { 38 42 45 private static final long serialVersionUID = 100L; 46 47 51 private static final String TAG_INSTANCE_ID_KEY = 52 "net.sf.uitags.FormGuideTag.instanceId"; 53 54 55 56 60 63 private List observedWidgets; 64 67 private List doTasks; 68 71 private List undoTasks; 72 75 private String listener; 76 77 78 82 85 public FormGuideTag() { 86 super(); 87 } 88 89 90 94 99 public void setListener(String val) { 100 this.listener = val; 101 } 102 103 104 108 114 public int doStartTag() throws JspException { 115 this.observedWidgets = new ArrayList (); 117 this.doTasks = new ArrayList (); 118 this.undoTasks = new ArrayList (); 119 120 makeVisibleToChildren(); 121 return EVAL_BODY_INCLUDE; 122 } 123 124 130 public int doEndTag() throws JspException { 131 long instanceId = ScopedIdGenerator.nextId( 133 PageContext.REQUEST_SCOPE, TAG_INSTANCE_ID_KEY, this.pageContext); 134 135 Template template = Template.forName(Template.FORM_GUIDE); 136 template.map("instanceId", new Long (instanceId)); 137 template.map("observedWidgets", this.observedWidgets); 138 template.map("doTasks", this.doTasks); 139 template.map("undoTasks", this.undoTasks); 140 template.map("listener", this.listener); 141 142 143 println(template.evalToString()); 144 145 makeInvisibleFromChildren(); 146 return EVAL_PAGE; 147 } 148 149 155 void addObservedElementName(String elementName, String value) { 156 this.observedWidgets.add(new ObservedWidget(null, elementName, value)); 157 } 158 159 165 void addObservedElementId(String elementId, String value) { 166 this.observedWidgets.add(new ObservedWidget(elementId, null, value)); 167 } 168 169 176 void addJavascriptCallback( 177 String doCallback, String undoCallback, 178 String elementId, String elementName) { 179 180 String idParam = getJsElementParameter(elementId); 181 String nameParam = getJsElementParameter(elementName); 182 183 this.doTasks.add(UiString.simpleConstruct( 184 "{0}(domEvent, {1}, {2});", 185 new String [] { doCallback, idParam, nameParam })); 186 187 this.undoTasks.add(UiString.simpleConstruct( 188 "{0}(domEvent, {1}, {2});", 189 new String [] { undoCallback, idParam, nameParam })); 190 } 191 192 private String getJsElementParameter(String value) { 193 if (value == null) { 194 return "null"; 195 } 196 return '"' + value + '"'; 197 } 198 199 200 204 208 public static final class ObservedWidget { 209 212 private String id; 213 216 private String name; 217 220 private String value; 221 222 228 private ObservedWidget(String id, String name, String value) { 229 this.id = id; 230 this.name = name; 231 this.value = value; 232 } 233 234 239 public String getId() { 240 return this.id; 241 } 242 243 248 public String getName() { 249 return this.name; 250 } 251 252 257 public String getValueInDoubleQuotesOrNull() { 258 return (this.value == null)? "null" : "\"" + this.value + "\""; 259 } 260 } 261 } 262 | Popular Tags |