package jas2.swingstudio;

import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

public class JASConsoleDocumentUtilities {
	private JASConsoleDocumentUtilities() {}//no instances allowed

	public static final String	consoleBufferLengthKey = "Console Buffer Length";
	public static final String	consoleBufferTrimLengthKey = "Console Buffer Trim Length";
	public static final int		MAX_DOC_CHARS = 10000;
	public static final int		NUM_REMOVE_CHARS = 1000;
	

	/**
	 * clear the contents of a Document
	 */
	public static void clearDoc(Document d) {
		try {
			d.remove(0,d.getLength());
		}
		catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	/**
	 * prevent a Document from getting so huge that performance degrades
	 */
	public static void trimDoc(Document d) {
		if (d.getLength() > MAX_DOC_CHARS) {
			try {
				d.remove(0,NUM_REMOVE_CHARS);
			} catch (BadLocationException e1) {
				try {
					d.remove(0,d.getLength());
				} catch (BadLocationException e2) {
					e2.printStackTrace();
				}
			}
		}
	}
}

 

Ads help maintain this website.