package com.handmark.pulltorefresh.library;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.handmark.pulltorefresh.configuration.xml.PullToRefreshXmlConfiguration;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.internal.IndicatorLayout;
import com.handmark.pulltorefresh.library.internal.DefaultIndicatorLayout;
import com.handmark.pulltorefresh.library.internal.Utils;

public class IndicatorLayoutFactory {

	private static final String LOG_TAG = IndicatorLayoutFactory.class.getName();
	
	public static IndicatorLayout createIndicatorFactory(Integer layoutCode, Context context, PullToRefreshBase.Mode mode) {
		String clazzName = PullToRefreshXmlConfiguration.getInstance().getIndicatorLayoutClazzName(layoutCode);
		Class<? extends IndicatorLayout> clazz = createIndicatorLayoutClazz(clazzName);
		return createIndicatorLayout(layoutCode, context, mode);
	}
	
	public static Class<? extends IndicatorLayout> createIndicatorLayoutClazz(Integer layoutCode) {
		String clazzName = PullToRefreshXmlConfiguration.getInstance().getIndicatorLayoutClazzName(layoutCode);
		return createIndicatorLayoutClazz(clazzName);
	}
	
	@SuppressWarnings("unchecked")
	public static Class<? extends IndicatorLayout> createIndicatorLayoutClazz(String clazzName) {
		Class<? extends IndicatorLayout> clazz = null;
		try {
			clazz = (Class<? extends IndicatorLayout> )Class.forName(clazzName);
			
		} catch (ClassNotFoundException e) {
			Utils.error("Class Not Found. It will be used default IndicatorLayout." +e.getMessage());
			clazz = DefaultIndicatorLayoutFactory.createIndicatorLayoutClazz(clazzName);
			
		}
		
		return clazz;
	}

	public static IndicatorLayout createIndicatorLayout(Integer layoutCode, Context context, PullToRefreshBase.Mode mode) {
		Class<? extends IndicatorLayout> clazz = createIndicatorLayoutClazz(layoutCode);
		return createIndicatorLayout(clazz, context, mode);
	}
	
	/**
	 * 
	 * @param context
	 * @param mode
	 * @param attrs
	 * @comment Need to be refactored!
	 * @return
	 */
	public static IndicatorLayout createIndicatorLayout(
			Class<? extends IndicatorLayout> clazz, Context context, Mode mode) {
		IndicatorLayout layout = null;

		try {
			Constructor<? extends IndicatorLayout> constructor = clazz
					.getConstructor(Context.class, Mode.class);
			layout = (IndicatorLayout) constructor.newInstance(context, mode);

		} catch (IllegalArgumentException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (InvocationTargetException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (SecurityException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (NoSuchMethodException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (InstantiationException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (IllegalAccessException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		} catch (NullPointerException e) {
			Log.e(LOG_TAG, "indicator layout has failed while creating. " + e);
		}

		if (layout == null) {
			layout = DefaultIndicatorLayoutFactory.createIndicatorLayout(clazz, context, mode);
		}

		layout.setVisibility(View.INVISIBLE);
		return layout;
	}	
	private static class DefaultIndicatorLayoutFactory {
		public static Class<? extends IndicatorLayout> createIndicatorLayoutClazz(String clazzName) {
			return DefaultIndicatorLayout.class;
		}

		public static IndicatorLayout createIndicatorLayout(Class<? extends IndicatorLayout> clazz, Context context, PullToRefreshBase.Mode mode) {
			return new DefaultIndicatorLayout(context, mode);
		}
		
	}
}

@@ -683,17 +683,99 @@
 ame();%0A%09
+/**%0A%09 * %0A%09 * @param layoutCode%0A%09 * @param context%0A%09 * @param mode%0A%09 * @return%0A%09 */
 %0A
-
 %09public 
@@ -1139,16 +1139,63 @@
 e);%0A%09%7D%0A%09
+/**%0A%09 * %0A%09 * @param layoutCode%0A%09 * @return%0A%09 */
 %0A%09public
@@ -1442,16 +1442,62 @@
 e);%0A%09%7D%0A%09
+/**%0A%09 * %0A%09 * @param clazzName%0A%09 * @return%0A%09 */
 %0A%09@Suppr
@@ -1665,24 +1665,157 @@
 = null;%0A
+%09%09if ( clazzName == null) %7B%0A%09%09%09clazz = DefaultIndicatorLayoutFactory.createIndicatorLayoutClazz(clazzName);%0A%09%09%09return clazz;%0A%09%09%7D%0A%09%09%0A 
 %09%09try %7B%0A
 %09%09%09clazz
@@ -1806,16 +1806,16 @@
 %09%09try %7B%0A
-
 %09%09%09clazz
@@ -2124,17 +2124,100 @@
 azz;%0A%09%7D%0A
+%09/**%0A%09 * %0A%09 * @param layoutCode%0A%09 * @param context%0A%09 * @param mode%0A%09 * @return%0A%09 */
 %0A
-
 %09public 
@@ -3886,16 +3886,51 @@
 ut;%0A%09%7D%09%0A
+%09/**%0A%09 * %0A%09 * @author NBP%0A%09 *%0A%09 */%0A
 %09private
@@ -4115,16 +4115,101 @@
 ss;%0A%09%09%7D%0A
+%09%09/**%0A%09%09 * %0A%09%09 * @param clazz%0A%09%09 * @param context%0A%09%09 * @param mode%0A%09%09 * @return%0A%09%09 */
 %0A%09%09publi

Add javadoc templates for methods of IndicatorLayoutFactory. Check clazzName parameter in LoadingLayoutFactory