| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BeanDisplay |
|
| 0.0;0 |
| 1 | // Copyright 2007, 2008 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.BindingConstants; | |
| 18 | import org.apache.tapestry5.ComponentResources; | |
| 19 | import org.apache.tapestry5.annotations.Parameter; | |
| 20 | import org.apache.tapestry5.annotations.Property; | |
| 21 | import org.apache.tapestry5.annotations.SupportsInformalParameters; | |
| 22 | import org.apache.tapestry5.beaneditor.BeanModel; | |
| 23 | import org.apache.tapestry5.beaneditor.PropertyModel; | |
| 24 | import org.apache.tapestry5.internal.beaneditor.BeanModelUtils; | |
| 25 | import org.apache.tapestry5.ioc.annotations.Inject; | |
| 26 | import org.apache.tapestry5.services.BeanModelSource; | |
| 27 | ||
| 28 | /** | |
| 29 | * Used to display the properties of a bean, using an underlying {@link BeanModel}. The output definition list: a | |
| 30 | * <dl> element containing a series of <dt>/<dd> pairs. The property label is used as the <dt> | |
| 31 | * and the property value (formatted as per the datatype) is the <dd>. Only properties that have a known data type | |
| 32 | * are displayed. | |
| 33 | * <p/> | |
| 34 | * The property id is used as the class attribute of the <dt> and <dd> element, allowing CSS customization | |
| 35 | * per property. This does not occur when lean is bound to true. | |
| 36 | * <p/> | |
| 37 | * The outer <dl> element has the CSS class "t-beandisplay". | |
| 38 | * | |
| 39 | * @see org.apache.tapestry5.beaneditor.DataType | |
| 40 | * @see BeanModel | |
| 41 | */ | |
| 42 | @SupportsInformalParameters | |
| 43 | 22 | public class BeanDisplay |
| 44 | { | |
| 45 | ||
| 46 | /** | |
| 47 | * The object to be rendered; if not explicitly bound, a default binding to a property whose name matches this | |
| 48 | * component's id will be used. | |
| 49 | */ | |
| 50 | @Parameter(required = true, allowNull = false, autoconnect = true) | |
| 51 | @Property(write = false) | |
| 52 | private Object object; | |
| 53 | ||
| 54 | /** | |
| 55 | * If true, then the CSS class attribute on the <dt> and <dd> elements will be ommitted. | |
| 56 | */ | |
| 57 | @Parameter(value = "false") | |
| 58 | private boolean lean; | |
| 59 | ||
| 60 | /** | |
| 61 | * The model that identifies the parameters to be displayed, their order, and every other aspect. If not specified, | |
| 62 | * a default bean model will be created from the type of the object bound to the object parameter. | |
| 63 | */ | |
| 64 | @Parameter | |
| 65 | @Property(write = false) | |
| 66 | private BeanModel model; | |
| 67 | ||
| 68 | /** | |
| 69 | * A comma-separated list of property names to be retained from the {@link org.apache.tapestry5.beaneditor.BeanModel}. | |
| 70 | * Only these properties will be retained, and the properties will also be reordered. The names are | |
| 71 | * case-insensitive. | |
| 72 | */ | |
| 73 | @SuppressWarnings("unused") | |
| 74 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 75 | private String include; | |
| 76 | ||
| 77 | /** | |
| 78 | * A comma-separated list of property names to be removed from the {@link BeanModel}. The names are | |
| 79 | * case-insensitive. | |
| 80 | */ | |
| 81 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 82 | private String exclude; | |
| 83 | ||
| 84 | /** | |
| 85 | * A comma-separated list of property names indicating the order in which the properties should be presented. The | |
| 86 | * names are case insensitive. Any properties not indicated in the list will be appended to the end of the display | |
| 87 | * order. | |
| 88 | */ | |
| 89 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 90 | private String reorder; | |
| 91 | ||
| 92 | /** | |
| 93 | * Where to search for local overrides of property display blocks as block parameters. Further, the container of the | |
| 94 | * overrides is used as the source for overridden validation messages. This is normally the component itself, but | |
| 95 | * when the component is used within a BeanEditForm, it will be the BeanEditForm's block parameter that will be | |
| 96 | * searched. | |
| 97 | */ | |
| 98 | @Parameter(value = "componentResources") | |
| 99 | @Property(write = false) | |
| 100 | private ComponentResources overrides; | |
| 101 | ||
| 102 | /** | |
| 103 | * A comma-separated list of property names to be added to the {@link org.apache.tapestry5.beaneditor.BeanModel}. | |
| 104 | */ | |
| 105 | @Parameter(defaultPrefix = BindingConstants.LITERAL) | |
| 106 | private String add; | |
| 107 | ||
| 108 | @Inject | |
| 109 | private ComponentResources resources; | |
| 110 | ||
| 111 | @Inject | |
| 112 | private BeanModelSource modelSource; | |
| 113 | ||
| 114 | @Property | |
| 115 | private String propertyName; | |
| 116 | ||
| 117 | void setupRender() | |
| 118 | { | |
| 119 | 24 | if (model == null) model = modelSource.createDisplayModel(object.getClass(), overrides.getContainerMessages()); |
| 120 | ||
| 121 | 22 | BeanModelUtils.modify(model, add, include, exclude, reorder); |
| 122 | 22 | } |
| 123 | ||
| 124 | /** | |
| 125 | * Returns the property model for the current property. | |
| 126 | */ | |
| 127 | public PropertyModel getPropertyModel() | |
| 128 | { | |
| 129 | 352 | return model.get(propertyName); |
| 130 | } | |
| 131 | ||
| 132 | ||
| 133 | public String getPropertyClass() | |
| 134 | { | |
| 135 | 176 | return lean ? null : getPropertyModel().getId(); |
| 136 | } | |
| 137 | } |