5

Is there a way to get the proxy that editor is editing?

The normal workflow would be:

 public class Class implments Editor<Proxy>{
  @Path("")
  @UiField AntoherClass subeditor;


  void someMethod(){
   Proxy proxy = request.create(Proxy.class);
   driver.save(proxy);
   driver.edit(proxy,request);
 }
}

Now if i got a subeditor of the same proxy

public class AntoherClass implements Editor<Proxy>{
   someMethod(){
   // method to get the editing proxy ?
    }
 } 

Yes i know i can just set the proxy to the Child editor with setProxy() after its creation, but i want to know if there is something like HasRequestContext but for the edited proxy.

This usefull when you use for example ListEditor in non UI objects.

Thank you.

ics_mauricio
  • 369
  • 3
  • 16

3 Answers3

7

Two ways you can get a reference to the object that a given editor is working on. First, some simple data and a simple editor:

public class MyModel {
  //sub properties...

}

public class MyModelEditor implements Editor<MyModel> {
  // subproperty editors...

}

First: Instead of implementing Editor, we can pick another interface that also extends Editor, but allows sub-editors (LeafValueEditor does not allow sub-editors). Lets try 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.
  }
}

This requires that you implement the various methods as in the example above, but the main one you are interested in will be setValue. You do not need to invoke these yourself, they will be called by the driver and its delegates. The flush method is also good to use if you plan to make changes to the object - making those changes before flush will mean that you are modifying the object outside of the expected driver lifecycle - not the end of the world, but might surprise you later.

Second: Use a SimpleEditor sub-editor:

public class MyModelEditor2 implements ValueAwareEditor<MyModel> {
  // subproperty editors...

  // one extra sub-property:
  @Path("")//bound to the MyModel itself
  SimpleEditor self = SimpleEditor.of();

  //...
}

Using this, you can call self.getValue() to read out what the current value is.

Edit: Looking at the AnotherEditor you've implemented, it looks like you are starting to make something like the GWT class SimpleEditor, though you might want other sub-editors as well:

Now if i got a subeditor of the same proxy

public class AntoherClass implements Editor<Proxy>{
  someMethod(){  
    // method to get the editing proxy ?
  }
}

This sub-editor could implement ValueAwareEditor<Proxy> instead of Editor<Proxy>, and be guaranteed that its setValue method would be called with the Proxy instance when editing starts.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • Yes it works, i just need implements ValueAwareEditor and setValue sets the proxy automatically. From the API "Editors whose behavior changes based on the value being edited will implement this interface." Thats my case. =) – ics_mauricio Apr 14 '12 at 00:12
  • Thanks Colin for this help (espacially for the @Path("") : I was tempted to do a @Path("this") but that would have required special treatement...) Now I'd like to know how to switch from an editor to another depending on the data value. I have a select box that should change the form (many common fields but layout and some fields appaer/disappear). I have a UiBinder for each type, and I would like to switch from one to another on user selection. I don't like the idea of creating them all and making them visible or not depending on the situation. I'd like to create a new editor and attribute it – Zied Hamdi Jun 12 '14 at 10:09
2

In your child editor class, you can just implement another interface TakesValue, you can get the editing proxy in the setValue method.

ValueAwareEditor works too, but has all those extra method you don't really need.

FreeDevz
  • 88
  • 2
  • 9
0

This is the only solution I found. It involves calling the context edit before you call the driver edit. Then you have the proxy to manipulate later.

Community
  • 1
  • 1
Deanna
  • 696
  • 1
  • 5
  • 15