package com.knutejohnson.pi.chat;

import javax.swing.text.*;

public class SelfLimitingDocument extends PlainDocument {
    private final int limit;

    public SelfLimitingDocument(int limit) {
        this.limit = limit;
    }

    @Override public void insertString(int offs, String str, AttributeSet a)
     throws BadLocationException {
        super.insertString(offs,str,a);

        // if the document is over the limiting size, remove 5% from the
        //  beginning of the document
        if (getLength() > limit)
            remove(0,limit/20);
    }
}