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 BroadcastReceiver class is created.
     30  */
     31 class PostReceiverCreationAction implements IPostTypeCreationAction {
     32 
     33     private final static PostReceiverCreationAction sAction = new PostReceiverCreationAction();
     34 
     35     private PostReceiverCreationAction() {
     36         // private constructor to enforce singleton.
     37     }
     38 
     39     /**
     40      * Returns the action.
     41      */
     42     public static IPostTypeCreationAction getAction() {
     43         return sAction;
     44     }
     45 
     46     /**
     47      * Processes a newly created Activity.
     48      *
     49      */
     50     @Override
     51     public void processNewType(IType newType) {
     52         try {
     53             String methodContent =
     54                 "    @Override\n" +
     55                 "    public void onReceive(Context context, Intent intent) {\n" +
     56                 "        // TODO Auto-generated method stub\n" +
     57                 "    }";
     58             newType.createMethod(methodContent, null /* sibling*/, false /* force */,
     59                     new NullProgressMonitor());
     60 
     61             // we need to add the import for Bundle, so we need the compilation unit.
     62             // Since the type could be enclosed in other types, we loop till we find it.
     63             ICompilationUnit compilationUnit = null;
     64             IJavaElement element = newType;
     65             do {
     66                 IJavaElement parentElement = element.getParent();
     67                 if (parentElement !=  null) {
     68                     if (parentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
     69                         compilationUnit = (ICompilationUnit)parentElement;
     70                     }
     71 
     72                     element = parentElement;
     73                 } else {
     74                     break;
     75                 }
     76             } while (compilationUnit == null);
     77 
     78             if (compilationUnit != null) {
     79                 compilationUnit.createImport(SdkConstants.CLASS_CONTEXT,
     80                         null /* sibling */, new NullProgressMonitor());
     81                 compilationUnit.createImport(SdkConstants.CLASS_INTENT,
     82                         null /* sibling */, new NullProgressMonitor());
     83             }
     84         } catch (JavaModelException e) {
     85             // looks like the class already existed (this happens when the user check to create
     86             // inherited abstract methods).
     87         }
     88     }
     89 }
     90