2016-11-08 13 views
1

说我有一个ItemListPart类在我的RCP项目,并有显示在它下面的表格:在Eclipse RCP项目中,更新零件表格视图的正确方法是什么?

import java.util.List; 

import javax.annotation.PostConstruct; 

import org.eclipse.e4.ui.di.Focus; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Table; 
import org.eclipse.swt.widgets.TableColumn; 
import org.eclipse.swt.widgets.TableItem; 

public class ItemListPart { 
    private Table table; 

    @PostConstruct 
    public void createControls(Composite parent){ 
     //I wish the ItemList could be changed from outside the ItemListPart class. 
     List<Item> ItemList = getItemList(); 

     parent.setLayout(new GridLayout(2, false)); 

     table = new Table(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); 
     table.setLinesVisible(true); 
     table.setHeaderVisible(true); 
     GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 
     data.heightHint = 200; 
     table.setLayoutData(data); 

     String[] titles = { "Item Name", "Description"}; 
     for (int i = 0; i < titles.length; i++) { 
      TableColumn column = new TableColumn(table, SWT.NONE); 
      column.setText(titles[i]); 
      table.getColumn(i).pack(); 
     } 


     for(Item it:ItemList){ 
      TableItem item = new TableItem(table, SWT.NONE); 
      item.setText(0, it.getName()); 
      item.setText(1, it.getDesc()); 
     } 

     for (int i=0; i<titles.length; i++) { 
      table.getColumn (i).pack(); 
     } 
    } 

    @Focus 
    public void onFocus() { 
     table.setFocus(); 
    } 
} 

这里ItemList<Item>是我希望能够从ItemListPart类外修改列表,并且每当ItemList<Item>中的数据发生更改时,都可以根据更新的数据自动刷新表视图。什么是实现这一目标的正确途径?谢谢。

回答

1

没有一个“正确”的方式。

一种方法是使用事件代理,让您的视图部分监听更新列表的代码中的事件。

在其更新列表后一个事件的代码:

@Inject 
IEventBroker eventBroker; 

... 

eventBroker.post("/list/updated", Collections.EMPTY_MAP); 

在您看来侦听更新事件:

@Inject 
@Optional 
public void listUpdated(@UIEventTopic("/list/updated") Event event) 
{ 
    // TODO handle the update 
} 

你也许会发现它更容易使用TableViewer而非Table因为单独的内容提供者使得更新表格以更换内容变得更容易。

您可以在事件中使用类似的东西也传递数据:

MyClass myClass = .... 

eventBroker.post("/list/updated", myClass); 


@Inject 
@Optional 
public void listUpdated(@UIEventTopic("/list/updated") MyClass myClass) 

有事件经纪人here

+0

谢谢大家一些更多的信息。在你的代码示例中,传递的数据是否为'Collections.EMPTY_MAP'以便更新?如果是这样,我怎么能在方法'listUpdated(@UIEventTopic(“/ list/updated”)事件事件)中获取数据? – kenshinji

+0

是的,我添加了一个例子,将事件中的一些数据传递给答案。 –

相关问题