Eclipse Search

Loading

Nov 20, 2008

Creating a custom Marker View

Do you know that creating a custom markers view is very simple and requires very less code? I didn't know it till few weeks back. I've been exploring this Marker view since then. It may not be very useful in an Eclipse/Eclipse based IDE, as we already have Problems View, Markers View and Task View. But this might come handy in an RCP where you want to have more control. I'll explain how to create a basic markers view and then move on to customize it. You would at least require Ganymede (Eclipse 3.4) for all the samples here to work.
First lets create a view:

<extension
      point="org.eclipse.ui.views">
   <view
         class="com.eclipse_tips.markers.MyCustomMarkersView"
         icon="icons/sample.gif"
         id="com.eclipse-tips.markers.customMarker"
         name="My Custom Markers">
   </view>
</extension>


There is no much code in the View class, its just a constructor:

public class MyCustomMarkersView extends MarkerSupportView {
    public MyCustomMarkersView() {
        super("com.eclipse-tips.markers.myCustomMarkerGenerator");
    }

}

The string that we pass in the constructor is the id of the content generator. It should be defined thru the extension point org.eclipse.ui.ide.markerSupport:

<extension
      point="org.eclipse.ui.ide.markerSupport">
   <markerContentGenerator
         id="com.eclipse-tips.markers.myCustomMarkerGenerator"
         name="My Marker Generator">
        <markerTypeReference
             id="org.eclipse.core.resources.marker"/>
        <markerFieldReference
             id="org.eclipse.ui.ide.descriptionField"/>
    </markerContentGenerator>
</extension>


The above is the minimum configuration the marker view requires. The markerContentGenerator needs at least one markerTypeReference and one markerFieldReference. Before digging into what all that refers to, lets have a look at the custom marker view:

Custom Markers View
As you have seen in the above picture, the table just displays only one field - Description. If we want more fields, we need to add more markerFieldReferences. Eclipse provides you most frequently used fields like severity, icon, location, etc. We can add all those fields:

<markerContentGenerator
      id="com.eclipse-tips.markers.myCustomMarkerGenerator"
      name="My Marker Generator">
   <markerTypeReference
         id="org.eclipse.core.resources.marker"/>
   <markerFieldReference
         id="org.eclipse.ui.ide.severityAndDescriptionField"/>
   <markerFieldReference
         id="org.eclipse.ui.ide.locationField"/>
   <markerFieldReference
         id="org.eclipse.ui.ide.markerType"/>
   <markerFieldReference
         id="org.eclipse.ui.ide.priorityField"/>
   <markerFieldReference
         id="org.eclipse.ui.ide.resourceField"/>
</markerContentGenerator>


Now our view looks like:
image

Note earlier we used org.eclipse.ui.ide.descriptionField field, so just the description was displayed. To get the icon and the description, we need to use the org.eclipse.ui.ide.severityAndDescriptionField.

So far we have been using the fields defined by Eclipse. Creating our own field is also possible.

Lets try to create a field called Project, which will display the project name of the marker's resource. You need to extend org.eclipse.ui.ide.markerSupport's markerField:

<markerField
      class="com.eclipse_tips.markers.ProjectField"
      id="com.eclipse-tips.markers.projectField"
      name="Project">
</markerField>


In the class, you just need to extend one method - getValue()

public String getValue(MarkerItem item) {
    return item.getMarker().getResource().getProject().getName();
}


The MarkerItem is the UI representation of IMarkers (and the categories as well). Once you get the IMarker, you can navigate and get the project name. Now to add this field in our view, just add a markerFieldReference in our markerContentGenerator:

<markerContentGenerator
      id="com.eclipse-tips.markers.myCustomMarkerGenerator"
      name="My Marker Generator">
    .. other items ...
   <markerFieldReference
         id="com.eclipse-tips.markers.projectField"/>

image

