如何更新/刷新 Eclipse 插件中的视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13234090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:19:55  来源:igfitidea点击:

How to update/refresh a view in an eclipse plug-in?

javaeclipsepluginsview

提问by casaout

I have an eclipse plug-inwith a single view(like the eclipse helloworld-view-plugin-project). In the view-file I get an event when I want to update the view.

我有一个带有单一视图eclipse 插件(如 eclipse helloworld-view-plugin-project)。在视图文件中,当我想更新视图时会收到一个事件

In this view I have a GridData in a Group with multiple labels. I have several services which register to the programe and whose status should be shownin this GridData.

在此视图中,我在具有多个标签的组中有一个 GridData。我有几个注册到程序的服务,其状态应显示在此 GridData 中。

Edit: In order to better show my problem I updated this post and added the whole code:

编辑:为了更好地展示我的问题,我更新了这篇文章并添加了整个代码:



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():

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)
}


Unfortunately, I don't know what the right way is to update/reset it. I tried the following:

不幸的是,我不知道更新/重置它的正确方法是什么。我尝试了以下方法:

listener (which should update the view / the services-list):

侦听器(应该更新视图/服务列表):

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(); 
      }
    });
  }
});

I also tried the following statements (individually); all without success:

我还尝试了以下语句(单独);都没有成功:

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


In this postI read the following:

在这篇文章中,我阅读了以下内容:

createPartControls is that time of the life cycle of a view, where its contained widgets are created (when the view becomes visible initially). This code is only executed once during the view life cycle, therefore you cannot add anything here directly to refresh your view.

Eclipse parts typically update their content as a reaction to a changed selection inside of the workbench (e.g. the user might click on another stack frame in the debug view).

createPartControls 是视图生命周期的那个时间,在那里创建其包含的小部件(当视图最初变得可见时)。此代码仅在视图生命周期内执行一次,因此您无法在此处直接添加任何内容来刷新视图。

Eclipse 部件通常更新它们的内容,作为对工作台内更改的选择的反应(例如,用户可能单击调试视图中的另一个堆栈帧)。

Unfortunately, I don't know what to else I could try and I didn't find anything helpful with searches... thank's for your help and suggestions!

不幸的是,我不知道我还能尝试什么,而且我没有找到任何对搜索有帮助的东西......感谢您的帮助和建议

采纳答案by casaout

I finally found the answer (together with AndreiC's help!):

我终于找到了答案(在 AndreiC 的帮助下!):

my listener now looks like this:

我的听众现在看起来像这样:

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);
      }
    });
  }
});

The rest is the same like the code above.

其余的和上面的代码一样。

回答by acostache

I do not know the API by heart, but have you tried parent.update()?

我对 API 一无所知,但您尝试过parent.update()吗?