您可以通过两种方式获取对给定编辑器正在处理的对象的引用。首先,一些简单的数据和一个简单的编辑器:
public class MyModel {
//sub properties...
}
public class MyModelEditor implements Editor<MyModel> {
// subproperty editors...
}
第一:而不是实施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 类的东西SimpleEditor
,尽管您可能还需要其他子编辑:
现在如果我有同一个代理的副编辑
public class AntoherClass implements Editor<Proxy>{
someMethod(){
// method to get the editing proxy ?
}
}
该副编辑可以实现ValueAwareEditor<Proxy>
代替Editor<Proxy>
,并保证其setValue
编辑开始时,将使用 Proxy 实例调用该方法。