Home | History | Annotate | Download | only in project
      1 /*
      2  * Copyright (C) 2012 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.project;
     18 
     19 import com.android.ide.eclipse.adt.AdtPlugin;
     20 
     21 import org.eclipse.core.resources.IMarker;
     22 import org.eclipse.core.resources.IProject;
     23 import org.eclipse.core.resources.IResource;
     24 import org.eclipse.core.resources.ResourcesPlugin;
     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.core.runtime.jobs.Job;
     30 import org.eclipse.jdt.core.ClasspathContainerInitializer;
     31 
     32 /**
     33  * Base CPC initializer providing support to all our initializer.
     34  *
     35  */
     36 abstract class BaseClasspathContainerInitializer extends ClasspathContainerInitializer {
     37 
     38 
     39     /**
     40      * Adds an error to a project, or remove all markers if error message is null
     41      * @param project the project to modify
     42      * @param errorMessage the errorMessage or null to remove errors.
     43      * @param markerType the marker type to be used.
     44      * @param outputToConsole whether to output to the console.
     45      */
     46     protected static void processError(final IProject project, final String errorMessage,
     47             final String markerType, boolean outputToConsole) {
     48         if (errorMessage != null) {
     49             // log the error and put the marker on the project if we can.
     50             if (outputToConsole) {
     51                 AdtPlugin.printErrorToConsole(project, errorMessage);
     52             }
     53 
     54             try {
     55                 BaseProjectHelper.markProject(project, markerType,
     56                         errorMessage, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_HIGH);
     57             } catch (CoreException e) {
     58                 // In some cases, the workspace may be locked for modification when we
     59                 // pass here.
     60                 // We schedule a new job to put the marker after.
     61                 final String fmessage = errorMessage;
     62                 Job markerJob = new Job("Android SDK: Resolving error markers") {
     63                     @Override
     64                     protected IStatus run(IProgressMonitor monitor) {
     65                         try {
     66                             BaseProjectHelper.markProject(project,
     67                                     markerType,
     68                                     fmessage, IMarker.SEVERITY_ERROR,
     69                                     IMarker.PRIORITY_HIGH);
     70                         } catch (CoreException e2) {
     71                             AdtPlugin.log(e2, null);
     72                             // Don't return e2.getStatus(); the job control will then produce
     73                             // a popup with this error, which isn't very interesting for the
     74                             // user.
     75                         }
     76 
     77                         return Status.OK_STATUS;
     78                     }
     79                 };
     80 
     81                 // build jobs are run after other interactive jobs
     82                 markerJob.setPriority(Job.BUILD);
     83                 markerJob.setRule(ResourcesPlugin.getWorkspace().getRoot());
     84                 markerJob.schedule();
     85             }
     86         } else {
     87             // no error, remove existing markers.
     88             try {
     89                 if (project.isAccessible()) {
     90                     project.deleteMarkers(markerType, true,
     91                             IResource.DEPTH_INFINITE);
     92                 }
     93             } catch (CoreException ce) {
     94                 // In some cases, the workspace may be locked for modification when we pass
     95                 // here, so we schedule a new job to put the marker after.
     96                 Job markerJob = new Job("Android SDK: Resolving error markers") {
     97                     @Override
     98                     protected IStatus run(IProgressMonitor monitor) {
     99                         try {
    100                             if (project.isAccessible()) {
    101                                 project.deleteMarkers(markerType, true,
    102                                         IResource.DEPTH_INFINITE);
    103                             }
    104                         } catch (CoreException e2) {
    105                             AdtPlugin.log(e2, null);
    106                         }
    107 
    108                         return Status.OK_STATUS;
    109                     }
    110                 };
    111 
    112                 // build jobs are run after other interactive jobs
    113                 markerJob.setPriority(Job.BUILD);
    114                 markerJob.schedule();
    115             }
    116         }
    117     }
    118 }
    119