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.ide.eclipse.adt.internal.editors.manifest.model.UiClassAttributeNode.IPostTypeCreationAction;
     20 import com.android.sdklib.SdkConstants;
     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     public void processNewType(IType newType) {
     52         try {
     53             String methodContent =
     54                 "    /** Called when the activity is first created. */\n" +
     55                 "    @Override\n" +
     56                 "    public void onCreate(Bundle savedInstanceState) {\n" +
     57                 "        super.onCreate(savedInstanceState);\n" +
     58                 "\n" +
     59                 "        // TODO Auto-generated method stub\n" +
     60                 "    }";
     61             newType.createMethod(methodContent, null /* sibling*/, false /* force */,
     62                     new NullProgressMonitor());
     63 
     64             // we need to add the import for Bundle, so we need the compilation unit.
     65             // Since the type could be enclosed in other types, we loop till we find it.
     66             ICompilationUnit compilationUnit = null;
     67             IJavaElement element = newType;
     68             do {
     69                 IJavaElement parentElement = element.getParent();
     70                 if (parentElement !=  null) {
     71                     if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
     72                         compilationUnit = (ICompilationUnit)parentElement;
     73                     }
     74 
     75                     element = parentElement;
     76                 } else {
     77                     break;
     78                 }
     79             } while (compilationUnit == null);
     80 
     81             if (compilationUnit != null) {
     82                 compilationUnit.createImport(SdkConstants.CLASS_BUNDLE,
     83                         null /* sibling */, new NullProgressMonitor());
     84             }
     85         } catch (JavaModelException e) {
     86         }
     87     }
     88 }
     89