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

org.ioc.commons.ui.impl.HasValueAdapter 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.

There is a newer version: 1.2.1
Show 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.HasValue;
import org.ioc.commons.ui.IsWidget;

public class HasValueAdapter implements HasValue {

	protected D value;
	private OnChangeCommand onChangeCommand;

	public HasValueAdapter() {
		// Nothing
	}

	public HasValueAdapter(D initialValue) {
		this.value = initialValue;
	}

	@Override
	public void setValue(D value) {
		// Override me
		boolean changed = (value == null ? this.value != null : !value.equals(this.value));
		this.value = value;
		if (this.onChangeCommand != null && changed) {
			this.onChangeCommand.execute(value);
		}
	}

	@Override
	public D getValue() {
		return this.value;
	}

	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 {@link #cleanAdapters(IsWidget)} method. 
*
* Usage:
	{@literal @}Override
	public HasValue<Short> divider() {
		return HasValueAdapter.<Short> forField("divider", this).onChange(new OnChangeCommand<Short>() {

			{@literal @}Override
			public void execute(Short value) {
				lblDivider.setText(value != null ? value.toString() : "--");
			}
		});
	}
	
	...
	
	{@literal @}Override
	protected void onUnload() {
		super.onUnload();
		HasValueAdapter.cleanAdapters(this);
	}

	 * 
* * @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 HasValueAdapter forField(String fieldName, IsWidget fieldOwner) { HasValueAdapter adapter = forField(fieldName, fieldOwner, null); return adapter; } /** * * @param fieldName * Unique name of the field on the owner * @param fieldOwner * The owner of the field * @param initialValue * Initial value * @see #forField(String, IsWidget) * @return An unique instance for this field in this field owner. */ @SuppressWarnings("unchecked") public static HasValueAdapter forField(String fieldName, IsWidget fieldOwner, D initialValue) { Field field = new Field(fieldName, fieldOwner); HasValueAdapter adapter = globalFields.get(field); if (adapter == null) { adapter = new HasValueAdapter(initialValue); globalFields.put(field, adapter); } return (HasValueAdapter) adapter; } /** * Clean all the instantiated adapters for this owner using * {@link HasValueAdapter#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 HasValueAdapter onChange(OnChangeCommand onChangeCommand) { this.onChangeCommand = onChangeCommand; return this; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy