/**
 * Copyright 2010-2014 Jingjing Li.
 *
 * This file is part of jplot2d.
 *
 * jplot2d is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or any later version.
 *
 * jplot2d is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jplot2d. If not, see .
 */
package org.jplot2d.element;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import org.jplot2d.data.ArrayPair;
import org.jplot2d.data.ByteDataBuffer;
import org.jplot2d.data.DoubleDataBuffer;
import org.jplot2d.data.FloatDataBuffer;
import org.jplot2d.data.ImageDataBuffer;
import org.jplot2d.data.IntDataBuffer;
import org.jplot2d.data.MultiBandImageData;
import org.jplot2d.data.ShortDataBuffer;
import org.jplot2d.data.SingleBandImageData;
import org.jplot2d.data.XYGraphData;
import org.jplot2d.element.impl.AxisImpl;
import org.jplot2d.element.impl.AxisRangeLockGroupImpl;
import org.jplot2d.element.impl.AxisTransformEx;
import org.jplot2d.element.impl.AxisTickManagerEx;
import org.jplot2d.element.impl.AxisTickManagerImpl;
import org.jplot2d.element.impl.AxisTitleEx;
import org.jplot2d.element.impl.CoordinateAnnotationImpl;
import org.jplot2d.element.impl.ElementEx;
import org.jplot2d.element.impl.HLineAnnotationImpl;
import org.jplot2d.element.impl.HStripAnnotationImpl;
import org.jplot2d.element.impl.ImageMappingEx;
import org.jplot2d.element.impl.ImageMappingImpl;
import org.jplot2d.element.impl.LayerImpl;
import org.jplot2d.element.impl.LegendEx;
import org.jplot2d.element.impl.LegendItemEx;
import org.jplot2d.element.impl.ImageGraphImpl;
import org.jplot2d.element.impl.PlotImpl;
import org.jplot2d.element.impl.PlotMarginEx;
import org.jplot2d.element.impl.AxisTransformImpl;
import org.jplot2d.element.impl.RGBImageGraphImpl;
import org.jplot2d.element.impl.RGBImageMappingEx;
import org.jplot2d.element.impl.RGBImageMappingImpl;
import org.jplot2d.element.impl.RectangleAnnotationImpl;
import org.jplot2d.element.impl.SymbolAnnotationImpl;
import org.jplot2d.element.impl.TitleImpl;
import org.jplot2d.element.impl.VLineAnnotationImpl;
import org.jplot2d.element.impl.VStripAnnotationImpl;
import org.jplot2d.element.impl.XYGraphImpl;
import org.jplot2d.env.DummyEnvironment;
import org.jplot2d.env.ElementAddition;
import org.jplot2d.env.ElementIH;
import org.jplot2d.env.Environment;
import org.jplot2d.env.StyleConfiguration;
import org.jplot2d.sizing.FillContainerSizeMode;
import org.jplot2d.util.Range;
import org.jplot2d.util.SymbolShape;

/**
 * A factory to produce all kind of plot components.
 * 
 * @author Jingjing Li
 * 
 */
public class ElementFactory {

	private static ElementFactory instance = new ElementFactory(false, null);

	private static ElementFactory threadSafeInstance = new ElementFactory(true, null);

	/**
	 * Returns an instance of ComponentFactory. All component created by this factory are not thread-safe.
	 * 
	 * @return an instance of ComponentFactory
	 */
	public static ElementFactory getInstance() {
		return instance;
	}

	/**
	 * Returns an instance of ComponentFactory. All component created by this factory are thread-safe.
	 * 
	 * @return an instance of ComponentFactory
	 */
	public static ElementFactory getThreadSafeInstance() {
		return threadSafeInstance;
	}

	/**
	 * Returns an instance of ComponentFactory which is configured by profile. All component created by this factory are
	 * not thread-safe.
	 * 
	 * @param profile
	 * @return an instance of ComponentFactory
	 */
	public static ElementFactory getInstance(StyleConfiguration profile) {
		return new ElementFactory(false, profile);
	}

	/**
	 * Returns an instance of ComponentFactory which is configured by profile. All component created by this factory are
	 * thread-safe.
	 * 
	 * @param profile
	 * @return an instance of ComponentFactory
	 */
	public static ElementFactory getThreadSafeInstance(StyleConfiguration profile) {
		return new ElementFactory(true, profile);
	}

	private final boolean threadSafe;

	private final StyleConfiguration profile;

	protected ElementFactory(boolean threadSafe, StyleConfiguration profile) {
		this.threadSafe = threadSafe;
		this.profile = profile;
	}

	/**
	 * Apply profile to the given element
	 * 
	 * @param element
	 */
	protected final void applyProfile(Element element) {
		if (profile != null) {
			profile.applyTo(element);
		}
	}

	protected DummyEnvironment createDummyEnvironment() {
		return new DummyEnvironment(threadSafe);
	}

	/**
	 * Create proxy for the given impl
	 * 
	 * @param 
	 *            the proxy type
	 * @param impl
	 *            the impl
	 * @param clazz
	 *            the class of proxy type
	 * @return the proxy
	 */
	@SuppressWarnings("unchecked")
	public  T proxy(ElementEx impl, Class clazz) {
		ElementIH ih = new ElementIH(impl, clazz);
		return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz, ElementAddition.class }, ih);
	}

	/**
	 * Create a plot with the initial values
	 * 

Ads help maintain this website.