myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Robinson" <andrew.rw.robin...@gmail.com>
Subject Re: Detecting whether or not the user changed anything on a form
Date Fri, 13 Jul 2007 16:23:52 GMT
> I think this will do unless you can think of a way to avoid having to add the VCL tag
to every component.

This is when you may consider a PhaseListener, or component binding.
You could surf the entire component tree recursively, and add your VCL
to all components implementing EditableValueHolder.

public class VCLPhaseListener implement PhaseListener {
  public void afterPhase(PhaseEvent event) { }

	public void beforePhase(PhaseEvent event) {
    UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
    if (viewRoot != null) {
      addVCL(viewRoot);
    }
  }

	public PhaseId getPhaseId() {
    return PhaseId.PROCESS_VALIDATIONS;
  }

  private void addVCL(UIComponent component) {
    if (component instanceof EditableValueHolder &&
      !hasVCL((EditableValueHolder)component)) {
      ((EditableValueHolder)component).addValueChangeListener(this);
    }
    for (Iterator iter = component.getFacetsAndChildren(); iter.hasNext(); ) {
      addVCL(iter.next());
    }
  }

  private boolean hasVCL(EditableValueHolder holder) {
    for (ValueChangeListener listener : holder.getValueChangeListeners()) {
      if (VCLPhaseListener.class.assignableFrom(listener.getClass())) {
        return true;
      }
    }
    return false;
  }

  ... VCL Code
}

Mime
View raw message