/*
 * Copyright (c) 2005-2007 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
package org.jvnet.lafwidget;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;

import javax.swing.JComponent;

import org.jvnet.lafwidget.utils.FadeIgnoreManager;

/**
 * Repository of LAF widgets.
 * 
 * @author Kirill Grouchnikov
 */
public class LafWidgetRepository {
	/**
	 * All registered widgets. Key is {@link Class} in the UI component
	 * hierarchy, value is a {@link Set} of fully-qualified widget class names.
	 */
	protected Map<Class<?>, Set<ClassInfo>> widgets;

	/**
	 * Currently registered LAF support.
	 */
	protected LafWidgetSupport lafSupport;

	/**
	 * Indicates whether the currently registered LAF support is custom (not
	 * {@link LafWidgetSupport}).
	 */
	protected boolean isCustomLafSupportSet;

	/**
	 * Manages the components that ignore animations.
	 */
	protected FadeIgnoreManager fadeIgnoreManager;

	/**
	 * Singleton instance.
	 */
	protected static LafWidgetRepository repository;

	/**
	 * Resource bundle for <b>Substance</b> labels.
	 */
	private static ResourceBundle LABEL_BUNDLE = null;

	/**
	 * Class loader for the {@link #LABEL_BUNDLE}.
	 * 
	 * @since version 1.1
	 */
	private static ClassLoader labelBundleClassLoader;

	/**
	 * Information on a single class.
	 * 
	 * @author Kirill Grouchnikov
	 */
	protected static class ClassInfo {
		/**
		 * Class name.
		 */
		public String className;

		/**
		 * Indicates whether the matching should be exact.
		 */
		public boolean isExact;

		/**
		 * Creates a new info object.
		 * 
		 * @param className
		 *            Class name.
		 * @param isExact
		 *            Indicates whether the matching should be exact.
		 */
		public ClassInfo(String className, boolean isExact) {
			this.className = className;
			this.isExact = isExact;
		}
	}

	/**
	 * Creates a new repository. Marked private to enforce single instance.
	 */
	private LafWidgetRepository() {
		this.widgets = new HashMap<Class<?>, Set<ClassInfo>>();
		this.lafSupport = new LafWidgetSupport();
		this.isCustomLafSupportSet = false;
		this.fadeIgnoreManager = new FadeIgnoreManager();
	}

	/**
	 * Returns the widget repository.
	 * 
	 * @return Widget repository.
	 */
	public static synchronized LafWidgetRepository getRepository() {
		if (LafWidgetRepository.repository == null) {
			LafWidgetRepository.repository = new LafWidgetRepository();
			LafWidgetRepository.repository.populate();
			LafWidgetRepository.repository.fadeIgnoreManager.populate();
		}
		return LafWidgetRepository.repository;
	}

