package hep.aida.ref.plotter.style.registry;

import hep.aida.IPlotterFactory;
import hep.aida.IPlotterStyle;
import hep.aida.ref.plotter.PlotterFactory;
import hep.aida.ref.plotter.PlotterStyle;
import hep.aida.ref.plotter.Style;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.freehep.util.FreeHEPLookup;

public class StyleRegistry implements IStyleRegistry, IGlobalIndexProvider {
    public static String systemStoreProperty = "hep.aida.ref.plotter.style.registry.system.store";
    public static String groupStoreProperty  = "hep.aida.ref.plotter.style.registry.group.store";
    public static String userStoreProperty   = "hep.aida.ref.plotter.style.registry.user.store";
    private static String systemStoreFile    = "SystemStyleStore.xml";
    private static StyleRegistry registry;
    private IPlotterFactory pf;
    private List stores;
    private Map categories;
    private Object lock;
    
    private int globalIndex = 0;
    
    public static StyleRegistry getStyleRegistry() {
        if (registry == null) registry = new StyleRegistry();
        return registry;
    }
    
    private StyleRegistry() {
        stores = new ArrayList(10);
        categories = new HashMap(5);
        lock = new Object();
        String systemFile = System.getProperty(systemStoreProperty, systemStoreFile);
        String groupFile  = System.getProperty(groupStoreProperty, null);
        String userFile   = System.getProperty(userStoreProperty, null);
        try {
            //URL url = getClass().getResource(systemFile);
            InputStream stream = getClass().getResourceAsStream(systemFile);
            IStyleStore systemStore = StyleStoreXMLReader.restoreFromStream(stream);
            addStore(systemStore);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (groupFile != null && !groupFile.trim().equals("")) {
            try {
                InputStream stream = getClass().getResourceAsStream(groupFile);
                IStyleStore store = StyleStoreXMLReader.restoreFromStream(stream);
                addStore(store);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (userFile != null && !userFile.trim().equals("")) {
            try {
                InputStream stream = getClass().getResourceAsStream(userFile);
                IStyleStore store = StyleStoreXMLReader.restoreFromStream(stream);
                addStore(store);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (FreeHEPLookup.instance().lookup(IStyleRegistry.class) == null) FreeHEPLookup.instance().add(this);
    }

    
    // IGlobalIndexProvider methods
    
    public int getIndex() {
        int index = globalIndex;
        globalIndex++;
        return index;
    }
    
    public void resetIndex() {
        globalIndex = 0;
    }
    
    // Service methods
    
    IPlotterStyle mergeStyles(List styles, IPlotterState state) {
        if (styles == null || styles.size() == 0) return null;
        if (pf == null) pf = new PlotterFactory();
        PlotterStyle style = (PlotterStyle) pf.createPlotterStyle();
        style.setParameter(Style.PLOTTER_STYLE_NAME, "StyleRegistry");
        
        // Set parameters that are revolving
        /*
        String fillColor    = null;
        String lineColor    = null;
        String errorColor   = null;
        String markerColor  = null;
        String outlineColor = null;
        for (int i=0; i= 0) {
                rule = rule.substring(index+11);
                index = rule.indexOf("attribute(");
                String token = null;
                if (index > 0) token = rule.substring(0, index);
                else token = rule;
                int index3 = token.indexOf("\").");
                String key = token.substring (0, index3);
                String value = token.substring (index3+1);
                value = value.substring(value.indexOf("\"")+1);
                value = value.substring(0, value.indexOf("\""));
                //System.out.println("\t\tKey :: "+key+"  Value="+value);
                if (key != null && !key.trim().equals("")) {
                    Object obj = hash.get(key);
                    if (obj == null) {
                        ArrayList list = new ArrayList();
                        list.add(value);
                        hash.put(key, list);
                    } else {
                        ArrayList list = (ArrayList) obj;
                        if (!list.contains(value)) list.add(value);
                    }
                }
            }
        }
        if (hash.isEmpty()) return;
        Iterator it = hash.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            ArrayList list = (ArrayList) hash.get(key);
            if (list.isEmpty()) continue;
            Object tmpCat = categories.get(key);
            Category cat = null;
            if (tmpCat == null) cat = new Category(key);
            else cat = (Category) tmpCat;
            for (int i=0; i 

Ads help maintain this website.