打開editor的接口討論

?
????????【打開editor的接口討論】
??????? 先來看一下workbench吧,workbench從靜態劃分應該大致如下:
???????
????????????從結構圖我們大致就可以猜測出來,workbench page作為一個IWorkbenchPart(無論是eidtor part還是view part)的容器,肯定會接受workbench page的管理。看了一下,IWorkbenchPage接口定義中確實提供給了如下打開編輯器的操作:

?????????????【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?}
??????????
????????????那到這邊,可能很多人已經知道了怎么調用這些接口了:
?????????? ?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(...)
??????????(說明:PlatformUI可以看作是整個eclipse ui框架的門面類,當然最核心的作用就是讓用戶獲取到workbench。Eclipse中存在的其他一些門面類如:ResourcesPlugin、Platform、JavaCore、JavaUI等)

????????????我們再仔細看一下IWorkbenchPage對應的實現類(org.eclipse.ui.internal.WorkbenchPage)中的以上接口的實現代碼,真正在管理Editor的是一個叫做EditorManager的東東(同理,view part對應的管理器角色類是叫做ViewFactory的東東)。 這里的EditorManager和View Factory是workbench實現中非常精華的部分,看一下里面的實現就會很大程度上理解workbench所謂懶加載、懶初始化是如何實現的了,如何實現part 復用的...等等 。?
????????????
?????????? 上圖就用來說明workbench是如何來管理各種part的,其中descriptor角色的核心作用是延遲加載擴展(延遲加載用戶通過editors或者views提供的擴展),reference角色的核心作用是用來延遲初時化具體的part(例如避免過早的創建對應的control等等)。再說下去有點偏離主題了,這部分,以后有時間再寫
????????????
??????????? 【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)?{?}
?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?
???????????上面5個接口操作中,?對于上面的三個操作,Eclipse會自動為你準備IEditorInput實例,并動態綁定合適的編輯器類型。對于下面的兩個操作,Eclipse會為你自動準備IEditorInput實例,但是需要用戶自己指定editor id。
????????????
???????????接下來我們看兩個問題,一是如何創建IEditorInput實例的;而是如何動態計算對應的editor id的。
???????????
??????????【有關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包中)。
????????????我們看一下org.eclipse.ui.part.FileEditorInput中是如何實現IEditorInput.exists()接口的:
1?public?class?FileEditorInput?implements?IFileEditorInput,IPathEditorInput,IPersistableElement?{
2?????private?IFile?file;
3?
4?????public?boolean?exists()?{
5?????????return?file.exists();
6?????}
7?}
????????? 我們看到內部的實現是持有了IFile句柄,如果IFile代表的資源沒有存在于工作區之內,那么就會返回false。(疑問:如果我們打開工作區外部的文件呢???顯然,FileEditorInput并不合適,稍后看...)
????????
????????【動態計算editor id】
???????? 下面,我們再來看一下IDE類是如何計算所謂的默認eidtor id的。追蹤實現,我們看到了IDE.getDefaultEditor
??????????
?1??public?static?IEditorDescriptor?getDefaultEditor(IFile?file,?boolean?determineContentType)?{
?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)?{
14?????????????//?do?nothing
15?????????}
16?????????
17?????????IContentType?contentType?=?null;
18?????????if?(determineContentType)?{
19?????????????contentType?=?getContentType(file);
20?????????}????
21?????????//?Try?lookup?with?filename
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):
<!--?===================================================================================?-->
<!--?Extension:?Java?Content?Types???????????????????????????????????????????????????????-->
<!--?===================================================================================?-->
<extension?point="org.eclipse.core.runtime.contentTypes">
????<!--?declares?a?content?type?for?Java?Properties?files?-->
????<content-type?id="javaProperties"?name="%javaPropertiesName"?
????????base-type
="org.eclipse.core.runtime.text"
????????priority
="high"????????????????
????????file-extensions
="properties"
????????default-charset
="ISO-8859-1"/>
????<!--?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
????????????
class="org.eclipse.core.runtime.content.BinarySignatureDescriber">
????????????<parameter?name="signature"?value="CA,?FE,?BA,?BE"/>
????????</describer>
????</content-type>????????
????<!--?declares?a?content?type?for?JAR?manifest?files?-->
????<content-type?id="JARManifest"?name="%jarManifestName"?
????????base-type
="org.eclipse.core.runtime.text"
????????priority
="high"????????????????
????????file-names
="MANIFEST.MF"
????????default-charset
="UTF-8"/>
</extension>
???????????? 那如果我們在注冊編輯器的時候和對應的content type綁定,這不就聯系起來了嗎~_~。那我們看一下java源碼編輯器擴展描述(摘自org.eclipse.jdt.ui/plugin.xml):
<editor
????????????
name="%JavaEditor.label"
????????????default
="true"
????????????icon
="$nl$/icons/full/obj16/jcu_obj.gif"
????????????contributorClass
="org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditorActionContributor"
????????????class
="org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor"
????????????symbolicFontName
="org.eclipse.jdt.ui.editors.textfont"
????????????id
="org.eclipse.jdt.ui.CompilationUnitEditor">
????????????<contentTypeBinding
???????????????
contentTypeId="org.eclipse.jdt.core.javaSource"
????????????
/>?
??????</editor>

