Home | History | Annotate | Download | only in descriptors
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.adt.internal.editors.manifest.descriptors;
     18 
     19 import com.android.SdkConstants;
     20 import com.android.ide.eclipse.adt.internal.editors.manifest.model.UiClassAttributeNode.IPostTypeCreationAction;
     21 
     22 import org.eclipse.core.runtime.NullProgressMonitor;
     23 import org.eclipse.jdt.core.ICompilationUnit;
     24 import org.eclipse.jdt.core.IJavaElement;
     25 import org.eclipse.jdt.core.IType;
     26 import org.eclipse.jdt.core.JavaModelException;
     27 
     28 /**
     29  * Action to be executed after an Activity class is created.
     30  */
     31 class PostActivityCreationAction implements IPostTypeCreationAction {
     32 
     33     private final static PostActivityCreationAction sAction = new PostActivityCreationAction();
     34 
     35     private PostActivityCreationAction() {
     36         // private constructor to enforce singleton.
     37     }
     38 
     39 
     40     /**
     41      * Returns the action.
     42      */
     43     public static IPostTypeCreationAction getAction() {
     44         return sAction;
     45     }
     46 
     47     /**
     48      * Processes a newly created Activity.
     49      *
     50      */
     51     @Override
     52     public void processNewType(IType newType) {
     53         try {
     54             String methodContent =
     55                 "    /** Called when the activity is first created. */\n" +
     56                 "    @Override\n" +
     57                 "    public void onCreate(Bundle savedInstanceState) {\n" +
     58                 "        super.onCreate(savedInstanceState);\n" +
     59                 "\n" +
     60                 "        // TODO Auto-generated method stub\n" +
     61                 "    }";
     62             newType.createMethod(methodContent, null /* sibling*/, false /* force */,
     63                     new NullProgressMonitor());
     64 
     65             // we need to add the import for Bundle, so we need the compilation unit.
     66             // Since the type could be enclosed in other types, we loop till we find it.
     67             ICompilationUnit compilationUnit = null;
     68             IJavaElement element = newType;
     69             do {
     70                 IJavaElement parentElement = element.getParent();
     71                 if (parentElement !=  null) {
     72                     if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
     73                         compilationUnit = (ICompilationUnit)parentElement;
     74                     }
     75 
     76                     element = parentElement;
     77                 } else {
     78                     break;
     79                 }
     80             } while (compilationUnit == null);
     81 
     82             if (compilationUnit != null) {
     83                 compilationUnit.createImport(SdkConstants.CLASS_BUNDLE,
     84                         null /* sibling */, new NullProgressMonitor());
     85             }
     86         } catch (JavaModelException e) {
     87         }
     88     }
     89 }
     90