2 * LimitedStyledDocument
4 * @version 1.0 20010418.
6 * Copyright (C) 2001 Ian Norton.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public Licence as published by
10 * the Free Software Foundation; either version 2 of the Licence, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public Licence for more details.
18 * You should have received a copy of the GNU General Public Licence
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Contacting the author :
24 * i.norton@lancaster.ac.uk
25 * http://www.lancs.ac.uk/~norton/
30 import javax.swing.text.*;
31 import java.awt.Toolkit;
34 public class LimitedStyledDocument extends DefaultStyledDocument
36 int scrollbufferlines ;
37 SimpleAttributeSet attr ;
40 public LimitedStyledDocument(int i)
42 scrollbufferlines = i ;
43 attr = new SimpleAttributeSet() ;
48 * append - append a string to the end of the document keeping the
49 * number of lines in the document at or below maxscrollbuffer.
50 * @param String s - String to append.
51 * @param AttributeSet a - Attributes of the string to append.
53 public void append(String s, AttributeSet a)
55 // Try and append the string to the document.
58 super.insertString(super.getLength(), s, a) ;
60 catch(BadLocationException ex)
64 StringTokenizer st = null ;
66 // Split the document into tokens delimited by '\n'.
69 // Need to do clever stuff here to chop the top off the buffer.
70 st = new StringTokenizer(super.getText(0, super.getLength()), "\n") ;
72 catch(BadLocationException ex)
78 // Are there more lines than there should be?
79 if(st.countTokens() > scrollbufferlines)
81 // How many lines too many?
82 i = st.countTokens() - scrollbufferlines ;
85 // For each line too many
88 String tmp = st.nextToken() ;
93 super.remove(0, super.getText(0, super.getLength()).indexOf(tmp) + tmp.length()) ;
95 catch(BadLocationException ex)
98 } // End of for(;i>0;i--)