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.runtime.CoreException;
     25 import org.eclipse.core.runtime.IProgressMonitor;
     26 import org.eclipse.core.runtime.IStatus;
     27 import org.eclipse.core.runtime.Status;
     28 import org.eclipse.core.runtime.jobs.Job;
     29 import org.eclipse.jdt.core.ClasspathContainerInitializer;
     30 
     31 /**
     32  * Base CPC initializer providing support to all our initializer.
     33  *
     34  */
     35 abstract class BaseClasspathContainerInitializer extends ClasspathContainerInitializer {
     36 
     37 
     38     /**
     39      * Adds an error to a project, or remove all markers if error message is null
     40      * @param project the project to modify
     41      * @param errorMessage the errorMessage or null to remove errors.
     42      * @param markerType the marker type to be used.
     43      * @param outputToConsole whether to output to the console.
     44      */
     45     protected static void processError(final IProject project, final String errorMessage,
     46             final String markerType, boolean outputToConsole) {
     47         if (errorMessage != null) {
     48             // log the error and put the marker on the project if we can.
     49             if (outputToConsole) {
     50                 AdtPlugin.printErrorToConsole(project, errorMessage);
     51             }
     52 
     53             try {
     54                 BaseProjectHelper.markProject(project, markerType,
     55                         errorMessage, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_HIGH);
     56             } catch (CoreException e) {
     57                 // In some cases, the workspace may be locked for modification when we
     58                 // pass here.
     59                 // We schedule a new job to put the marker after.
     60                 final String fmessage = errorMessage;
     61                 Job markerJob = new Job("Android SDK: Resolving error markers") {
     62                     @Override
     63                     protected IStatus run(IProgressMonitor monitor) {
     64                         try {
     65                             BaseProjectHelper.markProject(project,
     66                                     markerType,
     67                                     fmessage, IMarker.SEVERITY_ERROR,
     68                                     IMarker.PRIORITY_HIGH);
     69                         } catch (CoreException e2) {
     70                             return e2.getStatus();
     71                         }
     72 
     73                         return Status.OK_STATUS;
     74                     }
     75                 };
     76 
     77                 // build jobs are run after other interactive jobs
     78                 markerJob.setPriority(Job.BUILD);
     79                 markerJob.schedule();
     80             }
     81         } else {
     82             // no error, remove existing markers.
     83             try {
     84                 if (project.exists()) {
     85                     project.deleteMarkers(markerType, true,
     86                             IResource.DEPTH_INFINITE);
     87                 }
     88             } catch (CoreException ce) {
     89                 // In some cases, the workspace may be locked for modification when we pass
     90                 // here, so we schedule a new job to put the marker after.
     91                 Job markerJob = new Job("Android SDK: Resolving error markers") {
     92                     @Override
     93                     protected IStatus run(IProgressMonitor monitor) {
     94                         try {
     95                             project.deleteMarkers(markerType, true,
     96                                     IResource.DEPTH_INFINITE);
     97                         } catch (CoreException e2) {
     98                             return e2.getStatus();
     99                         }
    100 
    101                         return Status.OK_STATUS;
    102                     }
    103                 };
    104 
    105                 // build jobs are run after other interactive jobs
    106                 markerJob.setPriority(Job.BUILD);
    107                 markerJob.schedule();
    108             }
    109         }
    110     }
    111 }
    112