?????????? 我們看到上面的xml中有contentTypeBinding元素,里面指定了綁定java源碼content type。

????????????那如果我們在注冊編輯器的時候,沒有綁定對應的content type呢?Eclipse允許你配置,往下看:

????????????
????????????

????????????我想看到這邊對eclipse如何動態計算一個文件對應的editor應該是明白了吧,再回顧一下吧:
????????????1、查看資源本身是否有EIDTOR_ID持久屬性(注意:一、只有工作區中存在的資源才允許設置持久屬性;二、資源屬性知識針對特定資源,不會影響同類型資源,即你對工作區中特定的.java文件設定了EIDTOR_ID持久屬性,并不會影響工作區中其他.java文件資源的編輯器綁定操作)
????????????2、查找對應的content type,然后查找對應的editor擴展或者查找Eclipse中的Content Types和File Associations配置
??????????? 3、如果都找不到,則直接給一個默認的編輯器。例如,我們經常碰到是"org.eclipse.ui.DefaultTextEditor"

???????????? 【IDE工具類提供的接口 VS? IWorkbenchPage提供的接口】
??????????????看一下以上提到的各個角色之間的調用關系圖吧:
??????????????

???????? 【使用Eclipse提供的打開editor的接口】
????????還是那句話,需求決定一切。我們看一下打開編輯器的需求:
????????1、打開工作區中工程內的文件資源
????????2、打開工作區.metadata目錄中的文件資源
????????3、打開工作區外部的文件資源

????????【說明】Eclipse工作區實際上是有數據區和元數據區兩個區域組成的,示意如下:
????????
????????????
????????????對于Eclipse來說,.metadata目錄下存放的是插件運行時的關鍵狀態數據,不建議用戶再工作區實例運行期間做相應修改,為此eclipse干了兩件事情:1、運行期間會自動在.metadata目錄下產生一個進程鎖定的.lock文件;2、Eclipse不允許用戶通過IResource系列接口直接訪問或修改.meatadata目錄下的資源

???????????【打開工作區工程內的資源】
????????????
?假設工作區中有測試工程TestProject,工程下有文本文件java_file.txt。對應創建代碼如下:
????????????
?1?try?{
?2?????????????//創建工程
?3?????????????IProject?project?=?ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
?4?????????????if?(!project.exists())
?5?????????????????project.create(null);
?6?????????????if?(!project.isOpen())
?7?????????????????project.open(null);
?8?????????????
?9?????????????//創建文件
10?????????????IFile?java_file?=?project.getFile(new?Path("/java_file.txt"));
11?????????????InputStream?inputStreamJava?=?new?ByteArrayInputStream("class?MyType{}".getBytes());
12?????????????if?(!java_file.exists())
13?????????????????java_file.create(inputStreamJava,?false,?null);
14?????????}?catch?(CoreException?e)?{
15?????????????IStatus?status?=?new?Status(IStatus.ERROR,?"myplugin",?101,?"創建資源失敗",?e);
16?????????????Activator.getDefault().getLog().log(status);
17?????????}

????????????
????????
????????打開方式一:Eclipse默認計算對應的editor id,會用default text?editor打開?
?1?try?{
?2?????????????IWorkbenchPage?page?=?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
?3?????????????IProject?project?=?ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
?4?????????????
?5?????????????IFile?java_file?=?project.getFile(new?Path("/java_file.txt"));
?6?????????????IDE.openEditor(page,?java_file);????????????
?7?????????}?catch?(CoreException?e)?{
?8?????????????IStatus?status?=?new?Status(IStatus.ERROR,?"myplugin",?102,?"打開工作區內文件出錯",?e);
?9?????????????Activator.getDefault().getLog().log(status);
10?????????}

