1 | /*
|
---|
2 | * 09/13/2005
|
---|
3 | *
|
---|
4 | * SquiggleUnderlineHighlightPainter.java - Highlighter that draws a squiggle
|
---|
5 | * underline under "highlighted" text, similar to error markers in Microsoft
|
---|
6 | * Visual Studio or Eclipse.
|
---|
7 | *
|
---|
8 | * This library is distributed under a modified BSD license. See the included
|
---|
9 | * RSyntaxTextArea.License.txt file for details.
|
---|
10 | */
|
---|
11 | package org.fife.ui.rsyntaxtextarea;
|
---|
12 |
|
---|
13 | import java.awt.Color;
|
---|
14 | import java.awt.Graphics;
|
---|
15 | import java.awt.Rectangle;
|
---|
16 | import java.awt.Shape;
|
---|
17 | import javax.swing.text.BadLocationException;
|
---|
18 | import javax.swing.text.JTextComponent;
|
---|
19 | import javax.swing.text.Position;
|
---|
20 | import javax.swing.text.View;
|
---|
21 |
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Highlight painter that paints a squiggly underline underneath text, similar
|
---|
25 | * to what popular IDE's such as Visual Studio and Eclipse do to indicate
|
---|
26 | * errors, warnings, etc.<p>
|
---|
27 | *
|
---|
28 | * This class must be used as a <code>LayerPainter</code>.
|
---|
29 | *
|
---|
30 | * @author Robert Futrell
|
---|
31 | * @version 1.0
|
---|
32 | */
|
---|
33 | public class SquiggleUnderlineHighlightPainter
|
---|
34 | extends ChangeableColorHighlightPainter {
|
---|
35 |
|
---|
36 | private static final int AMT = 2;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Constructor.
|
---|
40 | *
|
---|
41 | * @param color The color of the squiggle. This cannot be
|
---|
42 | * <code>null</code>.
|
---|
43 | */
|
---|
44 | public SquiggleUnderlineHighlightPainter(Color color) {
|
---|
45 | super(color);
|
---|
46 | setColor(color);
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Paints a portion of a highlight.
|
---|
52 | *
|
---|
53 | * @param g the graphics context
|
---|
54 | * @param offs0 the starting model offset >= 0
|
---|
55 | * @param offs1 the ending model offset >= offs1
|
---|
56 | * @param bounds the bounding box of the view, which is not
|
---|
57 | * necessarily the region to paint.
|
---|
58 | * @param c the editor
|
---|
59 | * @param view View painting for
|
---|
60 | * @return region drawing occurred in
|
---|
61 | */
|
---|
62 | public Shape paintLayer(Graphics g, int offs0, int offs1,
|
---|
63 | Shape bounds, JTextComponent c, View view) {
|
---|
64 |
|
---|
65 | g.setColor(getColor());
|
---|
66 |
|
---|
67 | if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
|
---|
68 | // Contained in view, can just use bounds.
|
---|
69 | Rectangle alloc;
|
---|
70 | if (bounds instanceof Rectangle)
|
---|
71 | alloc = (Rectangle)bounds;
|
---|
72 | else
|
---|
73 | alloc = bounds.getBounds();
|
---|
74 | paintSquiggle(g, alloc);
|
---|
75 | return alloc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Otherwise, should only render part of View.
|
---|
79 | try {
|
---|
80 | // --- determine locations ---
|
---|
81 | Shape shape = view.modelToView(offs0, Position.Bias.Forward,
|
---|
82 | offs1,Position.Bias.Backward,
|
---|
83 | bounds);
|
---|
84 | Rectangle r = (shape instanceof Rectangle) ?
|
---|
85 | (Rectangle)shape : shape.getBounds();
|
---|
86 | paintSquiggle(g, r);
|
---|
87 | return r;
|
---|
88 | } catch (BadLocationException e) {
|
---|
89 | e.printStackTrace(); // can't render
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Only if exception
|
---|
93 | return null;
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Paints a squiggle underneath text in the specified rectangle.
|
---|
100 | *
|
---|
101 | * @param g The graphics context with which to paint.
|
---|
102 | * @param r The rectangle containing the text.
|
---|
103 | */
|
---|
104 | protected void paintSquiggle(Graphics g, Rectangle r) {
|
---|
105 | int x = r.x;
|
---|
106 | int y = r.y + r.height - 1;
|
---|
107 | int delta = -AMT;
|
---|
108 | while (x<r.x+r.width) {
|
---|
109 | g.drawLine(x,y, x+AMT,y+delta);
|
---|
110 | y += delta;
|
---|
111 | delta = -delta;
|
---|
112 | x += AMT;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | } |
---|