Trying to switch editor input from within setInput()

144 Views Asked by At

I have an Eclipse editor that modifies multiple files. When there is an error in any one of these files, I want it to open MyEditor instead of the standard text editor that it might otherwise open.

I forced the marker to open MyEditor by setting the marker attribute:

marker.setAttribute(IDE.EDITOR_ID_ATTR, "com.example.xyz.MyEditorID");

Now, within MyEditor.setInput(), I have the following code to switch the input from the incoming file to the main file:

    protected void setInput(final IEditorInput input) {
        FileEditorInput fileEditorInput = (FileEditorInput) input; 
        final IFile file = (IFile) input.getAdapter(IFile.class);
        if( file.getName().endsWith(mySuffix) ) {
        // this is a coded values type file. It needs to be opened with the default profile
            fileEditorInput = new FileEditorInput(theCorrectFileName);
        }
        ... 
        super.setInput(fileEditorInput);
        ...
    }

I changed the Editor's equals method to return true if the inputs are equal

public boolean equals(final Object obj) {
    if(obj == null) {
        return false;
    }

    if(this == obj) {
        return true;
    }

    if( !getClass().isInstance(obj) ) {
        return false;
    }
    final MyEdior otherEditor = (MyEditor) obj;
    return getEditorInput().equals( otherEditor.getEditorInput() );
}

But when I click on my error marker, it still opens up a new editor every time. What am I doing wrong?

1

There are 1 best solutions below

1
On BEST ANSWER

The equals method is not used to match the editor to the editor input. Instead you need to use an IEditorMatchingStrategy class to match your editor to the input.

You specify this class on your org.eclipse.ui.editors extension point using the matchingStrategy attribute.

For example, this is the Manifest.mf / plugin.xml / build.properties editor:

<extension
     point="org.eclipse.ui.editors">
  <editor
        default="true"
        name="%editors.pluginManifest.name"
        icon="$nl$/icons/obj16/plugin_mf_obj.gif"
        class="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditor"
        contributorClass="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditorContributor"
        matchingStrategy="org.eclipse.pde.internal.ui.editor.plugin.ManifestEditorMatchingStrategy"
        id="org.eclipse.pde.ui.manifestEditor">
        <contentTypeBinding contentTypeId="org.eclipse.pde.pluginManifest"/>
        <contentTypeBinding contentTypeId="org.eclipse.pde.fragmentManifest"/>
        <contentTypeBinding contentTypeId="org.eclipse.pde.bundleManifest"/>            
  </editor>

The matching strategy just has the one method to match a editor reference to the input:

public boolean matches(IEditorReference editorRef, IEditorInput input)

All this is intended for editors (such as the Manifest.mf editor) which edit several files at once, but I think you may be able to use it.