????????打開方式二:指定java源碼編輯器打開,會用java源碼編輯器打開
?1?try?{
?2?????????????IWorkbenchPage?page?=?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
?3?????????????IProject?project?=?ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
?4?????????????
?5?????????????IFile?java_file?=?project.getFile(new?Path("/java_file.txt"));
?6?????????????IDE.openEditor(page,?java_file,?"org.eclipse.jdt.ui.CompilationUnitEditor");
?7?????????}?catch?(CoreException?e)?{
?8?????????????IStatus?status?=?new?Status(IStatus.ERROR,?"myplugin",?102,?"打開工作區內文件出錯",?e);
?9?????????????Activator.getDefault().getLog().log(status);
10?????????}

?????????
?????????? 打開方式三:設定editor id屬性,該文件以后默認都用此editor id打開
?1?try?{
?2?????????????IWorkbenchPage?page?=?PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
?3?????????????IProject?project?=?ResourcesPlugin.getWorkspace().getRoot().getProject("TestProject");
?4?????????????
?5?????????????IFile?java_file?=?project.getFile(new?Path("/java_file.txt"));
?6?????????????java_file.setPersistentProperty(IDE.EDITOR_KEY,?"org.eclipse.jdt.ui.CompilationUnitEditor");
?7?????????????IDE.openEditor(page,?java_file);
?8?????????}?catch?(CoreException?e)?{
?9?????????????IStatus?status?=?new?Status(IStatus.ERROR,?"myplugin",?102,?"打開工作區內文件出錯",?e);
10?????????????Activator.getDefault().getLog().log(status);
11?????????}

????????說明:對于工作區工程內的資源,可以有兩種方式:一是local的,那就是物理存在與工程之內;二是link進入的。打開編輯器的時候,不需要做區分。

????????【打開工作區外部的資源】
????????說明:既存在于工作區外部,同時又沒有被link進工程。
????????
??????? 在Eclipse中有個功能,就是File->Open File,可以打開一個外部文件。那我們看一下它是怎么實現的。我們只需要打開對應的對話框,然后掛起主線程,就可以找到對應的action了(掛起線程可以幫我們很方便的調試很多類型的問題,以后細說~_~):
??????????????????????

?????????? 分析一下OpenExternalFileAction的實現,我們發現它自己構建了一個editor input

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388122.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388122.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388122.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【三角函數】已知直角三角形的斜邊長度和一個銳角角度,求另外兩條直角邊的長度...

如圖,已知直角三角形ABC中,∠C90, ∠Aa ,ABc ,求直角邊AC、BC的長度. ∵ ∠C90,∠Aa ,ABc ,Cos∠AAC/AB ,Sin∠ABC/AB ,∴ ACAB*Cos∠Ac*Cosa ,BCAB*Sin∠Ac*Sina . 復制代碼

網絡攻防技術實驗五

2018-10-23 實驗五 學 號201521450005 中國人民公安大學 Chinese people’ public security university 網絡對抗技術 實驗報告 實驗五 綜合滲透 學生姓名 陳軍 年級 2015 區隊 五 指導教師 高見 信息技術與網絡安全學院 2018年10月23日 實驗任務總綱 2018—2019 …

usgs地震記錄如何下載_用大葉草繪制USGS地震數據

usgs地震記錄如何下載One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.美國地質調查局(USGS)…

Springboot 項目中 xml文件讀取yml 配置文件

2019獨角獸企業重金招聘Python工程師標準>>> 在xml文件中讀取yml文件即可&#xff0c;代碼如下&#xff1a; 現在spring-boot提倡零配置&#xff0c;但是的如果要集成老的spring的項目&#xff0c;涉及到的bean的配置。 <bean id"yamlProperties" clas…

eclipse 插件打包發布

如果想把調試好的插件打包發布&#xff0c;并且在ECLIPSE中可以使用. 1.File-->Export 2.選擇 PLug-in Development下 的 Deployable plug-ins and fragments 3.進入 Deployable plug-ins and fragments 頁面 4.把底下的 Destubatuib 的選項中選擇 Archive file 在這里添入要…

無法獲取 vmci 驅動程序版本: 句柄無效

https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 將 vmci0.present "TURE" 改為 “FALSE”; 轉載于:https://www.cnblogs.com/limanjihe/p/9868462.html

數據可視化 信息可視化_更好的數據可視化的8個技巧

數據可視化 信息可視化Ggplot is R’s premier data visualization package. Its popularity can likely be attributed to its ease of use — with just a few lines of code you are able to produce great visualizations. This is especially great for beginners who are…

分布式定時任務框架Elastic-Job的使用

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、前言 Elastic-Job是一個優秀的分布式作業調度框架。 Elastic-Job是一個分布式調度解決方案&#xff0c;由兩個相互獨立的子項目Elastic-Job-Lite和Elastic-Job-Cloud組成。 Elastic-Job-Lite定位為輕量級無中心化…

