Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Editor framework

Tags:

editor

gwt

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.

like image 499
ics_mauricio Avatar asked Apr 13 '12 16:04

ics_mauricio


2 Answers

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.

like image 155
Colin Alworth Avatar answered Oct 03 '22 15:10

Colin Alworth


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.

like image 44
FreeDevz Avatar answered Oct 03 '22 14:10

FreeDevz