KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > SessionManager


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: SessionManager.java 103 2006-09-21 11:13:05Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import com.mountainminds.eclemma.core.ICoverageSession;
17 import com.mountainminds.eclemma.core.ISessionListener;
18 import com.mountainminds.eclemma.core.ISessionManager;
19
20 /**
21  * ISessionManager implementation.
22  *
23  * @author Marc R. Hoffmann
24  * @version $Revision: 103 $
25  */

26 public class SessionManager implements ISessionManager {
27
28   private List JavaDoc sessions = new ArrayList JavaDoc();
29   private Map JavaDoc keymap = new HashMap JavaDoc();
30   private ICoverageSession activeSession = null;
31   private List JavaDoc listeners = new ArrayList JavaDoc();
32
33   public void addSession(ICoverageSession session, boolean activate, Object JavaDoc key) {
34     if (session == null) throw new NullPointerException JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc();
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     // avoid concurrent modification issues
128
Iterator JavaDoc i = new ArrayList JavaDoc(listeners).iterator();
129     while (i.hasNext()) {
130       ((ISessionListener) i.next()).sessionAdded(session);
131     }
132   }
133
134   protected void fireSessionRemoved(ICoverageSession session) {
135     // avoid concurrent modification issues
136
Iterator JavaDoc i = new ArrayList JavaDoc(listeners).iterator();
137     while (i.hasNext()) {
138       ((ISessionListener) i.next()).sessionRemoved(session);
139     }
140   }
141
142   private void fireSessionActivated(ICoverageSession session) {
143     // avoid concurrent modification issues
144
Iterator JavaDoc i = new ArrayList JavaDoc(listeners).iterator();
145     while (i.hasNext()) {
146       ((ISessionListener) i.next()).sessionActivated(session);
147     }
148   }
149
150 }
151
Popular Tags