Home‎ > ‎UI‎ > ‎

EditorWorkFlow


Example of One of My Editors Work Flow

My activity starts this editor below. Paths can be challenging in loading an object, but if you initialize the editor before you find the object you can use the editor to tell what paths to get. So I always initialize then find the object for editing and I've found this is working great for my app.

public class EditPersonWorkFlow extends Composite {

 
public static enum State {
    EDITING
,
    SAVING
;
 
}

 
interface Driver extends RequestFactoryEditorDriver<PeopleJdoProxy, PersonEditor> {}
 
interface EditPersonWorkFlowUiBinder extends UiBinder<Widget, EditPersonWorkFlow> {}
 
private static EditPersonWorkFlowUiBinder uiBinder = GWT.create(EditPersonWorkFlowUiBinder.class);
 
 
@UiField FlowPanel pPersonEdit;
 
@UiField LoadingWidget wLoading;
 
@UiField PushButton bFinish;
 
@UiField PushButton bSave;
 
@UiField ErrorWidget wError;

 
private Driver driver = GWT.create(Driver.class);
 
private ClientFactory clientFactory;
 
private PersonEditor editor;
 
private boolean brandNew;
 
private Presenter presenter;
 
private boolean changed = false;
 
private boolean goBack;

 
/**
   * constructor - init the edit workflow
   * @param clientFactory
   * @param presenter
   * @param presenter
   */

 
public EditPersonWorkFlow(ClientFactory clientFactory, Presenter presenter) {
   
this.clientFactory = clientFactory;
   
this.presenter = presenter;
    initWidget
(uiBinder.createAndBindUi(this));

    setState
(State.EDITING);

    bFinish
.addClickHandler(new ClickHandler() {
     
public void onClick(ClickEvent event) {
        goBack
();
     
}
   
});
   
 
}

 
private void goBack() {
   
boolean isRunning = presenter.isRunning();
   
if (isRunning == true) {
      goBack
= true;
      save
();
     
return;
   
}
    goBack
= false;
   
Place place = clientFactory.getHistoryLast();
   
while (place instanceof DashPlace == false) {
      place
= clientFactory.getHistoryLast();
   
}
    clientFactory
.getPlaceController().goTo(place);
 
}

 
/**
   * on persist do this
   * @return
   */

 
private Receiver<? super PeopleJdoProxy> getReciever() {
   
Receiver<PeopleJdoProxy> r = new Receiver<PeopleJdoProxy>() {
     
public void onSuccess(PeopleJdoProxy response) {
//        if (brandNew == true) {
//          EntityProxyId<PeopleJdoProxy> id = (EntityProxyId<PeopleJdoProxy>) response.stableId();
//          presenter.goTo(new PeopleEditPlace(id));
//        } else {
          edit
(response);
       
//}
     
}
     
public void onFailure(ServerFailure error) {
       
super.onFailure(error);
     
}
     
public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
       
super.onConstraintViolation(violations);
     
}
   
};
   
return r;
 
}
 
 
public void findId(EntityProxyId<PeopleJdoProxy> id) {
    initEditor
();
   
   
if (id == null) {
      edit
(null); // create a new one
     
return;
   
}
   
   
Request<PeopleJdoProxy> req = clientFactory.getRequestFactory().getPeopleJdoRequest().find(id).with(driver.getPaths());
    req
.fire(new Receiver<PeopleJdoProxy>() {
     
public void onSuccess(PeopleJdoProxy peopleJdoProxy) {
        edit
(peopleJdoProxy);
     
}
   
});
 
}
 
 
/**
   * edit object
   * @param peopleDataProxy
   */

 
