001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.util;
043:
044: import java.util.List;
045: import java.util.concurrent.CopyOnWriteArrayList;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048:
049: /**
050: * A simple equivalent of {@link java.beans.PropertyChangeSupport} for
051: * {@link ChangeListener}s. This class is not serializable.
052: *
053: * @since 7.8
054: * @author Andrei Badea
055: */
056: public final class ChangeSupport {
057:
058: // not private because used in unit tests
059: final List<ChangeListener> listeners = new CopyOnWriteArrayList<ChangeListener>();
060: private final Object source;
061:
062: /**
063: * Creates a new <code>ChangeSupport</code>
064: *
065: * @param source the instance to be given as the source for events.
066: */
067: public ChangeSupport(Object source) {
068: this .source = source;
069: }
070:
071: /**
072: * Adds a <code>ChangeListener</code> to the listener list. The same
073: * listener object may be added more than once, and will be called
074: * as many times as it is added. If <code>listener</code> is null,
075: * no exception is thrown and no action is taken.
076: *
077: * @param listener the <code>ChangeListener</code> to be added.
078: */
079: public void addChangeListener(ChangeListener listener) {
080: if (listener == null) {
081: return;
082: }
083: listeners.add(listener);
084: }
085:
086: /**
087: * Removes a <code>ChangeListener</code> from the listener list.
088: * If <code>listener</code> was added more than once,
089: * it will be notified one less time after being removed.
090: * If <code>listener</code> is null, or was never added, no exception is
091: * thrown and no action is taken.
092: *
093: * @param listener the <code>ChangeListener</code> to be removed.
094: */
095: public void removeChangeListener(ChangeListener listener) {
096: if (listener == null) {
097: return;
098: }
099: listeners.remove(listener);
100: }
101:
102: /**
103: * Fires a change event to all registered listeners.
104: */
105: public void fireChange() {
106: if (listeners.isEmpty()) {
107: return;
108: }
109: fireChange(new ChangeEvent(source));
110: }
111:
112: /**
113: * Fires the specified <code>ChangeEvent</code> to all registered
114: * listeners. If <code>event</code> is null, no exception is thrown
115: * and no action is taken.
116: *
117: * @param event the <code>ChangeEvent</code> to be fired.
118: */
119: private void fireChange(ChangeEvent event) {
120: assert event != null;
121: for (ChangeListener listener : listeners) {
122: try {
123: listener.stateChanged(event);
124: } catch (RuntimeException x) {
125: Exceptions.printStackTrace(x);
126: }
127: }
128: }
129:
130: /**
131: * Checks if there are any listeners registered to this<code>ChangeSupport</code>.
132: *
133: * @return true if there are one or more listeners for the given property,
134: * false otherwise.
135: */
136: public boolean hasListeners() {
137: return !listeners.isEmpty();
138: }
139: }
|