Andrew Gacek
Andrew Gacek

Reputation: 98

Check for project errors before performing action

I have an eclipse plug-in which provides a menu item which can be selected to run a command on the currently active file. I would like the plug-in to display a warning message if the currently active file has any errors on it (as reported in the Problems view), similar to how Eclipse acts when you try to run a java project with errors.

Upvotes: 1

Views: 66

Answers (2)

jadkik94
jadkik94

Reputation: 7078

I know it's an old question, but I found a solution similar to the one proposed. The code that does what you descrive is in org.eclipse.debug.core.model.LaunchConfigurationDelegate. It checks if the project has errors and show the dialog if needed. Here is the relevant code, from Eclipse Luna:

/**
 * Returns whether the given project contains any problem markers of the
 * specified severity.
 *
 * @param proj the project to search
 * @return whether the given project contains any problems that should
 *  stop it from launching
 * @throws CoreException if an error occurs while searching for
 *  problem markers
 */
protected boolean existsProblems(IProject proj) throws CoreException {
    IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
    if (markers.length > 0) {
        for (int i = 0; i < markers.length; i++) {
            if (isLaunchProblem(markers[i])) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Returns whether the given problem should potentially abort the launch.
 * By default if the problem has an error severity, the problem is considered
 * a potential launch problem. Subclasses may override to specialize error
 * detection.
 *
 * @param problemMarker candidate problem
 * @return whether the given problem should potentially abort the launch
 * @throws CoreException if any exceptions occur while accessing marker attributes
 */
protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException {
    Integer severity = (Integer)problemMarker.getAttribute(IMarker.SEVERITY);
    if (severity != null) {
        return severity.intValue() >= IMarker.SEVERITY_ERROR;
    }

    return false;
}

The same code can run on any IResource instead of an IProject.

I managed to find it easily by suspending from the debugger when the dialog was shown and setting a breakpoint on the relevant class and tracing back from there.

Upvotes: 1

sgibly
sgibly

Reputation: 3838

Errors are usually saved as IMarkers on a resource (IFile in your case), so you can query the IFile for the markers you are looking for.

You'll need to know the type of the markers before the look-up (either by debugging and get all the current markers, or by looking at the code the contributed them in the validation process of the file).

Hope that helps.

Upvotes: 0

Related Questions