001/** 002 * Copyright 2013 John Ericksen 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.androidtransfuse.gen.variableBuilder.resource; 017 018import com.sun.codemodel.JType; 019import org.androidtransfuse.adapter.ASTType; 020import org.androidtransfuse.adapter.classes.ASTClassFactory; 021import org.androidtransfuse.analysis.AnalysisContext; 022import org.androidtransfuse.gen.ClassGenerationUtil; 023import org.androidtransfuse.util.AndroidLiterals; 024 025import javax.inject.Inject; 026import java.util.HashMap; 027import java.util.Map; 028 029/** 030 * @author John Ericksen 031 */ 032public class ResourceExpressionBuilderFactory { 033 034 private static final String GET_STRING = "getString"; 035 private static final String GET_BOOLEAN = "getBoolean"; 036 private static final String GET_COLORSTATELIST = "getColorStateList"; 037 private static final String GET_INTEGER = "getInteger"; 038 private static final String GET_DRAWABLE = "getDrawable"; 039 private static final String GET_STRINGARRAY = "getStringArray"; 040 private static final String GET_INTARRAY = "getIntArray"; 041 private static final String GET_MOVIE = "getMovie"; 042 043 private final ClassGenerationUtil generationUtil; 044 private final Map<JType, ResourceExpressionBuilderAdaptor> resourceExpressionBuilderMap = new HashMap<JType, ResourceExpressionBuilderAdaptor>(); 045 private final MethodBasedResourceExpressionBuilderAdaptorFactory adaptorFactory; 046 private final ASTClassFactory astClassFactory; 047 048 @Inject 049 public ResourceExpressionBuilderFactory(MethodBasedResourceExpressionBuilderAdaptorFactory adaptorFactory, ClassGenerationUtil generationUtil, ASTClassFactory astClassFactory) { 050 this.generationUtil = generationUtil; 051 this.adaptorFactory = adaptorFactory; 052 this.astClassFactory = astClassFactory; 053 054 addMethodBasedResourceBuider(String.class, GET_STRING); 055 addMethodBasedResourceBuider(String.class, GET_STRING); 056 addMethodBasedResourceBuider(Boolean.class, GET_BOOLEAN); 057 addMethodBasedResourceBuider(boolean.class, GET_BOOLEAN); 058 addMethodBasedResourceBuider(AndroidLiterals.COLOR_STATE_LIST, GET_COLORSTATELIST); 059 addMethodBasedResourceBuider(Integer.class, GET_INTEGER); 060 addMethodBasedResourceBuider(int.class, GET_INTEGER); 061 addMethodBasedResourceBuider(AndroidLiterals.GRAPHICS_DRAWABLE, GET_DRAWABLE); 062 addMethodBasedResourceBuider(String[].class, GET_STRINGARRAY); 063 addMethodBasedResourceBuider(Integer[].class, GET_INTARRAY); 064 addMethodBasedResourceBuider(int[].class, GET_INTARRAY); 065 addMethodBasedResourceBuider(AndroidLiterals.GRAPHICS_MOVIE, GET_MOVIE); 066 addAnimationResourceBuilder(AndroidLiterals.ANIMATION); 067 } 068 069 private void addMethodBasedResourceBuider(ASTType type, String method) { 070 JType refClass = generationUtil.type(type); 071 resourceExpressionBuilderMap.put(refClass, adaptorFactory.buildMethodBasedResourceExpressionBuilderAdaptor(type, method)); 072 } 073 074 private void addMethodBasedResourceBuider(Class clazz, String method) { 075 JType refClass = generationUtil.type(clazz); 076 resourceExpressionBuilderMap.put(refClass, adaptorFactory.buildMethodBasedResourceExpressionBuilderAdaptor(astClassFactory.getType(clazz), method)); 077 } 078 079 private void addAnimationResourceBuilder(ASTType type) { 080 JType refClass = generationUtil.type(type); 081 resourceExpressionBuilderMap.put(refClass, adaptorFactory.buildAnimationResourceExpressionBuilderAdaptor()); 082 } 083 084 public ResourceExpressionBuilder buildResourceExpressionBuilder(JType resourceType, AnalysisContext context) { 085 086 ResourceExpressionBuilderAdaptor resourceExpressionBuilderAdaptor = resourceExpressionBuilderMap.get(resourceType); 087 088 return resourceExpressionBuilderAdaptor.buildResourceExpressionBuilder(context); 089 } 090}