前几篇讲过SWT的使用,但是SWT是基本的小控件,只能针对使用简单的数据类型,比如字符串,数字等等。但是遇到了复杂的类,该怎么办呢?

【插件开发】—— 10 JFace开发详解-LMLPHP

  不要担心!这里就可以使用JFace了,它是对SWT的封装,可以应用于复杂的数据类型。

  下面的介绍中有时候说控件,有时候说视图,原因就是这个JFace通常用于某个视图中,但是它其实就是一个复杂的组合控件而已,只不过需要我们额外的去设定内容与复杂类的内容匹配。因此如果想使用复杂的数据类型,必然要与JFace提供的控件进行兼容,就涉及到两个主要的函数:setContentProvider() 以及 setLabelProvider()。这两个函数下面会进行详细的介绍:

  setContentProvider() 内容提供者

  这个函数为JFace的控件提供内容,总的来说一共有一下几种:

  ListViewer列表视图 TabelViewer表格视图 TreeViewer树形视图 TextViewer文本视图

  除了最后一种比较特殊,不需压指定内容提供者,其他的三个JFace视图都需要设置内容提供者,以便设定关联的内容。

  setLabelProvider()标签提供者

  这个函数设置了标签提供者,用于JFace的控件视图现实内容,一般来说都有两个函数:

  getColumnImage()和getColumnText(),一个用于设定标签上现实的图片,一个用于设定现实的内容

  那么下面就看一下不同的JFace视图,这两个函数使用的差异。

  ListViewer

  这个列表视图,属于最简单的视图了,这里我们创建一个复杂点的元素

 class Person{
private int id;
private String name;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return name;
}
}

  当我们从扩展点创建一个视图,并打开它的实现类时,插件为我们自动添加好了接口,里面有三个直接生成的函数

public class ListViewerTest extends ViewPart {

    public ListViewerTest() {

    }

    public void createPartControl(Composite parent) {

    }

    public void setFocus() {

    }
}

  此时,我们需要扩展createPartControl,在这里面创建一个view,并在其中添加 内容提供者函数,以及标签提供者函数。

viewer = new ListViewer(parent, SWT.SINGLE);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new MySorter());
viewer.setInput(getSite());

  通常来说,都会有上面的五句话

  第一行:创建了一个ListViewer的视图

  第二行:设置它的内容提供者

  第三行:设置它的标签提供者

  第四行:设置排序规则

  第五行:设置输入,这里面我们的输入由内容提供者提供,因此这里就直接设置参数为getSite()(可以理解为一个字符串,这个地方随便写都行,但是一定要有这个函数,空串都可以)就可以了。

  下面看一下这几个函数的实现:

   class ViewContentProvider implements IStructuredContentProvider{
public Object[] getElements(Object inputElement) { Person[] persons = new Person[];
persons[] = new Person();
persons[].setId();
persons[].setName("xingoo");
persons[].setAge();
persons[].setAddress("ChangChun"); persons[] = new Person();
persons[].setId();
persons[].setName("halo");
persons[].setAge();
persons[].setAddress("ShenYang"); persons[] = new Person();
persons[].setId();
persons[].setName("haha");
persons[].setAge();
persons[].setAddress("DaLian"); return persons;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
class ViewLabelProvider extends LabelProvider{
public Image getColumnImage(Object element) {
return null;
}
public String getColumnText(Object element) {
return getText(element);
}
}
class MySorter extends ViewerSorter{
public int compare(Viewer viewer,Object ob1,Object ob2){
return ((Person)ob1).getId() - ((Person)ob2).getId();
}
}

  对于内容提供者来说,最重要的一个方法就是getElements,因为视图上现实的数据,都要从这个方法获得。

  下面看一下对应的全部代码

 package testpreference.views;

 import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
class Person{
private int id;
private String name;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return name;
}
} public class ListViewerTest extends ViewPart {
public static final String ID = "testpreference.views.ListViewerTest";
private ListViewer viewer; public ListViewerTest() { } public void createPartControl(Composite parent) {
viewer = new ListViewer(parent, SWT.SINGLE);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new MySorter());
viewer.setInput(getSite()); viewer.addSelectionChangedListener( new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
System.out.println("Now selecting "+selection.getFirstElement().toString());
}
}); viewer.addDoubleClickListener( new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
System.out.println("Double clicking "+selection.getFirstElement().toString());
}
});
} public void setFocus() { }
class ViewContentProvider implements IStructuredContentProvider{
public Object[] getElements(Object inputElement) { Person[] persons = new Person[];
persons[] = new Person();
persons[].setId();
persons[].setName("xingoo");
persons[].setAge();
persons[].setAddress("ChangChun"); persons[] = new Person();
persons[].setId();
persons[].setName("halo");
persons[].setAge();
persons[].setAddress("ShenYang"); persons[] = new Person();
persons[].setId();
persons[].setName("haha");
persons[].setAge();
persons[].setAddress("DaLian"); return persons;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
class ViewLabelProvider extends LabelProvider{
public Image getColumnImage(Object element) {
return null;
}
public String getColumnText(Object element) {
return getText(element);
}
}
class MySorter extends ViewerSorter{
public int compare(Viewer viewer,Object ob1,Object ob2){
return ((Person)ob1).getId() - ((Person)ob2).getId();
}
}
}