Memcached和Redis

Memcached和Redis作為兩種Inmemory的key-value數據庫&#xff0c;在設計和思想方面有著很多共通的地方&#xff0c;功能和應用方面在很多場合下(作為分布式緩存服務器使用等) 也很相似&#xff0c;在這里把兩者放在一起做一下對比的介紹 基本架構和思想 首先簡單介紹一下兩者的…

第4章 springboot熱部署 4-1 SpringBoot 使用devtools進行熱部署

/imooc-springboot-starter/src/main/resources/application.properties #關閉緩存, 即時刷新 #spring.freemarker.cachefalse spring.thymeleaf.cachetrue#熱部署生效 spring.devtools.restart.enabledtrue #設置重啟的目錄,添加那個目錄的文件需要restart spring.devtools.r…

border-radius 漲知識的寫法

<div idapp></div>復制代碼#app{width:100%;height:80px;background:pink;border-radius:75%/20% 20% 0 0;}復制代碼僅供自己總結記憶轉載于:https://juejin.im/post/5c80afd66fb9a049f81a1217

ibm python db_使用IBM HR Analytics數據集中的示例的Python獨立性卡方檢驗

ibm python dbSuppose you are exploring a dataset and you want to examine if two categorical variables are dependent on each other.假設您正在探索一個數據集&#xff0c;并且想要檢查兩個分類變量是否相互依賴。 The motivation could be a better understanding of …

Oracle優化檢查表

分類檢查項目相關文件或結果狀態備注日志及文件Oracle Alert 日志bdump/udump下是否存在明顯的報警listener相關日志SQL* Net日志參數/參數文件listener.ora/tnsnames.ora操作系統操作系統版本檢查操作系統補丁節點名操作系統vmstat狀態操作系統I/O狀態操作系統進程情況操作系統…

spring分布式事務學習筆記(2)

此文已由作者夏昀授權網易云社區發布。歡迎訪問網易云社區&#xff0c;了解更多網易技術產品運營經驗。Model類如下&#xff1a;package com.xy.model1 package com.xy.model;2 3 /**4 * Created by helloworld on 2015/1/30.5 */6 public class NameQa {7 private long …

sql 左聯接 全聯接_通過了解自我聯接將您SQL技能提升到一個新的水平

sql 左聯接 全聯接The last couple of blogs that I have written have been great for beginners ( Data Concepts Without Learning To Code or Developing A Data Scientist’s Mindset). But, I would really like to push myself to create content for other members of …

如何查看linux中文件打開情況

如何查看linux中文件打開情況 前言 我們都知道&#xff0c;在linux下&#xff0c;“一切皆文件”&#xff0c;因此有時候查看文件的打開情況&#xff0c;就顯得格外重要&#xff0c;而這里有一個命令能夠在這件事上很好的幫助我們-它就是lsof。 linux下有哪些文件 在介紹lsof命…

hadoop windows

1、安裝JDK1.6或更高版本 官網下載JDK&#xff0c;安裝時注意&#xff0c;最好不要安裝到帶有空格的路徑名下&#xff0c;例如:Programe Files&#xff0c;否則在配置Hadoop的配置文件時會找不到JDK&#xff08;按相關說法&#xff0c;配置文件中的路徑加引號即可解決&#xff…

Ocelot中文文檔入門

入門 Ocelot僅適用于.NET Core&#xff0c;目前是根據netstandard2.0構建的&#xff0c;如果Ocelot適合您&#xff0c;這個文檔可能會有用。 .NET Core 2.1 安裝NuGet包 使用nuget安裝Ocelot及其依賴項。 您需要創建一個netstandard2.0項目并將其打包到其中。 然后按照下面的“…

科學價值 社交關系 大數據_服務的價值:數據科學和用戶體驗研究美好生活

科學價值 社交關系 大數據A crucial part of building a product is understanding exactly how it provides your customers with value. Understanding this is understanding how you fit into the lives of your customers, and should be central to how you build on wha…

在Ubuntu下創建hadoop組和hadoop用戶

一、在Ubuntu下創建hadoop組和hadoop用戶 增加hadoop用戶組&#xff0c;同時在該組里增加hadoop用戶&#xff0c;后續在涉及到hadoop操作時&#xff0c;我們使用該用戶。 1、創建hadoop用戶組 2、創建hadoop用戶 sudo adduser -ingroup hadoop hadoop 回車后會提示輸入新的UNIX…