12 - Eclipse Builders, Natures and Markers: Page 2 of 3

Markers

As described earlier, Markers are used mark locations in editor. Java editor uses these markers to mark error locations, warnings, book marks etc.

There are many types of predefined markers defined as constants in IMarker interface.

org.eclipse.core.resources.bookmarkIMarker.BOOKMARK 
org.eclipse.core.resources.markerIMarker.MARKER 
org.eclipse.core.resources.problemmarker IMarker.PROBLEM 
org.eclipse.core.resources.taskmarkerIMarker.TASK 
org.eclipse.core.resources.textmarkerIMarker.TEXT
 

Let’s review the plug-in manifest file to see what it takes to create a new marker. New marker is created by extending org.eclipse.core.resources.markers extension point. ID and Name are datavalue and name respectively. Point is nothing but the extension point we are using to create this marker.

       Eclipse new marker

Next, we need to define the super type marker for new marker. Since we want to show errors in Problems View we are using org.eclipse.core.resources.problemmarker as supertype.

        Eclipse Plugin defining supertype marker

Next is the persistent element. Since we want this markers to persist across eclipse sessions we are setting this property to true.

       Eclipse plugin setting the persistent property .

 

Reviewing the generated code

addMarker method in SampleBuilder class is used to show above defined marker in the editor whenever problem/error occurs.

private static final String MARKER_TYPE = "com.myplugin.rmp.xmlProblem";
private void addMarker(IFile file, String message, int lineNumber, int severity) {
              try {
                        IMarker marker = file.createMarker(MARKER_TYPE);
                        marker.setAttribute(IMarker.MESSAGE, message);
                        marker.setAttribute(IMarker.SEVERITY, severity);
                        if (lineNumber == -1) {
                                  lineNumber = 1;
                        }
                        marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
              } catch (CoreException e) {
              }
}

IMarker is the main interface when we are dealing with eclipse markers. Please refer to Eclipse API Specification to know more about IMarker Interface.

Like us on Facebook