001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: KListModelSupport.java,v $ 023 Revision 1.1 2004/05/13 21:44:19 markl 024 new classes 025 026 ---------------------------------------------------------------------------- 027*/ 028 029package kiwi.event; 030 031import java.util.*; 032 033/** A support object for generating <code>KListModelEvent</code>s. 034 * 035 * @see kiwi.event.KListModelEvent 036 * @author Mark Lindner 037 * @since Kiwi 2.0 038 */ 039 040public class KListModelSupport 041 { 042 private Vector listeners; 043 private Object source; 044 045 /** Construct a new <code>KListModelSupport</code> object. 046 * 047 * @param source The owner of this object (and the source of the events that 048 * will be generated by it). 049 */ 050 051 public KListModelSupport(Object source) 052 { 053 listeners = new Vector(); 054 this.source = source; 055 } 056 057 /** Add a <code>KListModelListener</code> to this object's list of 058 * listeners. 059 * 060 * @param listener The listener to add. 061 */ 062 063 public void addListModelListener(KListModelListener listener) 064 { 065 listeners.addElement(listener); 066 } 067 068 /** Remove a <code>KListModelListener</code> from this object's list 069 * of listeners. 070 * 071 * @param listener The listener to remove. 072 */ 073 074 public void removeListModelListener(KListModelListener listener) 075 { 076 listeners.removeElement(listener); 077 } 078 079 /** Fire an <i>items added</i> event for a single item. 080 * 081 * @param index The offset at which this item will be inserted. 082 */ 083 084 public void fireItemAdded(int index) 085 { 086 fireItemsAdded(index, index); 087 } 088 089 /** Fire an <i>items added</i> event for a range of items. 090 * 091 * @param startIndex The offset of the first item in the range. 092 * @param endIndex The offset of the last item in the range. 093 */ 094 095 public void fireItemsAdded(int startIndex, int endIndex) 096 { 097 KListModelEvent evt = null; 098 Enumeration e = listeners.elements(); 099 while(e.hasMoreElements()) 100 { 101 KListModelListener l = (KListModelListener)e.nextElement(); 102 if(evt == null) 103 evt = new KListModelEvent(source, startIndex, endIndex); 104 l.itemsAdded(evt); 105 } 106 } 107 108 /** Fire an <i>items removed</i> event for a single item. 109 * 110 * @param index The offset of the item that was removed. 111 */ 112 113 public void fireItemRemoved(int index) 114 { 115 fireItemsRemoved(index, index); 116 } 117 118 /** Fire an <i>items removed</i> event for a range of items. 119 * 120 * @param startIndex The offset of the first item in the range. 121 * @param endIndex The offset of the last item in the range. 122 */ 123 124 public void fireItemsRemoved(int startIndex, int endIndex) 125 { 126 KListModelEvent evt = null; 127 Enumeration e = listeners.elements(); 128 while(e.hasMoreElements()) 129 { 130 KListModelListener l = (KListModelListener)e.nextElement(); 131 if(evt == null) 132 evt = new KListModelEvent(source, startIndex, endIndex); 133 l.itemsRemoved(evt); 134 } 135 } 136 137 /** Fire an <i>items changed</i> event for a single item. 138 * 139 * @param index The offset of the item that was removed. 140 */ 141 142 public void fireItemChanged(int index) 143 { 144 fireItemsChanged(index, index); 145 } 146 147 /** Fire an <i>items changed</i> event for a range of items. 148 * 149 * @param startIndex The offset of the first item in the range. 150 * @param endIndex The offset of the last item in the range. 151 */ 152 153 public void fireItemsChanged(int startIndex, int endIndex) 154 { 155 KListModelEvent evt = null; 156 Enumeration e = listeners.elements(); 157 while(e.hasMoreElements()) 158 { 159 KListModelListener l = (KListModelListener)e.nextElement(); 160 if(evt == null) 161 evt = new KListModelEvent(source, startIndex, endIndex); 162 l.itemsChanged(evt); 163 } 164 } 165 166 /** Fire a <i>data changed</i> event. 167 */ 168 169 public void fireDataChanged() 170 { 171 KListModelEvent evt = null; 172 Enumeration e = listeners.elements(); 173 while(e.hasMoreElements()) 174 { 175 KListModelListener l = (KListModelListener)e.nextElement(); 176 if(evt == null) 177 evt = new KListModelEvent(source); 178 l.dataChanged(evt); 179 } 180 } 181 182 } 183 184/* end of source file */