---
title: "TabbedProperties实现"
author: "winseclone"
date: "2013-02-06T20:35:47Z"
tags: []
categories: ["编程语言"]
slug: "tabbedproperties"
draft: false
lastmod: "2013-02-09T12:34:09Z"
id: 1796635
url: "https://winseclone.iteye.com/blog/1796635"
---

前文中 [http://winseclone.iteye.com/blog/1774307](/blog/1774307) 介绍了使用Properties的方法。但是TableTree 形式的Properties视图，局限性比较大，不能很好的发挥。如下操作在Properties就很难实现：

- 调整属性，**重要**的**常用**的属性放置在前！
- 在属性上添加其他操作，如在Class对象上通过ctrl+&lt;单击&gt;能跳转到对应的class类，并用编辑器打开。

## Tabbed Properties效果

Tabbed Properties与表格式Properties不同，采用的是通过**表单形式**来编辑属性。如下图所示：

![](http://dl.iteye.com/upload/attachment/0080/3553/d255831f-25c4-3f5d-859b-fa3e0e986e19.png)

 

图中主要分3部分：

1、最外面层是Contributor，由Label + TabContainer；

2、中间层是TabContainer，可以包括n个Tab，每个Tab则是一个SectionContainer；

3、内层是SectionContainer， 由n个Section组成。

 

每个层可以使用一个扩展点来实现。对应Tabbed提供的3个扩展点：

- org.eclipse.ui.views.properties.tabbed.propertyContributor
- org.eclipse.ui.views.properties.tabbed.propertyTabs
- org.eclipse.ui.views.properties.tabbed.propertySections

* * *

 

 这里，我们先按照步骤一步步的实现效果，然后再讲解3个扩展点常用的属性。

## 实现Tabbed Properties View

### · 更新SampleView.java

1、实现ITabbedPropertySheetPageContributor接口

```
	public String getContributorId() {
		return SampleView.ID;
	}
```

2 重写SampleView的getAdapter方法。修改默认的IPropertySheetPage.class适配对象

```
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class) {
			return new TabbedPropertySheetPage(this);
		}

		return super.getAdapter(adapter);
	}
```

### · 实现ISection，定义控件以及布局

实现自定义的IFilter和Section

[StringFilter](https://github.com/winse/hello/blob/8c8ae8a1a83c7cfe27287631e5b575d50f17d75c/plugin.properties/src/plugin/properties/section/StringFilter.java) 

[StringTypeSection](https://github.com/winse/hello/blob/8c8ae8a1a83c7cfe27287631e5b575d50f17d75c/plugin.properties/src/plugin/properties/section/StringTypeSection.java)

### · 扩展点实现

添加tabbed的3个扩展点。

contributorId填写为对应SampleView的Id（即SampleView.ID的值）。具体Contributor，Tab，Section的属性配置按照层次关系填写即可。

![](http://dl.iteye.com/upload/attachment/0080/3658/36bbb8ff-b295-3326-ba34-87c3dedc9f30.png)  
 

```
   <extension
         point="org.eclipse.ui.views.properties.tabbed.propertyContributor">
      <propertyContributor
            contributorId="plugin.properties.views.SampleView"
            labelProvider="org.eclipse.jface.viewers.LabelProvider">
         <propertyCategory
               category="Sample"></propertyCategory>
         <propertyCategory
               category="AdvancedCategory"></propertyCategory>
      </propertyContributor>
   </extension>
   <extension
         point="org.eclipse.ui.views.properties.tabbed.propertyTabs">
      <propertyTabs
            contributorId="plugin.properties.views.SampleView">
         <propertyTab
               category="AdvancedCategory"
               id="plugin.properties.advancedTab"
               label="Advanced">
         </propertyTab>
         <propertyTab
               category="Sample"
               id="plugin.properties.sampleTab"
               label="Sample">
         </propertyTab>
      </propertyTabs>
   </extension>
   <extension
         point="org.eclipse.ui.views.properties.tabbed.propertySections">
      <propertySections
            contributorId="plugin.properties.views.SampleView">
         <propertySection
               class="org.eclipse.ui.views.properties.tabbed.AdvancedPropertySection"
               id="plugin.properties.advanceSection"
               tab="plugin.properties.advancedTab">
            <input
                  type="plugin.properties.views.StringWrapper">
            </input>
         </propertySection>
         <propertySection
               class="plugin.properties.section.StringTypeSection"
               filter="plugin.properties.section.StringFilter"
               id="plugin.properties.sampleSection"
               tab="plugin.properties.sampleTab">
         </propertySection>
      </propertySections>
   </extension>
```

 

另外，编写Tabbed Properties页签视图需要懂swt，以及FormLayout布局。

对于一个属性显示一行的TabbedProperties可以通过topcased插件来生成。这个插件还是不错的。但是如果复杂一点的就需要自己定义了。

* * *

## 扩展点含义

下面主要讲一下3个扩展点的常用属性，及其子节点

### 1、 org.eclipse.ui.views.properties.tabbed.propertyContributor

**#propertyContributor节点的属性**

- **contributorId** Tabbed PropertySheetPage贡献者的ID，即选中元素所在**View或Editor的ID**。 同时View和Editor也需要实现ITabbedPropertySheetPageContributor 接口，这个接口只有一个方法，要求返回的值即该contributorId。
- **typeMapper** - 实现类型的映射。TabbedPropertyRegistryClassSectionFilter.appliesToSelection(ISectionDescriptor, ISelection) 起到mappingFilter的作用。因为我们选择的元素并不一定是实现IPropertySource的元素（即能够给property view提供内容的元素），比如在GEF中，我们选择的finger实际上是选择了对应的EditPart，而实际上实现了IPropertySource一般的是model部分的元素，所以这时候我们要将Editpart映射到对应的model元素\[r3]
- **labelProvider** - 标题显示提供者Provider(`ILabelProvider)`
- **actionProvider** - 没啥用。TabbedPropertySheetPage.validateRegistry(ISelection)中会调用registry.getActionProvider()，但在中间就返回了。
- **sectionDescriptorProvider** - （应该是类似于propertySections扩展点的作用）
- **tabDescriptorProvider** - （应该是类似于propertyTabs扩展点的作用）

 

通过Extensions标签页扩展点的“Show extension point description”查看该扩展点的详细描述。

 

    **#propertyContributor#propertyCategory属性**

用于聚合多个tabs，将会在propertyTab标签中用到；

注意至少要定义一个category，来聚合tabs，否则，可能会显示property失败。

### 2、org.eclipse.ui.views.properties.tabbed.propertyTabs

**#propertyTabs#propertyTab节点的属性**

- **label** - Tab显示的名称.
- **category** - 对应上面的propertyCategory属性的category的值.
- **id** - The unique id for the tab，将会用于propertySection标签。

### 3、org.eclipse.ui.views.properties.tabbed.propertySections

**#propertySections#propertySection属性**

- **tab** - 该section需要放置到Tab，对应propertyTab扩展点的id属性值.
- **id** - 表示该section的唯一ID.
- **class** - section的实现类，用于描述这个section的控件和布局。
- **filter** - 对象过滤，只有通过该filter的对象才会显示该section

如果没有选择fitler，则需要添加**input子节点**！

* * *

## 源码：

s1: 本文实现的例子

[https://github.com/winse/hello/tree/8c8ae8a1a83c7cfe27287631e5b575d50f17d75c](https://github.com/winse/hello/tree/8c8ae8a1a83c7cfe27287631e5b575d50f17d75c)

s2: topcased相关源码

[http://subversion.moskitt.org/gvcase-adaptations/topcased/branches/org.topcased.modeler/src/org/topcased/modeler/editor/properties/filters/DiagramElementFilter.java](http://subversion.moskitt.org/gvcase-adaptations/topcased/branches/org.topcased.modeler/src/org/topcased/modeler/editor/properties/filters/DiagramElementFilter.java)

 

[http://gforge.enseeiht.fr/scm/viewvc.php/plugins/modeler/org.topcased.tabbedproperties/src/org/topcased/tabbedproperties/sections/?diff\_format=s&root=topcased-mf](http://gforge.enseeiht.fr/scm/viewvc.php/plugins/modeler/org.topcased.tabbedproperties/src/org/topcased/tabbedproperties/sections/?diff_format=s&root=topcased-mf)

 

[http://gforge.enseeiht.fr/scm/viewvc.php/\*checkout\*/plugins/modeler/org.topcased.modeler/plugin.xml?revision=1.127&root=topcased-mf](http://gforge.enseeiht.fr/scm/viewvc.php/*checkout*/plugins/modeler/org.topcased.modeler/plugin.xml?revision=1.127&root=topcased-mf)

 

## 参考：

r1：最权威的tabbed\_properties的使用说明

[http://www.eclipse.org/articles/Article-Tabbed-Properties/tabbed\_properties\_view.html](http://www.eclipse.org/articles/Article-Tabbed-Properties/tabbed_properties_view.html)

r2：一个通用的页签式视图框架（可以了解一下，使用的不是Eclipse提供的扩展点）

[http://www.ibm.com/developerworks/cn/opensource/os-cn-eclipse-tabview/index.html](http://www.ibm.com/developerworks/cn/opensource/os-cn-eclipse-tabview/index.html)

r3：Eclipse Tabbed Properties View

[http://hi.baidu.com/uvfluhpkkjfglor/item/5526fd459565ec14886d1071](http://hi.baidu.com/uvfluhpkkjfglor/item/5526fd459565ec14886d1071)