/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.application.icons;

import java.awt.Dimension;
import javax.swing.Icon;
import java.util.HashMap;

import java.util.Properties;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Collection;

public class IconLibrary {

	/** The key for the default icon */
	private static final String DEFAULT_KEY = "default";
	
	/** The file containing the icon keys and file names */
	private static final String ICON_INDEX = "images/icons.properties";	

	private HashMap<String,VectorIcon> icons = null;
	
	/** The current displayable icon size */
	private Dimension icon_size = new Dimension(24,24);
	
	public IconLibrary(String listing) {
		
		icons = new HashMap<String,VectorIcon>();
		
		// Load the icons
		this.loadIcons(listing);
		
		// Size all icons
		this.sizeIcons();
	}
	
	public IconLibrary() {
		this(ICON_INDEX);
	}
	
	/** Creates a new VectorIcon object and stores the value in the
	 * master map using the specified key.
	 *
	 * @param key the key associated with this icon
	 * @param icon_resource the string specifying the resource path
	 */
	private void loadIcon(String key, String icon_resource) {
		VectorIcon thisone = new VectorIcon(icon_resource);
		icons.put(key,thisone);
	}

	/** Returns a desired Icon based on the passed key value.  If the
	 * key is not currently in storage, a default icon is passed.
	 *
	 * @param key a string used as the lookup key
	 * @return a valid Icon object
	 */
	public VectorIcon getIcon(String key) {
		if(icons.containsKey(key)) {
			return icons.get(key);
		} else {
			return icons.get(DEFAULT_KEY);
		}
	}

	/** Sets all stored icons to the size currently specified in the library's
	 * Dimension member.
	 *
	 */
	private void sizeIcons() {
		for(VectorIcon vi : (Collection<VectorIcon>)icons.values())
			vi.setDimension(this.icon_size);
	}

	/** Sets the size of all icons to the specified dimension
	 *
	 * @param d the dimension for all icons
	 */
	public void setIconSize(Dimension d) {
		if(d == null) {
			// Size using the default
			this.sizeIcons();
		}
		else if(!d.equals(this.icon_size)) {
			this.icon_size = d;
			this.sizeIcons();
		}
	}
	
	/** Returns a copy of the current icon dimension value.  A copy is used
	 * to dissuade any calling mehtods from attempting to change the 
	 * stored object value.
	 *
	 * @return a Dimension object which is a clone of this object's stored value
	 */
	public Dimension getIconSize() {
		return (Dimension)this.icon_size.clone();
	}

	/** Loads all the icons based on the filenames provided in the icons properties
	 * file and stores them in the internal HashMap.
	 *
	 * @param listing_resource the resource containing the icon filenames
	 */
	private void loadIcons(String listing_resource) {
		try {
			Properties props = new Properties();
			props.load(this.getClass().getResourceAsStream(listing_resource));
			
			for (Enumeration e = props.propertyNames() ; e.hasMoreElements() ;) {
				String element = (String)e.nextElement();
				this.loadIcon(element,(String)props.getProperty(element));
			}
			
			// Adds a default icon if one was not specified
			if(!this.icons.containsKey(DEFAULT_KEY)) {
				if(this.icons.values().toArray().length > 0) {
					this.icons.put(DEFAULT_KEY,(VectorIcon)this.icons.values().toArray()[0]);
				}
			}
						
		} catch(IOException ioe) {
			System.err.println("Problem loading icon index...");
			ioe.printStackTrace();
		}
	}	

}
