Eclipse工作臺提供了一個性能視圖用於查看(和/或編輯)選定項目的屬性。在本文中,您將學習如何使用選項卡式的屬性視圖創建一個性能增強的用戶界面視圖。
1.引言
Eclipse工作臺提供了一個屬性視圖,即在文章中詳細描述你的性能控制。默認的用戶界面是屬性和值對錶,並且值使用一個標準的對話框單元格編輯器。
工作臺使用擴展(extensions)爲屬性視圖定義了一個自定義的用戶界面。利用這個擴展,Properties視圖已創建。
屬性視圖允許您創建任何用戶界面的屬性。此外,您可以爲元素創建用戶接口而不用實現IPropertySource。事實上,屬性視圖可以被擴展到查看在工作臺中被選擇的任何數據。
2.實例解析
依賴 org.eclipse.ui.views.properties.tabbed 插件
需要實現三個擴展點:
org.eclipse.ui.views.properties.tabbed.PropertyContributor
Tabbed View 由一個 PropertyContributor 構成,其中包含多個 PropertyTab。
<extension point="org.eclipse.ui.views.properties.tabbed.propertyContributor">
<propertyContributor contributorId="mview.views.SampleView">
<propertyCategory category="sample"/>
</propertyContributor>
</extension>
org.eclipse.ui.views.properties.tabbed.PropertyTabs每個 PropertyTab 由多個 PropertySection 構成。
<extension point="org.eclipse.ui.views.properties.tabbed.propertyTabs">
<propertyTabs contributorId="mview.views.SampleView">
<propertyTab
category="sample"
id="mview.ButtonTab"
label="Button"/>
<propertyTab
afterTab="mview.ButtonTab"
category="sample"
id="mview.AdvancedTab"
label="Advanced"/>
</propertyTabs>
</extension>
org.eclipse.ui.views.properties.tabbed.PropertySections每個 PropertySection 實現爲一個或一組 widget,對應着一個 property。
<extension point="org.eclipse.ui.views.properties.tabbed.propertySections">
<propertySections contributorId="mview.views.SampleView">
<propertySection
class="mview.views.LabelSection"
id="mview.LabelSection"
tab="mview.ButtonTab">
<input type="mview.views.ButtonElement"/>
</propertySection>
<propertySection
afterSection="mview.LabelSection"
class="mview.views.SizeSection"
id="mview.SizeSection"
tab="mview.ButtonTab">
<input type="mview.views.ButtonElement"/>
</propertySection>
<propertySection
afterSection="mview.SizeSection"
class="mview.views.FontSection"
id="mview.FontSection"
tab="mview.ButtonTab">
<input type="mview.views.ButtonElement"/>
</propertySection>
<propertySection
class="org.eclipse.ui.views.properties.tabbed.AdvancedPropertySection"
id="mview.AdvancedSection"
tab="mview.AdvancedTab">
<input type="mview.views.ButtonElement"/>
</propertySection>
</propertySections>
</extension>
Tabbed Property是eclipse3.2中新加入一個view,可以使屬性編輯器的功能近乎無限的擴大。這裏說明一些Tabbed Property的使用方法。Tabbed Property中分成三個部分,Contributer,Tabs,Sections,一個Contributor包含若干個Tabs,一個Tabs又可以包含若干個sections。下面我們來分別進行描述。
1.Contributor 這需要擴展org.eclipse.ui.views.properties.tabbed.PropertyContributor擴展點,定義時,最重要的是定義contributId,這個id必須是全局唯一的,這樣在加載屬性頁時,才能找到這個我們定義的屬性頁,一般地,我們都將對應於這個屬性頁的workbenchpart的id作爲本contributor的id,這樣我們在代碼中可以不硬編碼本id字符串,而使用getSite().getId()就可以得到這個id了(當然,這樣定義id不是必須的)。一個property view可以被多個workbench part共享,但 一個workbench part只能有一個property view,這個workbench part需要實現ITabbedPropertySheetPageContributor 接口,這個接口只有一個方法,要求返回本part對應的tabbed property Contributor id,一般只要return getSite().getId();
contributor有如下幾個attribute:
1)typeMapper,這個類需要實現org.eclipse.ui.views.properties.tabbed.ITypeMapper,主要是實現類型的映射,因爲我們選擇的元素並不一定是實現IPropertySource的元素(即能夠給property view提供內容的元素),比如在GEF中,我們選擇的finger實際上是選擇了對應的EditPart,而實際上實現了IPropertySource一般的是model部分的元素,所以這時候我們要將Editpart映射到對應的model元素。
2)labelProvider,需要一個實現org.eclipse.jface.viewers.ILabelProvider的類,主要是在各個tabs的最上面顯示文字和圖片。
3)propertyCategory,用於聚合多個tabs,注意至少要定義一個category,來聚合tabs,否則,可能會顯示property失敗。
2.Tabs,這個需要擴展org.eclipse.ui.views.properties.tabbed.propertyTabs擴展點,其中contributorId就是與之相關聯的Contributor的id,然後我們可以定義多個tab,這些tab的屬性如下:
1)label,用於顯示在property view的tab bar上的字
2)category,填入的就是在Contributor擴展點中定義的那些category,用於聚合tabs
3)id,本tab的唯一標識
4)afterTab,用於tab之間的排序,如果這是第一個tab,則沒有afterTab,afterTab指的是在本tab之前的那個tab,並且afterTab描述的是在同一個category中的tabs,不同category之間的順序是按照在contributor中定義category的順序來定義的。
5)indented,如果爲ture,則各個tabs是有縮進的
6)image,本tab的圖片
3.section ,需要擴展 org.eclipse.ui.views.properties.tabbed.PropertySections擴展點,它的contributionId就是本section所在的Contribution的id,針對每個tab,我們可以定義多個section,每個section的attribut描述如下:
1)id,本secation的唯一標識
2)tab,本section所屬tab的標識
3)class,實現了org.eclipse.ui.views.properties.tabbed.AbstractPropertySection抽象類的類,用於描述這個section的控件和佈局。
4)aftersection和上面的aftertab差不多,描述的是同一個tab中的section的順序,注意afterserction描述的是本section之前的section的id
5)filter:一個實現org.eclipse.jface.viewers.IFilter接口的過濾器,對選中元素進行過濾。
6)enableFor:一個用於只是選擇數目的值,必須要符合這個數目才能使能這個section。如果不符合,則這個section就被過濾了,如果省略本值,則section的使能器就不會工作了。這是一個自然數,比如,當enableFor=1時,僅僅只有一個元素被選擇的時候,本section纔會被使能。
some notes:
上面說過實現ITabbedPropertySheetPageContributor接口的workbench part除了要實現getContributeId方法外,還需要重載getAdapter方法,因爲eclipse的默認加載的property veiw時原來的那個view,爲了使tabbed property view能夠加載,我們就需要重載getAdapter方法,返回一個TabbedPropertySheetPage對象。
在實現section class的時候需要注意,createcontrol時首先應該先創建一個composite,一般是 Composite composite = getWidgetFactory().createFlatFormComposite(parent); 然後各個控件在這個composite上創建。