1 15 package org.apache.tapestry.components; 16 17 import java.io.IOException ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.HiveMind; 21 import org.apache.tapestry.IActionListener; 22 import org.apache.tapestry.IBinding; 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.AbstractFormComponent; 29 import org.apache.tapestry.services.DataSqueezer; 30 31 34 public abstract class IfBean extends AbstractFormComponent 35 { 36 public final static String IF_VALUE_ATTRIBUTE = "org.mb.tapestry.base.IfValue"; 37 38 public abstract IBinding getConditionValueBinding(); 39 40 public abstract boolean getCondition(); 41 public abstract boolean getVolatile(); 42 public abstract String getElement(); 43 public abstract IActionListener getListener(); 44 45 private boolean _rendering = false; 46 private boolean _conditionValue; 47 48 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 49 { 50 boolean cycleRewinding = cycle.isRewinding(); 51 52 IForm form = (IForm) cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE); 54 55 if (cycleRewinding && form != null && !cycleRewinding) 58 return; 59 60 _conditionValue = evaluateCondition(cycle, form, cycleRewinding); 62 _rendering = true; 63 64 try { 65 IActionListener listener = getListener(); 67 if (listener != null) 68 listener.actionTriggered(this, cycle); 69 70 if (_conditionValue) 72 { 73 String element = getElement(); 74 75 boolean render = !cycleRewinding && HiveMind.isNonBlank(element); 76 77 if (render) 78 { 79 writer.begin(element); 80 renderInformalParameters(writer, cycle); 81 } 82 83 renderBody(writer, cycle); 84 85 if (render) 86 writer.end(element); 87 } 88 } 89 finally { 90 _rendering = false; 91 } 92 93 cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean (_conditionValue)); 94 } 95 96 protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding) 97 { 98 boolean condition; 99 100 if (form == null || getVolatile()) { 101 condition = getCondition(); 102 } 103 else { 104 String name = form.getElementId(this); 106 107 if (!cycleRewinding) 108 { 109 condition = getCondition(); 110 writeValue(form, name, condition); 111 } 112 else 113 { 114 condition = readValue(cycle, name); 115 } 116 } 117 118 IBinding conditionValueBinding = getConditionValueBinding(); 120 if (conditionValueBinding != null) 121 conditionValueBinding.setObject(new Boolean (condition)); 122 123 return condition; 124 } 125 126 private void writeValue(IForm form, String name, boolean value) 127 { 128 String externalValue; 129 130 Object booleanValue = new Boolean (value); 131 try 132 { 133 externalValue = getDataSqueezer().squeeze(booleanValue); 134 } 135 catch (IOException ex) 136 { 137 throw new ApplicationRuntimeException( 138 Tapestry.format("If.unable-to-convert-value", booleanValue), 139 this, 140 null, 141 ex); 142 } 143 144 form.addHiddenValue(name, externalValue); 145 } 146 147 private boolean readValue(IRequestCycle cycle, String name) 148 { 149 String submittedValue = cycle.getParameter(name); 150 151 try 152 { 153 Object valueObject = getDataSqueezer().unsqueeze(submittedValue); 154 if (!(valueObject instanceof Boolean )) 155 throw new ApplicationRuntimeException( 156 Tapestry.format("If.invalid-condition-type", submittedValue)); 157 158 return ((Boolean ) valueObject).booleanValue(); 159 } 160 catch (IOException ex) 161 { 162 throw new ApplicationRuntimeException( 163 Tapestry.format("If.unable-to-convert-string", submittedValue), 164 this, 165 null, 166 ex); 167 } 168 } 169 170 public abstract DataSqueezer getDataSqueezer(); 171 172 173 public boolean isDisabled() 174 { 175 return false; 176 } 177 178 182 public boolean getConditionValue() 183 { 184 if (!_rendering) 185 throw Tapestry.createRenderOnlyPropertyException(this, "conditionValue"); 186 187 return _conditionValue; 188 } 189 190 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) { } 192 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) { } 193 194 } 195 | Popular Tags |