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.resources.ResourcesPlugin;
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.core.runtime.IProgressMonitor;
27import org.eclipse.core.runtime.IStatus;
28import org.eclipse.core.runtime.Status;
29import org.eclipse.core.runtime.jobs.Job;
30import org.eclipse.jdt.core.ClasspathContainerInitializer;
31
32/**
33 * Base CPC initializer providing support to all our initializer.
34 *
35 */
36abstract 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            // Use a job to prevent requiring a workspace lock in this thread.
55            final String fmessage = errorMessage;
56            Job markerJob = new Job("Android SDK: Resolving error markers") {
57                @Override
58                protected IStatus run(IProgressMonitor monitor) {
59                    try {
60                        BaseProjectHelper.markProject(project,
61                                markerType,
62                                fmessage, IMarker.SEVERITY_ERROR,
63                                IMarker.PRIORITY_HIGH);
64                    } catch (CoreException e2) {
65                        AdtPlugin.log(e2, null);
66                        // Don't return e2.getStatus(); the job control will then produce
67                        // a popup with this error, which isn't very interesting for the
68                        // user.
69                    }
70
71                    return Status.OK_STATUS;
72                }
73            };
74
75            // build jobs are run after other interactive jobs
76            markerJob.setPriority(Job.BUILD);
77            markerJob.setRule(ResourcesPlugin.getWorkspace().getRoot());
78            markerJob.schedule();
79        } else {
80            // Use a job to prevent requiring a workspace lock in this thread.
81            Job markerJob = new Job("Android SDK: Resolving error markers") {
82                @Override
83                protected IStatus run(IProgressMonitor monitor) {
84                    try {
85                        if (project.isAccessible()) {
86                            project.deleteMarkers(markerType, true,
87                                    IResource.DEPTH_INFINITE);
88                        }
89                    } catch (CoreException e2) {
90                        AdtPlugin.log(e2, null);
91                    }
92
93                    return Status.OK_STATUS;
94                }
95            };
96
97            // build jobs are run after other interactive jobs
98            markerJob.setPriority(Job.BUILD);
99            markerJob.setRule(ResourcesPlugin.getWorkspace().getRoot());
100            markerJob.schedule();
101        }
102    }
103}
104