add DXCIDR, fix version no tracking
[spider.git] / SpiderConsole / src / LimitedStyledDocument.java
1 /**
2  * LimitedStyledDocument
3  * @author Ian Norton
4  * @version 1.0 20010418.
5  *
6  * Copyright (C) 2001 Ian Norton.
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Contacting the author :
23  * Ian Norton
24  * i.norton@lancaster.ac.uk
25  * http://www.lancs.ac.uk/~norton/
26  **/
27
28 import java.awt.* ;
29 import javax.swing.*; 
30 import javax.swing.text.*; 
31 import java.awt.Toolkit;
32 import java.util.* ;
33
34 public class LimitedStyledDocument extends DefaultStyledDocument 
35     {
36     int scrollbufferlines ;
37     SimpleAttributeSet attr ;
38     int num ;
39
40     public LimitedStyledDocument(int i) 
41         {
42         scrollbufferlines = i ;
43         attr = new SimpleAttributeSet() ;
44         num = 0 ;
45         }
46
47     /**
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.
52      **/
53     public void append(String s, AttributeSet a)
54         {
55         // Try and append the string to the document.
56         try
57             {
58             super.insertString(super.getLength(), s, a) ;
59             }
60         catch(BadLocationException ex)
61             {
62             }
63
64         StringTokenizer st = null ;
65
66         // Split the document into tokens delimited by '\n'.
67         try
68             {
69             // Need to do clever stuff here to chop the top off the buffer.
70             st = new StringTokenizer(super.getText(0, super.getLength()), "\n") ;
71             }
72         catch(BadLocationException ex)
73             {
74             }
75  
76         int i = 0;
77
78         // Are there more lines than there should be?
79         if(st.countTokens() > scrollbufferlines)
80             {
81             // How many lines too many?
82             i = st.countTokens() - scrollbufferlines ;
83             }
84  
85         // For each line too many
86         for(;i>0;i--)
87             {
88             String tmp = st.nextToken() ;
89
90             try
91                 {
92                 // Remove the line.
93                 super.remove(0, super.getText(0, super.getLength()).indexOf(tmp) + tmp.length()) ;
94                 }
95             catch(BadLocationException ex)
96                 {
97                 }
98             } // End of for(;i>0;i--)
99         } // End of append
100     } // End of class.