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: DefaultKListModel.java,v $ 023 Revision 1.2 2004/05/31 07:30:26 markl 024 Final cleanup and bugfixes of kiwi.ui.model. 025 026 Revision 1.1 2004/05/13 21:45:58 markl 027 new class 028 ---------------------------------------------------------------------------- 029*/ 030 031package kiwi.ui.model; 032 033import java.util.*; 034import javax.swing.Icon; 035 036import kiwi.event.*; 037 038/** A default implementation of the <code>KListModel</code> interface. 039 * 040 * @author Mark Lindner 041 * @since Kiwi 2.0 042 */ 043 044public class DefaultKListModel implements KListModel 045 { 046 /** The actual data for this model. */ 047 protected ArrayList data; 048 /** The support object for firing <code>KListModelEvent</code>s. */ 049 protected KListModelSupport support; 050 051 /** Construct a new <code>KBasicListModel</code>. 052 */ 053 054 public DefaultKListModel() 055 { 056 support = new KListModelSupport(this); 057 data = new ArrayList(); 058 } 059 060 /* 061 */ 062 063 public boolean isEmpty() 064 { 065 return(data.size() == 0); 066 } 067 068 /* 069 */ 070 071 public void clear() 072 { 073 int index = data.size(); 074 075 if(index > 0) 076 { 077 data.clear(); 078 support.fireItemsRemoved(0, index - 1); 079 } 080 } 081 082 /* 083 */ 084 085 public int getItemCount() 086 { 087 return(data.size()); 088 } 089 090 /* 091 */ 092 093 public Iterator getItems() 094 { 095 return(data.iterator()); 096 } 097 098 /* 099 */ 100 101 public Object getItemAt(int index) 102 { 103 return(data.get(index)); 104 } 105 106 /* 107 */ 108 109 public int indexOf(Object item) 110 { 111 return(data.indexOf(item)); 112 } 113 114 /* 115 */ 116 117 public void addItem(Object item) 118 { 119 int index = data.size(); 120 121 data.add(item); 122 support.fireItemAdded(index); 123 } 124 125 /* 126 */ 127 128 public void insertItemAt(Object item, int index) 129 { 130 data.add(index, item); 131 support.fireItemAdded(index); 132 } 133 134 /* 135 */ 136 137 public void removeItemAt(int index) 138 { 139 if((index < 0) || (index >= data.size())) 140 return; 141 142 Object item = data.get(index); 143 data.remove(index); 144 145 support.fireItemRemoved(index); 146 } 147 148 /* 149 */ 150 151 public void removeItem(Object item) 152 { 153 int index = data.indexOf(item); 154 155 if(index >= 0) 156 { 157 data.remove(index); 158 support.fireItemRemoved(index); 159 } 160 } 161 162 /* 163 */ 164 165 public void updateItem(Object item) 166 { 167 int index = data.indexOf(item); 168 if(index >= 0) 169 support.fireItemChanged(index); 170 } 171 172 /* 173 */ 174 175 public void updateItemAt(int index) 176 { 177 if((index >= 0) && (index < data.size())) 178 support.fireItemChanged(index); 179 } 180 181 /* 182 */ 183 184 public void addListModelListener(KListModelListener listener) 185 { 186 support.addListModelListener(listener); 187 } 188 189 /* 190 */ 191 192 public void removeListModelListener(KListModelListener listener) 193 { 194 support.removeListModelListener(listener); 195 } 196 197 /* 198 */ 199 200 public String getLabel(Object item) 201 { 202 return(item == null ? null : item.toString()); 203 } 204 205 /* 206 */ 207 208 public Icon getIcon(Object item) 209 { 210 return(null); 211 } 212 213 /* 214 */ 215 216 public Object getValueForProperty(Object item, String property) 217 { 218 return(null); 219 } 220 221 /** Sort the model. 222 * 223 * @param comparator The <code>Comparator</code> to use for the sorting. 224 */ 225 226 public void sort(Comparator comparator) 227 { 228 Collections.sort(data, comparator); 229 support.fireDataChanged(); 230 } 231 232 } 233 234/* end of source file */