|
@@ -22,7 +22,11 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.ui.swing.textpane;
|
24
|
24
|
|
|
25
|
+import com.dmdirc.util.RollingList;
|
|
26
|
+
|
25
|
27
|
import java.io.Serializable;
|
|
28
|
+import java.text.AttributedCharacterIterator;
|
|
29
|
+import java.text.AttributedString;
|
26
|
30
|
import java.util.ArrayList;
|
27
|
31
|
import java.util.List;
|
28
|
32
|
|
|
@@ -32,26 +36,31 @@ import javax.swing.event.EventListenerList;
|
32
|
36
|
* Data contained in a TextPane.
|
33
|
37
|
*/
|
34
|
38
|
public final class IRCDocument implements Serializable {
|
35
|
|
-
|
|
39
|
+
|
36
|
40
|
/**
|
37
|
41
|
* A version number for this class. It should be changed whenever the class
|
38
|
42
|
* structure is changed (or anything else that would prevent serialized
|
39
|
43
|
* objects being unserialized with the new class).
|
40
|
44
|
*/
|
41
|
45
|
private static final long serialVersionUID = 4;
|
42
|
|
-
|
43
|
46
|
/** List of lines of text. */
|
44
|
47
|
private final List<Line> lines;
|
45
|
|
-
|
46
|
48
|
/** Listener list. */
|
47
|
49
|
private final EventListenerList listeners;
|
48
|
|
-
|
|
50
|
+ /** Cached lines. */
|
|
51
|
+ private RollingList<Line> cachedLines;
|
|
52
|
+ /** Cached attributed strings. */
|
|
53
|
+ private RollingList<AttributedString> cachedStrings;
|
|
54
|
+
|
49
|
55
|
/** Creates a new instance of IRCDocument. */
|
50
|
56
|
public IRCDocument() {
|
51
|
57
|
lines = new ArrayList<Line>();
|
52
|
58
|
listeners = new EventListenerList();
|
|
59
|
+
|
|
60
|
+ cachedLines = new RollingList<Line>(50);
|
|
61
|
+ cachedStrings = new RollingList<AttributedString>(50);
|
53
|
62
|
}
|
54
|
|
-
|
|
63
|
+
|
55
|
64
|
/**
|
56
|
65
|
* Returns the number of lines in this document.
|
57
|
66
|
*
|
|
@@ -60,7 +69,7 @@ public final class IRCDocument implements Serializable {
|
60
|
69
|
public int getNumLines() {
|
61
|
70
|
return lines.size();
|
62
|
71
|
}
|
63
|
|
-
|
|
72
|
+
|
64
|
73
|
/**
|
65
|
74
|
* Returns the Line at the specified number.
|
66
|
75
|
*
|
|
@@ -70,8 +79,8 @@ public final class IRCDocument implements Serializable {
|
70
|
79
|
*/
|
71
|
80
|
public Line getLine(final int lineNumber) {
|
72
|
81
|
return lines.get(lineNumber);
|
73
|
|
- }
|
74
|
|
-
|
|
82
|
+ }
|
|
83
|
+
|
75
|
84
|
/**
|
76
|
85
|
* Adds the stylised string to the canvas.
|
77
|
86
|
*
|
|
@@ -83,7 +92,7 @@ public final class IRCDocument implements Serializable {
|
83
|
92
|
fireLineAdded(lines.indexOf(text));
|
84
|
93
|
}
|
85
|
94
|
}
|
86
|
|
-
|
|
95
|
+
|
87
|
96
|
/**
|
88
|
97
|
* Adds the stylised string to the canvas.
|
89
|
98
|
*
|
|
@@ -98,7 +107,7 @@ public final class IRCDocument implements Serializable {
|
98
|
107
|
fireLinesAdded(start, text.size());
|
99
|
108
|
}
|
100
|
109
|
}
|
101
|
|
-
|
|
110
|
+
|
102
|
111
|
/**
|
103
|
112
|
* Trims the document to the specified number of lines.
|
104
|
113
|
*
|
|
@@ -112,7 +121,7 @@ public final class IRCDocument implements Serializable {
|
112
|
121
|
fireTrimmed();
|
113
|
122
|
}
|
114
|
123
|
}
|
115
|
|
-
|
|
124
|
+
|
116
|
125
|
/** Clears all lines from the document. */
|
117
|
126
|
protected void clear() {
|
118
|
127
|
synchronized (lines) {
|
|
@@ -120,7 +129,7 @@ public final class IRCDocument implements Serializable {
|
120
|
129
|
fireCleared();
|
121
|
130
|
}
|
122
|
131
|
}
|
123
|
|
-
|
|
132
|
+
|
124
|
133
|
/**
|
125
|
134
|
* Adds a IRCDocumentListener to the listener list.
|
126
|
135
|
*
|
|
@@ -134,7 +143,7 @@ public final class IRCDocument implements Serializable {
|
134
|
143
|
listeners.add(IRCDocumentListener.class, listener);
|
135
|
144
|
}
|
136
|
145
|
}
|
137
|
|
-
|
|
146
|
+
|
138
|
147
|
/**
|
139
|
148
|
* Removes a IRCDocumentListener from the listener list.
|
140
|
149
|
*
|
|
@@ -143,7 +152,7 @@ public final class IRCDocument implements Serializable {
|
143
|
152
|
public void removeIRCDocumentListener(final IRCDocumentListener listener) {
|
144
|
153
|
listeners.remove(IRCDocumentListener.class, listener);
|
145
|
154
|
}
|
146
|
|
-
|
|
155
|
+
|
147
|
156
|
/**
|
148
|
157
|
* Fires the line added method on all listeners.
|
149
|
158
|
*
|
|
@@ -153,11 +162,12 @@ public final class IRCDocument implements Serializable {
|
153
|
162
|
final Object[] listenerList = listeners.getListenerList();
|
154
|
163
|
for (int i = 0; i < listenerList.length; i += 2) {
|
155
|
164
|
if (listenerList[i] == IRCDocumentListener.class) {
|
156
|
|
- ((IRCDocumentListener) listenerList[i + 1]).lineAdded(index, lines.size());
|
|
165
|
+ ((IRCDocumentListener) listenerList[i + 1]).lineAdded(index,
|
|
166
|
+ lines.size());
|
157
|
167
|
}
|
158
|
168
|
}
|
159
|
169
|
}
|
160
|
|
-
|
|
170
|
+
|
161
|
171
|
/**
|
162
|
172
|
* Fires the lines added method on all listeners.
|
163
|
173
|
*
|
|
@@ -168,11 +178,14 @@ public final class IRCDocument implements Serializable {
|
168
|
178
|
final Object[] listenerList = listeners.getListenerList();
|
169
|
179
|
for (int i = 0; i < listenerList.length; i += 2) {
|
170
|
180
|
if (listenerList[i] == IRCDocumentListener.class) {
|
171
|
|
- ((IRCDocumentListener) listenerList[i + 1]).linesAdded(index, size, lines.size());
|
|
181
|
+ ((IRCDocumentListener) listenerList[i + 1]).linesAdded(index,
|
|
182
|
+ size,
|
|
183
|
+ lines.
|
|
184
|
+ size());
|
172
|
185
|
}
|
173
|
186
|
}
|
174
|
187
|
}
|
175
|
|
-
|
|
188
|
+
|
176
|
189
|
/**
|
177
|
190
|
* Fires the trimmed method on all listeners.
|
178
|
191
|
*/
|
|
@@ -184,7 +197,7 @@ public final class IRCDocument implements Serializable {
|
184
|
197
|
}
|
185
|
198
|
}
|
186
|
199
|
}
|
187
|
|
-
|
|
200
|
+
|
188
|
201
|
/**
|
189
|
202
|
* fires the cleared method on all listeners.
|
190
|
203
|
*/
|
|
@@ -196,5 +209,65 @@ public final class IRCDocument implements Serializable {
|
196
|
209
|
}
|
197
|
210
|
}
|
198
|
211
|
}
|
|
212
|
+
|
|
213
|
+ /**
|
|
214
|
+ * Returns an attributed string for a particular line, utilising the
|
|
215
|
+ * document cache where possible.
|
|
216
|
+ *
|
|
217
|
+ * @param line Line to be styled
|
|
218
|
+ *
|
|
219
|
+ * @return Styled line
|
|
220
|
+ */
|
|
221
|
+ public AttributedString getStyledString(final Line line) {
|
|
222
|
+ AttributedString styledLine = null;
|
|
223
|
+ if (cachedLines.contains(line)) {
|
|
224
|
+ final int index = cachedLines.getList().indexOf(line);
|
|
225
|
+ styledLine = cachedStrings.get(index);
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ if (styledLine == null) {
|
|
229
|
+ styledLine = line.getStyled();
|
|
230
|
+ cachedLines.add(line);
|
|
231
|
+ cachedStrings.add(styledLine);
|
|
232
|
+ }
|
|
233
|
+
|
|
234
|
+ return styledLine;
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ /**
|
|
238
|
+ * Returns an attributed string for a particular line, utilising the
|
|
239
|
+ * document cache where possible.
|
|
240
|
+ *
|
|
241
|
+ * @param line Line number to be styled
|
|
242
|
+ *
|
|
243
|
+ * @return Styled line
|
|
244
|
+ */
|
|
245
|
+ public AttributedString getStyledString(final int line) {
|
|
246
|
+ return getStyledString(getLine(line));
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+ /**
|
|
250
|
+ * Returns an attributed character iterator for a particular line,
|
|
251
|
+ * utilising the document cache where possible.
|
|
252
|
+ *
|
|
253
|
+ * @param line Line to be styled
|
|
254
|
+ *
|
|
255
|
+ * @return Styled line iterator
|
|
256
|
+ */
|
|
257
|
+ public AttributedCharacterIterator getStyledIterator(final Line line) {
|
|
258
|
+ return getStyledString(line).getIterator();
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ /**
|
|
262
|
+ * Returns an attributed character iterator for a particular line,
|
|
263
|
+ * utilising the document cache where possible.
|
|
264
|
+ *
|
|
265
|
+ * @param line Line number to be styled
|
|
266
|
+ *
|
|
267
|
+ * @return Styled line iterator
|
|
268
|
+ */
|
|
269
|
+ public AttributedCharacterIterator getStyledIterator(final int line) {
|
|
270
|
+ return getStyledString(line).getIterator();
|
|
271
|
+ }
|
199
|
272
|
}
|
200
|
273
|
|