001    // Copyright 2007, 2008, 2010, 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.internal.beaneditor;
016    
017    import org.apache.tapestry5.PropertyConduit;
018    import org.apache.tapestry5.beaneditor.BeanModel;
019    import org.apache.tapestry5.beaneditor.PropertyModel;
020    import org.apache.tapestry5.internal.TapestryInternalUtils;
021    import org.apache.tapestry5.ioc.Messages;
022    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
023    import org.apache.tapestry5.plastic.PlasticUtils;
024    
025    import java.lang.annotation.Annotation;
026    
027    @SuppressWarnings("all")
028    public class PropertyModelImpl implements PropertyModel
029    {
030        private final BeanModel model;
031    
032        private final String name;
033    
034        private final PropertyConduit conduit;
035    
036        private final String id;
037    
038        private String label;
039    
040        private String dataType;
041    
042        private boolean sortable;
043    
044        public PropertyModelImpl(BeanModel model, String name, PropertyConduit conduit, Messages messages)
045        {
046            this.model = model;
047            this.name = name;
048            this.conduit = conduit;
049    
050            id = TapestryInternalUtils.extractIdFromPropertyExpression(name);
051    
052            label = TapestryInternalUtils.defaultLabel(id, messages, name);
053    
054            // Primitive types need to be converted to wrapper types before checking to see
055            // if they are sortable.
056    
057            Class wrapperType = PlasticUtils.toWrapperType(getPropertyType());
058    
059            sortable = Comparable.class.isAssignableFrom(wrapperType);
060        }
061    
062        public String getId()
063        {
064            return id;
065        }
066    
067        public Class getPropertyType()
068        {
069            return conduit == null ? Object.class : conduit.getPropertyType();
070        }
071    
072        public PropertyConduit getConduit()
073        {
074            return conduit;
075        }
076    
077        public PropertyModel label(String label)
078        {
079            assert InternalUtils.isNonBlank(label);
080            this.label = label;
081    
082            return this;
083        }
084    
085        public String getLabel()
086        {
087            return label;
088        }
089    
090        public String getPropertyName()
091        {
092            return name;
093        }
094    
095        public BeanModel model()
096        {
097            return model;
098        }
099    
100        public PropertyModel dataType(String dataType)
101        {
102            this.dataType = dataType;
103    
104            return this;
105        }
106    
107        public String getDataType()
108        {
109            return dataType;
110        }
111    
112        public boolean isSortable()
113        {
114            return sortable;
115        }
116    
117        public PropertyModel sortable(boolean sortable)
118        {
119            this.sortable = sortable;
120    
121            return this;
122        }
123    
124        public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
125        {
126            return conduit == null ? null : conduit.getAnnotation(annotationClass);
127        }
128    }