You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2007/03/07 20:58:36 UTC

svn commit: r515722 - in /tapestry/tapestry5/tapestry-core/trunk/src: main/java/org/apache/tapestry/beaneditor/ main/java/org/apache/tapestry/corelib/components/ main/java/org/apache/tapestry/internal/beaneditor/ main/java/org/apache/tapestry/internal/...

Author: hlship
Date: Wed Mar  7 11:58:35 2007
New Revision: 515722

URL: http://svn.apache.org/viewvc?view=rev&rev=515722
Log:
TAPESTRY-1318: Rename PropertyModel.getEditorType() to PropertyModel.getDataType() (since it applies to presentation of data as well as editting of data).
Refactor BeanModelSource to use an external service, DataTypeAnalyzer, to determine the data type for a particular property.
Introduce a DefaultDataTypeAnalyzer service (based on propety type) as the tail end of a chain of command, DataTypeAnalyzer (for extensibility).

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/DefaultDataTypeAnalyzer.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/DataTypeAnalyzer.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/beaneditor/PropertyModel.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/BeanEditForm.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridCell.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/beaneditor/PropertyModelImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/BeanModelSourceImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/ValidationConstraintGenerator.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/BeanModelSourceImplTest.java

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/beaneditor/PropertyModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/beaneditor/PropertyModel.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/beaneditor/PropertyModel.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/beaneditor/PropertyModel.java Wed Mar  7 11:58:35 2007
@@ -40,18 +40,18 @@
     Class getPropertyType();
 
     /**
-     * Returns a logical name for the type of UI needed to edit the property. This is initially
-     * determined from the property type.
+     * Returns a logical name for the type of UI needed to view or edit the property. This is
+     * initially determined from the property type.
      */
-    String getEditorType();
+    String getDataType();
 
     /**
-     * Changes the editor type for the property.
+     * Changes the data type for the property.
      * 
-     * @param editorType
+     * @param dataType
      * @return the property edit model, for further changes
      */