30 comments:

  1. I am using the 3.4 on Mac, but can not seem to find the dependency required for this to work. Which plugin should I install and include?

    ReplyDelete
  2. Still not working, according to the docs it should be in internal.views.markers ? But I can not add this in dependencies list.

    http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/part/class-use/ViewPart.html

    ReplyDelete
  3. Solved it. I downloaded an org.eclipse.ui.ide_3.3 from here http://www.java2s.com/Code/Jar/Eclipse/Downloadorgeclipseuiide330I20070620jar.htm

    Restarted eclipse and it worked.

    ReplyDelete
  4. @Alex
    Extensibility of marker views was added since 3.4.

    ReplyDelete
  5. Thanks for this tutorial. It was really helpfull and prevents me of coding something like this on my own.

    A link (or tutorial) on how to create and use IMarker (org.eclipse.core.resources) and own markers would be a nice addon.
    Unfortunaly, I found something only in the eclipse book and cant provide you a link.

    ReplyDelete
  6. Thanks so much for this tutorial. I have two questions in addition:

    1. When following this tutorial everything works fine except for the icon (error, warning) which is not displayed. Do i have to copy them to my own project or should be references automagically?

    2. Is it possible to define the column width in advance? In my case the first column (severity) should have a width of 100px and the rest should be for column two.

    Cheers,

    Thomas E.-E.

    ReplyDelete
  7. The problem comes when you want to open the mark with your own IEditorInput ...

    ReplyDelete
  8. hi am confused with this example, what is that MarkerSupportView class is it a standard API or the customized class which is extending ViewPart, ans where is the constructor with that string holding id of the markercontentgenerator defined, please help me . . . .

    ReplyDelete
  9. @Anonymous,
    Yes that class is an API and is in the org.eclipse.ui.ide plugin. Its available from 3.4 onwards

    ReplyDelete
  10. Hello,

    I have a question about refreshing views. In my plugin I create problemmarkers for some resources and they immediately appear on the bar next to line number. However, sometimes they don't appear in the Marker View at the same time - sometimes I have to wait a while for them to be displayed. Does anyone have any idea how can I provide immediate feedback inside the Marker View as well?

    Thanks in advance

    ReplyDelete
  11. @Blekit,
    AFAIK, Problem marker is not updating itself in realtime. There is some time delay associated with the update job. I'm not sure of the details and don't think its a configurable item. However, try the newsgroups, someone might know a way to do it

    ReplyDelete
  12. How are the markers created and added to the view. A code sample would be very helpful. There is no example or documentation on how the view is filled with items. I can create the view with the required columns, but I don't know how to add items. Any help is appreciated.

    ReplyDelete
  13. @Anonymous,
    Markers are created by IResource.createMarker() method

    ReplyDelete
  14. Tnx for the tutorial!
    I have a question about the menu from the standard problemsview.
    I really would like my custom problems view to inherit the menu contributions from the standard org.eclipse.ui.views.ProblemView.

    Do i have to create my own menu contribution or is there a simpler way?

    ReplyDelete
  15. Great article, worked a treat :-)

    ReplyDelete
  16. And is it possible, to add a listener that would react when user selects/double-clicks marker presented in custom view?

    ReplyDelete
  17. @Michal,
    I don't see a way to do it. Unless you get access to the viewer field in the super class thru reflection

    ReplyDelete
  18. how can I change the name of the column?

    ReplyDelete
  19. @Anon,
    The name is contributed thru the markerField extension point. You have to change that.

    ReplyDelete
  20. I share the very same problem with Tee:
    1. When following this tutorial everything works fine except for the icon (error, warning) which is not displayed. Do i have to copy them to my own project or should be references automagically?

    All icons are shown as small red boxes ...
    Any hint?

    ReplyDelete
  21. Wish you would have provided complete example, which newbe can download and try on its own. Most of the information is in bits and pieces and one will have to spend another few nights figuring out how to fit these pieces together to make sense ..

    ReplyDelete
  22. @Anonymous,
    I'll try to post the new blog entries with complete executable samples.

    ReplyDelete
  23. Hey i have copied the same code. The My custom marker view is visible but i cant see the errors in the Marker view. I have also added the org.eclipse.ui.ide_3.3.0.I20070620.jar and restarted ..but still doesnt work.. Also can u please provide help on how to add our own resolutions for the marker..any help will be highly appreciated..thanks :)

    ReplyDelete
  24. Thanks for the article.It worked for me till all default fields.But for own field(i.e. project field) how to create a ProjectField class ,how to connect it with MyCustomMarkersView class and where to add
    public String getValue(MarkerItem item) {
    return item.getMarker().getResource().getProject().getName();
    }

    ReplyDelete
  25. Thank you for the tutorial ^^

    I tried it and all works :)

    Anyway, do you know if does exist a way to add the standard problem view menu ( with sort, group by and filters ) to the custom view?

    Thanks, Marco.

    ReplyDelete
  26. @Gaucho,

    Not sure. You can ask the question in the Forums, if anyone knows you would get a reply. If such a feature doesn't exist and you want to have it, raise a bug: http://bit.ly/NewBugInPlatformUI

    ReplyDelete
  27. Could you please give some information on how to register our own context menu, and also add a listener to the Marker Support View

    ReplyDelete
  28. Thanks, Prakash, for the nice tutorial. Still works in Eclipse Juno :)
    One question: is it possible to activate Quick Fix functionality in a custom marker view? In the Problems view it's possible to press Ctrl+1 if a quick fix is available, or do a right-click and select "quick fix". It would be nice to have this functionality available, too, in my custom marker view.

    ReplyDelete
  29. Great tutorial thanks! I have the same questions as a few of the other posters regarding the "Configure contents" filtering options which do not show up. I have posted a query in the forums, if I get a response I will post that here as well.

    ReplyDelete