001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * Portions Copyrighted 2007 Sun Microsystems, Inc.
016: */
017: package org.netbeans.modules.bpel.project;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.concurrent.locks.Lock;
022: import java.util.concurrent.locks.ReentrantReadWriteLock;
023:
024: /**
025: *
026: * @author Vitaly Bychkov
027: */
028: public class ProjectCloseSupport {
029:
030: private Lock writeLock = new ReentrantReadWriteLock().writeLock();
031: private List<ProjectCloseListener> myListeners = new ArrayList<ProjectCloseListener>();
032:
033: /**
034: * Add a ProjectCloseListener to the listener list.
035: * The listener is registered for close event.
036: * The same listener object may be added more than once, and will be called
037: * as many times as it is added.
038: * If <code>listener</code> is null, assert exception will occure
039: * is taken.
040: *
041: * @param listener The ProjectCloseListener to be added
042: */
043: public void addProjectCloseListener(ProjectCloseListener listener) {
044: assert listener != null : "Try to add null listener."; // NOI18N
045: writeLock.lock();
046: try {
047: myListeners.add(listener);
048: } finally {
049: writeLock.unlock();
050: }
051: }
052:
053: /**
054: * Remove a ProjectCloseListener from the listener list.
055: * This removes a ProjectCloseListener that was registered
056: * for close project event.
057: * If <code>listener</code> was added more than once to the same event
058: * source, it will be notified one less time after being removed.
059: * If <code>listener</code> is null, assert exception will occur
060: * If <code>listener</code> was never added, no exception is
061: * thrown and no action is taken.
062: *
063: * @param listener The ProjectCloseListener to be removed
064: */
065: public void removeProjectCloseListener(ProjectCloseListener listener) {
066: assert listener != null : "Try to remove null listener."; // NOI18N
067: writeLock.lock();
068: try {
069: myListeners.remove(listener);
070: } finally {
071: writeLock.unlock();
072: }
073: }
074:
075: private void removeAllProjectCloseListeners() {
076: writeLock.lock();
077: try {
078: myListeners.removeAll(myListeners);
079: } finally {
080: writeLock.unlock();
081: }
082: }
083:
084: /**
085: * Invoke projectClose() method on every registered listeners.
086: * After it all registered listeners are removed from listeners list.
087: */
088: public void fireProjectClosed() {
089: writeLock.lock();
090: try {
091: for (ProjectCloseListener listener : myListeners) {
092: listener.projectClosed();
093: }
094: removeAllProjectCloseListeners();
095: } finally {
096: writeLock.unlock();
097: }
098: }
099:
100: }
|