简体   繁体   English

从Eclipse插件中,我如何以编程方式使Eclipse编辑某个文件?

[英]From an Eclipse plugin, how can I programmatically cause Eclipse to edit a certain file?

I have a plugin which creates a new view. 我有一个插件,可以创建一个新视图。 In my view I display certain information about certain Java classes in the project. 在我看来,我显示了项目中某些Java类的某些信息。 I want to allow the user to double click on a class in my view and when s/he does, I want to open that class for editing in the editor. 我希望允许用户在我的视图中双击一个类,当他/她这样做时,我想在编辑器中打开该类进行编辑。 Basically similarly to what the Hierarchy View does: it displays the tree of classes and when the user double-clicks on one of them, it goes into the editor. 基本上类似于层次结构视图的作用:它显示类树,当用户双击其中一个时,它进入编辑器。 How do I do that if what I have is an object of type IType ? 如果我拥有的是IType类型的对象,我该怎么做?

I will be showing a high-level implementation for your requirement. 我将根据您的要求展示高级实现。 You have to do following steps. 您必须执行以下步骤。

  • first you have to do entry in Plugin.xml for the class which extends viewPart and editorPart for a view and editor respectively. 首先,您必须在Plugin.xml为该类进行输入,该类分别为视图和编辑器扩展viewParteditorPart
  • for the hierarchy view, you have to load the information with a tree structure in ViewPart . 对于层次结构视图,您必须在ViewPart使用树结构加载信息。
  • On a node's double click Listener you have to open the editor. 在节点上双击Listener,您必须打开编辑器。

To open a file in the editor, use the following code to open the editor. 要在编辑器中打开文件,请使用以下代码打开编辑器。

 if (fileToOpen.exists() && fileToOpen.isFile()) {
     String path = //Path for that to file to open;
     IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
     URI fromString = org.eclipse.core.runtime.URIUtil.fromString("file://" + path);
     try {
         IEditorPart openEditor = IDE.openEditor(page, fromString, Editor.ID, true);
         IEditorInput editorInput = openEditor.getEditorInput();
         //editorInput.
     } catch ( PartInitException e ) {
         //Put your exception handler here if you wish to.
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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