001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * StyleChangeSupport.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.style;
030:
031: import java.lang.ref.WeakReference;
032: import java.util.ArrayList;
033:
034: /**
035: * A utility class for managing a collection of {@link StyleChangeListener} objects.
036: *
037: * @author Thomas Morgner.
038: */
039: public class StyleChangeSupport {
040: /**
041: * Storage for the listeners.
042: */
043: private ArrayList listeners;
044:
045: /**
046: * The source.
047: */
048: private final ElementStyleSheet source;
049:
050: /**
051: * Creates a new support object.
052: *
053: * @param source the source of change events.
054: */
055: public StyleChangeSupport(final ElementStyleSheet source) {
056: this .source = source;
057: }
058:
059: /**
060: * Adds a listener.
061: *
062: * @param l the listener.
063: */
064: public void addListener(final StyleChangeListener l) {
065: if (l == null) {
066: throw new NullPointerException("Listener == null");
067: }
068: if (listeners == null) {
069: listeners = new ArrayList(5);
070: }
071: listeners.add(new WeakReference(l));
072: }
073:
074: /**
075: * Removes a listener.
076: *
077: * @param l the listener.
078: */
079: public void removeListener(final StyleChangeListener l) {
080: if (l == null) {
081: throw new NullPointerException("Listener == null");
082: }
083: if (listeners == null) {
084: return;
085: }
086: listeners.remove(l);
087: }
088:
089: /**
090: * Notifies all listeners that a style has changed.
091: *
092: * @param key the style key.
093: * @param value the new style value.
094: */
095: public void fireStyleChanged(final StyleKey key, final Object value) {
096: if (listeners == null) {
097: return;
098: }
099: ArrayList removeList = null;
100:
101: for (int i = 0; i < listeners.size(); i++) {
102: final WeakReference ref = (WeakReference) listeners.get(i);
103: final StyleChangeListener l = (StyleChangeListener) ref
104: .get();
105: if (l != null) {
106: l.styleChanged(source, key, value);
107: } else {
108: if (removeList == null) {
109: removeList = new ArrayList(5);
110: }
111: removeList.add(ref);
112: }
113: }
114: if (removeList != null) {
115: listeners.removeAll(removeList);
116: }
117: }
118:
119: /**
120: * Notifies all listeners that a style has been removed.
121: *
122: * @param key the style key.
123: */
124: public void fireStyleRemoved(final StyleKey key) {
125: if (listeners == null) {
126: return;
127: }
128: ArrayList removeList = null;
129:
130: for (int i = 0; i < listeners.size(); i++) {
131: final WeakReference ref = (WeakReference) listeners.get(i);
132: final StyleChangeListener l = (StyleChangeListener) ref
133: .get();
134: if (l != null) {
135: l.styleRemoved(source, key);
136: } else {
137: if (removeList == null) {
138: removeList = new ArrayList(5);
139: }
140: removeList.add(ref);
141: }
142: }
143: if (removeList != null) {
144: listeners.removeAll(removeList);
145: }
146: }
147: }
|