Eclipse插件开发之DataBinding(TableViewer与SWT组件不能同时使用)

Eclipse插件开发之DataBinding(TableViewer与SWT组件不能同时使用)

概述

Databinding可以在模型和用户界面之间建立一种关联。当任何一方(模型或用户界面)发生变化时,都可以同步到另一方。 另外你可以使用默认的或者是自定义的数据验证器(validator)来验证数据同时可以使用转换器(converter)在模型和界面之间转换数据。

不包含Viewer的界面数据绑定步骤

  1. 设计数据模型(Person )
  2. Person 实现PropertyChangeListener接口
    代码来自:vogella Databinding
package de.vogella.databinding.person.model;
 
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
 
public class Person implements PropertyChangeListener {
	private String firstName;
	private String lastName;
	private boolean married;
	private String gender;
	private Integer age;
	private Address address;
	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);
 
	public Person() {
	}
 
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
	}
 
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(listener);
	}
 
	public String getFirstName() {
		return firstName;
	}
 
	public String getGender() {
		return gender;
	}
 
	public String getLastName() {
		return lastName;
	}
 
	public boolean isMarried() {
		return married;
	}
 
	public void setFirstName(String firstName) {
		propertyChangeSupport.firePropertyChange("firstName", this.firstName,
				this.firstName = firstName);
	}
 
	public void setGender(String gender) {
		propertyChangeSupport.firePropertyChange("gender", this.gender,
				this.gender = gender);
	}
 
	public void setLastName(String lastName) {
		propertyChangeSupport.firePropertyChange("lastName", this.lastName,
				this.lastName = lastName);
	}
 
	public void setMarried(boolean isMarried) {
		propertyChangeSupport.firePropertyChange("married", this.married,
				this.married = isMarried);
	}
 
	public Integer getAge() {
		return age;
	}
 
	public void setAge(Integer age) {
		propertyChangeSupport.firePropertyChange("age", this.age,
				this.age = age);
	}
 
	public Address getAddress() {
		return address;
	}
 
	public void setAddress(Address address) {
		address.addPropertyChangeListener("country", this);
		propertyChangeSupport.firePropertyChange("address", this.address,
				this.address = address);
	}
 
	@Override
	public String toString() {
		return firstName + " " + lastName;
	}
 
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		propertyChangeSupport.firePropertyChange("address", null, address);
	}
 
}
				
				
 

3.设计界面(WindowsBuilder——可视化设计工具)
Eclipse插件开发之DataBinding(TableViewer与SWT组件不能同时使用)_第1张图片
4.选中需要绑定的界面组件,如Text,右键选择bindings绑定数据,弹出配置界面如下:
Eclipse插件开发之DataBinding(TableViewer与SWT组件不能同时使用)_第2张图片
5.测试

包含Viewer的界面数据绑定

ViewerSupport.bind()
WritableList

TableViewer+Text+Combo

定义数据模型包含Person和WritableList。

待续……

参考:

https://git.eclipse.org/c/platform/eclipse.platform.ui.git/plain/examples/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet034ComboViewerAndEnum.java
https://wiki.eclipse.org/JFace_Data_Binding/Snippets
https://www.vogella.com/tutorials/EclipseDataBinding/article.html

你可能感兴趣的:(Java,eclipse)