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;
017
018import org.androidtransfuse.adapter.ASTAnnotation;
019import org.androidtransfuse.adapter.ASTBase;
020import org.androidtransfuse.analysis.AnalysisContext;
021import org.androidtransfuse.analysis.Analyzer;
022import org.androidtransfuse.analysis.InjectionPointFactory;
023import org.androidtransfuse.annotations.View;
024import org.androidtransfuse.gen.ClassGenerationUtil;
025import org.androidtransfuse.model.InjectionNode;
026import org.androidtransfuse.model.InjectionSignature;
027import org.androidtransfuse.util.AndroidLiterals;
028import org.androidtransfuse.validation.Validator;
029
030import javax.inject.Inject;
031
032/**
033 * @author John Ericksen
034 */
035public class ViewInjectionNodeBuilder extends InjectionNodeBuilderSingleAnnotationAdapter {
036
037    private final ClassGenerationUtil generationUtil;
038    private final InjectionPointFactory injectionPointFactory;
039    private final VariableInjectionBuilderFactory variableInjectionBuilderFactory;
040    private final Analyzer analyzer;
041    private final Validator validator;
042
043    @Inject
044    public ViewInjectionNodeBuilder(ClassGenerationUtil generationUtil,
045                                    InjectionPointFactory injectionPointFactory,
046                                    VariableInjectionBuilderFactory variableInjectionBuilderFactory,
047                                    Analyzer analyzer,
048                                    Validator validator) {
049        super(View.class);
050        this.generationUtil = generationUtil;
051        this.injectionPointFactory = injectionPointFactory;
052        this.variableInjectionBuilderFactory = variableInjectionBuilderFactory;
053        this.analyzer = analyzer;
054        this.validator = validator;
055    }
056
057    @Override
058    public InjectionNode buildInjectionNode(ASTBase target, InjectionSignature signature, AnalysisContext context, ASTAnnotation annotation) {
059        Integer viewId = annotation.getProperty("value", Integer.class);
060        String viewTag = annotation.getProperty("tag", String.class);
061
062        InjectionNode injectionNode = analyzer.analyze(signature, context);
063
064        if(viewId == null && viewTag == null){
065            //validation error
066            validator.error("@View must contain ast least a value or tag parameter.")
067                    .element(target)
068                    .annotation(annotation)
069                    .build();
070        }
071        else {
072            InjectionNode activityInjectionNode = injectionPointFactory.buildInjectionNode(AndroidLiterals.ACTIVITY, context);
073            injectionNode.addAspect(VariableBuilder.class, variableInjectionBuilderFactory.buildViewVariableBuilder(viewId, viewTag, activityInjectionNode, generationUtil.type(signature.getType())));
074        }
075        return injectionNode;
076    }
077}