1 15 package org.apache.tapestry.contrib.form; 16 17 import java.io.IOException ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.HiveMind; 21 import org.apache.tapestry.AbstractComponent; 22 import org.apache.tapestry.IActionListener; 23 import org.apache.tapestry.IForm; 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.Tapestry; 27 import org.apache.tapestry.TapestryUtils; 28 import org.apache.tapestry.form.IFormComponent; 29 import org.apache.tapestry.listener.ListenerInvoker; 30 import org.apache.tapestry.services.DataSqueezer; 31 32 42 43 public abstract class FormConditional extends AbstractComponent implements IFormComponent 44 { 45 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 46 { 47 IForm form = TapestryUtils.getForm(cycle, this); 48 49 boolean cycleRewinding = cycle.isRewinding(); 50 51 54 if (cycleRewinding && !form.isRewinding()) 55 return; 56 57 String name = form.getElementId(this); 58 59 boolean condition = getCondition(cycle, form, name); 60 61 getListenerInvoker().invokeListener(getListener(), this, cycle); 62 63 if (condition) 65 { 66 String element = getElement(); 67 68 boolean render = !cycleRewinding && HiveMind.isNonBlank(element); 69 70 if (render) 71 { 72 writer.begin(element); 73 renderInformalParameters(writer, cycle); 74 } 75 76 renderBody(writer, cycle); 77 78 if (render) 79 writer.end(element); 80 } 81 } 82 83 private boolean getCondition(IRequestCycle cycle, IForm form, String name) 84 { 85 boolean condition; 86 87 if (!cycle.isRewinding()) 88 { 89 condition = getCondition(); 90 writeValue(form, name, condition); 91 } 92 else 93 { 94 String submittedCondition = cycle.getParameter(name); 95 condition = convertValue(submittedCondition); 96 } 97 98 if (isParameterBound("conditionValue")) 99 setConditionValue(condition); 100 101 return condition; 102 } 103 104 private void writeValue(IForm form, String name, boolean value) 105 { 106 String externalValue; 107 108 try 109 { 110 externalValue = getDataSqueezer().squeeze(value ? Boolean.TRUE : Boolean.FALSE); 111 } 112 catch (IOException ex) 113 { 114 throw new ApplicationRuntimeException(Tapestry.format( 115 "FormConditional.unable-to-convert-value", 116 Boolean.toString(value)), this, null, ex); 117 } 118 119 form.addHiddenValue(name, externalValue); 120 } 121 122 private boolean convertValue(String value) 123 { 124 try 125 { 126 Boolean b = (Boolean ) getDataSqueezer().unsqueeze(value); 127 return b.booleanValue(); 128 } 129 catch (IOException ex) 130 { 131 throw new ApplicationRuntimeException(Tapestry.format( 132 "FormConditional.unable-to-convert-string", 133 value), this, null, ex); 134 } 135 } 136 137 public abstract DataSqueezer getDataSqueezer(); 138 139 141 public boolean isDisabled() 142 { 143 return false; 144 } 145 146 public abstract boolean getCondition(); 147 148 public abstract void setConditionValue(boolean value); 149 150 public abstract String getElement(); 151 152 public abstract IActionListener getListener(); 153 154 159 160 public abstract ListenerInvoker getListenerInvoker(); 161 162 } | Popular Tags |