private void edit(PeopleJdoProxy peopleDataProxy) {
   
PeopleJdoRequest context = clientFactory.getRequestFactory().getPeopleJdoRequest();
   
   
if (peopleDataProxy == null) {
      peopleDataProxy
= context.create(PeopleJdoProxy.class);
     
      peopleDataProxy
.setAccountId(clientFactory.getAccountId());
     
     
// google emails
     
if (peopleDataProxy.getGoogleEmails() == null) {
       
List<String> list = new ArrayList<String>();
        peopleDataProxy
.setGoogleEmails(list);
     
}
     
     
// consultants
     
if (peopleDataProxy.getConsultantsPeopleIds() == null) {
       
List<Long> list = new ArrayList<Long>();
        peopleDataProxy
.setConsultantsPeopleIds(list);
     
}
     
     
// teachers
     
if (peopleDataProxy.getTeachersPeopleIds() == null) {
       
List<Long> list = new ArrayList<Long>();
        peopleDataProxy
.setTeachersPeopleIds(list);
     
}
     
     
// parents
     
if (peopleDataProxy.getParentPeopleIds() == null) {
       
List<Long> list = new ArrayList<Long>();
        peopleDataProxy
.setParentPeopleIds(list);
     
}
     
     
// plans
     
if (peopleDataProxy.getPlans() == null) {
       
List<Long> list = new ArrayList<Long>();
        peopleDataProxy
.setPlans(list);
     
}
     
   
}
   
   
try {
      driver
.edit(peopleDataProxy, context);
   
} catch (Exception e) {
      e
.printStackTrace();
   
}

   
String[] paths = driver.getPaths();
    context
.persist().using(peopleDataProxy).with(paths).to(getReciever());
 
}

 
/**
   * setup the driver and editor and add to ui
   */

 
private void initEditor() {
   
if (editor != null) {
     
return;
   
}

   
// PersonEditor is a that extends Editor<Person> and render the ui
    editor
= new PersonEditor();
    editor
.setClientFactory(clientFactory);
    editor
.setPresenter(presenter);
    pPersonEdit
.clear();
    pPersonEdit
.add(editor);
   
    driver
.initialize(clientFactory.getRequestFactory(), editor);
   
    editor
.addEditHandler(new EditorEditEventHandler<PeopleJdoProxy>() {
     
public void onEditEvent(EditorEditEvent<PeopleJdoProxy> event) {
       
if (event.getEvent() == Edit.SAVEIT) {
          save
();
       
}
     
}
   
});
 
}

 
private void save() {

   
// TODO comes back null, maybe due to no sub editors
   
//    if (driver.hasErrors()) {
   
//      // A sub-editor reported errors
   
//      System.out.println("test");
   
//    }

   
// DEBUG
   
String s = EditorHierarchyPrinter.toString(driver);
   
System.out.println("EditorHierarchyPrinter = " + s);

   
    setState
(State.SAVING);
   
RequestContext req = driver.flush();
    req
.fire(new Receiver<Void>() {
     
public void onSuccess(Void response) {
        presenter
.setRunning(false);
        setState
(State.EDITING);
       
if (goBack == true) {
          goBack
();
       
}
     
}
     
public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
       
super.onConstraintViolation(violations);
        setState
(State.EDITING);
        wError
.showError(5000, "Server Error, please try again.");
     
}
     
public void onFailure(ServerFailure error) {
       
super.onFailure(error);
        setState
(State.EDITING);
        wError
.showError(5000, "Server Error, please try again.");
     
}
   
});
 
}

 
private void setState(State state) {
   
if (state == State.EDITING) {
      wLoading
.showLoading(false);
      bSave
.setEnabled(true);
      presenter
.setRunning(false);

   
} else if (state == State.SAVING) {
      wLoading
.showLoading(true);
      bSave
.setEnabled(false);
      presenter
.setRunning(true);
   
}
 
}

 
@UiHandler("bSave")
 
void onBSaveClick(ClickEvent event) {
    save
();
 
}

 
public final HandlerRegistration addEditHandler(EditorEditEventHandler<PeopleJdoProxy> handler) {
   
return addHandler(handler, EditorEditEvent.TYPE);
 
}

}

This image illustrates the editor workflow for my PersonEditor. The top Finish and Save are in the Workflow and everything below that resides in another editor class. 





Comments