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: ScrollbackView.java,v $
023   Revision 1.10  2004/05/12 18:23:08  markl
024   comment block update
025
026   Revision 1.9  2004/01/23 00:03:58  markl
027   javadoc corrections
028
029   Revision 1.8  2003/02/06 07:41:37  markl
030   corrected array bounds exception
031
032   Revision 1.7  2003/01/19 09:50:53  markl
033   Javadoc & comment header updates.
034
035   Revision 1.6  2001/08/31 22:09:07  markl
036   Completed implementation of FastListModel
037
038   Revision 1.5  2001/06/26 06:18:45  markl
039   Added (currently unused) FastListModel.
040
041   Revision 1.4  2001/03/12 09:27:59  markl
042   Source code and Javadoc cleanup.
043
044   Revision 1.3  2000/10/11 10:52:52  markl
045   Added mutator.
046
047   Revision 1.2  1999/01/10 02:56:27  markl
048   added GPL header & RCS tag
049   ----------------------------------------------------------------------------
050*/
051
052package kiwi.ui;
053
054import java.awt.Color;
055import java.util.*;
056import javax.swing.*;
057
058import kiwi.util.ListConsumer;
059
060/** This class represents a visual <i>scrollback</i> buffer. Lines of text may
061  * be added to the buffer, which always displays the most recently-added
062  * lines. Older lines may be viewed by scrolling back to them using the
063  * scrollbar.
064  * <p>
065  * Once the buffer is full, the oldest lines are discarded as new lines
066  * are added.
067  *
068  * @author Mark Lindner
069  */
070
071public class ScrollbackView extends JList implements ListConsumer
072  {
073  private int saveLines = 100;
074  private FastListModel model;
075  private boolean autoScrollsOnAdd = true;
076
077  /** Construct a new <code>ScrollBackView</code>.
078    */
079
080  public ScrollbackView()
081    {
082    model = new FastListModel();
083    setModel(model);
084    }
085
086  /** Clear the scrollback. Removes all rows from the buffer. */
087
088  public void clear()
089    {
090    model.removeAllElements();
091    }
092
093  /** Set the buffer size.
094   *
095   * @param lines The maximum number of lines to save in the buffer.
096   * @see #getSaveLines
097    */
098
099  public void setSaveLines(int lines)
100    {
101    if(lines > 0)
102      saveLines = lines;
103    }
104
105  /** Get the buffer size.
106    *
107    * @return The maximum number of lines that this buffer will save.
108    * @see #setSaveLines
109    */
110
111  public int getSaveLines()
112    {
113    return(saveLines);
114    }
115
116  /** Add an item to the buffer. Adds the specified item to the end of the
117    * buffer. If the buffer was full, the oldest line is discarded. If
118    * necessary, the buffer is scrolled to make the new item visible.
119    *
120    * @param item The item to add.
121    */
122
123  public void addItem(Object item)
124    {
125    if(model.getSize() >= saveLines)
126      model.removeRange(0, (model.getSize() - saveLines));
127    model.addElement(item);
128    if(autoScrollsOnAdd)
129      ensureIndexIsVisible(model.getSize() - 1);
130    }
131
132  /**
133   */
134  
135  public void addItems(Vector items)
136    {
137    int t = model.getSize() + items.size();
138    
139    if(t > saveLines)
140      model.removeRange(0, t - saveLines);
141    model.addElements(items);
142
143    if(autoScrollsOnAdd)
144      ensureIndexIsVisible(model.getSize() - 1);
145    }
146
147  /** Specify whether the buffer should automatically scroll to the bottom
148   * when a new item is added.
149   *
150   * @param flag A flag specifying whether autoscrolling should be enabled or
151   * disabled.
152   */
153
154  public void setAutoScrollsOnAdd(boolean flag)
155    {
156    autoScrollsOnAdd = flag;
157    }
158
159  /**
160   */
161
162  private class FastListModel extends AbstractListModel
163    {
164    private Vector data = new Vector();
165
166
167    public int getSize()
168      {
169      return(data.size());
170      }
171    
172    public void addElement(Object o)
173      {
174      int x = data.size();
175
176      data.addElement(o);
177      fireIntervalAdded(this, x, x);
178      }
179    
180    public void addElements(Vector v)
181      {
182      int sz = v.size();
183      int x = data.size();
184      for(int i = 0; i < sz; i++)
185        data.addElement(v.elementAt(i));
186          
187      fireIntervalAdded(this,  x, x + sz - 1);
188      }
189
190    public void removeRange(int fromIndex, int toIndex)
191      {
192      for(int i = toIndex; i >= fromIndex; i--)
193        data.removeElementAt(fromIndex);
194
195      fireIntervalRemoved(this, fromIndex, toIndex);
196      }
197
198    public void removeAllElements()
199      {
200      int sz = data.size();
201      data.removeAllElements();
202
203      if(sz > 0)
204        fireIntervalRemoved(this, 0, --sz);
205      }
206
207    public Object getElementAt(int index)
208      {
209      return(data.elementAt(index));
210      }
211
212    }
213
214  }
215
216/* end of source file */