Wicket Loose Coupling

We can do it in two ways,

  1. Using Model : Partially loosely coupled , it is really worth considering
  2. Using Custom Events : Fully loosely coupled

Loose Coupling Using Model

public class ChangeSensitiveModel extends Model {

	private static final long serialVersionUID = 1L;

	public ChangeSensitiveModel() {

	}

	public ChangeSensitiveModel(final T object) {
		super(object);
	}

	final public void setObject(final T newObject) {
		if (onBeforeSet(newObject)) {
			super.setObject(newObject);
			onAfterSet();
		}
	}

	protected boolean onBeforeSet(T newObject) {
		return true;
	}

	protected void onAfterSet() {

	}
}

ChangeSensitiveModel In Action

Set object to the ChangeSensitiveModel Model

private Button newButton(final ChangeSensitiveModel<ValueMap> model, final Form<ValueMap> searchForm) {
		return new AjaxButton(("sayHello")) {

			private static final long serialVersionUID = 1L;
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				model.setObject(searchForm.getModelObject());
			}
		};
	}

Override the call back

public LooseCouplingUsingModelPage() {

		changeSensitiveModel = new ChangeSensitiveModel(){

			private static final long serialVersionUID = 1L;

			@Override
			protected void onAfterSet() {
				LooseCouplingUsingModelPage.this.addOrReplace(new ResultPanel(RESULT_PANEL_ID, changeSensitiveModel));
				AjaxRequestTarget ajaxRequestTarget = AjaxRequestTarget.get();
				if (ajaxRequestTarget != null) {
					ajaxRequestTarget.addComponent(LooseCouplingUsingModelPage.this.get(RESULT_PANEL_ID));
				}
			}
		};
		add(new SearchPanel(SEARCH_PANEL_ID, changeSensitiveModel));
		add(newEmptyPanel());
	}

Loose Coupling Using Custom Event

Firing an Event

new RefreashFeedBackPanelEvent(SearchPanel.this, target, searchForm.getModelObject()).fire();

Responding to Event : Any component wanting to respond to Customer event should implement AjaxUpdateListener

public void handleAjaxUpdate(final AjaxUpdateEvent event) {
		if (event instanceof RefreashFeedBackPanelEvent) {
			info("You said : " + ((RefreashFeedBackPanelEvent)event).getSourcData().getString("name"));
			event.getTarget().addComponent(feedbackPanel);
		}
	}

Go Grab the source code from  GitHub

do mvn jetty:run and see the example in Action.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s