1 package ca.uhn.hl7v2.testpanel.ui.editor;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Rectangle;
6 import java.awt.Shape;
7
8 import javax.swing.text.BadLocationException;
9 import javax.swing.text.DefaultHighlighter;
10 import javax.swing.text.Highlighter;
11 import javax.swing.text.JTextComponent;
12 import javax.swing.text.LayeredHighlighter;
13 import javax.swing.text.Position;
14 import javax.swing.text.View;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class UnderlineHighlighter extends DefaultHighlighter {
20 protected static final Highlighter.HighlightPainter sharedPainter = new UnderlineHighlightPainter();
21
22 private static final Logger ourLog = LoggerFactory.getLogger(UnderlineHighlighter.class);
23
24 public UnderlineHighlighter() {
25 setDrawsLayeredHighlights(true);
26 }
27
28
29
30
31 @Override
32 public void paint(Graphics theG) {
33 ourLog.debug("Repainting");
34 super.paint(theG);
35 }
36
37
38
39
40 @Override
41 public void paintLayeredHighlights(Graphics theG, int theP0, int theP1, Shape theViewBounds, JTextComponent theEditor, View theView) {
42 ourLog.debug("Paintlayeredhighlights");
43 super.paintLayeredHighlights(theG, theP0, theP1, theViewBounds, theEditor, theView);
44 }
45
46 @Override
47 public Object addHighlight(int theP0, int theP1, HighlightPainter theP) throws BadLocationException {
48 ourLog.debug("Adding highlight");
49 return addHighlight(theP0, theP1);
50 }
51
52 public Object addHighlight(int p0, int p1) throws BadLocationException {
53 return super.addHighlight(p0, p1, sharedPainter);
54 }
55
56 public void setDrawsLayeredHighlights(boolean newValue) {
57 if (newValue == false) {
58 throw new IllegalArgumentException("UnderlineHighlighter only draws layered highlights");
59 }
60 super.setDrawsLayeredHighlights(true);
61 }
62
63
64 public static class UnderlineHighlightPainter extends LayeredHighlighter.LayerPainter {
65 protected Color lineColour = new Color(0.2f, 0.6f, 0.2f);
66 protected Color fillColour = new Color(0.75f, 1.0f, 0.75f);
67
68 public UnderlineHighlightPainter() {
69 }
70
71 public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
72 System.out.println("Paint called");
73 }
74
75 public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
76
77 ourLog.debug("Repainting");
78
79 Rectangle alloc = null;
80 if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
81 if (bounds instanceof Rectangle) {
82 alloc = (Rectangle) bounds;
83 } else {
84 alloc = bounds.getBounds();
85 }
86 } else {
87 try {
88 Shape shape = view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds);
89 alloc = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
90 } catch (BadLocationException e) {
91 return null;
92 }
93 }
94
95
96
97
98
99
100
101 g.setColor(fillColour);
102 g.fillRect(alloc.x + 1, alloc.y + 1, alloc.width - 1, alloc.height - 1);
103
104 if (!c.hasFocus()) {
105 int y = alloc.y + alloc.height - 2;
106
107 ourLog.debug("Repainting highlight at {}, {}", alloc.x, y);
108
109 g.setColor(lineColour);
110
111
112
113 g.drawLine(alloc.x, y, alloc.x + alloc.width, y);
114 y++;
115 g.drawLine(alloc.x, y, alloc.x + alloc.width, y);
116 }
117
118 return alloc;
119 }
120 }
121 }