All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.gwt.client.presenter.APresenter Maven / Gradle / Ivy

package com.jpattern.gwt.client.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gwt.user.client.ui.HasWidgets;
import com.jpattern.gwt.client.navigationevent.INavigationEvent;
import com.jpattern.gwt.client.navigationevent.INavigationEventCallback;
import com.jpattern.gwt.client.navigationevent.INavigationEventData;
import com.jpattern.gwt.client.navigationevent.NavigationEventData;
import com.jpattern.gwt.client.navigationevent.NullNavigationEventData;
import com.jpattern.gwt.client.view.IShowViewStrategy;
import com.jpattern.gwt.client.view.IView;
import com.jpattern.shared.result.IErrorMessage;

/**
 * 
 * @author Francesco Cina'
 *
 * 14 Apr 2011
 */

public abstract class APresenter implements IPresenter {
	
	private final IShowViewStrategy showViewStrategy;
	private IPresenter parentPresenter;
	private final INavigationEvent navigationEvent;
	private final Map registeredNavigationEvents = new HashMap();
	private final Map> initialNavigationEvents = new HashMap>();
	private final List initialNavigationEventsToIgnore = new ArrayList();
	private boolean initialized = false;

	public APresenter(IShowViewStrategy showViewStrategy, INavigationEvent navigationEvent) {
		this.showViewStrategy = showViewStrategy;
		this.navigationEvent = navigationEvent;
		showViewStrategy.getView().visit(this);
	}
	
	@Override
	public final void onEventError(List errorMessages) {
		showViewStrategy.getView().getErrorArea().addErrorMessages(errorMessages);
	}
	
	@Override
	public final void onEventStart() {
		showViewStrategy.getView().getErrorArea().clear();
	}
	
	@Override
	public final T getView() {
		return showViewStrategy.getView();
	}
	
	/**
	 * This is the first step performed to a complete visualization of the associated IView.
	 * This is performed just before the IShowViewStrategy.onLoadStart() method.
	 * This method is called before the display of the IView.
	 * DO NOT perform asynchronous actions in this method.
	 */
	public abstract void preDisplay();
	
	/**
	 * This is the second step performed to a complete visualization of the associated IView
	 * This method is called just after the IShowViewStrategy.onLoadStart() method and before the
	 * IShowViewStrategy.onLoadCompleted() method.
	 * Here the asynchronous actions should be performed.
	 * Its VERY important to call the render() method when all the actions are performed otherwise
	 * the IView should not be completely rendered. 
	 */
	public abstract void postDisplay();

	/**
	 * This method must be called when all the necessary operations to load necessary data are ended.
	 * This causes the final rendering of the IView. 
	 */
	@Override
	public final void render() {
		showViewStrategy.onLoadCompleted();
		initialized = true;
	}

	@Override
	public final void setParent(IPresenter parentPresenter) {
		this.parentPresenter = parentPresenter;
		showViewStrategy.setParent(getParentPresenter().getNavigationEventData(getNavigationEvent().getName()).getEventTarget());
	}
	
	@Override
	public final void init() {
		if (!initialized) {
			onEventStart();
			preDisplay();
			launchInitialEvents();
			showViewStrategy.onLoadStart();
			postDisplay(); 
		} else {
			render();
		}
	}
	
	@Override
	public final void hierarchy(List hierarchyResult) {
		getParentPresenter().hierarchy(hierarchyResult);
		hierarchyResult.add(getName());
	}
	
	@Override
	public final void registerNavigationEvent(INavigationEvent navigationEvent, HasWidgets eventTarget, INavigationEventCallback navigationEventCallback) {
		registeredNavigationEvents.put(navigationEvent.getName(), new NavigationEventData(navigationEvent, eventTarget, navigationEventCallback));
	}
	
	@Override
	public final void launchNavigationEvent(String navigationEventName, boolean registerHistory) {
		launchNavigationEvent(navigationEventName, registerHistory, new HashMap());
	}
	
	@Override
	public final void launchNavigationEvent(String navigationEventName, boolean registerHistory, Map queryStringValues) {
		if (!localLaunchNavigationEvent(navigationEventName, registerHistory, queryStringValues)) {
			getParentPresenter().launchNavigationEvent(navigationEventName, registerHistory);
		} 
	}
	
	private boolean localLaunchNavigationEvent(String navigationEventName, boolean registerHistory, Map queryStringValues) {
		if (registeredNavigationEvents.containsKey(navigationEventName)) {
			INavigationEventData eventData = registeredNavigationEvents.get(navigationEventName);
			eventData.getNavigationEvent().notifyNavigationEvent(this, queryStringValues, true);
			eventData.getNavigationEventCallback().callback();
			return true;
		} 
		return false;
	}
	
	@Override
	public final INavigationEventData getNavigationEventData(String navigationEventName) {
		if (registeredNavigationEvents.containsKey(navigationEventName)) {
			return registeredNavigationEvents.get(navigationEventName);
		}
		return new NullNavigationEventData();
	}

	
	public final IPresenter getParentPresenter() {
		if (parentPresenter==null) {
			parentPresenter = new NullPresenter();
		}
		return parentPresenter;
	}


	@Override
	public final INavigationEvent getNavigationEvent() {
		return navigationEvent;
	}

	@Override
	public final boolean isInitialized() {
		return initialized;
	}

	@Override
	public final void addInitialNavigationEvent(String navigationEventName) {
		addInitialNavigationEvent(navigationEventName, new HashMap());
	}
	
	@Override
	public final void addInitialNavigationEvent(String navigationEventName, Map queryStringValues) {
		INavigationEventData newEventData = getNavigationEventData(navigationEventName);
		HasWidgets eventTarget = newEventData.getEventTarget();
		boolean targetInUse = false;
		for (Entry> entry : initialNavigationEvents.entrySet()) {
			if (getNavigationEventData(entry.getKey()).getEventTarget().equals(eventTarget)) {
				targetInUse = true;
				break;
			}
		}
		if (!targetInUse) {
			initialNavigationEvents.put(navigationEventName, queryStringValues);
		}
	}
	
	@Override
	public void ignoreInitialEventWithSameTargetOf(String navigationEventName) {
		INavigationEventData newEventData = getNavigationEventData(navigationEventName);
		HasWidgets eventTarget = newEventData.getEventTarget();
		for (Entry> entry : initialNavigationEvents.entrySet()) {
			if (getNavigationEventData(entry.getKey()).getEventTarget().equals(eventTarget)) {
				initialNavigationEventsToIgnore.add(entry.getKey());
			}
		}
	}
	
	public final void launchInitialEvents() {
		for (Entry> entry : initialNavigationEvents.entrySet()) {
			if ( !initialNavigationEventsToIgnore.contains(entry.getKey()) ) {
				localLaunchNavigationEvent(entry.getKey(), false, entry.getValue());
			}
		}
		initialNavigationEventsToIgnore.clear();
	}

	@Override
	public String getName() {
		return getNavigationEvent().getName();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy