package com.jrockit.console.methodprofiler; import java.util.*; import javax.swing.event.*; /** * Provides a listener interface for notifications about when * a template has been added, removed or when a certain template * has had its members changed. * * @author Marcus Hirt */ public class TemplateCollection implements ChangeListener { private Map m_map = new HashMap(); /** The listeners waiting for model changes. */ private EventListenerList m_templateListenerList = new EventListenerList(); /** * Constructor for MethodProfilerTemplateCollection. */ public TemplateCollection() { super(); } /** * Adds a template change listeners to listen on this collection. * The listener will be notified whenever changes to the collection * is made, or whenever a template in the collection has been modified. * * @param l the listener to add. */ public void addTemplateChangeListener(TemplateChangeListener l) { m_templateListenerList.add(l.getClass(), l); } /** * Removes a TemplateChangeListener from this TemplateCollection. * * @param l the listener to remove. * * @see #addTemplateChangeListener(TemplateChangeListener) */ public void removeTemplateChangeListener(TemplateChangeListener l) { m_templateListenerList.add(l.getClass(), l); } /** * Works like the HashMap equivalent. Also fires a state change. * @param key * @param value * @see HashMap#put(java.lang.Object, java.lang.Object) */ public void addTemplate(String name, MethodProfilerTemplate template) { m_map.put(name, template); template.addChangeListener(this); fireTemplateChange(TemplateChangeEvent.TYPE_TEMPLATE_ADDED, name); } /** * Method get. * @see HashMap#get(Object) */ public Object getTemplate(String name) { return m_map.get(name); } /** * @see HashMap#values() */ public Collection values() { return m_map.values(); } /** * Removes a template from this collection. * * @param name the name of the template to remove. */ public void removeTemplate(String name) { m_map.remove(name); fireTemplateChange(TemplateChangeEvent.TYPE_TEMPLATE_REMOVED, name); } /** * @see HashMap#keySet() */ public Collection keySet() { return m_map.keySet(); } /** * @see HashMap#containsKey(java.lang.Object) */ public boolean containsKey(Object o) { return m_map.containsKey(o); } private void fireTemplateChange(String eventType, String templateName) { Object[] listeners = m_templateListenerList.getListenerList(); for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i + 1] instanceof TemplateChangeListener) { TemplateChangeEvent e = new TemplateChangeEvent(eventType, templateName); ((TemplateChangeListener) listeners[i + 1]).onTemplateChange(e); } } } /** * @see javax.swing.event.ChangeListener#stateChanged(ChangeEvent) */ public void stateChanged(ChangeEvent e) { MethodProfilerTemplate template = (MethodProfilerTemplate) e.getSource(); fireTemplateChange( TemplateChangeEvent.TYPE_TEMPLATE_CONTENT_CHANGED, template.getName()); } /** * @return boolean true if the collection contains any templates. */ public boolean hasTemplates() { if (values() == null || values().size() <= 0) return false; return true; } }