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

org.ioc.commons.impl.gwt.client.ui.HasLoaderWrapper Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package org.ioc.commons.impl.gwt.client.ui;

import org.ioc.commons.ui.HasLoader;
import org.ioc.commons.ui.HasStorage;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.HasVisibility;
import com.google.gwt.user.client.ui.UIObject;

/**
 * Wrapper for GWT components into {@link HasLoader}.
 * 
 * @author Jesús Lunar Pérez
 * 
 */
public class HasLoaderWrapper implements HasLoader {

	private static final class ElementHasVisibility implements HasVisibility {
		private final Element element;

		private ElementHasVisibility(Element element) {
			this.element = element;
		}

		@Override
		public void setVisible(boolean visible) {
			UIObject.setVisible(element, visible);
		}

		@Override
		public boolean isVisible() {
			return UIObject.isVisible(element);
		}
	}

	private final Boolean[] previousVisibility;

	private final HasVisibility[] notLoadingArray;
	private final HasVisibility[] loadingArray;

	private boolean loading;

	private HasLoaderWrapper(HasVisibility[] notLoading, HasVisibility[] loading) {
		this.notLoadingArray = notLoading;
		this.loadingArray = loading;
		this.previousVisibility = notLoadingArray != null ? new Boolean[notLoadingArray.length] : null;
	}

	@Override
	public void setLoading(boolean loading) {
		if (notLoadingArray != null) {
			for (int i = 0; i < notLoadingArray.length; i++) {
				HasVisibility notLoadingV = notLoadingArray[i];

				if (loading) {
					if (previousVisibility[i] == null) {
						previousVisibility[i] = notLoadingV.isVisible();
					}

					notLoadingV.setVisible(false);
				} else if (previousVisibility[i] != null) {
					notLoadingV.setVisible(previousVisibility[i]);
					previousVisibility[i] = null;
					// } else {
					// notLoadingV.setVisible(true);
				}
			}
		}

		if (loadingArray != null) {
			for (HasVisibility loadingV : loadingArray) {
				loadingV.setVisible(loading);
			}
		}

		this.loading = loading;
	}

	@Override
	public boolean isLoading() {
		return this.loading;
	}

	/**
	 * It returns an {@link HasLoader} instance which will turn into visible an
	 * indicator when is loading and will hide it when is not.
	 * 
	 * @param indicator
	 *            The indicator which will appear/disappear
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisible(HasVisibility indicator) {
		return fromVisibles((HasVisibility[]) null, new HasVisibility[] { indicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show an indicator
	 * when is loading and another one when is not.
	 * 
	 * @param notLoadingIndicator
	 *            The visible indicator when is not loading
	 * @param loadingIndicator
	 *            The visible indicator when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(HasVisibility notLoadingIndicator, HasVisibility loadingIndicator) {
		return fromVisibles(new HasVisibility[] { notLoadingIndicator }, new HasVisibility[] { loadingIndicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show an indicator
	 * when is loading and others when is not.
	 * 
	 * @param notLoadingIndicator
	 *            The visible indicator when is not loading
	 * @param loadingIndicators
	 *            The visible indicators when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(HasVisibility notLoadingIndicator, HasVisibility[] loadingIndicators) {
		return fromVisibles(new HasVisibility[] { notLoadingIndicator }, loadingIndicators);
	}

	/**
	 * It returns an {@link HasLoader} instance which will show some indicators
	 * when is loading and another one when is not.
	 * 
	 * @param notLoadingIndicators
	 *            The visible indicators when is not loading
	 * @param loadingIndicator
	 *            The visible indicator when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(HasVisibility[] notLoadingIndicators, HasVisibility loadingIndicator) {
		return fromVisibles(notLoadingIndicators, new HasVisibility[] { loadingIndicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show some indicators
	 * when is loading and others when is not.
	 * 
	 * @param notLoadingIndicators
	 *            The visible indicators when is not loading
	 * @param loadingIndicators
	 *            The visible indicators when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(HasVisibility[] notLoadingIndicators, HasVisibility[] loadingIndicators) {
		return new HasLoaderWrapper(notLoadingIndicators, loadingIndicators);
	}

	/**
	 * It returns an {@link HasLoader} instance which will turn into visible an
	 * indicator when is loading and will hide it when is not.
	 * 
	 * @param indicator
	 *            The indicator which will appear/disappear
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisible(HasVisibility indicator, HasStorage cache) {
		Object cached = cache.retrieve(indicator);
		HasLoader hasLoader = (cached instanceof HasLoader) ? (HasLoader) cached : null;
		if (hasLoader == null) {
			hasLoader = fromVisible(indicator);
			cache.store(indicator, hasLoader);
		}

		return hasLoader;
	}

	/**
	 * It returns an {@link HasLoader} instance which will turn into visible an
	 * indicator when is loading and will hide it when is not.
	 * 
	 * @param indicator
	 *            The indicator which will appear/disappear
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisible(Element indicator) {
		return fromVisibles((Element[]) null, new Element[] { indicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show an indicator
	 * when is loading and another one when is not.
	 * 
	 * @param notLoadingIndicator
	 *            The visible indicator when is not loading
	 * @param loadingIndicator
	 *            The visible indicator when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(Element notLoadingIndicator, Element loadingIndicator) {
		return fromVisibles(new Element[] { notLoadingIndicator }, new Element[] { loadingIndicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show an indicator
	 * when is loading and others when is not.
	 * 
	 * @param notLoadingIndicator
	 *            The visible indicator when is not loading
	 * @param loadingIndicators
	 *            The visible indicators when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(Element notLoadingIndicator, Element[] loadingIndicators) {
		return fromVisibles(new Element[] { notLoadingIndicator }, loadingIndicators);
	}

	/**
	 * It returns an {@link HasLoader} instance which will show some indicators
	 * when is loading and another one when is not.
	 * 
	 * @param notLoadingIndicators
	 *            The visible indicators when is not loading
	 * @param loadingIndicator
	 *            The visible indicator when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(Element[] notLoadingIndicators, Element loadingIndicator) {
		return fromVisibles(notLoadingIndicators, new Element[] { loadingIndicator });
	}

	/**
	 * It returns an {@link HasLoader} instance which will show some indicators
	 * when is loading and others when is not.
	 * 
	 * @param notLoadingIndicators
	 *            The visible indicators when is not loading
	 * @param loadingIndicators
	 *            The visible indicators when is loading
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisibles(Element[] notLoadingIndicators, Element[] loadingIndicators) {
		return new HasLoaderWrapper(toHasVisiblity(notLoadingIndicators), toHasVisiblity(loadingIndicators));
	}

	public static HasVisibility[] toHasVisiblity(Element[] elements) {
		HasVisibility[] hvarray = elements != null ? new HasVisibility[elements.length] : null;
		if (hvarray != null) {
			for (int i = 0; i < elements.length; i++) {
				Element element = elements[i];
				hvarray[i] = new ElementHasVisibility(element);
			}
		}
		return hvarray;
	}

	/**
	 * It returns an {@link HasLoader} instance which will turn into visible an
	 * indicator when is loading and will hide it when is not.
	 * 
	 * @param indicator
	 *            The indicator which will appear/disappear
	 * 
	 * @return The {@link HasLoader} instance
	 */
	public static HasLoader fromVisible(Element indicator, HasStorage cache) {
		Object cached = cache.retrieve(indicator);
		HasLoader hasLoader = (cached instanceof HasLoader) ? (HasLoader) cached : null;
		if (hasLoader == null) {
			hasLoader = fromVisible(indicator);
			cache.store(indicator, hasLoader);
		}

		return hasLoader;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy