package com.mindoo.ec09.example5.ca;

import java.util.HashMap;

/**
 * Class to register viewparts and their CA data exchange PCSBeans
 */
public class ViewBeanRegistry {
	private static ViewBeanRegistry INSTANCE = null;

	private HashMap	mRegistry;

	private ViewBeanRegistry() {
		mRegistry = new HashMap();
	}

	public static synchronized ViewBeanRegistry getInstance() {
		if (INSTANCE == null)
			INSTANCE = new ViewBeanRegistry();
		return INSTANCE;
	}

	public void registerViewBean(String viewID, Object bean) {
		mRegistry.put(viewID, bean);
	}

	public void registerViewBean(String viewID, String secondaryID, Object bean) {
		mRegistry.put(viewID+":"+secondaryID, bean);
	}

	public void unregisterViewBean(String viewID) {
		mRegistry.remove(viewID);
	}

	public void unregisterViewBean(String viewID, String secondaryID) {
		mRegistry.remove(viewID+":"+secondaryID);
	}

	public Object getViewBean(String viewID) {
		int o = viewID.indexOf(':');
		if (o > 0)
			return getViewBean(viewID.substring(0, o), viewID.substring(o + 1));
		else
			return mRegistry.get(viewID);
	}

	public Object getViewBean(String viewID, String secondaryID) {
		Object ret = mRegistry.get(viewID+":"+secondaryID);
		if (ret == null)
			ret = mRegistry.get(viewID);
		return ret;
	}
}
