001    // Copyright 2007, 2008, 2011 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.corelib.pages;
016    
017    import org.apache.tapestry5.FieldTranslator;
018    import org.apache.tapestry5.FieldValidator;
019    import org.apache.tapestry5.SelectModel;
020    import org.apache.tapestry5.ValueEncoder;
021    import org.apache.tapestry5.annotations.Component;
022    import org.apache.tapestry5.annotations.Environmental;
023    import org.apache.tapestry5.corelib.components.*;
024    import org.apache.tapestry5.ioc.annotations.Inject;
025    import org.apache.tapestry5.ioc.services.TypeCoercer;
026    import org.apache.tapestry5.services.BeanBlockContribution;
027    import org.apache.tapestry5.services.BeanBlockSource;
028    import org.apache.tapestry5.services.PropertyEditContext;
029    import org.apache.tapestry5.util.EnumSelectModel;
030    import org.apache.tapestry5.util.EnumValueEncoder;
031    
032    /**
033     * A page that exists to contain blocks used to edit different types of properties. The blocks on this page are
034     * contributed into the {@link BeanBlockSource} service configuration.
035     *
036     * @see BeanBlockContribution
037     * @see BeanEditForm
038     */
039    public class PropertyEditBlocks
040    {
041        @Environmental
042        private PropertyEditContext context;
043    
044        @Component(
045                parameters = {"value=context.propertyValue", "label=prop:context.label",
046                        "translate=prop:textFieldTranslator", "validate=prop:textFieldValidator",
047                        "clientId=prop:context.propertyId", "annotationProvider=context"})
048        private TextField textField;
049    
050        @Component(
051                parameters = {"value=context.propertyValue", "label=prop:context.label",
052                        "translate=prop:numberFieldTranslator", "validate=prop:numberFieldValidator",
053                        "clientId=prop:context.propertyId", "annotationProvider=context"})
054        private TextField numberField;
055    
056    
057        @Component(
058                parameters = {"value=context.propertyValue", "label=prop:context.label", "encoder=valueEncoderForProperty",
059                        "model=selectModelForProperty", "validate=prop:selectValidator",
060                        "clientId=prop:context.propertyId"})
061        private Select select;
062    
063        @SuppressWarnings("unused")
064        @Component(
065                parameters = {"value=context.propertyValue", "label=prop:context.label",
066                        "clientId=prop:context.propertyId"})
067        private Checkbox checkboxField;
068    
069        @SuppressWarnings("unused")
070        @Component(
071                parameters = {"value=context.propertyValue", "label=prop:context.label", "clientId=prop:context.propertyid",
072                        "validate=prop:dateFieldValidator"})
073        private DateField dateField;
074    
075        @SuppressWarnings("unused")
076        @Component(
077                parameters = {"value=context.propertyValue", "label=prop:context.label", "clientId=prop:context.propertyid",
078                        "validate=prop:calendarFieldValidator"})
079        private DateField calendarField;
080    
081        @Component(
082                parameters = {"value=context.propertyValue", "label=prop:context.label",
083                        "translate=prop:passwordFieldTranslator", "validate=prop:passwordFieldValidator",
084                        "clientId=prop:context.propertyId", "annotationProvider=context"})
085        private PasswordField passwordField;
086    
087        @Component(
088                parameters = {"value=context.propertyValue", "label=prop:context.label",
089                        "translate=prop:textAreaTranslator",
090                        "validate=prop:textAreaValidator", "clientId=prop:context.propertyId",
091                        "annotationProvider=context"})
092        private TextArea textArea;
093    
094    
095        @Inject
096        private TypeCoercer typeCoercer;
097    
098        public PropertyEditContext getContext()
099        {
100            return context;
101        }
102    
103    
104        public FieldTranslator getTextFieldTranslator()
105        {
106            return context.getTranslator(textField);
107        }
108    
109        public FieldValidator getTextFieldValidator()
110        {
111            return context.getValidator(textField);
112        }
113    
114        public FieldTranslator getNumberFieldTranslator()
115        {
116            return context.getTranslator(numberField);
117        }
118    
119        public FieldValidator getNumberFieldValidator()
120        {
121            return context.getValidator(numberField);
122        }
123    
124        public FieldTranslator getPasswordFieldTranslator()
125        {
126            return context.getTranslator(passwordField);
127        }
128    
129        public FieldValidator getPasswordFieldValidator()
130        {
131            return context.getValidator(passwordField);
132        }
133    
134        public FieldTranslator getTextAreaTranslator()
135        {
136            return context.getTranslator(textArea);
137        }
138    
139        public FieldValidator getTextAreaValidator()
140        {
141            return context.getValidator(textArea);
142        }
143    
144    
145        public FieldValidator getDateFieldValidator()
146        {
147            return context.getValidator(dateField);
148        }
149    
150        public FieldValidator getCalendarFieldValidator()
151        {
152            return context.getValidator(calendarField);
153        }
154    
155        public FieldValidator getSelectValidator()
156        {
157            return context.getValidator(select);
158        }
159    
160        /**
161         * Provide a value encoder for an enum type.
162         */
163        @SuppressWarnings("unchecked")
164        public ValueEncoder getValueEncoderForProperty()
165        {
166            return new EnumValueEncoder(typeCoercer, context.getPropertyType());
167        }
168    
169        /**
170         * Provide a select mode for an enum type.
171         */
172        @SuppressWarnings("unchecked")
173        public SelectModel getSelectModelForProperty()
174        {
175            return new EnumSelectModel(context.getPropertyType(), context.getContainerMessages());
176        }
177    }