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.experiment.generators;
017
018import com.sun.codemodel.JBlock;
019import com.sun.codemodel.JExpr;
020import com.sun.codemodel.JInvocation;
021import com.sun.codemodel.JVar;
022import org.androidtransfuse.adapter.ASTMethod;
023import org.androidtransfuse.adapter.ASTParameter;
024import org.androidtransfuse.adapter.ASTType;
025import org.androidtransfuse.adapter.element.ASTElementFactory;
026import org.androidtransfuse.annotations.Layout;
027import org.androidtransfuse.experiment.*;
028import org.androidtransfuse.gen.ClassGenerationUtil;
029import org.androidtransfuse.gen.UniqueVariableNamer;
030import org.androidtransfuse.gen.variableBuilder.InjectionBindingBuilder;
031import org.androidtransfuse.model.MethodDescriptor;
032import org.androidtransfuse.model.TypedExpression;
033import org.androidtransfuse.model.r.RResourceReferenceBuilder;
034import org.androidtransfuse.util.AndroidLiterals;
035
036import javax.inject.Inject;
037
038/**
039 * @author John Ericksen
040 */
041public class FragmentLayoutGenerator implements Generation {
042
043    private final RResourceReferenceBuilder rResourceReferenceBuilder;
044    private final ASTElementFactory astElementFactory;
045    private final UniqueVariableNamer namer;
046    private final ClassGenerationUtil generationUtil;
047    private final InjectionBindingBuilder injectionBindingBuilder;
048
049    @Inject
050    public FragmentLayoutGenerator(RResourceReferenceBuilder rResourceReferenceBuilder,
051                                   ASTElementFactory astElementFactory,
052                                   UniqueVariableNamer namer,
053                                   ClassGenerationUtil generationUtil,
054                                   InjectionBindingBuilder injectionBindingBuilder) {
055        this.rResourceReferenceBuilder = rResourceReferenceBuilder;
056        this.astElementFactory = astElementFactory;
057        this.namer = namer;
058        this.generationUtil = generationUtil;
059        this.injectionBindingBuilder = injectionBindingBuilder;
060    }
061
062    @Override
063    public void schedule(final ComponentBuilder builder, final ComponentDescriptor descriptor) {
064
065        final ASTMethod onCreateViewMethod = astElementFactory.findMethod(AndroidLiterals.FRAGMENT, "onCreateView", AndroidLiterals.LAYOUT_INFLATER, AndroidLiterals.VIEW_GROUP, AndroidLiterals.BUNDLE);
066        builder.add(onCreateViewMethod, GenerationPhase.LAYOUT, new ComponentMethodGenerator() {
067            @Override
068            public void generate(MethodDescriptor methodDescriptor, JBlock block) {
069                ASTType target = descriptor.getTarget();
070                final JVar viewDeclaration = block.decl(generationUtil.ref(AndroidLiterals.VIEW), namer.generateName(AndroidLiterals.VIEW));
071                builder.getAnalysisContext().getInjectionNodeBuilders().putType(AndroidLiterals.VIEW, injectionBindingBuilder.buildExpression(new TypedExpression(AndroidLiterals.VIEW, viewDeclaration)));
072
073                if (target.isAnnotated(Layout.class)) {
074                    Layout layoutAnnotation = target.getAnnotation(Layout.class);
075
076                    Integer layout = layoutAnnotation == null ? null : layoutAnnotation.value();
077
078                    block.assign(viewDeclaration, methodDescriptor.getExpression(AndroidLiterals.LAYOUT_INFLATER).getExpression()
079                            .invoke("inflate")
080                            .arg(rResourceReferenceBuilder.buildReference(layout))
081                            .arg(methodDescriptor.getExpression(AndroidLiterals.VIEW_GROUP).getExpression())
082                            .arg(JExpr.lit(false)));
083                } else {
084                    JInvocation onCreateView = JExpr._super().invoke("onCreateView");
085
086                    for (ASTParameter astParameter : methodDescriptor.getASTMethod().getParameters()) {
087                        onCreateView.arg(methodDescriptor.getExpression(astParameter.getASTType()).getExpression());
088                    }
089
090                    block.assign(viewDeclaration, onCreateView);
091                }
092
093                builder.add(onCreateViewMethod, GenerationPhase.RETURN, new ComponentMethodGenerator() {
094                    @Override
095                    public void generate(MethodDescriptor methodDescriptor, JBlock block) {
096                        block._return(viewDeclaration);
097                    }
098                });
099            }
100        });
101    }
102}