You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2008/06/18 19:31:34 UTC

[Tapestry Wiki] Update of "Tapestry5AnotherSelectWithObjects" by MarceloLotif

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by MarceloLotif:
http://wiki.apache.org/tapestry/Tapestry5AnotherSelectWithObjects

The comment on the change is:
Updated to 5.0.13

------------------------------------------------------------------------------
  	private List<T> list;
  
  	public GenericSelectionModel(List<T> list, String labelField, PropertyAccess access) {
+ 		if(list == null) list = new ArrayList<T>();		
  		if(labelField != null && !labelField.equalsIgnoreCase("null")){
  			if(list.size() > 0){
  				this.labelFieldAdapter = access.getAdapter(list.get(0).getClass()).getPropertyAdapter(labelField);
@@ -54, +55 @@

  			if (labelFieldAdapter == null) {
  				optionModelList.add(new OptionModelImpl(nvl(obj) + "", obj));
  			} else {
- 				optionModelList.add(new OptionModelImpl(nvl(labelFieldAdapter.get(obj)),obj));
+ 				optionModelList.add(new OptionModelImpl(nvl(labelFieldAdapter.get(obj)), obj));
  			}
  		}
  		return optionModelList;
@@ -85, +86 @@

  	private List<T> list;
  
  	public GenericValueEncoder(List<T> list, String idField, PropertyAccess access) {
+ 		if(list == null) list = new ArrayList<T>();
  		if (idField != null && !idField.equalsIgnoreCase("null")){
  			if(list.size() > 0){
  				this.idFieldAdapter = access.getAdapter(list.get(0).getClass()).getPropertyAdapter(idField);
@@ -102, +104 @@

  	}
  
  	public T toValue(String string) {
-         if (idFieldAdapter == null) {
+ 		if (idFieldAdapter == null) {
-             for (T obj : list) {
+ 			for (T obj : list) {
-                 if (nvl(obj).equals(string)) return obj;
+ 				if (nvl(obj).equals(string)) return obj;
-             }
-         } else {
-             for (T obj : list) {
+ 			}
+ 		} else {
+ 			for (T obj : list) {
-                 if (nvl(idFieldAdapter.get(obj)).equals(string)) return obj;
+ 				if (nvl(idFieldAdapter.get(obj)).equals(string)) return obj;
-             }
-         }
-         return null;
-     }
- 	
+ 			}
+ 		}
+ 		return null;
+ 	}
+ 
  	private String nvl(Object o) {
-         if (o == null)
-             return "";
-         else
-             return o.toString();
-     }
+ 		if (o == null)
+ 			return "";
+ 		else
+ 			return o.toString();
+ 	}
  }
  }}}
  
@@ -184, +186 @@

  
  		for (String fieldName : transformation.findFieldsWithAnnotation(InjectSelectionModel.class)) {
  			InjectSelectionModel annotation = transformation.getFieldAnnotation(fieldName, InjectSelectionModel.class);
- 			
+                         
  			if (_logger.isDebugEnabled()){
  				_logger.debug("Creating selection model getter method for the field " + fieldName);
  			}
- 
- 			String accessActualName = transformation.addField(Modifier.PRIVATE, "org.apache.tapestry.ioc.services.PropertyAccess", "_access");
+ 			String accessActualName = transformation.addField(Modifier.PRIVATE
+ 				, "org.apache.tapestry5.ioc.services.PropertyAccess", "_access");
  			transformation.injectField(accessActualName, _access);
- 			
+                         
- 			addGetSelectionModelMethod(transformation, fieldName, annotation.labelField(), accessActualName);
+ 			addGetSelectionModelMethod(transformation, fieldName, annotation.labelField()
+ 				, accessActualName);
  
- 			if (_logger.isDebugEnabled()){
+ 				if (_logger.isDebugEnabled()){
- 				_logger.debug("Creating value encoder getter method for the field " + fieldName);
+ 					_logger.debug("Creating value encoder getter method for the field " + fieldName);
- 			}
+ 				}
  
- 			addGetValueEncoderMethod(transformation, fieldName, annotation.idField(), accessActualName);
+ 				addGetValueEncoderMethod(transformation, fieldName, annotation.idField(), accessActualName);
- 			
+                         
  		}
  
  	}
  
  	private void addGetSelectionModelMethod(ClassTransformation transformation, String fieldName, String labelField, String accessName) {
- 		
- 		String methodName = "get" + InternalUtils.capitalize(InternalUtils.stripMemberPrefix(fieldName)) + "SelectionModel";
- 				
+                 
+ 		String methodName = "get" + InternalUtils.capitalize(InternalUtils
+ 			.stripMemberPrefix(fieldName)) + "SelectionModel";
+                                 
  		String modelQualifiedName = (GenericSelectionModel.class).getName();
  		TransformMethodSignature sig = 
- 			new TransformMethodSignature(Modifier.PUBLIC, modelQualifiedName, methodName, null, null);
+ 			new TransformMethodSignature(Modifier.PUBLIC, modelQualifiedName
+ 			, methodName, null, null);
  
  		BodyBuilder builder = new BodyBuilder();
  		builder.begin();
- 		builder.addln("return new " + modelQualifiedName + "(" + fieldName + ", \"" + labelField +"\", " + accessName + ");");
+ 		builder.addln("return new " + modelQualifiedName + "(" + fieldName 
+ 			+ ", \"" + labelField +"\", " + accessName + ");");
  		builder.end();
  
  		transformation.addMethod(sig, builder.toString());
  
  	}
- 	
+         
  	private void addGetValueEncoderMethod(ClassTransformation transformation, String fieldName, String idField, String accessName) {
- 		
- 		String methodName = "get" + InternalUtils.capitalize(InternalUtils.stripMemberPrefix(fieldName)) + "ValueEncoder";
- 		
+                 
+ 		String methodName = "get" + InternalUtils.capitalize(InternalUtils
+ 			.stripMemberPrefix(fieldName)) + "ValueEncoder";
+                 
  		String encoderQualifiedName = (GenericValueEncoder.class).getName();
  		TransformMethodSignature sig = 
- 			new TransformMethodSignature(Modifier.PUBLIC, encoderQualifiedName, methodName, null, null);
+ 			new TransformMethodSignature(Modifier.PUBLIC, encoderQualifiedName
+ 			, methodName, null, null);
  
  		BodyBuilder builder = new BodyBuilder();
  		builder.begin();
@@ -322, +330 @@

  
  }}}
  
- If you want a '''List<String>''', just instantiate it and pass '''nothing''' as the ''labelField'' and ''idField'' for the annotation.
- 
  '''Important:''' your bean have to override the '''equals()''' method from the '''Object''' superclass, so the component can compare your current selection with the appropriate bean inside the list. The Eclipse IDE provide a simple default implementation that solves the problem. Go to the menu '''"Source/Generate hashCode() and equals()..."''' to use this feature.
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org