package com.test.cxm.plugin01.perspective;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class HelloWorldSWT {
public static void main(String args[]){
Display display=Display.getDefault();
Shell shell=new Shell();
shell.setText("mmm");
shell.setLayout(new FillLayout());
final TableViewer tv=new TableViewer(shell, SWT.MULTI|SWT.BORDER);
Table tb=tv.getTable();
tb.setHeaderVisible(true);
tb.setLinesVisible(true);
//注意TableLayout布局的使用方法,它可以自定义列的长度
TableLayout layout=new TableLayout();
tb.setLayout(layout);
layout.addColumnData(new ColumnWeightData(50));
TableColumn tc1=new TableColumn(tb,SWT.NONE);
tc1.setText("ID号");
tc1.addSelectionListener(new SelectionAdapter() {
boolean asc=true;
public void widgetSelected(SelectionEvent e){
tv.setSorter(asc?ViewSorter.ID_ASC:ViewSorter.ID_DESC);
asc=!asc;
}
});
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(tb,SWT.NONE).setText("姓名");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(tb, SWT.NONE).setText("性别");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(tb, SWT.NONE).setText("年龄");
layout.addColumnData(new ColumnWeightData(60));
new TableColumn(tb, SWT.NONE).setText("创建时间");
tv.setContentProvider(new ContentPro());
tv.setLabelProvider(new ViewPro());
Object data=PeopleFactory.getPeople();
tv.setInput(data);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
内容提供者
package com.test.cxm.plugin01.perspective;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* 内容提供者
* 主要分为用于列表和表的IStructuredContentProvider和用于树的ITreeContentProvider
* 前者主要作用将输入的数据转换为数组,后者为在树中获得子或父项目提供支持
* 查看器通过setContentProvider方法与内容提供者相关联
*
* @author Administrator
*
*/
public class ContentPro implements IStructuredContentProvider{
public void dispose() {
// TODO Auto-generated method stub
}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
public Object[] getElements(Object arg0) {
if(arg0 instanceof List){
return ((List)arg0).toArray();
}else{
return new Object[0];
}
}
}
标签提供者
package com.test.cxm.plugin01.perspective;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;
/**
* 标签提供者的主要作用是:决定实体的哪个字段在哪个列上显示,同样还有图像
* @author Administrator
*
*/
public class ViewPro implements ITableLabelProvider{
private Image[] images=new Image[]{
new Image(null,"icons/1.jpg"),
new Image(null,"icons/2.jpg")
};
public void addListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
public boolean isLabelProperty(Object arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
public void removeListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
public Image getColumnImage(Object arg0, int arg1) {
People p=(People)arg0;
if(p.getName().equals("ss2")){
if(arg1==3){
return images[1];
}
}
if(p.getName().equals("ss1")){
if(arg1==3){
return images[0];
}
}
return null;
}
/**
* 第一个参数代表记录
* 第二个参数代表哪一列
*/
public String getColumnText(Object arg0, int arg1) {
People p=(People)arg0;
if(arg1 == 0){
return p.getId().toString();
}
if(arg1 == 1){
return p.getName();
}
if(arg1 == 2){
return p.isSex()?"男":"女";
}
if(arg1 == 3){
return String.valueOf(p.getAge());
}
if(arg1 == 4){
return p.getCreateDate().toLocaleString();
}
return null;
}
}
实体类
package com.test.cxm.plugin01.perspective;
import java.util.Date;
public class People {
private Long id;
private String name;
private boolean sex;
private int age;
private Date createDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
工厂类
package com.test.cxm.plugin01.perspective;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class PeopleFactory {
public static List<People> getPeople(){
List<People> list =new ArrayList<People>();
People p1=new People();
p1.setId(1l);
p1.setName("ss1");
p1.setSex(true);
p1.setAge(28);
p1.setCreateDate(new Date());
People p2=new People();
p2.setId(2l);
p2.setName("ss2");
p2.setSex(true);
p2.setAge(28);
p2.setCreateDate(new Date());
People p3=new People();
p3.setId(3l);
p3.setName("ss3");
p3.setSex(false);
p3.setAge(28);
p3.setCreateDate(new Date());
list.add(p1);
list.add(p2);
list.add(p3);
return list;
}
}
排序类
package com.test.cxm.plugin01.perspective;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
/**
* 编写排序类是表格排序的关系,排序类一般继承ViewerSorter类
* @author Administrator
*
*/
public class ViewSorter extends ViewerSorter{
private static final int ID=1;
private static final int NAME=2;
private int sortType;
public static final ViewSorter ID_ASC=new ViewSorter(ID);
public static final ViewSorter ID_DESC=new ViewSorter(-ID);
public static final ViewSorter NAME_ASC=new ViewSorter(ID);
public static final ViewSorter NAME_DESC=new ViewSorter(ID);
private ViewSorter(int sortType){
this.sortType=sortType;
}
public int compare(Viewer viewer,Object obj1,Object obj2){
People p1=(People)obj1;
People p2=(People)obj2;
switch(sortType){
case ID:{
Long l1=p1.getId();
Long l2=p2.getId();
return l1.compareTo(l2);
}
case -ID:{
Long l1=p1.getId();
Long l2=p2.getId();
return l2.compareTo(l1);
}
case NAME:{
String s1=p1.getName();
String s2=p2.getName();
return s1.compareTo(s2);
}
case -NAME:{
String s1=p1.getName();
String s2=p2.getName();
return s2.compareTo(s1);
}
}
return 0;
}
}