| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.corelib.components; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.BindingConstants; |
| 18 | |
import org.apache.tapestry5.ComponentAction; |
| 19 | |
import org.apache.tapestry5.ComponentResources; |
| 20 | |
import org.apache.tapestry5.PropertyOverrides; |
| 21 | |
import org.apache.tapestry5.annotations.Environmental; |
| 22 | |
import org.apache.tapestry5.annotations.Parameter; |
| 23 | |
import org.apache.tapestry5.annotations.Property; |
| 24 | |
import org.apache.tapestry5.annotations.SupportsInformalParameters; |
| 25 | |
import org.apache.tapestry5.beaneditor.BeanModel; |
| 26 | |
import org.apache.tapestry5.corelib.internal.InternalMessages; |
| 27 | |
import org.apache.tapestry5.internal.beaneditor.BeanModelUtils; |
| 28 | |
import org.apache.tapestry5.ioc.annotations.Inject; |
| 29 | |
import org.apache.tapestry5.ioc.internal.util.TapestryException; |
| 30 | |
import org.apache.tapestry5.services.BeanEditContext; |
| 31 | |
import org.apache.tapestry5.services.BeanModelSource; |
| 32 | |
import org.apache.tapestry5.services.Environment; |
| 33 | |
import org.apache.tapestry5.services.FormSupport; |
| 34 | |
|
| 35 | |
import java.lang.annotation.Annotation; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
@SupportsInformalParameters |
| 43 | 62 | public class BeanEditor |
| 44 | |
{ |
| 45 | 232 | public static class Prepare implements ComponentAction<BeanEditor> |
| 46 | |
{ |
| 47 | |
private static final long serialVersionUID = 6273600092955522585L; |
| 48 | |
|
| 49 | |
public void execute(BeanEditor component) |
| 50 | |
{ |
| 51 | 138 | component.doPrepare(); |
| 52 | 138 | } |
| 53 | |
|
| 54 | |
@Override |
| 55 | |
public String toString() |
| 56 | |
{ |
| 57 | 0 | return "BeanEditor.Prepare"; |
| 58 | |
} |
| 59 | |
} |
| 60 | |
|
| 61 | 144 | static class CleanupEnvironment implements ComponentAction<BeanEditor> |
| 62 | |
{ |
| 63 | |
private static final long serialVersionUID = 6867226962459227016L; |
| 64 | |
|
| 65 | |
public void execute(BeanEditor component) |
| 66 | |
{ |
| 67 | 138 | component.cleanupEnvironment(); |
| 68 | 138 | } |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public String toString() |
| 72 | |
{ |
| 73 | 0 | return "BeanEditor.CleanupEnvironment"; |
| 74 | |
} |
| 75 | |
} |
| 76 | |
|
| 77 | 6 | private static final ComponentAction<BeanEditor> CLEANUP_ENVIRONMENT = new CleanupEnvironment(); |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
@Parameter(autoconnect = true) |
| 85 | |
private Object object; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
@Parameter(defaultPrefix = BindingConstants.LITERAL) |
| 93 | |
private String include; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
@Parameter(defaultPrefix = BindingConstants.LITERAL) |
| 100 | |
private String exclude; |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
@Parameter(defaultPrefix = BindingConstants.LITERAL) |
| 108 | |
private String reorder; |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
@Parameter(defaultPrefix = BindingConstants.LITERAL) |
| 114 | |
private String add; |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
@Parameter |
| 122 | |
@Property(write = false) |
| 123 | |
private BeanModel model; |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
@Parameter(value = "this", allowNull = false) |
| 132 | |
@Property(write = false) |
| 133 | |
private PropertyOverrides overrides; |
| 134 | |
|
| 135 | |
@Inject |
| 136 | |
private BeanModelSource modelSource; |
| 137 | |
|
| 138 | |
@Inject |
| 139 | |
private ComponentResources resources; |
| 140 | |
|
| 141 | |
@Inject |
| 142 | |
private Environment environment; |
| 143 | |
|
| 144 | |
@Environmental |
| 145 | |
private FormSupport formSupport; |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
@Property |
| 150 | |
private String propertyName; |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
private Object cachedObject; |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public Object getObject() |
| 160 | |
{ |
| 161 | 312 | return cachedObject; |
| 162 | |
} |
| 163 | |
|
| 164 | |
void setupRender() |
| 165 | |
{ |
| 166 | 94 | formSupport.storeAndExecute(this, new Prepare()); |
| 167 | 94 | } |
| 168 | |
|
| 169 | |
void cleanupRender() |
| 170 | |
{ |
| 171 | 94 | formSupport.storeAndExecute(this, CLEANUP_ENVIRONMENT); |
| 172 | 94 | } |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
void doPrepare() |
| 179 | |
{ |
| 180 | 144 | if (model == null) |
| 181 | |
{ |
| 182 | 28 | Class type = resources.getBoundType("object"); |
| 183 | 28 | model = modelSource.createEditModel(type, overrides.getOverrideMessages()); |
| 184 | |
} |
| 185 | |
|
| 186 | 144 | BeanModelUtils.modify(model, add, include, exclude, reorder); |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | 144 | if (object == null) |
| 193 | |
{ |
| 194 | |
try |
| 195 | |
{ |
| 196 | 16 | object = model.newInstance(); |
| 197 | |
} |
| 198 | 2 | catch (Exception ex) |
| 199 | |
{ |
| 200 | 2 | String message = InternalMessages.failureInstantiatingObject(model.getBeanType(), |
| 201 | |
resources.getCompleteId(), |
| 202 | |
ex); |
| 203 | 2 | throw new TapestryException(message, resources.getLocation(), ex); |
| 204 | 14 | } |
| 205 | |
} |
| 206 | |
|
| 207 | 142 | BeanEditContext context = new BeanEditContext() |
| 208 | |
{ |
| 209 | |
public Class<?> getBeanClass() |
| 210 | |
{ |
| 211 | 4 | return model.getBeanType(); |
| 212 | |
} |
| 213 | |
|
| 214 | 142 | public <T extends Annotation> T getAnnotation(Class<T> type) |
| 215 | |
{ |
| 216 | 0 | return getBeanClass().getAnnotation(type); |
| 217 | |
} |
| 218 | |
}; |
| 219 | |
|
| 220 | |
|
| 221 | 142 | cachedObject = object; |
| 222 | |
|
| 223 | 142 | environment.push(BeanEditContext.class, context); |
| 224 | 142 | } |
| 225 | |
|
| 226 | |
void cleanupEnvironment() |
| 227 | |
{ |
| 228 | 140 | environment.pop(BeanEditContext.class); |
| 229 | 140 | } |
| 230 | |
|
| 231 | |
|
| 232 | |
void inject(ComponentResources resources, PropertyOverrides overrides, BeanModelSource source, |
| 233 | |
Environment environment) |
| 234 | |
{ |
| 235 | 8 | this.resources = resources; |
| 236 | 8 | this.overrides = overrides; |
| 237 | 8 | this.environment = environment; |
| 238 | 8 | modelSource = source; |
| 239 | 8 | } |
| 240 | |
} |