unfuse
unfuse

Reputation: 35

IWorkbenchPart.openEditor() not opening custom editor

I'm designing an Eclipse plugin designed around a new perspective with an editor that stores code/comment snippets upon highlighting them. The parts to it include: the perspective, the editor, and a mouselistener.

I have the perspective made and can open it. I have the editor class code constructed, however, on programmatically opening the editor via IWorkbenchPart.openEditor() my custom editor does not seem to be initialized in any way. Only the default Eclipse editor appears. I can tell because my custom mouse events do not fire.

I used the vogella tutorial as a reference.

Why is my editor's init() method not being called upon being opened? I can tell it is not since the print statement in both init() and createPartControl() are not executed.

In googling this problem, I found a number of hits but they all revolved around error messages encountered (can't find editor, can't find file, ...). I am getting no error messages, just unexpected behaviour. So those articles were useless.

(I would ideally like a TextViewer instead, since I don't want them editing the contents in this mode anyway, but I decided to start here.)

Code below.

Perspective:

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class PluginPerspective implements IPerspectiveFactory {

    @Override
    public void createInitialLayout(IPageLayout layout) {
        layout.setEditorAreaVisible(true);
        layout.setFixed(true);

        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorInput iei = page.getActiveEditor().getEditorInput();
        try
        {
            // Open Editor code nabbed from Vogella tutorial. 
            // He creates an action to do so - I force it to happen when the 
            //   perspective is created.

            // I get the name of the current open file as expected.
            System.out.println(iei.getName());
            page.openEditor(iei, myplugin.PluginEditor.ID, true);
            // This message prints, as expected.
            System.out.println("open!");
        } catch (PartInitException e) {
            throw new RuntimeException(e);
          }

    }

}

Editor: (Removed the other basic editor stubs (isDirty, doSave) since they are not pertinent)

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.texteditor.ITextEditor;

public class PluginEditor extends EditorPart implements MouseListener {

    public static final String ID = "myplugin.plugineditor";

    @Override
    public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
        // TODO Auto-generated method stub

        System.out.println("editor init!");
        setSite(site);
        setInput(input);

    }

    @Override
    public void createPartControl(Composite parent) {
        // TODO Auto-generated method stub

        System.out.println("editor partcontrol!");
        //TextViewer tv = new TextViewer(parent, 0);

        //tv.setDocument(getCurrentDocument());
    }

    @Override
    public void mouseDoubleClick(MouseEvent e) {
        // TODO Auto-generated method stub
        // nothing?
    }

    @Override
    public void mouseDown(MouseEvent e) {
        // TODO Auto-generated method stub
        // grab start location?
        System.out.println("down!");
    }

    @Override
    public void mouseUp(MouseEvent e) {
        // TODO Auto-generated method stub
        // do stuff!
        System.out.println("up!");
    }

    // to be used for grabbing highlight-selection grabbing later
    public IDocument getCurrentDocument() {
        final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .getActiveEditor();
        if (!(editor instanceof ITextEditor)) return null;
        ITextEditor ite = (ITextEditor)editor;
        IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
        return doc;
        //return doc.get();
    }

}

Upvotes: 0

Views: 825

Answers (1)

Alex K.
Alex K.

Reputation: 3304

Have you registered your editor within your plugin.xml?

  <extension
         point="org.eclipse.ui.editors">
         <editor
               default="false"
               id="myplugin.plugineditor"
               name="name">
         </editor>
   </extension>

Also, you may want to implement IEditorInput to have specific input for your editor.

Upvotes: 1

Related Questions