	/**
	 * Populates the repository from the specified URL. The URL should point to
	 * a properties file, the key being the fully-qualified class name of the
	 * widget implementation, the value being semicolon-separated
	 * fully-qualified class names of classes in UI component hierarchy. Sample
	 * property file:
	 * 
	 * <pre>
	 *             org.jvnet.lafwidget.text.PasswordStrengthCheckerWidget = javax.swing.JPasswordField
	 *             org.jvnet.lafwidget.text.LockBorderWidget = javax.swing.text.JTextComponent;javax.swing.JComboBox
	 * </pre>
	 * 
	 * @param url
	 *            URL that points to a properties file.
	 */
	protected void populateFrom(URL url) {
		Properties props = new Properties();
		InputStream is = null;
		try {
			is = url.openStream();
			props.load(is);

			Enumeration<?> names = props.propertyNames();
			while (names.hasMoreElements()) {
				String name = (String) names.nextElement();
				String value = props.getProperty(name);
				String[] values = value.split(";");
				for (int i = 0; i < values.length; i++) {
					String className = values[i].trim();
					boolean isExact = className.startsWith("%");
					if (isExact)
						className = className.substring(1);
					try {
						this.registerWidget(name, Class.forName(className),
								isExact);
					} catch (ClassNotFoundException cnfe) {
					}
				}
			}

		} catch (IOException ioe) {
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ioe) {
				}
			}
		}
	}

	/**
	 * Populates the widget repository. The classpath is scanned for all
	 * resources that match the name <code>META-INF/lafwidget.properties</code>.
	 * 
	 * @see #populateFrom(URL)
	 */
	public void populate() {
		// the following is fix by Dag Joar and Christian Schlichtherle
		// for application running with -Xbootclasspath VM flag. In this case,
		// the using MyClass.class.getClassLoader() would return null,
		// but the context class loader will function properly
		// that classes will be properly loaded regardless of whether the lib is
		// added to the system class path, the extension class path and
		// regardless of the class loader architecture set up by some
		// frameworks.
		ClassLoader cl = Thread.currentThread().getContextClassLoader();

		try {
			Enumeration<?> rs = cl
					.getResources("META-INF/lafwidget.properties");
			while (rs.hasMoreElements()) {
				URL rUrl = (URL) rs.nextElement();
				this.populateFrom(rUrl);
			}
		} catch (IOException ioe) {
		}
	}

	/**
	 * Registers a new widget for the specified UI classes. The list should
	 * contain {@link Class} instances.
	 * 
	 * @param widgetClassName
	 *            Full-qualified class name for the widget.
	 * @param supportedClasses
	 *            Classes supported by the widget.
	 */
	public synchronized void registerWidget(String widgetClassName,
			List<Class<?>> supportedClasses) {
		for (Class<?> clazz : supportedClasses)
			this.registerWidget(widgetClassName, clazz, false);
	}

	/**
	 * Registers a new widget for the specified UI class.
	 * 
	 * @param widgetClassName
	 *            Full-qualified class name for the widget.
	 * @param supportedClass
	 *            Class supported by the widget.
	 * @param isExact
	 *            if <code>true</code>, the widget will be available only for
	 *            the components of the specified class. If <code>false</code>,
	 *            the widget be available for the components of the specified
	 *            class and all its descendants (as defined in the
	 *            {@link Class#isAssignableFrom(Class)}).
	 */
	public synchronized void registerWidget(String widgetClassName,
			Class<?> supportedClass, boolean isExact) {
		if (JComponent.class.isAssignableFrom(supportedClass)) {
			if (!this.widgets.containsKey(supportedClass))
				this.widgets.put(supportedClass, new HashSet<ClassInfo>());
		}
		this.widgets.get(supportedClass).add(
				new ClassInfo(widgetClassName, isExact));
	}

	/**
	 * Returns a set of widgets that match the specified component. The
	 * component hierarchy is scanned bottom-up and all matching widget classes
	 * are used to instantiate new instance of widgets. In case the
	 * {@link #isCustomLafSupportSet} is <code>false</code>, only widgets
	 * that return <code>false</code> in
	 * {@link LafWidget#requiresCustomLafSupport()} are returned.
	 * 
	 * 
	 * @param jcomp
	 *            UI component.
	 * @return Set of widgets that match the specified component.
	 */
	public synchronized Set<LafWidget> getMatchingWidgets(JComponent jcomp) {
		Set<LafWidget> result = new HashSet<LafWidget>();
		Class<?> clazz = jcomp.getClass();
		boolean isOriginator = true;
		while (clazz != null) {
			Set<ClassInfo> registered = this.widgets.get(clazz);
			if (registered != null) {
				for (Iterator<ClassInfo> it = registered.iterator(); it
						.hasNext();) {
					ClassInfo widgetClassInfo = it.next();
					if (widgetClassInfo.isExact && !isOriginator)
						continue;
					try {
						String widgetClassName = widgetClassInfo.className;
						// The code below will fail if no such class exists.
						// This allows safely removing the relevant widget
						// classes making the jar size smaller (lite versions).
						Object widgetObj = Class.forName(widgetClassName)
								.newInstance();
						if (widgetObj instanceof LafWidget) {
							LafWidget widget = (LafWidget) widgetObj;
							// only add widgets that do not require special LAF
							// support if no such support has been set.
							if (!widget.requiresCustomLafSupport()
									|| this.isCustomLafSupportSet) {
								widget.setComponent(jcomp);
								result.add(widget);
							}
						}
						// the exceptions are ignored - see the explanation
						// above.
					} catch (InstantiationException ie) {
					} catch (IllegalAccessException iae) {
					} catch (ClassNotFoundException cnfe) {
					}
				}
			}
			clazz = clazz.getSuperclass();
			isOriginator = false;
		}
		return result;
	}

	/**
	 * Sets LAF support.
	 * 
	 * @param lafSupport
	 *            LAF support.
	 * @throws IllegalArgumentException
	 *             If the LAF support is <code>null</code>.
	 */
	public void setLafSupport(LafWidgetSupport lafSupport) {
		if (lafSupport == null)
			throw new IllegalArgumentException("LAF support can't be null");
		this.lafSupport = lafSupport;
		this.isCustomLafSupportSet = (this.lafSupport.getClass() != LafWidgetSupport.class);
	}

	/**
	 * Unsets custom LAF support and reverts to the base LAF support.
	 */
	public void unsetLafSupport() {
		this.lafSupport = new LafWidgetSupport();
		this.isCustomLafSupportSet = false;
	}

	/**
	 * Returns the currently set LAF support. The result is guaranteed to be
	 * non-<code>null</code>.
	 * 
	 * @return Currently set non-<code>null</code> LAF support.
	 */
	public LafWidgetSupport getLafSupport() {
		return this.lafSupport;
	}

	/**
	 * Retrieves the current label bundle.
	 * 
	 * @return The current label bundle.
	 * @see #resetLabelBundle()
	 */
	public static synchronized ResourceBundle getLabelBundle() {
		// fix for RFE 157 on Substance (allowing custom class loader for
		// resource bundles which can remove server calls
		// in applets)
		if (LafWidgetRepository.labelBundleClassLoader == null) {
			LafWidgetRepository.LABEL_BUNDLE = ResourceBundle
					.getBundle("org.jvnet.lafwidget.resources.Labels", Locale
							.getDefault());
		} else {
			LafWidgetRepository.LABEL_BUNDLE = ResourceBundle.getBundle(
					"org.jvnet.lafwidget.resources.Labels",
					Locale.getDefault(),
					LafWidgetRepository.labelBundleClassLoader);
		}
		return LafWidgetRepository.LABEL_BUNDLE;
	}

	/**
	 * Resets the current label bundle. Useful when the application changes
	 * Locale at runtime.
	 * 
	 * @see #getLabelBundle()
	 */
	public static synchronized void resetLabelBundle() {
		LafWidgetRepository.LABEL_BUNDLE = null;
	}

	/**
	 * Sets the class loader for {@link #LABEL_BUNDLE}.
	 * 
	 * @param labelBundleClassLoader
	 *            Class loader for {@link #LABEL_BUNDLE}.
	 * @since version 1.1
	 */
	public static void setLabelBundleClassLoader(
			ClassLoader labelBundleClassLoader) {
		LafWidgetRepository.labelBundleClassLoader = labelBundleClassLoader;
	}

	/**
	 * Retrieves the manager for components that ignore animations.
	 * 
	 * @return The manager for components that ignore animations.
	 */
	public FadeIgnoreManager getFadeIgnoreManager() {
		return fadeIgnoreManager;
	}
}
