Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2010 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.refactorings.core;
     18 
     19 import org.eclipse.core.resources.IFile;
     20 import org.eclipse.core.resources.IMarker;
     21 import org.eclipse.core.resources.IProject;
     22 import org.eclipse.core.resources.IResource;
     23 import org.eclipse.core.resources.IncrementalProjectBuilder;
     24 import org.eclipse.core.resources.WorkspaceJob;
     25 import org.eclipse.core.runtime.CoreException;
     26 import org.eclipse.core.runtime.IProgressMonitor;
     27 import org.eclipse.core.runtime.IStatus;
     28 import org.eclipse.core.runtime.Status;
     29 import org.eclipse.jdt.core.ICompilationUnit;
     30 import org.eclipse.jdt.core.IJavaElement;
     31 import org.eclipse.jdt.core.IJavaProject;
     32 import org.eclipse.jdt.core.IPackageFragment;
     33 import org.eclipse.jdt.core.ISourceRange;
     34 import org.eclipse.jdt.core.JavaCore;
     35 import org.eclipse.jdt.core.dom.CompilationUnit;
     36 import org.eclipse.jdt.core.search.TypeNameMatch;
     37 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
     38 import org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation;
     39 import org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery;
     40 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
     41 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
     42 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
     43 import org.eclipse.jdt.ui.SharedASTProvider;
     44 import org.eclipse.swt.widgets.Display;
     45 import org.eclipse.ui.IEditorPart;
     46 import org.eclipse.ui.PlatformUI;
     47 import org.eclipse.ui.progress.IProgressService;
     48 
     49 /**
     50  * The helper class which fixes the import errors after refactoring
     51  *
     52  */
     53 @SuppressWarnings("restriction")
     54 public class FixImportsJob extends WorkspaceJob {
     55 
     56     private IFile mAndroidManifest;
     57 
     58     private String mJavaPackage;
     59 
     60     /**
     61      * Creates a new <code>FixImportsJob</code>
     62      *
     63      * @param name the job name
     64      * @param androidManifest the android manifest file
     65      * @param javaPackage the android java package
     66      */
     67     public FixImportsJob(String name, IFile androidManifest, String javaPackage) {
     68         super(name);
     69         this.mAndroidManifest = androidManifest;
     70         this.mJavaPackage = javaPackage;
     71     }
     72 
     73     @Override
     74     public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
     75         if (mJavaPackage == null || mAndroidManifest == null || !mAndroidManifest.exists()) {
     76             return Status.CANCEL_STATUS;
     77         }
     78         IProject project = mAndroidManifest.getProject();
     79         IJavaProject javaProject = JavaCore.create(project);
     80         if (javaProject == null || !javaProject.isOpen()) {
     81             return Status.CANCEL_STATUS;
     82         }
     83 
     84         project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
     85 
     86         IMarker[] markers = project.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
     87         for (int i = 0; i < markers.length; i++) {
     88             IMarker marker = markers[i];
     89             IResource resource = marker.getResource();
     90             try {
     91                 IJavaElement element = JavaCore.create(resource);
     92                 if (element != null && (element instanceof ICompilationUnit)) {
     93                     final ICompilationUnit cu = (ICompilationUnit) element;
     94                     IPackageFragment packageFragment = (IPackageFragment) cu
     95                             .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
     96                     if (packageFragment != null && packageFragment.exists()) {
     97                         String packageName = packageFragment.getElementName();
     98                         if (packageName != null && packageName.startsWith(mJavaPackage)) {
     99                             CompilationUnit astRoot = SharedASTProvider.getAST(cu,
    100                                     SharedASTProvider.WAIT_ACTIVE_ONLY, null);
    101                             CodeGenerationSettings settings = JavaPreferencesSettings
    102                                     .getCodeGenerationSettings(cu.getJavaProject());
    103                             final boolean hasAmbiguity[] = new boolean[] {
    104                                 false
    105                             };
    106                             IChooseImportQuery query = new IChooseImportQuery() {
    107                                 @Override
    108                                 public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices,
    109                                         ISourceRange[] ranges) {
    110                                     hasAmbiguity[0] = true;
    111                                     return new TypeNameMatch[0];
    112                                 }
    113                             };
    114                             final OrganizeImportsOperation op = new OrganizeImportsOperation(cu,
    115                                     astRoot, settings.importIgnoreLowercase, !cu.isWorkingCopy(),
    116                                     true, query);
    117                             Display.getDefault().asyncExec(new Runnable() {
    118 
    119                                 @Override
    120                                 public void run() {
    121                                     try {
    122                                         IProgressService progressService = PlatformUI
    123                                                 .getWorkbench().getProgressService();
    124                                         progressService.run(
    125                                                 true,
    126                                                 true,
    127                                                 new WorkbenchRunnableAdapter(op, op
    128                                                         .getScheduleRule()));
    129                                         IEditorPart openEditor = EditorUtility.isOpenInEditor(cu);
    130                                         if (openEditor != null) {
    131                                             openEditor.doSave(monitor);
    132                                         }
    133                                     } catch (Throwable e) {
    134                                         RefactoringUtil.log(e);
    135                                     }
    136                                 }
    137                             });
    138 
    139                         }
    140                     }
    141                 }
    142             } catch (Throwable e) {
    143                 RefactoringUtil.log(e);
    144             }
    145         }
    146         return Status.OK_STATUS;
    147     }
    148 }