Eclipse Jface MVC中的多个数据绑定对象

时间:2015-04-26 09:36:21

标签: java model-view-controller jface

我认为有关jFace TableViewer及其绑定的一些问题。我使用TableViewer的以下示例代码:

public class AppPersonViewer extends TableViewer
{
    public Table table;

    public AppPersonViewer(Composite parent, int style) 
    {
        super(parent, style);
        table = getTable();
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        table.setLayoutData(gridData);
        createColumns();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        setContentProvider(new AppContentProvider());
    }

    private void createColumns()
    {
        String[] titles = { "First Name", "Second Name", "Age", "Country", "Likes SO" };
        int[] bounds = { 150, 150, 100, 150, 100 };

        TableViewerColumn column = createTableViewerColumn(titles[0], bounds[0], 0);
        column.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof Person)
                    return ((Person)element).getFirst();
                return super.getText(element);
            }
        });

        column = createTableViewerColumn(titles[1], bounds[1], 1);
        column.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof Person)
                    return ((Person)element).getSecond();
                return super.getText(element);
            }
        });

        column = createTableViewerColumn(titles[2], bounds[2], 2);
        column.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof Person)
                    return ""+((Person)element).getAge();
                return super.getText(element);
            }
        });

        column = createTableViewerColumn(titles[3], bounds[3], 3);
        column.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof Person)
                    return ((Person)element).getCountry();
                return super.getText(element);
            }
        });

        column = createTableViewerColumn(titles[4], bounds[4], 4);
        column.setLabelProvider(new ColumnLabelProvider(){
            public Image getImage(Object element) {
                    return ((Person)element).getImage();
            }

            public String getText(Object element) {
                  return null;  // no string representation, we only want to display the image
                }

        });


    }

    private TableViewerColumn createTableViewerColumn(String header, int width, int idx) 
    {
        TableViewerColumn column = new TableViewerColumn(this, SWT.LEFT, idx);
        column.getColumn().setText(header);
        column.getColumn().setWidth(width);
        column.getColumn().setResizable(true);
        column.getColumn().setMoveable(true);

        return column;
    }
}

此处使用Person对象进行数据绑定:

import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

public class Person 
{
    private static final String[]   FIRST = {"Favonius", "Tim", "Brad", "Scott", "Linda"};
    private static final String[]   SECOND = {"Cruise", "Temp", "Abbey", "Adam", "Albert", "Thomas"};
    private static final String[]   COUNTRY = {"India", "USA", "Russia", "UK", "France", "Germany"};
    private static final int[]      AGE = {22, 23, 24, 25, 26, 27, 28, 29, 30};

    private static Random random = new Random(System.currentTimeMillis());

    private String first;
    private String second;
    private String country;
    private String likes;

    private int age;

    public Person(String first, String second, String country, String likes, int age) 
    {
        super();
        this.first = first;
        this.second = second;
        this.country = country;
        this.likes = likes;
        this.age = age;
    }
    public String getFirst() {
        return first;
    }
    public String getSecond() {
        return second;
    }
    public String getCountry() {
        return country;
    }
    public String getLikes() {
        return likes;
    }
    public int getAge() {
        return age;
    }
    public void setLikes(String likes) {
        this.likes = likes;
    }

    public Image getImage(){
        return Display.getDefault().getSystemImage(SWT.ICON_ERROR);
    }

    public static Person createRandomPerson(){
        return new  Person(FIRST[random.nextInt(FIRST.length)], 
                SECOND[random.nextInt(SECOND.length)], COUNTRY[random.nextInt(COUNTRY.length)], 
                "Y", AGE[random.nextInt(AGE.length)]);
    }
}

该示例仅将一个对象绑定到TableViewer。我的问题是我想将多个对象绑定到TableViewer。例如,TableViewer的第1列应该保持对象A的属性测试,而TableViewer的第2列应该保存对象B的属性newTest。

这可能吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

表格中的每一行都使用内容提供商提供的单个对象。

因此,如果您需要来自不同对象的值,则需要返回包含来自内容提供者的那些对象的复合类。