Search code examples
eclipseeclipse-plugineclipse-jdt

Issue while opening Marker in an editor programatically


I am trying to open a marker, while double-clicking on an entry from a TableViewer, inside an eclipse plug-in. I am able to get the associated resource from the marker, however nothing is happening while the openEditor method is executed.

The code is as below:

  viewer.addDoubleClickListener(new IDoubleClickListener() {
      public void doubleClick(DoubleClickEvent event) {
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            try {
                IStructuredSelection sel = (IStructuredSelection) event.getSelection();
                ReviewIssue reviewIssue = (ReviewIssue) sel.getFirstElement();
                if(reviewIssue != null){
                    MessageDialog.openError(window.getShell(), "Insta Review", reviewIssue.getMarker().getResource());
                    try {
                        IDE.openEditor(window.getActivePage(), reviewIssue.getMarker(), true);
                    } catch (PartInitException e) {
                        MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
                    }
                } 
            } catch (Exception e) {
                MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
            }
        }
  });

Please let me know, if am missing something here. Thanks in advance.
Also ignore the message dialogs, as I plan to implement the logging functionality later.


UPDATE:

Even though I created the marker on IFile, I was getting the same behaviour. I was finally able to open the editor by using the IFile, instead of the marker.

IFile iFile = markerProject.getFile(path);
//IMarker marker = iFile.createMarker("id.myMarker");
.....
IDE.openEditor(window.getActivePage(), reviewIssue.getiFile(), true);
//IDE.openEditor(window.getActivePage(), reviewIssue.getMarker()), true);

Solution

  • For this to work the IMarker.getResource() method must return an IFile. The code in IDE.openEditor is:

    // get the marker resource file
    if (!(marker.getResource() instanceof IFile)) {
        IDEWorkbenchPlugin
                    .log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
        return null;
    }
    

    so look in the .log file in the workspace .metadata directory to see if you are getting that log message.

    Normally you would create a marker for a file using the IFile.createMarker method (createMarker is actually an IResource method).