---
title: "Properties视图应用"
author: "winseclone"
date: "2013-01-23T12:54:28Z"
tags: []
categories: ["编程语言"]
slug: "properties"
draft: false
lastmod: "2013-02-09T13:13:03Z"
id: 1774307
url: "https://winseclone.iteye.com/blog/1774307"
---

eclipse平台为我们提供了一系列的基础设施，Properties视图就是一个。我们可以通过Properties视图来查看/修改模型的属性值。

若希望能在Properties视图编辑模型属性，需要适配出IPropertySource。实现getAdapter()返回模型对应的IPropertySource实现。

 

实现下面4个步骤，就可以实现简单的模型属性编辑了。

 

1、在模型所在的视图或Editor实现类的init()方法中，添加  **getSite().setSelectionProvider(viewer);**  把viewer视图的选择事件“广播”给workbench平台。

 

```
public class SampleView extends ViewPart {

	public void createPartControl(Composite parent) {
		// 。。。
		
		viewer.setContentProvider(new BaseWorkbenchContentProvider());
		viewer.setLabelProvider(new WorkbenchLabelProvider());			
		viewer.setInput(ctlList);
		getSite().setSelectionProvider(viewer);
	}
```

 

2、实现IPropertySource接口，返回与模型字段对应的PropertyDescriptor（&lt;-CellEditor）

![](http://dl.iteye.com/upload/attachment/0079/6998/0d164a6d-c4cc-33b4-878a-bc821fd92fde.png)

 

```
public class ButtonElementProperties implements IPropertySource {
	final protected ButtonElement element;
		 		
	protected static final String PROPERTY_FONT = "font"; 	
	protected static final String PROPERTY_SIZE = "size";	
	protected static final String PROPERTY_TEXT = "text";	
	
	private final Object PropertiesTable[][] = 
		{ { PROPERTY_FONT, new FontPropertyDescriptor(PROPERTY_FONT,"Font")},
                    { PROPERTY_SIZE, new PropertyDescriptor(PROPERTY_SIZE,"Size")},
		  { PROPERTY_TEXT, new TextPropertyDescriptor(PROPERTY_TEXT,"Text")},		  
		};	

	public IPropertyDescriptor[] getPropertyDescriptors() {
		// Create the property vector.
		IPropertyDescriptor[] propertyDescriptors = new IPropertyDescriptor[PropertiesTable.length];

		for (int i=0;i<PropertiesTable.length;i++) {				
			// Add each property supported.
			PropertyDescriptor descriptor;

			descriptor = (PropertyDescriptor)PropertiesTable[i][1];
			propertyDescriptors[i] = (IPropertyDescriptor)descriptor;
			descriptor.setCategory("Basic");
		}	
		// Return it.
		return propertyDescriptors;

	}

	public Object getPropertyValue(Object name) {
		if (name.equals(PROPERTY_FONT))
			return strFont;
		if (name.equals(PROPERTY_SIZE))
			return new SizePropertySource(element,ptSize);
		if (name.equals(PROPERTY_TEXT))
			return strText;

		return null;
	}

	public void setPropertyValue(Object name, Object value) {
		firePropertyChanged((String)name,value);
		
		if (name.equals(PROPERTY_FONT)) {
			strFont = (String)value;	
			return;
		}
		if (name.equals(PROPERTY_TEXT)) {
			strText = (String)value;	
			return;
		}
		if (name.equals(PROPERTY_SIZE)) {
			SizePropertySource sizeProp = (SizePropertySource)value;			
			ptSize = sizeProp.getValue();
		}
		
	}
```

 

如果**只**希望**查看属性值**，该属性的Descriptor则使用**PropertyDescriptor**即可。PropertyDescriptor#createPropertyEditor返回为null，即不可编辑。  
 

（通过Properties改变模型了，怎么通知视图呢？ 请点击[这里](/blog/1789736%20)） 

 

3、适配IPropertySource对象；

· 方式1&gt; 模型对象同时实现IAdaptable接口；

```
public class ButtonElement implements IWorkbenchAdapter,IAdaptable {

	public Object getAdapter(Class adapter) {
		if (adapter == IWorkbenchAdapter.class)
			return this;
		if (adapter == IPropertySource.class)
			return new ButtonElementProperties(this);
		return null;
	}
```

 

· 方式2&gt; 添加org.eclipse.core.runtime.adapters扩展点并实现IAdapterFactory.\[r3]

写道

public Object getAdapter(Object adaptableObject, Class adapterType) {  
if (adapterType== IPropertySource.class && adaptableObject instanceof Todo){  
return new ButtonElementProperties((ButtonElement) adaptableObject);  
}  
return null;  
}

  

4、如果希望能分门别类，可以按照下面的方式来实现嵌套（PropertySource-&gt;PropertySource-&gt;...-&gt;PropertyDescriptor，如SizePropertySource； 源码来自\[r1])

 

```
	public Object getPropertyValue(Object name) {
		if (name.equals(PROPERTY_FONT))
			return strFont;
		if (name.equals(PROPERTY_SIZE))
			return new SizePropertySource(element,ptSize);
		if (name.equals(PROPERTY_TEXT))
			return strText;

		return null;
	}
```

```
	public void setPropertyValue(Object name, Object value) {
		firePropertyChanged((String)name,value);
		
		if (name.equals(PROPERTY_FONT)) {
			strFont = (String)value;	
			return;
		}
		if (name.equals(PROPERTY_TEXT)) {
			strText = (String)value;	
			return;
		}
		if (name.equals(PROPERTY_SIZE)) {
			SizePropertySource sizeProp = (SizePropertySource)value;			
			ptSize = sizeProp.getValue();
		}
		
	}
```

 

## 源码：

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

 

## 参考：

 r1：Eclipse官网的《Properties如何使用》

[http://www.eclipse.org/articles/Article-Properties-View/properties-view.html](http://www.eclipse.org/articles/Article-Properties-View/properties-view.html)

 r2：自定义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)

[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)

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

 r3：FactoryAdapter实现

[http://www.vogella.com/articles/EclipsePlugIn/article.html](http://www.vogella.com/articles/EclipsePlugIn/article.html) # 7. Adapters