  【插件开发】—— 10 JFace开发详解-LMLPHP

  tableViewer

  这个对于上面的Listviewer来说,标签提供者方面复杂了一些

  首先,创建视图时,需要定义Table的样式

 viewer = new TableViewer(parent,SWT.SINGLE|SWT.FULL_SELECTION);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true); String[] columnNames = new String[]{"id","name","age","address"};
int[] columnWidths = new int[]{,,,};
int[] columnAlignments = new int[]{SWT.LEFT,SWT.LEFT,SWT.LEFT,SWT.LEFT};
for(int i=;i<columnNames.length;i++){
TableColumn tableColumn = new TableColumn(table,columnAlignments[i]);
tableColumn.setText(columnNames[i]);
tableColumn.setWidth(columnWidths[i]);
} viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new MySorter());
viewer.setInput(getSite());

  对于标签提供者函数来说,稍微麻烦一点,需要根据index指定每一列显示的内容

class ViewLabelProvider extends LabelProvider implements ITableLabelProvider{
public Image getColumnImage(Object element,int index) {
return null;
}
public String getColumnText(Object element,int index) {
Person person = (Person)element;
switch(index){
case :
return person.getId()+"";
case :
return person.getName();
case :
return person.getAge()+"";
case :
return person.getAddress();
default:
return "unknown"+index;
}
}
}

  全部代码及运行结果

 package testpreference.views;

 import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart; public class TableViewerTest extends ViewPart {
public static final String ID = "testpreference.views.TableViewerTest";
private TableViewer viewer;
public TableViewerTest() {
// TODO Auto-generated constructor stub
} public void createPartControl(Composite parent) {
viewer = new TableViewer(parent,SWT.SINGLE|SWT.FULL_SELECTION);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true); String[] columnNames = new String[]{"id","name","age","address"};
int[] columnWidths = new int[]{,,,};
int[] columnAlignments = new int[]{SWT.LEFT,SWT.LEFT,SWT.LEFT,SWT.LEFT};
for(int i=;i<columnNames.length;i++){
TableColumn tableColumn = new TableColumn(table,columnAlignments[i]);
tableColumn.setText(columnNames[i]);
tableColumn.setWidth(columnWidths[i]);
} viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new MySorter());
viewer.setInput(getSite());
} public void setFocus() {
// TODO Auto-generated method stub }
class ViewContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) { Person[] persons = new Person[];
persons[] = new Person();
persons[].setId();
persons[].setName("xingoo");
persons[].setAge();
persons[].setAddress("ChangChun"); persons[] = new Person();
persons[].setId();
persons[].setName("halo");
persons[].setAge();
persons[].setAddress("ShenYang"); persons[] = new Person();
persons[].setId();
persons[].setName("haha");
persons[].setAge();
persons[].setAddress("DaLian"); return persons;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider{
public Image getColumnImage(Object element,int index) {
return null;
}
public String getColumnText(Object element,int index) {
Person person = (Person)element;
switch(index){
case :
return person.getId()+"";
case :
return person.getName();
case :
return person.getAge()+"";
case :
return person.getAddress();
default:
return "unknown"+index;
}
}
}
class MySorter extends ViewerSorter{
public int compare(Viewer viewer,Object ob1,Object ob2){
return ((Person)ob1).getId() - ((Person)ob2).getId();
}
} }

【插件开发】—— 10 JFace开发详解-LMLPHP

  TreeViewer

  这个视图相比前面的这个复杂在它的内容提供者函数,我们需要让他的ContentProvider函数继承ITreeContentProvider接口,需要实现下面几个方法:

  getChildren();获取节点的孩子节点。

  getParent();获取节点的父节点

  hasChildren();判断是否有孩子节点

  getElements();获取全部的节点

  下面看一下示例的代码

 class ViewContentProvider extends ArrayContentProvider implements ITreeContentProvider{

         public Object[] getChildren(Object parentElement) {
TreePerson person = (TreePerson)parentElement;
return person.getChildren().toArray();
} public Object getParent(Object element) {
TreePerson person = (TreePerson)element;
return person.getParent();
} public boolean hasChildren(Object element) {
TreePerson person = (TreePerson)element;
return person.getChildren().size()>?true:false;
} public Object[] getElements(Object inputElement) {
TreePerson[] persons = new TreePerson[];
persons[] = new TreePerson();
persons[].setId();
persons[].setName("xingoo");
persons[].setAge();
persons[].setAddress("ChangChun"); persons[] = new TreePerson();
persons[].setId();
persons[].setName("halo");
persons[].setAge();
persons[].setAddress("ShenYang"); persons[] = new TreePerson();
persons[].setId();
persons[].setName("haha");
persons[].setAge();
persons[].setAddress("DaLian"); persons[].getChildren().add(persons[]);
persons[].getChildren().add(persons[]);
persons[].setParent(persons[]);
persons[].setParent(persons[]); return persons;
} }

  而他的标签提供者跟ListViewer的差不多,仅仅需要提供一个简单的名称就可以了。

