1 15 package org.apache.tapestry.form; 16 17 import java.io.IOException ; 18 import java.util.Iterator ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.tapestry.IActionListener; 22 import org.apache.tapestry.IForm; 23 import org.apache.tapestry.IMarkupWriter; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.Tapestry; 26 import org.apache.tapestry.coerce.ValueConverter; 27 import org.apache.tapestry.listener.ListenerInvoker; 28 import org.apache.tapestry.services.DataSqueezer; 29 30 39 40 public abstract class ListEdit extends AbstractFormComponent 41 { 42 45 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 46 { 47 this.render(writer, cycle, getSource()); 48 } 49 50 53 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) 54 { 55 String [] values = cycle.getParameters(getName()); 56 57 this.render(writer, cycle, (Iterator ) getValueConverter().coerceValue(values, Iterator .class)); 58 } 59 60 protected void render(IMarkupWriter writer, IRequestCycle cycle, Iterator i) 61 { 62 66 if (i == null) 67 return; 68 69 int index = 0; 70 71 String element = getElement(); 72 73 boolean indexBound = isParameterBound("index"); 74 75 while (i.hasNext()) 76 { 77 Object value = null; 78 79 if (indexBound) 80 setIndex(index++); 81 82 if (cycle.isRewinding()) 83 value = convertValue((String ) i.next()); 84 else 85 { 86 value = i.next(); 87 writeValue(getForm(), getName(), value); 88 } 89 90 setValue(value); 91 92 getListenerInvoker().invokeListener(getListener(), this, cycle); 93 94 if (element != null) 95 { 96 writer.begin(element); 97 renderInformalParameters(writer, cycle); 98 } 99 100 renderBody(writer, cycle); 101 102 if (element != null) 103 writer.end(); 104 } 105 } 106 107 private void writeValue(IForm form, String name, Object value) 108 { 109 String externalValue; 110 111 try 112 { 113 externalValue = getDataSqueezer().squeeze(value); 114 } 115 catch (IOException ex) 116 { 117 throw new ApplicationRuntimeException(Tapestry.format( 118 "ListEdit.unable-to-convert-value", 119 value), this, null, ex); 120 } 121 122 form.addHiddenValue(name, externalValue); 123 } 124 125 private Object convertValue(String value) 126 { 127 try 128 { 129 return getDataSqueezer().unsqueeze(value); 130 } 131 catch (IOException ex) 132 { 133 throw new ApplicationRuntimeException(Tapestry.format( 134 "ListEdit.unable-to-convert-string", 135 value), this, null, ex); 136 } 137 } 138 139 public abstract String getElement(); 140 141 142 143 public abstract IActionListener getListener(); 144 145 146 147 public boolean isDisabled() 148 { 149 return false; 150 } 151 152 153 154 public abstract Iterator getSource(); 155 156 157 158 public abstract void setValue(Object value); 159 160 161 162 public abstract void setIndex(int index); 163 164 165 166 public abstract DataSqueezer getDataSqueezer(); 167 168 169 170 public abstract ValueConverter getValueConverter(); 171 172 177 178 public abstract ListenerInvoker getListenerInvoker(); 179 } | Popular Tags |