/*
 * Created on 2004-08-19
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.templates;

import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;

/**
 * @author mikolaj
 *
 * A list of all the New Project templates
 */
public class TemplatesList {

    public static final String EXTENSION_POINT=SymbdevPlugin.getUniqueIdentifier()+".SubstitutionTemplate"; 
    public static final String ID_ATTRIBUTE="id"; 
    public static final String PATH_ATTRIBUTE="path";
    public static final String NAME_ATTRIBUTE="name";
    public static final String LONG_NAME_ATTRIBUTE="longName";    
    public static final String WIZARD_PAGE_ELEMENT="wizardPage";    
    
    protected TemplatesList() {
        super();
        fList.addLast(new EmptyProjectTemplate());
        
      	IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtensionPoint extensionPoint =
          registry.getExtensionPoint(EXTENSION_POINT);
        IConfigurationElement[] members =
          extensionPoint.getConfigurationElements();
        
        for (int m = 0; m < members.length; m++) {
        	IConfigurationElement member=members[m];
            String pluginLabel=member.getDeclaringExtension().getNamespace();
            String id=member.getAttribute(ID_ATTRIBUTE);
            String path=member.getAttribute(PATH_ATTRIBUTE);
            String name=member.getAttribute(NAME_ATTRIBUTE);
            String longName=member.getAttribute(LONG_NAME_ATTRIBUTE);
            
            if (id==null || path==null || name==null || longName==null)
            {
                SymbdevPlugin.logWarning(SymbdevPlugin.WARNING_TEMPLATE,
                        "No id, path, name or long name in template extenstion point");
                continue;
            }
            
            IConfigurationElement wizard[]=member.getChildren(WIZARD_PAGE_ELEMENT);
            ISubstitutionOptionsWizardPage wizPage=null;
            if (wizard.length>0)
            {
                Object o=null;
                try {
                    o=wizard[0].createExecutableExtension("class");
                }
                catch (CoreException e)
                {
                    SymbdevPlugin.getDefault().getLog().log(e.getStatus());
                    continue;
                }
                if (o==null || !(o instanceof ISubstitutionOptionsWizardPage))
                {
                    SymbdevPlugin.logWarning(SymbdevPlugin.WARNING_TEMPLATE,
                    	"Invalid class while loading wizard page "+name);
                    continue;                    
                }
                wizPage=(ISubstitutionOptionsWizardPage)o;
            }
            
            URL url=Platform.getBundle(pluginLabel).getEntry(path);
            fList.addLast(new SubstitutionsProjectTemplate(
                    url, name, longName, wizPage));
        }        
    }
    
    public static TemplatesList getDefault()
    {
        if (fInstance==null)
            fInstance=new TemplatesList();
        return fInstance;
    }
    
    /** 
     * The avaible project templates
     * @return An iterator to IProjectTemplate objects
     */
    public Iterator getIterator()
    {
        return fList.iterator();
    }

    public IProjectTemplate[] getTemplates()
    {
        return (IProjectTemplate[])fList.toArray(new IProjectTemplate[fList.size()]);
    }
    
    LinkedList fList=new LinkedList();
    static TemplatesList fInstance=null;
}
