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: ExternalKListModel.java,v $
023   Revision 1.1  2004/05/31 07:30:26  markl
024   Final cleanup and bugfixes of kiwi.ui.model.
025
026   ----------------------------------------------------------------------------
027*/
028
029package kiwi.ui.model;
030
031import javax.swing.Icon;
032
033/** An implementation of <code>KListModel</code> that obtains its data from
034 * an external data source.
035 *
036 * @author Mark Lindner
037 * @since Kiwi 2.0
038 */
039
040public class ExternalKListModel extends DefaultKListModel
041  {
042  /** The data source for this model. */
043  
044  protected ListDataSource source = null;
045
046  /** Construct a new <code>ExternalKListModel</code> with the given
047   * data source.
048   *
049   * @param source The data source.
050   */
051  
052  public ExternalKListModel(ListDataSource source)
053    {
054    super();
055
056    this.source = source;
057
058    reload();
059    }
060
061  /** Reload the list model from the data source.
062   */
063
064  public void reload()
065    {
066    if(source == null)
067      return;
068
069    data.clear();
070    Object items[] = source.getItems();
071    if(items != null)
072      for(int i = 0; i < items.length; i++)
073        data.add(items[i]);
074
075    support.fireDataChanged();
076    }
077
078  /**
079   */
080
081  public void addItem(Object item)
082    {
083    throw(new ImmutableModelException());
084    }
085
086  /**
087   */
088  
089  public void insertItemAt(Object item, int index)
090    {
091    throw(new ImmutableModelException());
092    }
093
094  /**
095   */
096  
097  public void removeItemAt(int index)
098    {
099    throw(new ImmutableModelException());    
100    }
101
102  /**
103   */
104  
105  public void removeItem(Object item)
106    {
107    throw(new ImmutableModelException());
108    }
109
110  /**
111   */
112  
113  public void updateItem(Object item)
114    {
115    throw(new ImmutableModelException());
116    }
117
118  /**
119   */
120  
121  public void updateItemAt(int index)
122    {
123    throw(new ImmutableModelException());
124    }
125
126  /**
127   */
128  
129  public Object getValueForProperty(Object item, String property)
130    {
131    return(source.getValueForProperty(item, property));
132    }
133
134  /** Get the label for an item.
135   *
136   * @param item The item.
137   * @return A string label for the item.
138   */
139
140  public String getLabel(Object item)
141    {
142    String label = source.getLabel(item);
143    
144    return(label == null ? item.toString() : label);
145    }
146
147  /** Get the icon for an item.
148   *
149   * @param item The item.
150   * @return An icon for the item.
151   */
152
153  public Icon getIcon(Object item)
154    {
155    return(source.getIcon(item));
156    }
157  
158  }
159
160/* end of source file */