| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.pde.api.tools.internal; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.HashSet; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import org.eclipse.pde.api.tools.internal.provisional.ISession; |
| 20 | import org.eclipse.pde.api.tools.internal.provisional.ISessionListener; |
| 21 | import org.eclipse.pde.api.tools.internal.provisional.ISessionManager; |
| 22 | |
| 23 | /** |
| 24 | * Implementation of the ISessionManager. |
| 25 | * Synchronize all accesses to prevent concurrent modifications. |
| 26 | */ |
| 27 | public class SessionManager implements ISessionManager { |
| 28 | |
| 29 | // use a list so that we can preserve the order |
| 30 | private List sessions = new ArrayList(); |
| 31 | private Set listeners = new HashSet(); |
| 32 | private ISession activeSession; |
| 33 | |
| 34 | public synchronized void addSession(ISession session, boolean activate) { |
| 35 | if (session == null) { |
| 36 | throw new IllegalArgumentException("The given session cannot be null"); //$NON-NLS-1$ |
| 37 | } |
| 38 | if (!this.sessions.contains(session)) { |
| 39 | this.sessions.add(session); |
| 40 | fireSessionAdded(session); |
| 41 | } |
| 42 | if (activate) { |
| 43 | this.activeSession = session; |
| 44 | fireSessionActivated(session); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public synchronized void removeSession(ISession session) { |
| 49 | if (sessions.remove(session)) { |
| 50 | if (session.equals(this.activeSession)) { |
| 51 | this.activeSession = null; |
| 52 | fireSessionActivated(null); |
| 53 | } |
| 54 | fireSessionRemoved(session); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public synchronized void removeAllSessions() { |
| 59 | ISession[] allSessions = (ISession[]) this.sessions.toArray(new ISession[this.sessions.size()]); |
| 60 | this.sessions.clear(); |
| 61 | this.activeSession = null; |
| 62 | fireSessionActivated(null); |
| 63 | for (int i = 0; i < allSessions.length; i++) { |
| 64 | ISession session = allSessions[i]; |
| 65 | fireSessionRemoved(session); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public synchronized ISession[] getSessions() { |
| 70 | return (ISession[]) sessions.toArray(new ISession[sessions.size()]); |
| 71 | } |
| 72 | |
| 73 | public synchronized void addSessionListener(ISessionListener listener) { |
| 74 | if (listener == null) throw new IllegalArgumentException("The given listener cannot be null"); //$NON-NLS-1$ |
| 75 | listeners.add(listener); |
| 76 | } |
| 77 | |
| 78 | public synchronized void removeSessionListener(ISessionListener listener) { |
| 79 | listeners.remove(listener); |
| 80 | } |
| 81 | |
| 82 | protected synchronized void fireSessionAdded(ISession session) { |
| 83 | Iterator i = listeners.iterator(); |
| 84 | while (i.hasNext()) { |
| 85 | ((ISessionListener) i.next()).sessionAdded(session); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | protected synchronized void fireSessionRemoved(ISession session) { |
| 90 | Iterator i = listeners.iterator(); |
| 91 | while (i.hasNext()) { |
| 92 | ((ISessionListener) i.next()).sessionRemoved(session); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public ISession getActiveSession() { |
| 97 | return this.activeSession; |
| 98 | } |
| 99 | |
| 100 | protected synchronized void fireSessionActivated(ISession session) { |
| 101 | Iterator i = listeners.iterator(); |
| 102 | while (i.hasNext()) { |
| 103 | ((ISessionListener) i.next()).sessionActivated(session); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public void activateSession(ISession session) { |
| 108 | if (this.sessions.contains(session) && !session.equals(this.activeSession)) { |
| 109 | this.activeSession = session; |
| 110 | fireSessionActivated(session); |
| 111 | } |
| 112 | } |
| 113 | } |