1 8 package com.mountainminds.eclemma.internal.core; 9 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 import com.mountainminds.eclemma.core.ICoverageSession; 17 import com.mountainminds.eclemma.core.ISessionListener; 18 import com.mountainminds.eclemma.core.ISessionManager; 19 20 26 public class SessionManager implements ISessionManager { 27 28 private List sessions = new ArrayList (); 29 private Map keymap = new HashMap (); 30 private ICoverageSession activeSession = null; 31 private List listeners = new ArrayList (); 32 33 public void addSession(ICoverageSession session, boolean activate, Object key) { 34 if (session == null) throw new NullPointerException (); 35 if (!sessions.contains(session)) { 36 sessions.add(session); 37 if (key != null) { 38 keymap.put(key, session); 39 } 40 fireSessionAdded(session); 41 if (activate) { 42 activeSession = session; 43 fireSessionActivated(session); 44 } 45 } 46 } 47 48 public void removeSession(ICoverageSession session) { 49 if (sessions.contains(session)) { 50 boolean sessionActivated = false; 51 if (session.equals(activeSession)) { 52 activeSession = null; 53 for (int i = sessions.size(); --i >= 0;) { 54 if (!session.equals(sessions.get(i))) { 55 activeSession = (ICoverageSession) sessions.get(i); 56 break; 57 } 58 } 59 sessionActivated = true; 60 } 61 sessions.remove(session); 62 keymap.values().remove(session); 63 fireSessionRemoved(session); 64 if (sessionActivated) { 65 fireSessionActivated(activeSession); 66 } 67 } 68 } 69 70 public void removeSession(Object key) { 71 removeSession(getSession(key)); 72 } 73 74 public void removeAllSessions() { 75 while (!sessions.isEmpty()) { 76 ICoverageSession session = (ICoverageSession) sessions.remove(0); 77 keymap.values().remove(session); 78 fireSessionRemoved(session); 79 } 80 if (activeSession != null) { 81 activeSession = null; 82 fireSessionActivated(null); 83 } 84 } 85 86 public ICoverageSession[] getSessions() { 87 return (ICoverageSession[]) sessions.toArray(new ICoverageSession[sessions.size()]); 88 } 89 90 public ICoverageSession getSession(Object key) { 91 return (ICoverageSession) keymap.get(key); 92 } 93 94 public void activateSession(ICoverageSession session) { 95 if (sessions.contains(session) && !session.equals(activeSession)) { 96 activeSession = session; 97 fireSessionActivated(session); 98 } 99 } 100 101 public void activateSession(Object key) { 102 activateSession(getSession(key)); 103 } 104 105 public ICoverageSession getActiveSession() { 106 return activeSession; 107 } 108 109 public void refreshActiveSession() { 110 if (activeSession != null) { 111 fireSessionActivated(activeSession); 112 } 113 } 114 115 public void addSessionListener(ISessionListener listener) { 116 if (listener == null) throw new NullPointerException (); 117 if (!listeners.contains(listener)) { 118 listeners.add(listener); 119 } 120 } 121 122 public void removeSessionListener(ISessionListener listener) { 123 listeners.remove(listener); 124 } 125 126 protected void fireSessionAdded(ICoverageSession session) { 127 Iterator i = new ArrayList (listeners).iterator(); 129 while (i.hasNext()) { 130 ((ISessionListener) i.next()).sessionAdded(session); 131 } 132 } 133 134 protected void fireSessionRemoved(ICoverageSession session) { 135 Iterator i = new ArrayList (listeners).iterator(); 137 while (i.hasNext()) { 138 ((ISessionListener) i.next()).sessionRemoved(session); 139 } 140 } 141 142 private void fireSessionActivated(ICoverageSession session) { 143 Iterator i = new ArrayList (listeners).iterator(); 145 while (i.hasNext()) { 146 ((ISessionListener) i.next()).sessionActivated(session); 147 } 148 } 149 150 } 151 | Popular Tags |