X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=SpiderConsole%2Fsrc%2FLimitedStyledDocument.java;fp=SpiderConsole%2Fsrc%2FLimitedStyledDocument.java;h=79aca45c21a022924ec6cc39da4dd5ecce38f50b;hb=1540d1ee26801546ad652653951bbc25744d85a0;hp=0000000000000000000000000000000000000000;hpb=5e145358734eabf8855fb2b4c1daabcc55bd9da0;p=spider.git diff --git a/SpiderConsole/src/LimitedStyledDocument.java b/SpiderConsole/src/LimitedStyledDocument.java new file mode 100644 index 00000000..79aca45c --- /dev/null +++ b/SpiderConsole/src/LimitedStyledDocument.java @@ -0,0 +1,100 @@ +/** + * LimitedStyledDocument + * @author Ian Norton + * @version 1.0 20010418. + * + * Copyright (C) 2001 Ian Norton. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public Licence as published by + * the Free Software Foundation; either version 2 of the Licence, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public Licence for more details. + * + * You should have received a copy of the GNU General Public Licence + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Contacting the author : + * Ian Norton + * i.norton@lancaster.ac.uk + * http://www.lancs.ac.uk/~norton/ + **/ + +import java.awt.* ; +import javax.swing.*; +import javax.swing.text.*; +import java.awt.Toolkit; +import java.util.* ; + +public class LimitedStyledDocument extends DefaultStyledDocument + { + int scrollbufferlines ; + SimpleAttributeSet attr ; + int num ; + + public LimitedStyledDocument(int i) + { + scrollbufferlines = i ; + attr = new SimpleAttributeSet() ; + num = 0 ; + } + + /** + * append - append a string to the end of the document keeping the + * number of lines in the document at or below maxscrollbuffer. + * @param String s - String to append. + * @param AttributeSet a - Attributes of the string to append. + **/ + public void append(String s, AttributeSet a) + { + // Try and append the string to the document. + try + { + super.insertString(super.getLength(), s, a) ; + } + catch(BadLocationException ex) + { + } + + StringTokenizer st = null ; + + // Split the document into tokens delimited by '\n'. + try + { + // Need to do clever stuff here to chop the top off the buffer. + st = new StringTokenizer(super.getText(0, super.getLength()), "\n") ; + } + catch(BadLocationException ex) + { + } + + int i = 0; + + // Are there more lines than there should be? + if(st.countTokens() > scrollbufferlines) + { + // How many lines too many? + i = st.countTokens() - scrollbufferlines ; + } + + // For each line too many + for(;i>0;i--) + { + String tmp = st.nextToken() ; + + try + { + // Remove the line. + super.remove(0, super.getText(0, super.getLength()).indexOf(tmp) + tmp.length()) ; + } + catch(BadLocationException ex) + { + } + } // End of for(;i>0;i--) + } // End of append + } // End of class.