views:

197

answers:

2

Hello everyone.

I seem to be having a problem with associating a problem marker with a resource; in my case, I'm trying to create a problem marker for the editor.

To achieve this, I've tried to do the following:

public class MyEditor extends TextEditor{

private ColorManager colorManager;

public MyEditor() {
         super();
         ...

         IResource resource = (IResource) getEditorInput().getAdapter(IResource.class);

         try 
         {
             marker = resource.createMarker(IMarker.PROBLEM);            
         }
         catch (CoreException e) 
         {
             e.printStackTrace();
         }
    }

However, the problem is getEditorInput() keeps returning null. I assume I am not calling it at the right location. I thought it would be ideal to create the marker once I'm setting up the editor, but this proves otherwise.

Does anyone have any advice to obtaining the proper resource I want so that I may create the problem marker? I would like to show errors and such within the editor.

I've looked at samples online for creating the marker, but most just show methods that pass the ITextEditor object without showing where the method call is. (for example: Creating Error Marker for Compiler -- see reportError method)

Thank you. Paul

Edit: I have also viewed the following link regarding problem markers, but again, it calls createMarker from a resource(res, in this case), but does not show the setup for it. See Show Syntax Errors in An Eclipse Editor Plugin

A: 

I create a marker (including a call to getEditorInput()) from the run() method of an Action object.

public class MyAction extends Action {
   ...
   public void run() {
     ...

     int line = ...;
     IEditorInput ei = editor.getEditorInput()
     if (ei != null)
        createMarkerAt(line, ei);
   }    
}

Addition (Following Paul's comment) How to get an Editor?

Well, In my app I am subclassing AbstractRulerActionDelegate, by overriding the createAction(ITextEditor e, IVerticalRulerInfo ri) method (which, BTW, Is a must - this method is abstract) my app can get the relevant ITextEditor object.

Itay
Thanks for the response!Within the run() function, you use the 'editor' object; however, where exactly were you able to obtain it from?
Paul Reisert
Please see my addition.
Itay
Thanks again for response.Is it necessary to subclass from AbstractRulerActionDelegate?I thought there could be a way to create the Marker within my Repair class(implements IPresentationRepairer) somehow. That's where I figure out errors and can report them to the editor(if I could obtain a reference to it).
Paul Reisert
No it is not necessary, in my code I needed an ruler action so I subclassed that class which also gave me an access to the editor. Why don't you give us a more complete description of your problem - what are you trying to achieve so that it will be easier to provide a precise answer?
Itay
+1  A: 

EditorInput is initialize in init method You can override init or

public class MyEditor extends TextEditor{

private ColorManager colorManager;

public MyEditor() {
         super();
         ...
    }

public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
         super.init(site, input);
         IResource resource = (IResource) getEditorInput().getAdapter(IResource.class);

         try 
         {
             marker = resource.createMarker(IMarker.PROBLEM);            
         }
         catch (CoreException e) 
         {
             e.printStackTrace();
         }
}
dpndeveloper
Worked perfectly, thanks!So I assume I was getting null before because it was calling the getEditorInput() too early?
Paul Reisert
Sorry for my so late response... You assumed right... You was calling getEditorInput before init(...) method, that initialize input
dpndeveloper