eclipse插件开发-编辑器详解

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

rsistableElement {
2 private IFile file;
3
4 public boolean exists() {
5
return file.exists();
6}
7}
我们看到内部的实现是持有了 IFile 句柄,如果 IFile 代表的资源没有存在于工作区 之内,那么就会返回 false。(疑问:如果我们打开工作区外部的文件呢???显然, FileEditorInput 并不合适,稍后看...)
<!-- =============================================
====================================== -->
<!-- Extension: Java Content Types
-->
<!-- =============================================
【IDE 工具类提供的接口】 上面 IWorkbenchPage 提供接口都需要用户准备两样东西:一是创建
IEditorInput 实例,二是指定 editor id。有些用户可能不想干这两件事情,所以在工具类 org.eclipse.ui.ide.IDE 中提供了其他的接口: 1 public static IEditorPart openEditor(IWorkbenchPage page, IFile input) throws PartInitException { } 2 3 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, boolean activate) throws PartInitException { } 4 5 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, boolean activate, boolean determineContentType) { }
上面 5 个接口操作中, 对于上面的三个操作,Eclipse 会自动为你准备 IEditorInput 实例,并动态绑定合适的编辑器类型。对于下面的两个操作,Eclipse 会为你 自动准备 IEditorInput 实例,但是需要用户自己指定 editor id。
接下来我们看两个问题,一是如何创建 IEditorInput 实例的;而是如何动态计算 对应的 editor id 的。
2
// Try file specific editor.
3
IEditorRegistry editorReg = PlatformUI.getWorkbench()
4
.getEditorRegistry();
5
try {
6
String editorID = file.getPersistentProperty(EDITOR_KEY);
7
if (editorID != null) {
8
IEditorDescriptor desc = editorReg.findEditor(editorID);
9
if (desc != null) {
10
return desc;
11
}
12
}
13
} catch (CoreException e) {
eclipse 插件开发--打开编辑器(上)
【打开 editor 的接口讨论】 先来看一下 workbench 吧,workbench 从静态划分应该大致如下:
从结构图我们大致就可以猜测出来,workbench page 作为一个 IWorkbenchPart(无论是 eidtor part 还是 view part)的容器,肯定会接受 workbench page 的管理。看了一下,IWorkbenchPage 接口定义中确实提供给了如下打开编辑器的 操作 :
====================================== -->
<extension point="org.eclipse.core.runtime.contentTypes">
<!-- declares a content type for Java Properties files -->
பைடு நூலகம்
22
return editorReg.getDefaultEditor(file.getName(), contentType);
23 }
上面的代码大致赶了如下两件事情: 1、如果对应的资源设定了一个特定的持久化属性 EDITOR_KEY,则会使用 EDITOR_KEY 属性值所代表的编辑器(说明:有关 Eclipse 资源的属性支持,请参阅其他 文档)。那如果一个资源不在工作区之内,又如何设定 EDITOR_KEY 属性呢??? (~ _~确实没法设定) 2、查找对应的 content type,用户通过 org.eclipse.core.runtime.contentTypes 扩展点来注册自定义的内容类型,在内容类型 中会指定对应的文件扩展名和默认编码,例如 JDT 中注册了如下内容类型(摘自 org.eclipse.jdt.core/plugin.xml):
用就是让用户获取到 workbench。Eclipse 中存在的其他一些门面类如:ResourcesPlugin、 Platform、JavaCore、JavaUI 等)
我们再仔细看一下 IWorkbenchPage 对应的实现类 (org.eclipse.ui.internal.WorkbenchPage)中的以上接口的实现代码,真正在管理 Editor 的是一个叫做 EditorManager 的东东(同理,view part 对应的管理器角色类是叫 做 ViewFactory 的东东)。这里的 EditorManager 和 View Factory 是 workbench 实现中非常精华的部分,看一下里面的实现就会很大程度上理解 workbench 所谓懒加载、 懒初始化是如何实现的了,如何实现 part 复用的...等等。
【IWokbenchPage 提供的接口】 1 public interface IWorkbenchPage extends IPartService, ISelectionService,ICompatibleWorkbenchPage { 2 3 public IEditorPart openEdito(IEditorInput input, String editorId)throws PartInitException; 4 5 public IEditorPart openEdito(IEditorInput input, String editorId, boolean activate) throws PartInitException; 6 7 public IEditorPart openEditor(final IEditorInput input, final String editorId, final boolean activate, final int matchFlags)throws PartInitException; 8}
<content-type id="javaProperties" name="%javaPropertiesName"
base-type="org.eclipse.core.runtime.text"
priority="high"
file-extensions="properties"
default-charset="ISO-8859-1"/>
我们看一下 org.eclipse.ui.part.FileEditorInput 中是如何实现 IEditorInput.exists()接口的:
1 public class FileEditorInput implements IFileEditorInput,IPathEditorInput,IPe
【动态计算 editor id】 下面,我们再来看一下 IDE 类是如何计算所谓的默认 eidtor id 的。追踪实现,我 们看到了 IDE.getDefaultEditor
1 public static IEditorDescriptor getDefaultEditor(IFile file, boolean determineContentType) {
那到这边,可能很多人已经知道了怎么调用这些接口了: PlatformUI.getWorkbench().getActiveWorkbenchWindow().get
ActivePage().openEditor(...) (说明:PlatformUI 可以看作是整个 eclipse ui 框架的门面类,当然最核心的作
上图就用来说明 workbench 是如何来管理各种 part 的,其中 descriptor 角色的 核心作用是延迟加载扩展(延迟加载用户通过 editors 或者 views 提供的扩展),reference 角色的核心作用是用来延迟初时化具体的 par(t 例如避免过早的创建对应的 control 等等)。 再说下去有点偏离主题了,这部分,以后有时间再写
<!-- Associates .classpath to the XML content type --> <file-association
content-type="org.eclipse.core.runtime.xml" file-names=".classpath"/> <!-- declares a content type for Java Source files --> <content-type id="javaSource" name="%javaSourceName" base-type="org.eclipse.core.runtime.text" priority="high" file-extensions="java"/> <!-- declares a content type for Java class files --> <content-type id="javaClass" name="%javaClassName" priority="high" file-extensions="class"> <describer
6 7 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, String editorId) throws PartInitException { } 8 9 public static IEditorPart openEditor(IWorkbenchPage page, IFile input, String editorId, boolean activate) throws PartInitException { } 10 11
14
// do nothing
15
}
16
17
IContentType contentType = null;
18
if (determineContentType) {
19
contentType = getContentType(file);
20
}
21
// Try lookup with filename
【有关 FileEditorInput】 在 IDE 工具类中提供的 5 个接受 IFile 对象的 openEditor 接口中,在对应的实现 中都是默认构造了一个 FileEditorInput(org.eclipse.ui.part.FileEditorInput)实例, 这个实例也是 org.eclipse.ui.IFileEditorInput 接口的默认实现类(注意:Eclipse 中很 多地方都使用这种 Interface/Default Impl 的方式,Interface 会暴露,Default Impl 则根据情况选择是否暴露,一般是如果 Interface 希望用户来扩展继承,则会暴露对应的 Default Impl,如果 Interface 不希望用户来扩展继承,例如 IResource 系列接口,则一 般会将 Default Impl 丢如对应的 internal 包中)。
相关文档
最新文档