class ViewLabelProvider extends LabelProvider{
public Image getColumnImage(Object element) {
return null;
}
public String getColumnText(Object element) {
return ((TreePerson)element).toString();
}
}

  全部代码以及运行结果

 package testpreference.views;

 import java.util.ArrayList;
import java.util.List; import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
class TreePerson{
private int id;
private String name;
private int age;
private String address;
private List<TreePerson> children;
private TreePerson parent = null; public TreePerson() {
children = new ArrayList();
}
public List<TreePerson> getChildren() {
return children;
}
public void setChildren(List<TreePerson> children) {
this.children = children;
}
public TreePerson getParent() {
return parent;
}
public void setParent(TreePerson parent) {
this.parent = parent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return name;
}
}
public class TreeViewerTest extends ViewPart {
public static final String ID = "testpreference.views.TreeViewerTest";
private TreeViewer viewer;
public TreeViewerTest() {
// TODO Auto-generated constructor stub
} public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent,SWT.SINGLE);
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setContentProvider(new ViewContentProvider());
viewer.setSorter(new MySorter());
viewer.setInput(getSite());
} public void setFocus() {
// TODO Auto-generated method stub }
class ViewContentProvider extends ArrayContentProvider implements ITreeContentProvider{ public Object[] getChildren(Object parentElement) {
TreePerson person = (TreePerson)parentElement;
return person.getChildren().toArray();
} public Object getParent(Object element) {
TreePerson person = (TreePerson)element;
return person.getParent();
} public boolean hasChildren(Object element) {
TreePerson person = (TreePerson)element;
return person.getChildren().size()>?true:false;
} public Object[] getElements(Object inputElement) {
TreePerson[] persons = new TreePerson[];
persons[] = new TreePerson();
persons[].setId();
persons[].setName("xingoo");
persons[].setAge();
persons[].setAddress("ChangChun"); persons[] = new TreePerson();
persons[].setId();
persons[].setName("halo");
persons[].setAge();
persons[].setAddress("ShenYang"); persons[] = new TreePerson();
persons[].setId();
persons[].setName("haha");
persons[].setAge();
persons[].setAddress("DaLian"); persons[].getChildren().add(persons[]);
persons[].getChildren().add(persons[]);
persons[].setParent(persons[]);
persons[].setParent(persons[]); return persons;
} }
class ViewLabelProvider extends LabelProvider{
public Image getColumnImage(Object element) {
return null;
}
public String getColumnText(Object element) {
return ((TreePerson)element).toString();
}
}
class MySorter extends ViewerSorter{
public int compare(Viewer viewer,Object ob1,Object ob2){
return ((TreePerson)ob1).getId() - ((TreePerson)ob2).getId();
}
}
}

【插件开发】—— 10 JFace开发详解-LMLPHP

  TextViewer

  这个前一篇的源码编辑器上有讲解过,是一种可以编辑的Text文本,它可以设定特定复杂的样式,这里就看一个简单的例子

 viewer = new TextViewer(parent,SWT.MULTI | SWT.V_SCROLL);
String str = "this is test \n this is test";
Document document = new Document(str);
viewer.setDocument(document); TextPresentation style = new TextPresentation();
style.addStyleRange(new StyleRange(,,null,null,SWT.BOLD));
viewer.changeTextPresentation(style, true); viewer.setInput(getSite());

  全部代码以及运行结果

 package testpreference.views;

 import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart; import testpreference.views.TreeViewerTest.MySorter;
import testpreference.views.TreeViewerTest.ViewContentProvider;
import testpreference.views.TreeViewerTest.ViewLabelProvider; public class TextViewerTest extends ViewPart {
public static final String ID = "testpreference.views.TreeViewerTest";
private TextViewer viewer; public TextViewerTest() {
// TODO Auto-generated constructor stub
} public void createPartControl(Composite parent) {
viewer = new TextViewer(parent,SWT.MULTI | SWT.V_SCROLL);
String str = "this is test \n this is test";
Document document = new Document(str);
viewer.setDocument(document); TextPresentation style = new TextPresentation();
style.addStyleRange(new StyleRange(,,null,null,SWT.BOLD));
viewer.changeTextPresentation(style, true); viewer.setInput(getSite());
} public void setFocus() {
// TODO Auto-generated method stub } }

【插件开发】—— 10 JFace开发详解-LMLPHP

05-14 17:30