2012-11-05 54 views
3

我有一個eclipse插件與一個單一的查看(如eclipse helloworld-view-plugin-project)。在視圖文件中,當我想更新視圖時,我得到一個事件。如何在eclipse插件中更新/刷新視圖?

在這個視圖中,我有一個具有多個標籤的組中的GridData。我有幾個服務註冊到該程序,其狀態應顯示在此GridData中

編輯:爲了更好地展現我的問題我更新了這篇文章,並添加整個代碼:


的createPartControl():

public void createPartControl(Composite _parent) { 
    parent = _parent; 
    createContents(); 
    addBindings(); 
    makeActions(); 
    contributeToActionBars(); 
} 

CreateContents():

protected void createContents() { 

    // fixed 
    checkIcon = //... 
    errorIcon = //... 
    smallFont = SWTResourceManager.getFont("Segoe UI", 7, SWT.NORMAL); 

    // content 
    gl_shell = new GridLayout(1, false); 
    //margins, etc. for gl_shell 
    parent.setLayout(gl_shell); 

    final Label lblGreeting = new Label(parent, SWT.NONE); 
    lblGreeting.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1)); 
    lblGreeting.setText("Hi " + Preferences.getPreName()); 

    // -- GROUP YOUR STATS (show all services) 
    createStatusGroupBox(); 
} 

createStatusGroupBox():

private Group grpYourStatus = null; // outside the method for access in listener (below) 

private void createStatusGroupBox() { 
    grpYourStatus = new Group(parent, SWT.NONE); 
    grpYourStatus.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1)); 
    grpYourStatus.setText("Your upload status"); 
    grpYourStatus.setLayout(new GridLayout(3, false)); 

    // add message if no service is registered 
    if (r.getServiceList().size() == 0) { 
    Label status = new Label(grpYourStatus, SWT.NONE); 
    status.setText("No service registered."); 
    new Label(grpYourStatus, SWT.NONE); //empty 
    new Label(grpYourStatus, SWT.NONE); //empty 
    } 

    // add labels (status, message, name) for each registered service 
    for (IRecorderObject service : r.getServiceList()) { 
    Label name = new Label(grpYourStatus, SWT.NONE); 
    Label status = new Label(grpYourStatus, SWT.NONE); 
    Label message = new Label(grpYourStatus, SWT.NONE); 
    message.setFont(smallFont); 
    message.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1)); 

    service.getServiceViewItem().setLabelsAndIcons(name, status, message, checkIcon, errorIcon); //this also sets the values of the labels (label.setText(...) via data binding) 
} 

不幸的是,我不知道正確的方式是更新/重置什麼。我試過如下:

監聽器(這應該更新視圖/服務列表):

r.addPropertyChangeListener(BindingNames.SERVICE_ADDED, new PropertyChangeListener() { 
    public void propertyChange(final PropertyChangeEvent evt) { 
    Display.getDefault().asyncExec(new Runnable() { 
     public void run() { 
     // This "redraws" the view, but just places the whole content (generated in createStatusGroupBox()) above the other parts. 
     //Display.getCurrent().update(); 
     //createStatusGroupBox(); 
     //parent.layout(true); 
     //parent.redraw(); 

     // disposes grpYourStatus-Group but doesn't show anything then 
     grpYourStatus.dispose(); 
     createStatusGroupBox(); 
     grpYourStatus.layout(); 
     grpYourStatus.redraw(); 
     } 
    }); 
    } 
}); 

我也嘗試下面的語句(個別);均無功而返:

parent.redraw(); 
parent.update(); 
parent.layout(); 
parent.layout(true); 
parent.refresh(); 

在這種post我閱讀以下內容:一個視圖,在其 含有小部件創建的生命週期

createPartControls的是,時間(當視圖首先變得可見 )。此代碼僅在視圖生命週期 期間執行一次,因此您無法直接在此添加任何內容以刷新您的 視圖。

Eclipse部件通常會更新其內容,作爲對工作臺內部改變的選擇的反應(例如,用戶可能在調試視圖中的另一個堆棧幀上單擊 )。

不幸的是,我不知道什麼東西我可以嘗試,我沒有找到任何與搜索有幫助...... 感謝對您的幫助和建議

回答

3

我終於找到了答案(連同AndreiC的幫助!):

我的聽衆,現在看起來是這樣的:

r.addPropertyChangeListener(BindingNames.SERVICE_ADDED, new PropertyChangeListener() { 
    public void propertyChange(final PropertyChangeEvent evt) { 
    Display.getDefault().asyncExec(new Runnable() { 
     public void run() { 
     // remove grpYourStatus from parent 
     grpYourStatus.dispose(); 

     // add grpYourStatus (with updated values) to parent 
     createStatusGroupBox(); 

     // refresh view 
     parent.pack(); 
     parent.layout(true); 
     } 
    }); 
    } 
}); 

剩下的就是像上面的代碼相同。

1

我不知道API的心臟,但你試過parent.update()

+0

謝謝安德烈您的回答。是的,我也嘗試過(添加到我的文章)。你有什麼其他的建議?謝謝 – casaout

+0

你有沒有嘗試過只使用layout(),沒有重繪()? – acostache

+0

不幸的是,parent.layout()沒有改變任何東西... – casaout