2014-01-21 143 views
0

我正在设计一个Eclipse插件,它围绕一个新的视角设计,并带有一个编辑器,该编辑器在突出显示它们时存储代码/评论片段。其中的部分包括:视角,编辑器和一个mouselistener。IWorkbenchPart.openEditor()不打开自定义编辑器

我有视角,可以打开它。我编辑了编辑器类代码,但是,通过编程方式通过IWorkbenchPart.openEditor()打开编辑器,我的自定义编辑器似乎没有以任何方式初始化。只有默认的Eclipse编辑器出现。我可以说,因为我的自定义鼠标事件不会触发。

我用vogella tutorial作为参考。

为什么我的编辑器的init()方法在打开时不会被调用?我可以告诉它,因为init()createPartControl()中的打印语句都未执行。

在谷歌搜索这个问题,我发现了一些命中,但他们都围绕遇到的错误消息(无法找到编辑器,无法找到文件,...)。我没有收到错误消息,只是意外的行为。所以那些文章是无用的。

(我会非常喜欢TextViewer代替,因为我不想让他们编辑的内容在此模式下,无论如何,但我决定在这里开始。)下面

代码。

视角:

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); 
      } 

    } 

} 

编辑:(删除了其他基本编辑器存根(isDirty,DoSave就会),因为它们是不相关)

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(); 
    } 

} 

回答

1

有您注册的plugin.xml内编辑器?

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

另外,您可能希望执行IEditorInput来为您的编辑器提供特定的输入。

+0

是的,先生,我有。对于它的价值,我会在回家后用我的.xml进行编辑。然而,我敢肯定,我误解了一个IEditorInput是什么 - 一个关于vogella的例子似乎没有什么重要,进一步的研究/试验错误只是告诉我你想用edtior打开什么文件? – unfuse

+0

作为名称的编辑器输入暗示为您的编辑器提供了自定义输入。在'init'方法中,您可以将编辑器输入转换为您自己的实现,检索必要的数据并相应地更新编辑器。例如,您可以在编辑器输入中提供一些用户标识,然后在init方法中获取它,通过此标识从DB加载用户,并在编辑器中显示有关具体用户的信息。另外,编辑器输入用于从备忘录中恢复编辑器(基于可能创建编辑器输入的备忘录信息)。但也检查plugin.xml,可能是你有一个拼写错误的地方。 –

+0

即使我确定eclipse自动为我添加了.xml文件,但是对于EditorInput更多 - 因为我想要做的就是在当前文件中显示原始文本,我不应该需要自定义EditorInput , 对?否则,我可以再次尝试使用无操作编辑器输入。 – unfuse