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 org.androidtransfuse.adapter.ASTAnnotation;
021import org.androidtransfuse.adapter.ASTType;
022import org.androidtransfuse.adapter.element.ASTElementFactory;
023import org.androidtransfuse.annotations.WindowFeature;
024import org.androidtransfuse.experiment.*;
025import org.androidtransfuse.model.MethodDescriptor;
026import org.androidtransfuse.util.AndroidLiterals;
027
028import javax.inject.Inject;
029
030/**
031 * @author John Ericksen
032 */
033public class WindowFeatureGenerator implements Generation {
034
035    private final ASTElementFactory astElementFactory;
036
037    @Inject
038    public WindowFeatureGenerator(ASTElementFactory astElementFactory) {
039        this.astElementFactory = astElementFactory;
040    }
041
042    @Override
043    public void schedule(ComponentBuilder builder, ComponentDescriptor descriptor) {
044        ASTType target = descriptor.getTarget();
045        if (target.isAnnotated(WindowFeature.class)) {
046            ASTAnnotation windowFeatureAnnotation = target.getASTAnnotation(WindowFeature.class);
047            final Integer[] values = windowFeatureAnnotation.getProperty("value", Integer[].class);
048
049            builder.add(astElementFactory.findMethod(AndroidLiterals.ACTIVITY, "onCreate", AndroidLiterals.BUNDLE), GenerationPhase.INIT, new ComponentMethodGenerator() {
050                @Override
051                public void generate(MethodDescriptor methodDescriptor, JBlock block) {
052                    for (int value : values) {
053                        block.invoke("requestWindowFeature").arg(JExpr.lit(value));
054                    }
055                }
056            });
057        }
058    }
059}