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                             AdtPlugin.log(e2, null);
     71                             // Don't return e2.getStatus(); the job control will then produce
     72                             // a popup with this error, which isn't very interesting for the
     73                             // user.
     74                         }
     75 
     76                         return Status.OK_STATUS;
     77                     }
     78                 };
     79 
     80                 // build jobs are run after other interactive jobs
     81                 markerJob.setPriority(Job.BUILD);
     82                 markerJob.schedule();
     83             }
     84         } else {
     85             // no error, remove existing markers.
     86             try {
     87                 if (project.isAccessible()) {
     88                     project.deleteMarkers(markerType, true,
     89                             IResource.DEPTH_INFINITE);
     90                 }
     91             } catch (CoreException ce) {
     92                 // In some cases, the workspace may be locked for modification when we pass
     93                 // here, so we schedule a new job to put the marker after.
     94                 Job markerJob = new Job("Android SDK: Resolving error markers") {
     95                     @Override
     96                     protected IStatus run(IProgressMonitor monitor) {
     97                         try {
     98                             if (project.isAccessible()) {
     99                                 project.deleteMarkers(markerType, true,
    100                                         IResource.DEPTH_INFINITE);
    101                             }
    102                         } catch (CoreException e2) {
    103                             AdtPlugin.log(e2, null);
    104                         }
    105 
    106                         return Status.OK_STATUS;
    107                     }
    108                 };
    109 
    110                 // build jobs are run after other interactive jobs
    111                 markerJob.setPriority(Job.BUILD);
    112                 markerJob.schedule();
    113             }
    114         }
    115     }
    116 }
    117