如何在Eclipse中向Application Editor添加监听器?

时间:2011-02-19 05:12:18

标签: java eclipse-plugin eclipse-rcp

我正在编写一个Eclipse RCP插件,用于显示应用程序编辑器中显示的对象的属性。 我的插件扩展了PageBookView。每次,我选择在ApplicationEditor(即Canvas小部件)上打开一个新对象,我创建一个新页面&保存旧页面。

ApplicationEditor扩展了EditorPart。当对象(在活动编辑器上更改)时,它会触发propertyChange事件。我想要的是添加监听器到applicationEditor。当所需的事件触发时,我必须更新我的页面。

让我以简单的方式说出来。

    public Class MyPage implements IPage implements **WHICH_LISTENER**
    {

    public MyPage(ApplicationEditor editor)
    {

    this.addPropertyChangeListener(editor); 

    }
  . . . . . . 

}

应该通过propertyChange()实现哪个监听器来刷新页面。

PS:提前感谢您的宝贵建议。请随时向我提问,以便进一步明确问题!我无法更改编辑器设计或代码,因为我正在尝试为开源项目OpenVXML做出贡献。

1 个答案:

答案 0 :(得分:0)

通知UI元素的方法不是最佳的。您的UI元素应该将侦听器注册到正在更改的对象。向编辑器实现的监听器的问题取决于编辑器正在听哪个对象。在你的情况下,PageBookView需要引用ApplicationEditor来注册自己,这是不好的,因为1.PageBookView对编辑器有一个不必要的依赖,2)编辑器不负责传播更改,而是对象本身。我会做以下几点。

你的编辑:

public class MyEditor extends EditorPart implements PropertyChangeListener

public void init(IEditorSite site, IEditorInput input) {
// Getting the input and setting it to the editor
this.object = input.getObject();
// add PropertyChangeListener
this.object.addPropertyChangeListener(this)
}

public void propertyChanged(PropertyChangeEvents) {
 // some element of the model has changed. Perform here the UI things to react properly on the change.
}
}

同样的事情需要在你的pageBook上完成。

public class MyPropertyView extends PageBook implements PropertyChangeListener{

initModel() {
// you have to pass the model from the editor to the depending pageBook.
this.model = getModelFromEditor()
this.object.addPropertyChangeListener(this)

}
  public void propertyChanged(PropertyChangeEvents) {
     // some element of the model has changed. Perform here the UI things to react properly on the change.
    }
}

正如您所看到的,两个UI元素都直接对模型中的更改做出反应。

在编辑器中显示对象的另一种方法是使用ProperyViews,有关详细说明,请参阅http://www.eclipse.org/articles/Article-Tabbed-Properties/tabbed_properties_view.html

前一段时间,我在Eclipse中为所有这些通知内容编写了一个简单示例,请参阅here

HTH汤姆