FileEditor questions

Hi,

I want to create a plugin which should offer the ability to edit certain
xml files using an own FileEditor (like source editor or GUI builder
editor). But how to assoziate the file extension with my editor class,
so a double-click at the xml-file in the project will open my editor?

I've taken a look at ImageEditor from Alexey Efimov, but was not able to
open an editor by double-clicking images :(

Thanks in advance.

--
Cheers,
Tom

18 comments
Comment actions Permalink

I've taken a look at ImageEditor from Alexey Efimov, but was not able to
open an editor by double-clicking images :(


It must open... Hmm... That version of IDEA you test?

So, common logic is:
1. You implement FileEditorProvider
2. Check by IDEA request, can you edit file or not.
3. If your FileEditorProvider catch file for editing, then you may control
how it will be edited. As main editor, and disable other editors and as
additional editor - the editor will have tabs on botton.
Thanks!


0
Comment actions Permalink

It must open... Hmm... That version of IDEA you test?


Build 1178.

So, common logic is:
1. You implement FileEditorProvider
2. Check by IDEA request, can you edit file or not.
3. If your FileEditorProvider catch file for editing, then you may control
how it will be edited. As main editor, and disable other editors and as
additional editor - the editor will have tabs on botton.


I've created 3 implementations of
com.intellij.openapi.components.ApplicationComponent,
com.intellij.openapi.fileEditor.FileEditorProvider and
com.intellij.openapi.fileEditor.FileEditor. The content of the
META-INF/plugin.xml looks like:
<![CDATA[ FormBuilder FormBuilder Plugin 0.0.1 Thomas Singer de.regnis.formbuilder.FormBuilderApplicationComponent ]]>

But how do I tell IDEA the name of my FileEditorProvider?

Tom

0
Comment actions Permalink

Hello!

You must do implement both ApplicationComponent and FileEditorProvider for
your editor:

<![CDATA[
public class ImageEditorProvider implements ApplicationComponent,
FileEditorProvider {
  /** Valid extensions */
  private static final String[] extensions = {
    "png",
    "gif",
    "jpg",
    "jpeg"
  };

  // Here you must test accepting file by Your editor
  public boolean accept(Project project, VirtualFile virtualFile) {
    String ext = virtualFile.getExtension();
    if (Arrays.asList(extensions).contains(ext)) {
      // Extension is valid
      // Try to loading image
      try {
        ImageIcon imageIcon = new
ImageIcon(Streams.readBytes(virtualFile.getInputStream()));
        return imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE;
      } catch (Exception e) {
        logger.info("File " + virtualFile.getPath() + " is not a valid
image");
      }
    }
    return false;
  }

  // Here you create Editor for accepted file
  public FileEditor createEditor(Project project, VirtualFile virtualFile) {
    ImageEditor imageEditor = null;
    try {
      imageEditor = new ImageEditor(virtualFile);
    } catch (IOException e) {
      logger.error(e);
    }
    return imageEditor;
  }

  public void disposeEditor(FileEditor fileEditor) {
  }

  // Here you can read saved state (cursor position, etc)
  public FileEditorState readState(Element element, Project project,
VirtualFile virtualFile) {
    return new FileEditorState() {
    };
  }

  // Here you can write state (cursor position, etc)
  public void writeState(FileEditorState fileEditorState, Project project,
Element element) {
  }

  // It's a name of Editor (if policy allow to use more editors for one
  // file, then this string will show in botton tab)
  public String getEditorTypeId() {
    return "images";
  }

  // Here you define policy of your editor behavior
  // For example, this editor is disabled default editor, by ImageEditor
  public FileEditorPolicy getPolicy() {
    return FileEditorPolicy.HIDE_DEFAULT_EDITOR;
  }
}
]]>


This all that you need to create your ouw editor :)

Thanks!


0
Comment actions Permalink


Thomas Singer (MoTJ) wrote:

...
I've created 3 implementations of
com.intellij.openapi.components.ApplicationComponent,
com.intellij.openapi.fileEditor.FileEditorProvider and
com.intellij.openapi.fileEditor.FileEditor. The content of the
...
But how do I tell IDEA the name of my FileEditorProvider?


The trick is that the ApplicationComponent also has to implement the
FileEditorProvider, like this:

public class ExampleFileEditorProvider implements ApplicationComponent, FileEditorProvider { }

Then IDEA will find it by itself.


Sascha


0
Comment actions Permalink

It must open... Hmm... That version of IDEA you test?

>

Build 1178.


I check it just now, it work on double click and open images :)


0
Comment actions Permalink

Sorry, this does not work - neither with my plugin nor with imageEditor.
When I double click a file (which is shown as ) I get an "Register
New Extension" dialog, but there is no imageEditor available...

Maybe somebody from IntelliJ can comment on this issue?

Tom

0
Comment actions Permalink

Well, not here :( What icons your graphics have in IDEA's Project view?

Tom

0
Comment actions Permalink

Does it mean, IDEA calls accept(Project, VirtualFile) for all plugin's
Application-/ProjectComponents which implement FileEditorProvider and
takes the first one which returns true?

Tom

0
Comment actions Permalink


Thomas Singer (MoTJ) wrote:

Does it mean, IDEA calls accept(Project, VirtualFile) for all plugin's
Application-/ProjectComponents which implement FileEditorProvider and
takes the first one which returns true?


I'm pretty sure this is the way it works. However, for unknown file types you'll
always get IDEA's file-type selection box first. As far as I know there's currently
no way for an EditorProvider to register new file types, so this has to be done
manually once.

Sascha


0
Comment actions Permalink

Tom,

I have registered Custom type for binary... So, if you look for '?' as icon,
then you need register custom type.


0
Comment actions Permalink

Ah, OK, now it is working. I expected, that when IDEA shows the Register
New Extension dialog without any hint of the imageEditor, that IDEA did
not recognized that there is a custom file editor behind.

Any related RFE to vote for?

Tom

0
Comment actions Permalink

Any related RFE to vote for?


Any words of API of "FileTypeManager" was in NNTP, but Open API still not
allow do this from plugin.


0
Comment actions Permalink

Greetings!

I make it as workaround in PLUS. Take a look at PLUS 0.1.2 API.

Thanks!

0
Comment actions Permalink

See subject.

Tom

0
Comment actions Permalink

Hi,
IDEA handles document saving internally. Also you may look at com.intellij.openapi.fileEditor.FileDocumentManager may be it helps.

TIA,
Dmitry

0
Comment actions Permalink

Also you may look at com.intellij.openapi.fileEditor.FileDocumentManager may be it helps.


Could you please be a little bit more precise about the details? In what
relation FileEditor and Document stay? Could you please provide a simple
example, e.g. by using a JTextArea for editing text files? Thanks in
advance.

Tom

0
Comment actions Permalink

Hi,
Probaly like this:
By FileDocumentManager.getDocument(VirtualFile file) you'll get Document for your file. And after this you can do everithing you want with it.

Hope it helps.

TIA,
Dmitry

0
Comment actions Permalink

Where can I get this Image Editor code?

0

Please sign in to leave a comment.