Skip to content

Instantly share code, notes, and snippets.

@seebs
Created July 30, 2015 00:24
Show Gist options
  • Save seebs/a2c9c78420b5b22189d0 to your computer and use it in GitHub Desktop.
Save seebs/a2c9c78420b5b22189d0 to your computer and use it in GitHub Desktop.
/*
* SK's Minecraft Launcher
* Copyright (C) 2010-2014 Albert Pham <http://www.sk89q.com> and contributors
* Please see LICENSE.txt for license information.
*/
package com.skcraft.launcher.util;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AbstractDocument.BranchElement;
/**
* From http://tips4java.wordpress.com/2008/10/15/limit-lines-in-document/
*
* @author Rob Camick
*/
public class LimitLinesDocumentListener implements DocumentListener {
private int maximumLines;
private boolean isRemoveFromStart;
private volatile int removing;
/**
* Specify the number of lines to be stored in the Document. Extra lines
* will be removed from the start or end of the Document, depending on
* the boolean value specified.
*
* @param maximumLines number of lines
* @param isRemoveFromStart true to remove from the start
*/
public LimitLinesDocumentListener(int maximumLines,
boolean isRemoveFromStart) {
setLimitLines(maximumLines);
this.isRemoveFromStart = isRemoveFromStart;
this.removing = 0;
}
/**
* Set the maximum number of lines to be stored in the Document
*
* @param maximumLines number of lines
*/
public void setLimitLines(int maximumLines) {
if (maximumLines < 1) {
throw new IllegalArgumentException("Maximum lines must be greater than 0");
}
this.maximumLines = maximumLines;
}
@Override
public void insertUpdate(final DocumentEvent e) {
// Changes to the Document can not be done within the listener
// so we need to add the processing to the end of the EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (!this.removing) {
removeLines(e);
this.removing = 1;
}
}
});
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
private void removeLines(DocumentEvent e) {
System.out.println(">removeLines");
// The root Element of the Document will tell us the total number
// of line in the Document.
Document document = e.getDocument();
BranchElement root = (BranchElement) document.getDefaultRootElement();
int elements = root.getElementCount();
int new_elements;
int excess = elements - maximumLines;
if (excess > 0) {
if (isRemoveFromStart) {
root.replace(0, excess, new Element[0]);
} else {
root.replace(elements - excess, excess, new Element[0]);
}
}
new_elements = root.getElementCount();
if (new_elements > maximumLines) {
System.out.printf("After removing lines (%d), still have %d/%d lines.\n", elements, new_elements, maximumLines);
}
this.removing = 0;
System.out.println("<removeLines");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment