BaseClasspathContainerInitializer.java revision ca6ebe92816189f5387a4e1e73571dd959f0c708
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
17package com.android.ide.eclipse.adt.internal.project;
18
19import com.android.ide.eclipse.adt.AdtPlugin;
20
21import org.eclipse.core.resources.IMarker;
22import org.eclipse.core.resources.IProject;
23import org.eclipse.core.resources.IResource;
24import org.eclipse.core.runtime.CoreException;
25import org.eclipse.core.runtime.IProgressMonitor;
26import org.eclipse.core.runtime.IStatus;
27import org.eclipse.core.runtime.Status;
28import org.eclipse.core.runtime.jobs.Job;
29import org.eclipse.jdt.core.ClasspathContainerInitializer;
30
31/**
32 * Base CPC initializer providing support to all our initializer.
33 *
34 */
35abstract 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.isAccessible()) {
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                            if (project.isAccessible()) {
96                                project.deleteMarkers(markerType, true,
97                                        IResource.DEPTH_INFINITE);
98                            }
99                        } catch (CoreException e2) {
100                            return e2.getStatus();
101                        }
102
103                        return Status.OK_STATUS;
104                    }
105                };
106
107                // build jobs are run after other interactive jobs
108                markerJob.setPriority(Job.BUILD);
109                markerJob.schedule();
110            }
111        }
112    }
113}
114