29
29
import java .awt .Component ;
30
30
import java .awt .Dimension ;
31
31
import java .awt .Graphics ;
32
- import java .awt .Insets ;
32
+ import java .awt .Graphics2D ;
33
33
import java .awt .Rectangle ;
34
+ import java .awt .Shape ;
34
35
import java .awt .event .MouseAdapter ;
35
36
import java .awt .event .MouseEvent ;
36
37
import java .beans .PropertyChangeEvent ;
42
43
import javax .swing .JTable ;
43
44
import javax .swing .JTextArea ;
44
45
import javax .swing .JViewport ;
46
+ import javax .swing .SwingUtilities ;
45
47
import javax .swing .event .CaretEvent ;
46
48
import javax .swing .event .CaretListener ;
47
- import javax .swing .plaf .TextUI ;
48
49
import javax .swing .table .AbstractTableModel ;
49
50
import javax .swing .table .DefaultTableCellRenderer ;
50
51
import javax .swing .table .TableModel ;
51
52
import javax .swing .text .BadLocationException ;
52
53
import javax .swing .text .DefaultHighlighter ;
53
- import javax .swing .text .Highlighter ;
54
54
import javax .swing .text .JTextComponent ;
55
+ import javax .swing .text .View ;
56
+ import org .openide .util .Exceptions ;
55
57
56
58
/**
57
59
*
@@ -67,7 +69,7 @@ final class InternalSourceViewerComponent extends JPanel implements PropertyChan
67
69
private final JPanel lineNumbersPanel ;
68
70
69
71
70
- public InternalSourceViewerComponent (String text , int offset , InternalSourceAppearance appearance ) {
72
+ public InternalSourceViewerComponent (String text , int offset , int endOffset , InternalSourceAppearance appearance ) {
71
73
super (new BorderLayout ());
72
74
73
75
sourceArea = new SourceArea ();
@@ -77,7 +79,7 @@ public InternalSourceViewerComponent(String text, int offset, InternalSourceAppe
77
79
propertyChange (null );
78
80
79
81
sourceArea .setText (text );
80
- sourceArea . setOffset (offset );
82
+ setOffset (offset , endOffset );
81
83
82
84
lineNumbersPanel = new JPanel (new BorderLayout ());
83
85
lineNumbersPanel .setBorder (BorderFactory .createEmptyBorder (0 , 15 , 0 , 10 ));
@@ -109,8 +111,12 @@ public void propertyChange(PropertyChangeEvent evt) {
109
111
}
110
112
111
113
112
- void setOffset (int offset ) {
114
+ void setOffset (final int offset , final int endOffset ) {
113
115
sourceArea .setOffset (offset );
116
+
117
+ SwingUtilities .invokeLater (new Runnable () {
118
+ public void run () { sourceArea .select (offset , endOffset ); }
119
+ });
114
120
}
115
121
116
122
@@ -129,13 +135,17 @@ private static class SourceArea extends JTextArea implements CaretListener {
129
135
130
136
private int pendingOffset = -1 ;
131
137
138
+ private Object rowHighlight ;
139
+ private final LineHighlightPainter highlightPainter ;
140
+
141
+
132
142
SourceArea () {
133
143
super ();
134
144
135
145
setEditable (false );
136
146
setBorder (BorderFactory .createEmptyBorder (0 , 10 , 0 , 5 ));
137
147
138
- setHighlighter ( new LineHighlighter () );
148
+ highlightPainter = new LineHighlightPainter ( );
139
149
addCaretListener (this );
140
150
141
151
MouseAdapter adapter = new MouseAdapter () {
@@ -185,6 +195,11 @@ public void validate() {
185
195
pendingOffset = -1 ;
186
196
}
187
197
}
198
+
199
+ public void requestFocus () {
200
+ super .requestFocus ();
201
+ setHighlight (getCaretPosition ());
202
+ }
188
203
189
204
190
205
@ Override
@@ -193,19 +208,24 @@ public void caretUpdate(CaretEvent e) {
193
208
}
194
209
195
210
196
- public void setHighlight (int dot ) {
197
- getHighlighter ().removeAllHighlights ();
198
- int currentLine = getLineFromOffset (this , dot );
199
- int startPos = getLineStartOffsetForLine (this , currentLine );
200
- int endOffset = getLineEndOffsetForLine (this , currentLine );
211
+ public void setHighlight (final int dot ) {
212
+ SwingUtilities .invokeLater (new Runnable () {
213
+ public void run () {
214
+ if (rowHighlight != null ) getHighlighter ().removeHighlight (rowHighlight );
215
+
216
+ int currentLine = getLineFromOffset (SourceArea .this , dot );
217
+ int startOffset = getLineStartOffsetForLine (SourceArea .this , currentLine );
218
+ int endOffset = getLineEndOffsetForLine (SourceArea .this , currentLine );
219
+
220
+ try {
221
+ rowHighlight = getHighlighter ().addHighlight (startOffset , endOffset , highlightPainter );
222
+ } catch (BadLocationException ex ) {
223
+ Exceptions .printStackTrace (ex );
224
+ }
201
225
202
- try {
203
- getHighlighter ().addHighlight (startPos , endOffset , new DefaultHighlighter .DefaultHighlightPainter (new Color (233 , 239 , 248 )));
204
- // getHighlighter().addHighlight(startPos, endOffset, new CustomHighlightPainter());
205
- } catch (Exception ex ) {
206
- ex .printStackTrace ();
207
- }
208
- repaint ();
226
+ repaint ();
227
+ }
228
+ });
209
229
}
210
230
211
231
public int getLineFromOffset (JTextComponent component , int offset ) {
@@ -220,136 +240,31 @@ public int getLineEndOffsetForLine(JTextComponent component, int line) {
220
240
return component .getDocument ().getDefaultRootElement ().getElement (line ).getEndOffset ();
221
241
}
222
242
223
- static class LineHighlighter extends DefaultHighlighter {
224
- private JTextComponent component ;
225
-
226
- @ Override
227
- public final void install (final JTextComponent c ) {
228
- super .install (c );
229
- this .component = c ;
230
- }
231
-
232
- @ Override
233
- public final void deinstall (final JTextComponent c ) {
234
- super .deinstall (c );
235
- this .component = null ;
243
+
244
+ static final class LineHighlightPainter extends DefaultHighlighter .DefaultHighlightPainter {
245
+
246
+ LineHighlightPainter () {
247
+ super (new Color (233 , 239 , 248 ));
236
248
}
237
-
238
- @ Override
239
- public final void paint (final Graphics g ) {
240
- final Highlighter .Highlight [] highlights = getHighlights ();
241
- final int len = highlights .length ;
242
- for (int i = 0 ; i < len ; i ++) {
243
- Highlighter .Highlight info = highlights [i ];
244
- if (info .getClass ().getName ().contains ("LayeredHighlightInfo" )) { // NOI18N
245
-
246
- final Rectangle a = this .component .getBounds ();
247
- final Insets insets = this .component .getInsets ();
248
- a .x = insets .left ;
249
- a .y = insets .top ;
250
-
251
- a .height -= insets .top + insets .bottom ;
252
- final Highlighter .HighlightPainter p = info .getPainter ();
253
- p .paint (g , info .getStartOffset (), info .getEndOffset (), a , this .component );
254
-
255
- Rectangle alloc = a ;
256
- try {
257
- TextUI mapper = this .component .getUI ();
258
- Rectangle p0 = mapper .modelToView (this .component , this .component .getSelectionStart ());
259
- Rectangle p1 = mapper .modelToView (this .component , this .component .getSelectionEnd ());
260
-
261
- g .setColor (this .component .getSelectionColor ());
262
- if (p0 .y == p1 .y ) {
263
- Rectangle r = p0 .union (p1 );
264
- g .fillRect (r .x , r .y , r .width , r .height );
265
- } else {
266
- int p0ToMarginWidth = alloc .x + alloc .width - p0 .x ;
267
- g .fillRect (p0 .x , p0 .y , p0ToMarginWidth , p0 .height );
268
- if ((p0 .y + p0 .height ) != p1 .y ) g .fillRect (alloc .x , p0 .y + p0 .height , alloc .width , p1 .y - (p0 .y + p0 .height ));
269
- g .fillRect (alloc .x , p1 .y , (p1 .x - alloc .x ), p1 .height );
270
- }
271
- } catch (BadLocationException e ) {
272
- }
273
- }
249
+
250
+ public Shape paintLayer (Graphics g , int offs0 , int offs1 ,
251
+ Shape bounds , JTextComponent c , View view ) {
252
+ try {
253
+ Rectangle r = c .modelToView (offs0 );
254
+ r .x = 0 ;
255
+ r .width = c .getWidth ();
256
+
257
+ g .setColor (getColor ());
258
+ ((Graphics2D )g ).fill (r );
259
+
260
+ return r ;
261
+ } catch (BadLocationException ex ) {
262
+ return null ;
274
263
}
275
264
}
276
265
277
- @ Override
278
- public void removeAllHighlights () {
279
- if (component != null ) component .repaint (0 , 0 , component .getWidth (), component .getHeight ());
280
- super .removeAllHighlights ();
281
- }
282
266
}
283
267
284
- // static final class CustomHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
285
- //
286
- // CustomHighlightPainter() {
287
- // super(Color.ORANGE);
288
- //// super(new Color(233, 239, 248));
289
- // }
290
- //
291
- // public void paint(Graphics g, int offs0, int offs1,
292
- // Shape bounds, JTextComponent c) {
293
- //
294
- // int selStart = c.getSelectionStart();
295
- // int selEnd = c.getSelectionEnd();
296
- //
297
- // // No selection or selection fully outside of the highlight
298
- // if (selEnd - selStart == 0 || offs0 >= selEnd || offs1 <= selStart) super.paint(g, offs0, offs1, bounds, c);
299
- //
300
- // // Selection fully covers the highlight
301
- // if (offs0 >= selStart && offs1 <= selEnd) return;
302
- //
303
- // // Selection partially covers the highlight
304
- // if (offs0 < selStart || offs1 > selEnd) {
305
- // // Selection ends inside of the highlight
306
- // if (offs0 >= selStart) super.paint(g, selEnd, offs1, bounds, c);
307
- // // Selection starts inside of the highlight
308
- // else if (offs1 <= selEnd) super.paint(g, offs0, selStart, bounds, c);
309
- //
310
- // // Selection fully inside of the highlight
311
- // super.paint(g, offs0, selStart, bounds, c);
312
- // super.paint(g, selEnd, offs1, bounds, c);
313
- // }
314
- //
315
- //
316
- // Rectangle alloc = bounds.getBounds();
317
- // try {
318
- // // --- determine locations ---
319
- // TextUI mapper = c.getUI();
320
- // Rectangle p0 = mapper.modelToView(c, offs0);
321
- // Rectangle p1 = mapper.modelToView(c, offs1);
322
- //
323
- // // --- render ---
324
- // Color color = getColor();
325
- //
326
- // if (color == null) {
327
- // g.setColor(c.getSelectionColor());
328
- // }
329
- // else {
330
- // g.setColor(color);
331
- // }
332
- // if (p0.y == p1.y) {
333
- // // same line, render a rectangle
334
- // Rectangle r = p0.union(p1);
335
- // g.fillRect(r.x, r.y, r.width, r.height);
336
- // } else {
337
- // // different lines
338
- // int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
339
- // g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
340
- // if ((p0.y + p0.height) != p1.y) {
341
- // g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
342
- // p1.y - (p0.y + p0.height));
343
- // }
344
- // g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
345
- // }
346
- // } catch (BadLocationException e) {
347
- // // can't render
348
- // }
349
- // }
350
- //
351
- // }
352
-
353
268
}
354
269
355
270
private static class LineNumbers extends JTable {
0 commit comments