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 org.androidtransfuse.adapter.ASTMethod; 020import org.androidtransfuse.adapter.ASTType; 021import org.androidtransfuse.adapter.element.ASTElementFactory; 022import org.androidtransfuse.annotations.Layout; 023import org.androidtransfuse.experiment.*; 024import org.androidtransfuse.model.MethodDescriptor; 025import org.androidtransfuse.model.r.RResource; 026import org.androidtransfuse.model.r.RResourceReferenceBuilder; 027import org.androidtransfuse.model.r.ResourceIdentifier; 028import org.androidtransfuse.util.AndroidLiterals; 029 030import javax.inject.Inject; 031 032/** 033 * @author John Ericksen 034 */ 035public class LayoutGenerator implements Generation { 036 037 private final RResourceReferenceBuilder rResourceReferenceBuilder; 038 private final RResource rResource; 039 private final ASTElementFactory astElementFactory; 040 041 @Inject 042 public LayoutGenerator(RResourceReferenceBuilder rResourceReferenceBuilder, 043 RResource rResource, 044 ASTElementFactory astElementFactory) { 045 this.rResourceReferenceBuilder = rResourceReferenceBuilder; 046 this.rResource = rResource; 047 this.astElementFactory = astElementFactory; 048 } 049 050 @Override 051 public void schedule(ComponentBuilder builder, ComponentDescriptor descriptor) { 052 053 ASTType target = descriptor.getTarget(); 054 055 if(target.isAnnotated(Layout.class)) { 056 Layout layoutAnnotation = target.getAnnotation(Layout.class); 057 058 Integer layout = layoutAnnotation == null ? null : layoutAnnotation.value(); 059 060 //layout setting 061 final ResourceIdentifier layoutIdentifier = rResource.getResourceIdentifier(layout); 062 063 ASTMethod onCreateMethod = astElementFactory.findMethod(AndroidLiterals.ACTIVITY, "onCreate", AndroidLiterals.BUNDLE); 064 builder.add(onCreateMethod, GenerationPhase.LAYOUT, new ComponentMethodGenerator() { 065 @Override 066 public void generate(MethodDescriptor methodDescriptor, JBlock block) { 067 block.invoke("setContentView").arg(rResourceReferenceBuilder.buildReference(layoutIdentifier)); 068 } 069 }); 070 } 071 072 } 073}