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: KListModelTableAdapter.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
031package kiwi.ui.model;
032
033import java.util.*;
034import javax.swing.*;
035import javax.swing.event.*;
036import javax.swing.table.*;
037
038import kiwi.event.*;
039
040/** A model adapter that allows a <code>KListModel</code> to be used with a
041 * Swing <code>JTable</code> component. This adapter wraps a
042 * <code>KListModel</code> implementation and exposes a <code>TableModel</code>
043 * interface, and translates the corresponding model events.
044 *
045 * @author Mark Lindner
046 * @since Kiwi 2.0
047 */
048
049public class KListModelTableAdapter extends KListModelAdapter
050  implements TableModel
051  {
052  private static final String defaultColumnNames[] = { "Item" };
053  private String columnNames[] = defaultColumnNames;
054  private static final Class defaultColumnTypes[] = { String.class };
055  private Class columnTypes[] = defaultColumnTypes;
056  private boolean columnsAvailable = false;
057
058  /** Construct a new <code>KListModelTableAdapter</code>.
059   */
060
061  public KListModelTableAdapter()
062    {
063    }
064
065  /** Construct a new <code>KListModelTableAdapter</code> for the given
066   * list model. The <code>TableModel</code> will have a single column,
067   * marked "Item".
068   *
069   * @param model The <code>KListModel</code>.
070   */
071  
072  public KListModelTableAdapter(KListModel model)
073    {
074    setListModel(model);    
075    }
076
077  /*
078   */
079  
080  public void setListModel(KListModel model)
081    {
082    super.setListModel(model);
083
084    Object cols = null, types = null;
085
086    if(model != null)
087      {
088      cols = model.getValueForProperty(null, KListModel.COLUMN_NAMES_PROPERTY);
089      types = model.getValueForProperty(null,
090                                        KListModel.COLUMN_TYPES_PROPERTY);
091      }
092
093    if((cols != null) && (types != null))
094      {
095      columnNames = (String[])cols;
096      columnTypes = (Class[])types;
097      columnsAvailable = true;
098      }
099    else
100      {
101      columnNames = defaultColumnNames;
102      columnTypes = defaultColumnTypes;
103      columnsAvailable = false;
104      }
105    }
106  
107  /* Fire table events.
108   */
109
110  private void fireTableEvent(KListModelEvent evt, int type)
111    {
112    TableModelEvent tevt = null;
113
114    Enumeration e = listeners.elements();
115    while(e.hasMoreElements())
116      {
117      TableModelListener l = (TableModelListener)e.nextElement();
118      if(tevt == null)
119        tevt = new TableModelEvent(this, evt.getStartIndex(),
120                                   evt.getEndIndex(),
121                                   TableModelEvent.ALL_COLUMNS, type);
122
123      l.tableChanged(tevt);
124      }
125    }
126
127  /* implementation of KListModelListener */
128
129  /*
130   */
131  
132  public void itemsAdded(KListModelEvent evt)
133    {
134    fireTableEvent(evt, TableModelEvent.INSERT);
135    }
136
137  /*
138   */
139  
140  public void itemsChanged(KListModelEvent evt)
141    {
142    fireTableEvent(evt, TableModelEvent.UPDATE);
143    }
144
145  /*
146   */
147
148  public void itemsRemoved(KListModelEvent evt)
149    {
150    fireTableEvent(evt, TableModelEvent.DELETE);
151    }
152
153  /*
154   */
155
156  public void dataChanged(KListModelEvent evt)
157    {
158    fireModelChangedEvent();
159    }
160
161  /* implementation of ListModelAdapter */
162
163  protected void fireModelChangedEvent()
164    {
165    TableModelEvent evt = null;
166    Enumeration e = listeners.elements();
167    while(e.hasMoreElements())
168      {
169      if(evt == null)
170        evt = new TableModelEvent(this);
171      TableModelListener l = (TableModelListener)e.nextElement();
172      l.tableChanged(evt);
173      }
174    }
175  
176  /* implementation of TableModel */
177
178  /*
179   */
180
181  public Class getColumnClass(int col)
182    {
183    return(columnTypes[col]);
184    }
185
186  /*
187   */
188  
189  public String getColumnName(int col)
190    {
191    return(columnNames[col]);
192    }
193
194  /*
195   */
196  
197  public int getColumnCount()
198    {    
199    return(columnNames.length);
200    }
201
202  /*
203   */
204  
205  public int getRowCount()
206    {
207    if(model == null)
208      return(0);
209    
210    return(model.getItemCount());
211    }
212
213  /*
214   */
215  
216  public Object getValueAt(int row, int col)
217    {
218    if(model == null)
219      return(null);
220    
221    Object item = model.getItemAt(row);
222
223    if(columnsAvailable)
224      return(model.getValueForProperty(item, columnNames[col]));
225    else
226      return(model.getLabel(item));
227    }
228
229  /** This method is a no-op, since this adapter represents an immutable model.
230   */
231  
232  public void setValueAt(Object value, int row, int col)
233    {
234    /* no-op */
235    }
236
237  /** This method returns <code>false</code>, since this adapter represents an
238   * immutable model.
239   */
240  
241  public boolean isCellEditable(int row, int col)
242    {
243    return(false);
244    }
245
246  /*
247   */
248  
249  public void addTableModelListener(TableModelListener listener)
250    {
251    listeners.addElement(listener);
252    }
253
254  /*
255   */
256  
257  public void removeTableModelListener(TableModelListener listener)
258    {
259    listeners.removeElement(listener);
260    }
261  
262  }
263
264/* end of source file */