-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLimitLinesDocumentListener.java
More file actions
145 lines (127 loc) · 3.43 KB
/
LimitLinesDocumentListener.java
File metadata and controls
145 lines (127 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
/*
* A class to control the maximum number of lines to be stored in a Document
*
* Excess lines can be removed from the start or end of the Document
* depending on your requirement.
*
* a) if you append text to the Document, then you would want to remove lines
* from the start.
* b) if you insert text at the beginning of the Document, then you would
* want to remove lines from the end.
*/
public class LimitLinesDocumentListener implements DocumentListener
{
private int maximumLines;
private boolean isRemoveFromStart;
/*
* Specify the number of lines to be stored in the Document.
* Extra lines will be removed from the start of the Document.
*/
public LimitLinesDocumentListener(int maximumLines)
{
this(maximumLines, true);
}
/*
* 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.
*/
public LimitLinesDocumentListener(int maximumLines, boolean isRemoveFromStart)
{
setLimitLines(maximumLines);
this.isRemoveFromStart = isRemoveFromStart;
}
/*
* Return the maximum number of lines to be stored in the Document
*/
public int getLimitLines()
{
return maximumLines;
}
/*
* Set the maximum number of lines to be stored in the Document
*/
public void setLimitLines(int maximumLines)
{
if (maximumLines < 1)
{
String message = "Maximum lines must be greater than 0";
throw new IllegalArgumentException(message);
}
this.maximumLines = maximumLines;
}
// Handle insertion of new text into the Document
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()
{
public void run()
{
removeLines(e);
}
});
}
public void removeUpdate(DocumentEvent e) {}
public void changedUpdate(DocumentEvent e) {}
/*
* Remove lines from the Document when necessary
*/
private void removeLines(DocumentEvent e)
{
// The root Element of the Document will tell us the total number
// of line in the Document.
Document document = e.getDocument();
Element root = document.getDefaultRootElement();
while (root.getElementCount() > maximumLines)
{
if (isRemoveFromStart)
{
removeFromStart(document, root);
}
else
{
removeFromEnd(document, root);
}
}
}
/*
* Remove lines from the start of the Document
*/
private void removeFromStart(Document document, Element root)
{
Element line = root.getElement(0);
int end = line.getEndOffset();
try
{
document.remove(0, end);
}
catch(BadLocationException ble)
{
System.out.println(ble);
}
}
/*
* Remove lines from the end of the Document
*/
private void removeFromEnd(Document document, Element root)
{
// We use start minus 1 to make sure we remove the newline
// character of the previous line
Element line = root.getElement(root.getElementCount() - 1);
int start = line.getStartOffset();
int end = line.getEndOffset();
try
{
document.remove(start - 1, end - start);
}
catch(BadLocationException ble)
{
System.out.println(ble);
}
}
}