Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. Rich Faces - Natural Ajax for Java Server Faces (JSF) Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 
 package org.ajax4jsf.builder.component;
 
 import static org.ajax4jsf.builder.model.Argument.arg;
 
 
 
Build component class model from configs

Author(s):
Maksim Kaszynski
 
 public class ComponentModelBuilder {
 	
 	
 	public JavaClass build(ComponentBean componentBeanJSFGeneratorConfiguration configuration) {
 		
 		String superclassname = 
 			componentBean.getSuperclass();
 		
 		JavaClass javaClass = 
 			new JavaClass(componentBean.getSimpleClassName(), 
 					new JavaPackage(componentBean.getPackageName()));
 		
 		try {
 			Class<?> superClass = 
 				Class.forName(superclassnamefalseconfiguration.getClassLoader());
 			
 			javaClass.setSuperClass(superClass);
 			
 		} catch (ClassNotFoundException e) {
 			javaClass.setSuperClass(UIComponentBase.class);
 		}
 		
 		
 		javaClass.addField(getComponentFamily(componentBean));
 		javaClass.addField(getComponentType(componentBean));
 		
 		javaClass.addMethod(getConstructor(componentBeanjavaClass));
 		
 		Collection<PropertyBeanproperties = componentBean.getProperties();
 		
 		for (PropertyBean propertyBean : properties) {
 			PropertyProcessor proc = null;
 			
 			for (int i = 0; i < . && proc == nulli++) {
 				
 				PropertyProcessor processor = 
 				
 				if (processor.accept(propertyBeanjavaClassconfiguration)) {
 					proc = processor;
 				}
 			}
			if (proc != null) {
				proc.process(propertyBeanjavaClassconfiguration);
			}
		}
		javaClass.addMethod(getComponentFamilyMethod(componentBean));
		ComponentStateManager stateManager = new ComponentStateManager(javaClass);
		try {
			saveState.setMethodBody(stateManager.getSaveStateMethodBody(configuration));
catch (GeneratorException e) {
		}
		javaClass.addMethod(saveState);
		try {
			restoreState.setMethodBody(stateManager.getRestoreStateMethodBody(configuration));
catch (GeneratorException e) {
			// TODO Auto-generated catch block
		}
		javaClass.addMethod(restoreState);
		return javaClass;
	}
		JavaMethod javaMethod = new JavaMethod("getFamily"String.class);
		javaMethod.setMethodBody(new MethodBody() {
			public String toCode() {
				return "return COMPONENT_FAMILY;";
			}
		});
		return javaMethod;
	}
		JavaMethod method = 
			new JavaMethod("restoreState"
					arg("context"FacesContext.class), 
					arg("state"Object.class));
		method.addAnnotation(Override.class);
		return method;
	}
		JavaMethod method = 
			new JavaMethod("saveState"
					Object.class
					arg("context"FacesContext.class));
		method.addAnnotation(Override.class);
		method.setMethodBody(new MethodBody() {
			public String toCode() {
				return "return super.saveState(context);";
			}
		});
		return method;
	}
	private JavaField getComponentType(ComponentBean componentBean) {
		JavaField field = new JavaField(String.class"COMPONENT_TYPE");
		field.setValue("\"" + componentBean.getName() + "\"");
		return field;
	}
	private JavaField getComponentFamily(ComponentBean componentBean) {
		JavaField field = new JavaField(String.class"COMPONENT_FAMILY");
		field.setValue("\"" + componentBean.getFamily() + "\"");
		return field;
	}
	private JavaMethod getConstructor(ComponentBean componentBeanJavaClass javaClass) {
		final RendererBean renderer = componentBean.getRenderer();
		JavaMethod method = new JavaConstructor(javaClass);
		method.setMethodBody(new MethodBody(method) {
			public String toCode() {
				if (renderer != null) {
					return "setRendererType(\"" + renderer.getName() + "\");";
				}
				return super.toCode();
			}
		});
		return method;
	}
}	
New to GrepCode? Check out our FAQ X