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

org.ioc.commons.ui.impl.HasLoaderAdapter Maven / Gradle / Ivy

Go to download

Library which contains some extension classes for the library IOC-Commons. It provides some interface definitions and some tool classes which are non-dependent from the used technology; i.e. the classes and interfaces from this library do not depend on the choosen ioc-commons-implementation library.

The newest version!
package org.ioc.commons.ui.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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

public class HasLoaderAdapter implements HasLoader {

	protected boolean loading;
	private OnChangeCommand onChangeCommand;

	@Override
	public void setLoading(boolean loading) {
		// Override me
		boolean changed = (loading != this.loading);
		this.loading = loading;

		if (this.onChangeCommand != null && changed) {
			this.onChangeCommand.execute(loading);
		}
	}

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

	private static final Map globalFields = new HashMap();

	/**
	 * It creates a new adapter instance for a field owned by a widget and keep
	 * it in memory for reusing in the next calls. The widget will be
	 * responsible for cleaning them from memory when it is not going to use
	 * them anymore by calling cleaningAdapter() method.
	 * 
	 * @param fieldName
	 *            Unique name of the field on the owner
	 * @param fieldOwner
	 *            The owner of the field
	 * @return An unique instance for this field in this field owner.
	 */
	public static  HasLoaderAdapter forField(String fieldName, IsWidget fieldOwner) {
		Field field = new Field(fieldName, fieldOwner);
		HasLoaderAdapter adapter = globalFields.get(field);
		if (adapter == null) {
			adapter = new HasLoaderAdapter();
			globalFields.put(field, adapter);
		}
		return (HasLoaderAdapter) adapter;
	}

	/**
	 * Clean all the instantiated adapters for this owner using
	 * {@link HasLoaderAdapter#forField(String, IsWidget)} method.
	 * 
	 * @param fromOwner
	 *            Owner of the fields
	 */
	public static void cleanAdapters(IsWidget fromOwner) {
		cleanAdapters((Object) fromOwner);
	}

	public static void cleanAdapters(Object fromField) {
		for (Iterator it = globalFields.keySet().iterator(); it.hasNext();) {
			Field field = it.next();
			if (fromField.equals(field.getFieldOwner())) {
				it.remove();
			}
		}
	}

	/**
	 * Set a command to be executed when a change is produced.
	 * 
	 * @param onChangeCommand
	 *            Command to be executed.
	 * @return This adapter
	 */
	final public HasLoaderAdapter onChange(OnChangeCommand onChangeCommand) {
		this.onChangeCommand = onChangeCommand;
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy