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: KListModelListAdapter.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:40:21 markl 027 new classes 028 029 ---------------------------------------------------------------------------- 030*/ 031 032package kiwi.ui.model; 033 034import java.util.*; 035import javax.swing.*; 036import javax.swing.event.*; 037 038import kiwi.event.*; 039import kiwi.ui.*; 040 041/** A model adapter that allows a <code>KListModel</code> to be used with a 042 * Swing <code>JList</code> component. This adapter wraps a 043 * <code>KListModel</code> implementation and exposes a 044 * <code>ListModel</code> interface, and translates the corresponding 045 * model events. 046 * 047 * @author Mark Lindner 048 * @since Kiwi 2.0 049 */ 050 051public class KListModelListAdapter extends KListModelAdapter 052 implements ListModel 053 { 054 protected KListModelListCellRenderer renderer; 055 056 /** Construct a new <code>KListModelListAdapter</code>. */ 057 058 protected KListModelListAdapter() 059 { 060 renderer = new KListModelListCellRenderer(); 061 } 062 063 /** Construct a new <code>KListModelListAdapter</code> for the given 064 * <code>JList</code>. 065 * 066 * @param jlist The <code>JList</code> that will be used with this 067 * adapter. 068 */ 069 070 public KListModelListAdapter(JList jlist) 071 { 072 this(); 073 jlist.setCellRenderer(renderer); 074 } 075 076 /* 077 */ 078 079 /* implementation of KListModelAdapter */ 080 081 protected void fireModelChangedEvent() 082 { 083 ListDataEvent evt = null; 084 Enumeration e = listeners.elements(); 085 int ct = 0; 086 087 if(model != null) 088 { 089 ct = model.getItemCount(); 090 if(ct > 0) 091 ct--; 092 } 093 094 while(e.hasMoreElements()) 095 { 096 if(evt == null) 097 evt = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, ct); 098 099 ListDataListener l = (ListDataListener)e.nextElement(); 100 l.contentsChanged(evt); 101 } 102 } 103 104 /* 105 */ 106 107 public void setListModel(KListModel model) 108 { 109 super.setListModel(model); 110 111 renderer.setModel(model); 112 } 113 114 /* implementation of KListModelListener */ 115 116 /* 117 */ 118 119 public void itemsAdded(KListModelEvent evt) 120 { 121 ListDataEvent levt = null; 122 123 Enumeration e = listeners.elements(); 124 while(e.hasMoreElements()) 125 { 126 ListDataListener l = (ListDataListener)e.nextElement(); 127 if(levt == null) 128 levt = new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, 129 evt.getStartIndex(), evt.getEndIndex()); 130 131 l.intervalAdded(levt); 132 } 133 } 134 135 /* 136 */ 137 138 public void itemsChanged(KListModelEvent evt) 139 { 140 ListDataEvent levt = null; 141 142 Enumeration e = listeners.elements(); 143 while(e.hasMoreElements()) 144 { 145 ListDataListener l = (ListDataListener)e.nextElement(); 146 if(levt == null) 147 levt = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 148 evt.getStartIndex(), evt.getEndIndex()); 149 150 l.contentsChanged(levt); 151 } 152 } 153 154 /* 155 */ 156 157 public void itemsRemoved(KListModelEvent evt) 158 { 159 ListDataEvent levt = null; 160 161 Enumeration e = listeners.elements(); 162 while(e.hasMoreElements()) 163 { 164 ListDataListener l = (ListDataListener)e.nextElement(); 165 if(levt == null) 166 levt = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 167 evt.getStartIndex(), evt.getEndIndex()); 168 169 l.intervalRemoved(levt); 170 } 171 } 172 173 /* 174 */ 175 176 public void dataChanged(KListModelEvent evt) 177 { 178 fireModelChangedEvent(); 179 } 180 181 /* implementation of ListModel */ 182 183 /* 184 */ 185 186 public int getSize() 187 { 188 if(model == null) 189 return(0); 190 191 return(model.getItemCount()); 192 } 193 194 /* 195 */ 196 197 public Object getElementAt(int index) 198 { 199 if(model == null) 200 return(null); 201 202 return(model.getItemAt(index)); 203 } 204 205 } 206 207/* end of source file */ 208