| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BeanEditForm |
|
| 0.0;0 |
| 1 | // Copyright 2007, 2008, 2009 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry5.corelib.components; | |
| 16 | ||
| 17 | import org.apache.tapestry5.*; | |
| 18 | import org.apache.tapestry5.annotations.*; | |
| 19 | import org.apache.tapestry5.beaneditor.BeanModel; | |
| 20 | import org.apache.tapestry5.internal.beaneditor.BeanModelUtils; | |
| 21 | import org.apache.tapestry5.ioc.annotations.Inject; | |
| 22 | import org.apache.tapestry5.services.BeanModelSource; | |
| 23 | ||
| 24 | /** | |
| 25 | * A component that creates an entire form editing the properties of a particular bean. Inspired by <a | |
| 26 | * href="http://www.trailsframework.org/">Trails</a> and <a href="http://beanform.sourceforge.net/">BeanForm</a> (both | |
| 27 | * for Tapestry 4). Generates a simple UI for editing the properties of a JavaBean, with the flavor of UI for each | |
| 28 | * property (text field, checkbox, drop down list) determined from the property type (or by other means, such as an | |
| 29 | * annotation), and the order and validation for the properties determined from annotations on the property's getter and | |
| 30 | * setter methods. | |
| 31 | * <p/> | |
| 32 | * You may add block parameters to the component; when the name matches (case insensitive) the name of a property, then | |
| 33 | * the corresponding Block is renderered, rather than any of the built in property editor blocks. This allows you to | |
| 34 | * override specific properties with your own customized UI, for cases where the default UI is insufficient, or no | |
| 35 | * built-in editor type is appropriate. | |
| 36 | * <p/> | |
| 37 | * BeanEditForm contains a {@link org.apache.tapestry5.corelib.components.Form} component and will trigger all the | |
| 38 | * events of a Form. | |
| 39 | * | |
| 40 | * @see org.apache.tapestry5.beaneditor.BeanModel | |
| 41 | * @see org.apache.tapestry5.services.BeanModelSource | |
| 42 | * @see org.apache.tapestry5.corelib.components.PropertyEditor | |
| 43 | * @see org.apache.tapestry5.beaneditor.DataType | |
| 44 | */ | |
| 45 | @SupportsInformalParameters | |
| 46 | @Events(EventConstants.PREPARE) | |
| 47 | 40 | public class BeanEditForm implements ClientElement, FormValidationControl |
| 48 | { | |
| 49 | ||
| 50 | /** | |
| 51 | * The text label for the submit button of the form, by default "Create/Update". | |
| 52 | */ | |
| 53 | @Parameter(value = "message:submit-label", defaultPrefix = BindingConstants.LITERAL) | |
| 54 | @Property | |
| 55 | private String submitLabel; | |
| 56 | ||
| 57 | /** | |
| 58 | * The object to be edited. This will be read when the component renders and updated when the form for the component | |
| 59 | * is submitted. Typically, the container will listen for a "prepare" event, in order to ensure that a non-null | |
| 60 | * value is ready to be read or updated. Often, the BeanEditForm can create the object as needed (assuming a public, | |
| 61 | * no arguments constructor). The object property defaults to a property with the same name as the component id. | |
| 62 | */ | |
| 63 | @Parameter(required = true, autoconnect = true) | |
| 64 | @Property | |
| 65 | private Object object; | |
| 66 | ||
| 67 | /** | |
| 68 | * A comma-separated list of property names to be retained from the {@link org.apache.tapestry5.beaneditor.BeanModel}. | |
| 69 | * Only these properties will be retained, and the properties will also be reordered. The names are | |
| 70 | * case-insensitive. | |
| 71 | */ | |
| 72 | @SuppressWarnings("unused") | |
| 73 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 74 | private String include; | |
| 75 | ||
| 76 | /** | |
| 77 | * A comma-separated list of property names to be added to the {@link org.apache.tapestry5.beaneditor.BeanModel}. | |
| 78 | */ | |
| 79 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 80 | private String add; | |
| 81 | ||
| 82 | /** | |
| 83 | * A comma-separated list of property names to be removed from the {@link org.apache.tapestry5.beaneditor.BeanModel}. | |
| 84 | * The names are case-insensitive. | |
| 85 | */ | |
| 86 | @SuppressWarnings("unused") | |
| 87 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 88 | private String exclude; | |
| 89 | ||
| 90 | /** | |
| 91 | * A comma-separated list of property names indicating the order in which the properties should be presented. The | |
| 92 | * names are case insensitive. Any properties not indicated in the list will be appended to the end of the display | |
| 93 | * order. | |
| 94 | */ | |
| 95 | @SuppressWarnings("unused") | |
| 96 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 97 | private String reorder; | |
| 98 | ||
| 99 | @Component(parameters = | |
| 100 | "validationId=componentResources.id", publishParameters = "clientValidation,autofocus,zone") | |
| 101 | private Form form; | |
| 102 | ||
| 103 | /** | |
| 104 | * The model that identifies the parameters to be edited, their order, and every other aspect. If not specified, a | |
| 105 | * default bean model will be created from the type of the object bound to the object parameter. | |
| 106 | */ | |
| 107 | @SuppressWarnings("unused") | |
| 108 | @Parameter | |
| 109 | @Property | |
| 110 | private BeanModel model; | |
| 111 | ||
| 112 | @Inject | |
| 113 | private ComponentResources resources; | |
| 114 | ||
| 115 | @Inject | |
| 116 | private BeanModelSource beanModelSource; | |
| 117 | ||
| 118 | ||
| 119 | void onPrepareFromForm() | |
| 120 | { | |
| 121 | 116 | resources.triggerEvent(EventConstants.PREPARE, null, null); |
| 122 | ||
| 123 | 116 | if (model == null) |
| 124 | { | |
| 125 | 116 | Class beanType = resources.getBoundType("object"); |
| 126 | ||
| 127 | 116 | model = beanModelSource.createEditModel(beanType, resources.getContainerMessages()); |
| 128 | } | |
| 129 | ||
| 130 | 116 | BeanModelUtils.modify(model, add, include, exclude, reorder); |
| 131 | 116 | } |
| 132 | ||
| 133 | ||
| 134 | /** | |
| 135 | * Returns the client id of the embedded form. | |
| 136 | */ | |
| 137 | public String getClientId() | |
| 138 | { | |
| 139 | 0 | return form.getClientId(); |
| 140 | } | |
| 141 | ||
| 142 | public void clearErrors() | |
| 143 | { | |
| 144 | 6 | form.clearErrors(); |
| 145 | 6 | } |
| 146 | ||
| 147 | public boolean getHasErrors() | |
| 148 | { | |
| 149 | 0 | return form.getHasErrors(); |
| 150 | } | |
| 151 | ||
| 152 | public boolean isValid() | |
| 153 | { | |
| 154 | 0 | return form.isValid(); |
| 155 | } | |
| 156 | ||
| 157 | public void recordError(Field field, String errorMessage) | |
| 158 | { | |
| 159 | 0 | form.recordError(field, errorMessage); |
| 160 | 0 | } |
| 161 | ||
| 162 | public void recordError(String errorMessage) | |
| 163 | { | |
| 164 | 0 | form.recordError(errorMessage); |
| 165 | 0 | } |
| 166 | } |