-    PropertyModel editorType(String editorType);
+    PropertyModel dataType(String dataType);
 
     /**
      * Returns an object used to read or update the property. For virtual properties (properties

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/BeanEditForm.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/BeanEditForm.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/BeanEditForm.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/BeanEditForm.java Wed Mar  7 11:58:35 2007
@@ -167,30 +167,30 @@
             return;
         }
 
-        String editorType = _propertyEditModel.getEditorType();
+        String dataType = _propertyEditModel.getDataType();
 
-        if (editorType.equals("text"))
+        if (dataType.equals("text"))
         {
             _blockForProperty = _text;
             _fieldForProperty = _textField;
             return;
         }
 
-        if (editorType.equals("enum"))
+        if (dataType.equals("enum"))
         {
             _blockForProperty = _enum;
             _fieldForProperty = _select;
             return;
         }
 
-        if (editorType.equals("checkbox"))
+        if (dataType.equals("checkbox"))
         {
             _blockForProperty = _checkbox;
             _fieldForProperty = _checkboxField;
             return;
         }
 
-        throw new IllegalArgumentException(_messages.format("no-editor", editorType, propertyName));
+        throw new IllegalArgumentException(_messages.format("no-editor", dataType, propertyName));
     }
 
     boolean onPrepareFromForm()

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridCell.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridCell.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridCell.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/corelib/components/GridCell.java Wed Mar  7 11:58:35 2007
@@ -56,7 +56,7 @@
         if (override != null)
             return override;
 
-        Block builtin = _gridCellResources.findBlock(_model.getEditorType());
+        Block builtin = _gridCellResources.findBlock(_model.getDataType());
 
         if (builtin != null)
             return builtin;

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/beaneditor/PropertyModelImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/beaneditor/PropertyModelImpl.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/beaneditor/PropertyModelImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/beaneditor/PropertyModelImpl.java Wed Mar  7 11:58:35 2007
@@ -37,7 +37,7 @@
 
     private int _order;
 
-    private String _editorType;
+    private String _dataType;
 
     private boolean _sortable;
 
@@ -112,16 +112,16 @@
         return _model;
     }
 
-    public PropertyModel editorType(String editorType)
+    public PropertyModel dataType(String dataType)
     {
-        _editorType = editorType;
+        _dataType = dataType;
 
         return this;
     }
 
-    public String getEditorType()
+    public String getDataType()
     {
-        return _editorType;
+        return _dataType;
     }
 
     public boolean isSortable()

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/BeanModelSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/BeanModelSourceImpl.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/BeanModelSourceImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/BeanModelSourceImpl.java Wed Mar  7 11:58:35 2007
@@ -18,12 +18,10 @@
 import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
 
 import java.util.List;
-import java.util.Map;
 
 import org.apache.tapestry.ComponentResources;
 import org.apache.tapestry.beaneditor.BeanModel;
 import org.apache.tapestry.beaneditor.NonVisual;
-import org.apache.tapestry.events.InvalidationListener;
 import org.apache.tapestry.internal.TapestryInternalUtils;
 import org.apache.tapestry.internal.beaneditor.BeanModelImpl;
 import org.apache.tapestry.ioc.Messages;
@@ -32,11 +30,11 @@
 import org.apache.tapestry.ioc.services.PropertyAccess;
 import org.apache.tapestry.ioc.services.PropertyAdapter;
 import org.apache.tapestry.ioc.services.TypeCoercer;
-import org.apache.tapestry.ioc.util.StrategyRegistry;
 import org.apache.tapestry.services.BeanModelSource;
+import org.apache.tapestry.services.DataTypeAnalyzer;
 import org.apache.tapestry.services.PropertyConduitSource;
 
-public class BeanModelSourceImpl implements BeanModelSource, InvalidationListener
+public class BeanModelSourceImpl implements BeanModelSource
 {
     private final TypeCoercer _typeCoercer;
 
@@ -46,23 +44,17 @@
 
     private final ClassFactory _classFactory;
 
-    private final StrategyRegistry<String> _registry;
+    private final DataTypeAnalyzer _dataTypeAnalyzer;
 
-    public BeanModelSourceImpl(final TypeCoercer typeCoercer, final PropertyAccess propertyAccess,
-            final PropertyConduitSource propertyConduitSource, ClassFactory classFactory,
-            Map<Class, String> configuration)
+    public BeanModelSourceImpl(TypeCoercer typeCoercer, PropertyAccess propertyAccess,
+            PropertyConduitSource propertyConduitSource, ClassFactory classFactory,
+            DataTypeAnalyzer dataTypeAnalyzer)
     {
         _typeCoercer = typeCoercer;
         _propertyAccess = propertyAccess;
         _propertyConduitSource = propertyConduitSource;
         _classFactory = classFactory;
-
-        _registry = StrategyRegistry.newInstance(String.class, configuration);
-    }
-
-    public void objectWasInvalidated()
-    {
-        _registry.clearCache();
+        _dataTypeAnalyzer = dataTypeAnalyzer;
     }
 
     public BeanModel create(Class beanClass, boolean filterReadOnlyProperties,
@@ -93,14 +85,14 @@
             if (filterReadOnlyProperties && !pa.isUpdate())
                 continue;
 
-            String editorType = _registry.get(pa.getType());
+            String dataType = _dataTypeAnalyzer.identifyDataType(pa);
 
             // If an unregistered type, then ignore the property.
 
-            if (editorType.equals(""))
+            if (dataType == null)
                 continue;
 
-            model.add(propertyName).editorType(editorType);
+            model.add(propertyName).dataType(dataType);
 
             propertyNames.add(propertyName);
         }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/DefaultDataTypeAnalyzer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/DefaultDataTypeAnalyzer.java?view=auto&rev=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/DefaultDataTypeAnalyzer.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/DefaultDataTypeAnalyzer.java Wed Mar  7 11:58:35 2007
@@ -0,0 +1,63 @@
+// Copyright 2007 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.internal.services;
+
+import java.util.Map;
+
+import org.apache.tapestry.events.InvalidationListener;
+import org.apache.tapestry.ioc.services.PropertyAdapter;
+import org.apache.tapestry.ioc.util.StrategyRegistry;
+import org.apache.tapestry.services.DataTypeAnalyzer;
+
+/**
+ * The default data type analyzer, which is based entirely on the type of the property (and not on
+ * annotations or naming conventions). This is based on a configuration of property type class to
+ * string provided as an IoC service configuration.
+ */
+public class DefaultDataTypeAnalyzer implements DataTypeAnalyzer, InvalidationListener
+{
+    private final StrategyRegistry<String> _registry;
+
+    public DefaultDataTypeAnalyzer(Map<Class, String> configuration)
+    {
+        _registry = StrategyRegistry.newInstance(String.class, configuration);
+    }
+
+    /**
+     * Clears the registry on an invalidation event (this is because the registry caches results,
+     * and the keys are classes that may be component classes from the invalidated component class
+     * loader).
+     */
+    public void objectWasInvalidated()
+    {
+        _registry.clearCache();
+    }
+
+    public String identifyDataType(PropertyAdapter adapter)
+    {
+        Class propertyType = adapter.getType();
+
+        String dataType = _registry.get(propertyType);
+
+        // To avoid "no strategy" exceptions, we expect a contribution of Object.class to the empty
+        // string. We convert that back to a null.
+
+        if (dataType.equals(""))
+            return null;
+
+        return dataType;
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/DataTypeAnalyzer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/DataTypeAnalyzer.java?view=auto&rev=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/DataTypeAnalyzer.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/DataTypeAnalyzer.java Wed Mar  7 11:58:35 2007
@@ -0,0 +1,35 @@
+// Copyright 2007 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.services;
+
+import org.apache.tapestry.corelib.components.BeanEditForm;
+import org.apache.tapestry.corelib.components.Grid;
+import org.apache.tapestry.ioc.services.PropertyAdapter;
+
+/**
+ * Used by {@link BeanModelSource} to identify the type of data associated with a particular
+ * property (represented as a {@link PropertyAdapter}). The data type is a string used to determine
+ * what kind of interface to use for displaying the value of the property, or what kind of interface
+ * to use for editting the value of the property. Command property types are "text", "enum",
+ * "checkbox", but the list is extensible.
+ * 
+ * @see Grid
+ * @see BeanEditForm
+ */
+public interface DataTypeAnalyzer
+{
+    /** Identifies the data type, if known, or returns null if not known. */
+    String identifyDataType(PropertyAdapter adapter);
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java Wed Mar  7 11:58:35 2007
@@ -91,6 +91,7 @@
 import org.apache.tapestry.internal.services.CookieSink;
 import org.apache.tapestry.internal.services.CookieSource;
 import org.apache.tapestry.internal.services.CookiesImpl;
+import org.apache.tapestry.internal.services.DefaultDataTypeAnalyzer;
 import org.apache.tapestry.internal.services.DefaultInjectionProvider;
 import org.apache.tapestry.internal.services.DefaultValidationDelegateCommand;
 import org.apache.tapestry.internal.services.DocumentScriptBuilder;
@@ -1437,10 +1438,38 @@
     @Inject("infrastructure:TypeCoercer")
     TypeCoercer typeCoercer,
 
-    Map<Class, String> configuration)
+    @InjectService("DataTypeAnalyzer")
+    DataTypeAnalyzer analyzer)
     {
-        BeanModelSourceImpl service = new BeanModelSourceImpl(typeCoercer, _propertyAccess,
-                _propertyConduitSource, _componentClassFactory, configuration);
+        return new BeanModelSourceImpl(typeCoercer, _propertyAccess, _propertyConduitSource,
+                _componentClassFactory, analyzer);
+    }
+
+    public DataTypeAnalyzer buildDataTypeAnalyzer(List<DataTypeAnalyzer> configuration)
+    {
+        return _chainBuilder.build(DataTypeAnalyzer.class, configuration);
+    }
+
+    /**
+     * Adds the {@link #buildDefaultDataTypeAnalyzer(Map) DefaultDatatTypeAnalyzer} to the
+     * configuration, ordered explicitly last.
+     */
+    public static void contributeDataTypeAnalyzer(
+            OrderedConfiguration<DataTypeAnalyzer> configuration,
+            @InjectService("DefaultDataTypeAnalyzer")
+            DataTypeAnalyzer defaultDataTypeAnalyzer)
+    {
+        configuration.add("Default", defaultDataTypeAnalyzer, "after:*.*");
+    }
+
+    /**
+     * The default data type analyzer is the final analyzer consulted and identifies the type
+     * entirely pased on the property type, working against its own configuration (mapping property
+     * type class to data type).
+     */
+    public DataTypeAnalyzer buildDefaultDataTypeAnalyzer(Map<Class, String> configuration)
+    {
+        DefaultDataTypeAnalyzer service = new DefaultDataTypeAnalyzer(configuration);
 
         _componentInstantiatorSource.addInvalidationListener(service);
 
@@ -1448,7 +1477,7 @@
     }
 
     /**
-     * Maps types to corresponding property editor names:
+     * Maps property types to data type names
      * <ul>
      * <li>String --&gt; text
      * <li>Number --&gt; text
@@ -1456,10 +1485,20 @@
      * <li>Boolean --&gt; checkbox
      * </ul>
      */
-    public static void contributeBeanModelSource(MappedConfiguration<Class, String> configuration)
+    public static void contributeDefaultDataTypeAnalyzer(
+            MappedConfiguration<Class, String> configuration)
     {
+        // This is a special case contributed to avoid exceptions when a property type can't be
+        // matched. DefaultDataTypeAnalyzer converts the empty string to null.
+
         configuration.add(Object.class, "");
+
         configuration.add(String.class, "text");
+
+        // This may change; as currently implemented, "text" refers more to the edit component
+        // (TextField) than to
+        // the "flavor" of data.
+
         configuration.add(Number.class, "text");
         configuration.add(Enum.class, "enum");
         configuration.add(Boolean.class, "checkbox");

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/ValidationConstraintGenerator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/ValidationConstraintGenerator.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/ValidationConstraintGenerator.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/ValidationConstraintGenerator.java Wed Mar  7 11:58:35 2007
@@ -38,7 +38,8 @@
      * @param propertyType
      *            the type of the property for which constraints are needed
      * @param annotationProvider
-     *            provides access to any annotations conceerning the property
+     *            provides access to any annotations concerning the property (for implementations
+     *            that are based on analysis of property annotations)
      * @return a list of constraints
      * @see FieldValidatorSource
      */

Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/BeanModelSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/BeanModelSourceImplTest.java?view=diff&rev=515722&r1=515721&r2=515722
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/BeanModelSourceImplTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/BeanModelSourceImplTest.java Wed Mar  7 11:58:35 2007
@@ -69,13 +69,13 @@
 
         assertEquals(age.getLabel(), "Age");
         assertSame(age.getPropertyType(), int.class);
-        assertEquals(age.getEditorType(), "text");
+        assertEquals(age.getDataType(), "text");
 
         PropertyModel firstName = model.get("firstName");
 
         assertEquals(firstName.getLabel(), "First Name");
         assertEquals(firstName.getPropertyType(), String.class);
-        assertEquals(firstName.getEditorType(), "text");
+        assertEquals(firstName.getDataType(), "text");
 
         assertEquals(model.get("lastName").getLabel(), "Last Name");
 
@@ -137,7 +137,7 @@
 
         assertEquals(model.getPropertyNames(), Arrays.asList("token"));
 
-        assertEquals(model.get("token").getEditorType(), "enum");
+        assertEquals(model.get("token").getDataType(), "enum");
 
         verify();
     }