Home > database >  How to add a simple tab in Intellij Idea plugin
How to add a simple tab in Intellij Idea plugin

Time:03-26

I am creating a simple class diagram plugin for Intellij Idea. I'm struggling now with creating a simple tab in IDE. This tab I will fill up with a prepared JPanel and nothing else. I have already done the same in NetBeans and I would like to find something with similar behavior as TopComponent in NetBeans provides, but anything working would be cool.

CodePudding user response:

So here is the answer:

  1. create implementation of com.intellij.openapi.fileEditor.FileEditor. This is your actual tab

  2. create implementation of com.intellij.openapi.fileEditor.FileEditorProvider accept() defines type of files which your editor opens create() should returns the proper instance of your editor

  3. register your FileEditoProvider in plugin.xml

Editor:

public class YourEditor implements FileEditor {

    private VirtualFile file;
    
    public YourEditor(VirtualFile file) {
        this.file = file;
    }

    @Override
    public @NotNull JComponent getComponent() {
        JPanel tabContent = new JPanel();
        tabContent.add(new JButton("foo"));
        return tabContent;
    }

    @Override
    public @Nullable JComponent getPreferredFocusedComponent() {
        return null;
    }

    @Override
    public @Nls(capitalization = Nls.Capitalization.Title)
    @NotNull String getName() {
        return "name";
    }

    @Override
    public void setState(@NotNull FileEditorState fileEditorState) {
    }

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void addPropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {
    }

    @Override
    public void removePropertyChangeListener(@NotNull PropertyChangeListener propertyChangeListener) {
    }

    @Override
    public @Nullable FileEditorLocation getCurrentLocation() {
        return null;
    }

    @Override
    public void dispose() {
        Disposer.dispose(this);
    }

    @Override
    public <T> @Nullable T getUserData(@NotNull Key<T> key) {
        return null;
    }

    @Override
    public <T> void putUserData(@NotNull Key<T> key, @Nullable T t) {

    }

    @Override
    public @Nullable VirtualFile getFile() {
        return this.file;
    }
}

Provider:

public class YourEditorProvider implements FileEditorProvider, DumbAware {

    private static String EDITOR_TYPE_ID = "DiagramView";

    @Override
    public boolean accept(@NotNull Project project, @NotNull VirtualFile virtualFile) {
        return true; //will accept all kind of files, must be specified
    }

    @Override
    public @NotNull
    FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile virtualFile) {
        return new YourEditor(virtualFile);
    }

    @Override
    public @NotNull
    @NonNls
    String getEditorTypeId() {
        return EDITOR_TYPE_ID;
    }

    @Override
    public @NotNull
    FileEditorPolicy getPolicy() {
        return FileEditorPolicy.HIDE_DEFAULT_EDITOR;
    }
}

and finally put FileEditorProvider extension in pluxin.xml:

<extensions defaultExtensionNs="com.intellij">
    <fileEditorProvider implementation="classDiagramPainter.DiagramViewProvider"/>
</extensions>
  • Related