|
@@ -97,6 +97,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
97
|
97
|
textLayouts = new HashMap<TextLayout, LineInfo>();
|
98
|
98
|
positions = new HashMap<Rectangle, TextLayout>();
|
99
|
99
|
lineWrap = new HashMap<Integer, Integer>();
|
|
100
|
+ selection = new LinePosition(-1, -1, -1, -1);
|
100
|
101
|
addMouseListener(this);
|
101
|
102
|
addMouseMotionListener(this);
|
102
|
103
|
addComponentListener(this);
|
|
@@ -122,10 +123,6 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
122
|
123
|
float drawPosY = formatHeight;
|
123
|
124
|
int startLine = scrollBarPosition;
|
124
|
125
|
|
125
|
|
- int useStartLine;
|
126
|
|
- int useStartChar;
|
127
|
|
- int useEndLine;
|
128
|
|
- int useEndChar;
|
129
|
126
|
int paragraphStart;
|
130
|
127
|
int paragraphEnd;
|
131
|
128
|
LineBreakMeasurer lineMeasurer;
|
|
@@ -136,14 +133,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
136
|
133
|
textLayouts.clear();
|
137
|
134
|
positions.clear();
|
138
|
135
|
|
139
|
|
- //check theres something to draw
|
140
|
|
- if (document.getNumLines() == 0) {
|
141
|
|
- setCursor(Cursor.getDefaultCursor());
|
142
|
|
- return;
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
- //check there is some space to draw in
|
146
|
|
- if (formatWidth < 1) {
|
|
136
|
+ //check theres something to draw and theres some space to draw in
|
|
137
|
+ if (document.getNumLines() == 0 || formatWidth < 1) {
|
147
|
138
|
setCursor(Cursor.getDefaultCursor());
|
148
|
139
|
return;
|
149
|
140
|
}
|
|
@@ -161,34 +152,10 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
161
|
152
|
lastVisibleLine = startLine;
|
162
|
153
|
firstVisibleLine = startLine;
|
163
|
154
|
|
164
|
|
- if (selection.getStartLine() > selection.getEndLine()) {
|
165
|
|
- // Swap both
|
166
|
|
- useStartLine = selection.getEndLine();
|
167
|
|
- useStartChar = selection.getEndPos();
|
168
|
|
- useEndLine = selection.getStartLine();
|
169
|
|
- useEndChar = selection.getStartPos();
|
170
|
|
- }
|
171
|
|
- else if (selection.getStartLine() == selection.getEndLine() &&
|
172
|
|
- selection.getStartPos() > selection.getEndPos()) {
|
173
|
|
- // Just swap the chars
|
174
|
|
- useStartLine = selection.getStartLine();
|
175
|
|
- useStartChar = selection.getEndPos();
|
176
|
|
- useEndLine = selection.getEndLine();
|
177
|
|
- useEndChar = selection.getStartPos();
|
178
|
|
- }
|
179
|
|
- else {
|
180
|
|
- // Swap nothing
|
181
|
|
- useStartLine = selection.getStartLine();
|
182
|
|
- useStartChar = selection.getStartPos();
|
183
|
|
- useEndLine = selection.getEndLine();
|
184
|
|
- useEndChar = selection.getEndPos();
|
185
|
|
- }
|
186
|
|
-
|
187
|
155
|
// Iterate through the lines
|
188
|
156
|
for (int i = startLine; i >= 0; i--) {
|
189
|
157
|
float drawPosX;
|
190
|
|
- final AttributedCharacterIterator iterator = document.
|
191
|
|
- getStyledIterator(i);
|
|
158
|
+ final AttributedCharacterIterator iterator = document.getStyledLine(i);
|
192
|
159
|
paragraphStart = iterator.getBeginIndex();
|
193
|
160
|
paragraphEnd = iterator.getEndIndex();
|
194
|
161
|
lineMeasurer = new LineBreakMeasurer(iterator,
|
|
@@ -201,8 +168,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
201
|
168
|
if (lineWrap.containsKey(i)) {
|
202
|
169
|
//use it
|
203
|
170
|
wrappedLine = lineWrap.get(i);
|
204
|
|
- }
|
205
|
|
- else {
|
|
171
|
+ } else {
|
206
|
172
|
//get it and populate the cache
|
207
|
173
|
wrappedLine = getNumWrappedLines(lineMeasurer,
|
208
|
174
|
paragraphStart, paragraphEnd,
|
|
@@ -223,16 +189,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
223
|
189
|
// Calculate the Y offset
|
224
|
190
|
if (wrappedLine == 1) {
|
225
|
191
|
drawPosY -= lineHeight;
|
226
|
|
- }
|
227
|
|
- else if (j != 0) {
|
|
192
|
+ } else if (j != 0) {
|
228
|
193
|
drawPosY += lineHeight;
|
229
|
194
|
}
|
230
|
195
|
|
231
|
196
|
// Calculate the initial X position
|
232
|
197
|
if (layout.isLeftToRight()) {
|
233
|
198
|
drawPosX = 3;
|
234
|
|
- }
|
235
|
|
- else {
|
|
199
|
+ } else {
|
236
|
200
|
drawPosX = formatWidth - layout.getAdvance();
|
237
|
201
|
}
|
238
|
202
|
|
|
@@ -242,8 +206,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
242
|
206
|
g.setColor(textPane.getForeground());
|
243
|
207
|
|
244
|
208
|
layout.draw(g, drawPosX, drawPosY + lineHeight / 2f);
|
245
|
|
- doHighlight(i, useStartLine, useEndLine, useStartChar,
|
246
|
|
- useEndChar, chars, layout, g, drawPosY, drawPosX);
|
|
209
|
+ doHighlight(i, chars, layout, g, drawPosY, drawPosX);
|
247
|
210
|
firstVisibleLine = i;
|
248
|
211
|
textLayouts.put(layout, new LineInfo(i, j));
|
249
|
212
|
positions.put(new Rectangle(0, (int) drawPosY,
|
|
@@ -295,22 +258,41 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
295
|
258
|
* Redraws the text that has been highlighted.
|
296
|
259
|
*
|
297
|
260
|
* @param line Line number
|
298
|
|
- * @param startLine Selection start line
|
299
|
|
- * @param endLine Selection end line
|
300
|
|
- * @param startChar Selection start char
|
301
|
|
- * @param endChar Selection end char
|
302
|
261
|
* @param chars Number of characters so far in the line
|
303
|
262
|
* @param layout Current line textlayout
|
304
|
263
|
* @param g Graphics surface to draw highlight on
|
305
|
264
|
* @param drawPosY current y location of the line
|
306
|
265
|
* @param drawPosX current x location of the line
|
307
|
266
|
*/
|
308
|
|
- private void doHighlight(final int line, final int startLine,
|
309
|
|
- final int endLine, final int startChar,
|
310
|
|
- final int endChar,
|
311
|
|
- final int chars, final TextLayout layout,
|
312
|
|
- final Graphics2D g,
|
|
267
|
+ private void doHighlight(final int line, final int chars,
|
|
268
|
+ final TextLayout layout, final Graphics2D g,
|
313
|
269
|
final float drawPosY, final float drawPosX) {
|
|
270
|
+ int startLine;
|
|
271
|
+ int startChar;
|
|
272
|
+ int endLine;
|
|
273
|
+ int endChar;
|
|
274
|
+
|
|
275
|
+ if (selection.getStartLine() > selection.getEndLine()) {
|
|
276
|
+ // Swap both
|
|
277
|
+ startLine = selection.getEndLine();
|
|
278
|
+ startChar = selection.getEndPos();
|
|
279
|
+ endLine = selection.getStartLine();
|
|
280
|
+ endChar = selection.getStartPos();
|
|
281
|
+ } else if (selection.getStartLine() == selection.getEndLine() &&
|
|
282
|
+ selection.getStartPos() > selection.getEndPos()) {
|
|
283
|
+ // Just swap the chars
|
|
284
|
+ startLine = selection.getStartLine();
|
|
285
|
+ startChar = selection.getEndPos();
|
|
286
|
+ endLine = selection.getEndLine();
|
|
287
|
+ endChar = selection.getStartPos();
|
|
288
|
+ } else {
|
|
289
|
+ // Swap nothing
|
|
290
|
+ startLine = selection.getStartLine();
|
|
291
|
+ startChar = selection.getStartPos();
|
|
292
|
+ endLine = selection.getEndLine();
|
|
293
|
+ endChar = selection.getEndPos();
|
|
294
|
+ }
|
|
295
|
+
|
314
|
296
|
//Does this line need highlighting?
|
315
|
297
|
if (startLine <= line && endLine >= line) {
|
316
|
298
|
int firstChar;
|
|
@@ -319,16 +301,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
319
|
301
|
// Determine the first char we care about
|
320
|
302
|
if (startLine < line || startChar < chars) {
|
321
|
303
|
firstChar = chars;
|
322
|
|
- }
|
323
|
|
- else {
|
|
304
|
+ } else {
|
324
|
305
|
firstChar = startChar;
|
325
|
306
|
}
|
326
|
307
|
|
327
|
308
|
// ... And the last
|
328
|
309
|
if (endLine > line || endChar > chars + layout.getCharacterCount()) {
|
329
|
310
|
lastChar = chars + layout.getCharacterCount();
|
330
|
|
- }
|
331
|
|
- else {
|
|
311
|
+ } else {
|
332
|
312
|
lastChar = endChar;
|
333
|
313
|
}
|
334
|
314
|
|
|
@@ -346,7 +326,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
346
|
326
|
|
347
|
327
|
final int trans = (int) (lineHeight / 2f + drawPosY);
|
348
|
328
|
final AttributedCharacterIterator iterator = document.
|
349
|
|
- getStyledIterator(line);
|
|
329
|
+ getStyledLine(line);
|
350
|
330
|
final AttributedString as = new AttributedString(iterator,
|
351
|
331
|
firstChar,
|
352
|
332
|
lastChar);
|
|
@@ -416,8 +396,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
416
|
396
|
if (lineInfo.getIndex() == -1) {
|
417
|
397
|
start = -1;
|
418
|
398
|
end = -1;
|
419
|
|
- }
|
420
|
|
- else {
|
|
399
|
+ } else {
|
421
|
400
|
final int[] extent =
|
422
|
401
|
getSurroundingWordIndexes(clickedText,
|
423
|
402
|
lineInfo.getIndex());
|
|
@@ -430,8 +409,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
430
|
409
|
selection.setEndLine(lineInfo.getLine());
|
431
|
410
|
selection.setStartPos(start);
|
432
|
411
|
selection.setEndPos(end);
|
433
|
|
- }
|
434
|
|
- else if (e.getClickCount() == 3) {
|
|
412
|
+ } else if (e.getClickCount() == 3) {
|
435
|
413
|
selection.setStartLine(lineInfo.getLine());
|
436
|
414
|
selection.setEndLine(lineInfo.getLine());
|
437
|
415
|
selection.setStartPos(0);
|
|
@@ -452,8 +430,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
452
|
430
|
*/
|
453
|
431
|
public ClickType getClickType(final LineInfo lineInfo) {
|
454
|
432
|
if (lineInfo.getLine() != -1) {
|
455
|
|
- final AttributedCharacterIterator iterator = document.
|
456
|
|
- getStyledIterator(lineInfo.getLine());
|
|
433
|
+ final AttributedCharacterIterator iterator = document.getStyledLine(
|
|
434
|
+ lineInfo.getLine());
|
457
|
435
|
iterator.setIndex(lineInfo.getIndex());
|
458
|
436
|
Object linkattr =
|
459
|
437
|
iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
|
|
@@ -481,8 +459,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
481
|
459
|
*/
|
482
|
460
|
public Object getAttributeValueAtPoint(LineInfo lineInfo) {
|
483
|
461
|
if (lineInfo.getLine() != -1) {
|
484
|
|
- final AttributedCharacterIterator iterator = document.
|
485
|
|
- getStyledIterator(lineInfo.getLine());
|
|
462
|
+ final AttributedCharacterIterator iterator = document.getStyledLine(
|
|
463
|
+ lineInfo.getLine());
|
486
|
464
|
iterator.setIndex(lineInfo.getIndex());
|
487
|
465
|
Object linkattr =
|
488
|
466
|
iterator.getAttributes().get(IRCTextAttribute.HYPERLINK);
|
|
@@ -645,8 +623,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
645
|
623
|
|
646
|
624
|
if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine()) !=
|
647
|
625
|
null) {
|
648
|
|
- final AttributedCharacterIterator iterator = document.
|
649
|
|
- getStyledIterator(lineInfo.getLine());
|
|
626
|
+ final AttributedCharacterIterator iterator = document.getStyledLine(
|
|
627
|
+ lineInfo.getLine());
|
650
|
628
|
if (lineInfo.getIndex() < iterator.getBeginIndex() ||
|
651
|
629
|
lineInfo.getIndex() > iterator.getEndIndex()) {
|
652
|
630
|
return;
|
|
@@ -690,16 +668,14 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
690
|
668
|
final Point mousePos = e.getPoint();
|
691
|
669
|
if (mousePos.getX() < bounds.getX()) {
|
692
|
670
|
point.setLocation(bounds.getX() + 3, point.getY());
|
693
|
|
- }
|
694
|
|
- else if (mousePos.getX() > (bounds.getX() + bounds.getWidth())) {
|
|
671
|
+ } else if (mousePos.getX() > (bounds.getX() + bounds.getWidth())) {
|
695
|
672
|
point.setLocation(bounds.getX() + bounds.getWidth() - 3,
|
696
|
673
|
point.getY());
|
697
|
674
|
}
|
698
|
675
|
if (mousePos.getY() < bounds.getY()) {
|
699
|
676
|
point.setLocation(point.getX(), bounds.getY() + 6);
|
700
|
|
- }
|
701
|
|
- else if (mousePos.getY() >
|
702
|
|
- (bounds.getY() + bounds.getHeight())) {
|
|
677
|
+ } else if (mousePos.getY() >
|
|
678
|
+ (bounds.getY() + bounds.getHeight())) {
|
703
|
679
|
//Nice text selection behaviour
|
704
|
680
|
//point.setLocation(point.getX(), bounds.getY() +
|
705
|
681
|
// bounds.getHeight() - 6);
|
|
@@ -776,9 +752,8 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
776
|
752
|
if (textLayouts.get(entry.getValue()).getLine() == lineNumber) {
|
777
|
753
|
if (textLayouts.get(entry.getValue()).getPart() < linePart) {
|
778
|
754
|
pos += entry.getValue().getCharacterCount();
|
779
|
|
- }
|
780
|
|
- else if (textLayouts.get(entry.getValue()).getPart() ==
|
781
|
|
- linePart) {
|
|
755
|
+ } else if (textLayouts.get(entry.getValue()).getPart() ==
|
|
756
|
+ linePart) {
|
782
|
757
|
final TextHitInfo hit = entry.getValue().hitTestChar(x - 6,
|
783
|
758
|
y);
|
784
|
759
|
pos += hit.getInsertionIndex();
|
|
@@ -801,15 +776,13 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
|
801
|
776
|
selection.getEndPos(), selection.
|
802
|
777
|
getStartLine(),
|
803
|
778
|
selection.getStartPos());
|
804
|
|
- }
|
805
|
|
- else if (selection.getStartLine() == selection.getEndLine() &&
|
806
|
|
- selection.getStartPos() > selection.getEndPos()) {
|
|
779
|
+ } else if (selection.getStartLine() == selection.getEndLine() &&
|
|
780
|
+ selection.getStartPos() > selection.getEndPos()) {
|
807
|
781
|
// Just swap the chars
|
808
|
782
|
return new LinePosition(selection.getStartLine(), selection.
|
809
|
783
|
getEndPos(), selection.getEndLine(),
|
810
|
784
|
selection.getStartPos());
|
811
|
|
- }
|
812
|
|
- else {
|
|
785
|
+ } else {
|
813
|
786
|
// Swap nothing
|
814
|
787
|
return new LinePosition(selection.getStartLine(), selection.
|
815
|
788
|
getStartPos(), selection.getEndLine(),
|