您可以通过两种方式获得对给定编辑器正在处理的对象的引用。首先,一些简单的数据和一个简单的编辑器:
public class MyModel {
//sub properties...
}
public class MyModelEditor implements Editor<MyModel> {
// subproperty editors...
}
首先:我们可以选择另一个接口,而不是实现Editor
,它也扩展了 Editor,但允许子编辑器(LeafValueEditor
不允许子编辑器)。让我们试试ValueAwareEditor
:
public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
// subproperty editors...
// ValueAwareEditor methods:
public void setValue(MyModel value) {
// This will be called automatically with the current value when
// driver.edit is called.
}
public void flush() {
// If you were going to make any changes, do them here, this is called
// when the driver flushes.
}
public void onPropertyChange(String... paths) {
// Probably not needed in your case, but allows for some notification
// when subproperties are changed - mostly used by RequestFactory so far.
}
public void setDelegate(EditorDelegate<MyModel> delegate) {
// grants access to the delegate, so the property change events can
// be requested, among other things. Probably not needed either.
}
}
这要求您实现上述示例中的各种方法,但您感兴趣的主要方法是setValue
. 您不需要自己调用这些,它们将由驱动程序及其代表调用。如果您计划对对象进行更改,则该flush
方法也很好用 - 在刷新之前进行这些更改将意味着您在预期的驱动程序生命周期之外修改对象 - 不是世界末日,但以后可能会让您感到惊讶。
第二:使用SimpleEditor
子编辑器:
public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
// subproperty editors...
// one extra sub-property:
@Path("")//bound to the MyModel itself
SimpleEditor self = SimpleEditor.of();
//...
}
使用它,您可以调用self.getValue()
以读出当前值是什么。
编辑:看看AnotherEditor
你已经实现的,看起来你开始制作类似 GWT class 的东西SimpleEditor
,尽管你可能还需要其他子编辑器:
现在,如果我有同一个代理的子编辑器
public class AntoherClass implements Editor<Proxy>{
someMethod(){
// method to get the editing proxy ?
}
}
该子编辑器可以实现ValueAwareEditor<Proxy>
而不是Editor<Proxy>
,并保证setValue
在编辑开始时将使用 Proxy 实例调用其方法。