001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import java.util.LinkedList;
040: import edu.rice.cs.util.ReaderWriterLock;
041: import edu.rice.cs.util.swing.Utilities;
042: import edu.rice.cs.util.UnexpectedException;
043:
044: /** Base class for all component-specific EventNotifiers. This class provides common methods to
045: * manage listeners of a specific type. T the type of the listener class to be managed.
046: * @version $Id: EventNotifier.java 4268 2007-11-29 00:52:17Z mgricken $
047: */
048: public abstract class EventNotifier<T> {
049: /** All T Listeners that are listening to the model. Accesses to this collection are protected by the
050: * ReaderWriterLock. The collection must be synchronized, since multiple readers could access it at once.
051: */
052: protected final LinkedList<T> _listeners = new LinkedList<T>();
053:
054: /** Provides synchronization primitives for solving the readers/writers problem. In EventNotifier, adding and
055: * removing listeners are considered write operations, and all notifications are considered read operations. Multiple
056: * reads can occur simultaneously, but only one write can occur at a time, and no reads can occur during a write.
057: */
058: protected final ReaderWriterLock _lock = new ReaderWriterLock();
059:
060: /** Adds a listener to the notifier.
061: * @param listener a listener that reacts on events
062: */
063: public void addListener(T listener) {
064: // Utilities.showDebug("Adding listener " + listener + " to event notifier " + this);
065: _lock.startWrite();
066: try {
067: _listeners.add(listener);
068: } finally {
069: _lock.endWrite();
070: // new ScrollableDialog(null, "Released writeLock on event queue", "", "").show();
071: }
072: }
073:
074: /** Removes a listener from the notifier. If the thread already holds the lock,
075: * then the listener is removed later, but as soon as possible.
076: * Note: It is NOT guaranteed that the listener will not be executed again.
077: * @param listener a listener that reacts on events
078: */
079: public void removeListener(final T listener) {
080: // Utilities.show("writeLock on _listeners grabbed by " + this);
081: try {
082: _lock.startWrite();
083: try {
084: _listeners.remove(listener);
085: } finally {
086: _lock.endWrite();
087: // new ScrollableDialog(null, "Released writeLock on event queue", "", "").show();
088: }
089: } catch (ReaderWriterLock.DeadlockException e) {
090: // couldn't remove right now because this thread already owns a lock
091: // remember to remove it later
092: new Thread(new Runnable() {
093: public void run() {
094: _lock.startWrite();
095: try {
096: _listeners.remove(listener);
097: } finally {
098: _lock.endWrite();
099: }
100: }
101: }, "Pending Listener Removal").start();
102: // synchronized(_listenersToRemove) {
103: // _listenersToRemove.add(listener);
104: // }
105: }
106: }
107:
108: /** Removes all listeners from this notifier. If the thread already holds the lock,
109: * then the listener is removed later, but as soon as possible.
110: * Note: It is NOT guaranteed that the listener will not be executed again. */
111: public void removeAllListeners() {
112: // new ScrollableDialog(null, "Grabbing writeLock on event queue", "", "").show();
113: try {
114: _lock.startWrite();
115: try {
116: _listeners.clear();
117: } finally {
118: _lock.endWrite();
119: // new ScrollableDialog(null, "Released writeLock on event queue", "", "").show();
120: }
121: } catch (ReaderWriterLock.DeadlockException e) {
122: // couldn't remove right now because this thread already owns a lock
123: // remember to remove it later
124: new Thread(new Runnable() {
125: public void run() {
126: _lock.startWrite();
127: try {
128: _listeners.clear();
129: } finally {
130: _lock.endWrite();
131: }
132: }
133: }, "Pending Listener Removal").start();
134: }
135: }
136: }
|