如何更新 TableViewer 中的 ProgressBar?

How to update ProgressBar in TableViewer?

在我的 TableViewer 中,我有一个 OwnerDrawLabelProvider,在我 table 的特定列中,我将 ProgressBar 添加到 TableEditor 实例。

我的问题如下:

我可以设置 ProgressBar 的选择,但在尝试更新时它保持相同的值。

代码:

@Override
public void update(ViewerCell cell) {
if(columnIndex == 4){

            Table table = tableViewer.getTable();
            TableItem item;
            TableItem[] items;
            TableEditor editor;             

            items = table.getItems();
            ProgressBar bar = new ProgressBar(table, SWT.NONE);
            bar.setMinimum(0);
            bar.setMaximum(100);
            bar.setState(SWT.NORMAL);
            bar.setSelection(0);
            bar.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
            if(mediator.isSent()){
                List<Item> itemsSendToSubmit = mediator.getItemsSendToSubmit();
                if(!itemsSendToSubmit.isEmpty()){
                    for(Iterator<Item> itemIterator = itemsSendToSubmit.iterator(); itemIterator.hasNext(); )
                    {
                        Item itemSubmited = itemIterator.next();                        
                        for(TableItem tableItem: items)
                        {                               if(tableItem.getText(0).contains(itemSubmited.getId())) 
                            {                   
                            bar.setSelection(PluginUtility.getBuildProgress(itemSubmited.getPlan()));
                                editor = new TableEditor(table);
                                editor.grabHorizontal = true;
                                editor.grabVertical = true;

                                editor.setEditor(bar, tableItem, 4);                    

                            }
                        }                   
                    }
                }
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

我了解到一个问题,即 ProgressBar 的 setSelection 方法存在一些问题。我通过扩展基础 class 创建了自己的 ProgressBar,并用修复代码覆盖了 setSelection 方法,但仍然不起作用。

在正常的主函数中,这有效。

我能否就可能出现的问题或在 TableViewer 中添加此 ProgressBar 如何影响其行为获得一些建议?

编辑:如果我在创建标签提供程序时创建进度条的单个实例,然后将其传递给table编辑器,它将更新我所在的最后一个元素的进度条说 editor.setEditor(bar, tableItem, 4); 但我需要为每个项目显示一个进度条并为每个项目更新它!

也许更新发生了,但视觉效果没有更新,具体取决于您如何调用此代码。

您必须确保在执行此更新时没有调动 ui 线程。

在 Eclipse 中 3.x 这意味着使用

Display.getDefault().asyncExec(new Runnable() {
  public void run() {
    // ... do any work that updates the screen ...
  }
});

或者在4.x

中使用注入获取UISynchronize

基本上把setSelection代码放在​​那里。

请参阅这篇关于 eclipse 插件后台处理的文章: http://www.vogella.com/tutorials/EclipseJobs/article.html

当您更新 ProgressBar 的值时,之后您需要 redraw/repaint TableViewer 及其所有属性,然后它将显示更新进度条。希望这能解决您的问题。