简体   繁体   中英

close Eclipse editor if file is open twice

I´m creating my own editor here for Eclipse and found a problem. If I open a file and it´s already open, Eclipse opens a new editor.

So, I need to either avoid this or close the editor right after it´s opened.

My editor class is a child of MultiPageEditorPart and it has 2 tabs: first one is a Java editor and second one is a text editor. Java editor opens up a .java file and text editor opens up my own file. I saw some posts saying about how to fix this, but I don´t implement IEditorInput interface anywhere here.

Can anyone help me with this? Thanks a lot

Here´s my editor definition:

    <extension point="org.eclipse.ui.editors">
    <editor id="br.com.senior.wb.asas.editor.AsasEditor"
        class="br.com.senior.wb.asas.editor.AsasEditor"
        contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
        extensions="java, afm"
        icon="icons/editor_asas.png" name="Editor ASAS">
    </editor>
</extension>

If you mean you want to open one editor when either the java or afm file is opened when the other file is already open then you need to use the matchingStrategy attribute of the editor definition to define an editor matching strategy.

Something like:

 <extension point="org.eclipse.ui.editors">
    <editor id="br.com.senior.wb.asas.editor.AsasEditor"
        class="br.com.senior.wb.asas.editor.AsasEditor"
        contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
        matchingStrategy="br.com.senior.wb.asas.editor.AsasEditorMatchingStrategy"
        extensions="java, afm"
        icon="icons/editor_asas.png" name="Editor ASAS">
    </editor>
</extension>
public class AsasEditorMatchingStrategy implements IEditorMatchingStrategy
{ 
  public boolean matches(IEditorReference editorRef, IEditorInput input)
  {
    if (!(input instanceof IFileEditorInput))
      return false;

    IFile inputFile = (IFile)input.getAdapter(IFile.class);
    if (inputFile == null)
      return false;

    IFile currInputFile = (IFile)editorRef.getEditorInput().getAdapter(IFile.class);
    if (currInputFile == null)
      return false;

    if (!inputFile.getProject().equals(currInputFile.getProject()))
      return false;

    // TODO add more checks that 'inputFile' and 'currInputFile' are a matching pair of files
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM