001: // Copyright 2007 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.tapestry.internal.beaneditor;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
019:
020: import java.util.Arrays;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.tapestry.PropertyConduit;
027: import org.apache.tapestry.beaneditor.BeanModel;
028: import org.apache.tapestry.beaneditor.PropertyModel;
029: import org.apache.tapestry.internal.services.CoercingPropertyConduitWrapper;
030: import org.apache.tapestry.ioc.Messages;
031: import org.apache.tapestry.ioc.services.TypeCoercer;
032: import org.apache.tapestry.services.PropertyConduitSource;
033:
034: public class BeanModelImpl implements BeanModel,
035: Comparator<PropertyModel> {
036: private final Class _beanType;
037:
038: private final PropertyConduitSource _propertyConduitSource;
039:
040: private final TypeCoercer _typeCoercer;
041:
042: private final Messages _messages;
043:
044: private final Map<String, PropertyModel> _properties = newCaseInsensitiveMap();
045:
046: public BeanModelImpl(Class beanType,
047: PropertyConduitSource propertyConduitSource,
048: TypeCoercer typeCoercer, Messages messages) {
049: _beanType = beanType;
050: _propertyConduitSource = propertyConduitSource;
051: _typeCoercer = typeCoercer;
052: _messages = messages;
053: }
054:
055: public Class getBeanType() {
056: return _beanType;
057: }
058:
059: public PropertyModel add(String propertyName) {
060: if (_properties.containsKey(propertyName))
061: throw new RuntimeException(BeanEditorMessages
062: .duplicatePropertyName(_beanType, propertyName));
063:
064: PropertyConduit conduit = createConduit(propertyName);
065:
066: return add(propertyName, conduit);
067: }
068:
069: public PropertyModel add(String propertyName,
070: PropertyConduit conduit) {
071: PropertyModel propertyModel = new PropertyModelImpl(this ,
072: propertyName, conduit, _messages);
073:
074: _properties.put(propertyName, propertyModel);
075:
076: return propertyModel;
077: }
078:
079: private CoercingPropertyConduitWrapper createConduit(
080: String propertyName) {
081: return new CoercingPropertyConduitWrapper(
082: _propertyConduitSource.create(_beanType, propertyName),
083: _typeCoercer);
084: }
085:
086: public PropertyModel get(String propertyName) {
087: PropertyModel propertyModel = _properties.get(propertyName);
088:
089: if (propertyModel == null)
090: throw new RuntimeException(BeanEditorMessages
091: .unknownProperty(_beanType, propertyName,
092: _properties.keySet()));
093:
094: return propertyModel;
095: }
096:
097: public List<String> getPropertyNames() {
098: List<PropertyModel> propertyModels = newList(_properties
099: .values());
100:
101: // Sort the list of models by their order property.
102:
103: Collections.sort(propertyModels, this );
104:
105: List<String> result = newList();
106:
107: for (PropertyModel propertyModel : propertyModels)
108: result.add(propertyModel.getPropertyName());
109:
110: return result;
111: }
112:
113: public int compare(PropertyModel o1, PropertyModel o2) {
114: int result = o1.getOrder() - o2.getOrder();
115:
116: if (result == 0)
117: result = o1.getPropertyName().compareTo(
118: o2.getPropertyName());
119:
120: return result;
121: }
122:
123: public BeanModel remove(String... propertyNames) {
124: _properties.keySet().removeAll(Arrays.asList(propertyNames));
125:
126: return this;
127: }
128:
129: }
|