001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029: package nextapp.echo2.app.list;
030:
031: import java.io.Serializable;
032: import java.util.EventListener;
033:
034: import nextapp.echo2.app.event.EventListenerList;
035: import nextapp.echo2.app.event.ListDataEvent;
036: import nextapp.echo2.app.event.ListDataListener;
037:
038: /**
039: * A base class from which <code>ListModel</code> implementations may be
040: * derived. This class provides event listener management facilities.
041: */
042: public abstract class AbstractListModel implements ListModel,
043: Serializable {
044:
045: /**
046: * A storage facility for <code>EventListener</code>s.
047: */
048: private EventListenerList listenerList = new EventListenerList();
049:
050: /**
051: * Creates a new AbstractListModel.
052: */
053: public AbstractListModel() {
054: super ();
055: }
056:
057: /**
058: * @see nextapp.echo2.app.list.ListModel#addListDataListener(nextapp.echo2.app.event.ListDataListener)
059: */
060: public void addListDataListener(ListDataListener l) {
061: listenerList.addListener(ListDataListener.class, l);
062: }
063:
064: /**
065: * Returns the <code>EventListenerList</code> being used to manage event
066: * listeners.
067: *
068: * @return the listener list
069: */
070: protected EventListenerList getEventListenerList() {
071: return listenerList;
072: }
073:
074: /**
075: * Notifies listeners that the contents of the list have changed.
076: * Subclasses <strong>must</strong> call this method
077: * after one or elements are changed.
078: *
079: * @param index0 the index of the first changed item
080: * @param index1 the index of the last changed item
081: */
082: protected void fireContentsChanged(int index0, int index1) {
083: ListDataEvent e = new ListDataEvent(this ,
084: ListDataEvent.CONTENTS_CHANGED, index0, index1);
085:
086: EventListener[] listeners = listenerList
087: .getListeners(ListDataListener.class);
088: for (int index = 0; index < listeners.length; ++index) {
089: ((ListDataListener) listeners[index]).contentsChanged(e);
090: }
091: }
092:
093: /**
094: * Notifies listeners that an interval of items was added.
095: * Subclasses <strong>must</strong> call this method
096: * after one or elements are added.
097: *
098: * @param index0 the index of the first added item
099: * @param index1 the index of the last added item
100: */
101: protected void fireIntervalAdded(int index0, int index1) {
102: ListDataEvent e = new ListDataEvent(this ,
103: ListDataEvent.INTERVAL_ADDED, index0, index1);
104:
105: EventListener[] listeners = listenerList
106: .getListeners(ListDataListener.class);
107: for (int index = 0; index < listeners.length; ++index) {
108: ((ListDataListener) listeners[index]).intervalAdded(e);
109: }
110: }
111:
112: /**
113: * Notifies listeners that an interval of items was removed.
114: * Subclasses <strong>must</strong> call this method
115: * after one or elements are removed.
116: *
117: * @param index0 the index of the first removed index
118: * @param index1 the index of the last removed index
119: */
120: protected void fireIntervalRemoved(int index0, int index1) {
121: ListDataEvent e = new ListDataEvent(this ,
122: ListDataEvent.INTERVAL_REMOVED, index0, index1);
123:
124: EventListener[] listeners = listenerList
125: .getListeners(ListDataListener.class);
126: for (int index = 0; index < listeners.length; ++index) {
127: ((ListDataListener) listeners[index]).intervalRemoved(e);
128: }
129: }
130:
131: /**
132: * @see nextapp.echo2.app.list.ListModel#removeListDataListener(nextapp.echo2.app.event.ListDataListener)
133: */
134: public void removeListDataListener(ListDataListener l) {
135: listenerList.removeListener(ListDataListener.class, l);
136: }
137: }
|