01 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
02 *
03 * The main author is Santhosh Kumar T. Visit his great home page under:
04 * http://www.jroller.com/santhosh/
05 *
06 * The original source for this file is:
07 * http://www.jroller.com/santhosh/entry/document_guard
08 *
09 * It is distributed under the GNU Lesser General Public License:
10 * http://www.gnu.org/copyleft/lesser.html
11 */
12
13 package org.qedeq.gui.se.util;
14
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Graphics;
18 import java.awt.Graphics2D;
19 import java.awt.Rectangle;
20 import java.awt.Shape;
21
22 import javax.swing.plaf.TextUI;
23 import javax.swing.text.BadLocationException;
24 import javax.swing.text.Highlighter;
25 import javax.swing.text.JTextComponent;
26
27 /**
28 * Paint marked text blocks in underlined. Precondition is an installed
29 * {@link org.qedeq.gui.se.util.CurrentLineHighlighterUtility} for that text component.
30 *
31 * @author Michael Meyling
32 */
33 public class UnderlineDocumentMarkerPainter implements Highlighter.HighlightPainter {
34
35 /** Background color. */
36 private Color color;
37
38 /**
39 * Constructor.
40 *
41 * @param color Underline color.
42 */
43 public UnderlineDocumentMarkerPainter(final Color color) {
44 this.color = color;
45 }
46
47 public void paint(final Graphics g, final int p0, final int p1, final Shape bounds,
48 final JTextComponent c) {
49 specialPaint(g, p0 - 1, p1 + 1, bounds, c); // trick
50 }
51
52 private void specialPaint(final Graphics g, final int offs0, final int offs1,
53 final Shape bounds, final JTextComponent c) {
54 final Rectangle alloc = bounds.getBounds();
55 try {
56 // --- determine locations ---
57 TextUI mapper = c.getUI();
58 Rectangle p0 = mapper.modelToView(c, offs0);
59 Rectangle p1 = mapper.modelToView(c, offs1);
60
61 // --- render ---
62 g.setColor(color);
63 float[] dashPattern = {3, 1, 3, 1 };
64 ((Graphics2D) g).setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT,
65 BasicStroke.JOIN_MITER, 1,
66 dashPattern, 0));
67 ((Graphics2D) g).setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT,
68 BasicStroke.JOIN_MITER, 1,
69 dashPattern, 0));
70 if (p0.y == p1.y) {
71 // same line, render a rectangle
72 Rectangle r = p0.union(p1);
73 g.drawLine(r.x, r.y + r.height, r.x + r.width, r.y + r.height);
74 } else {
75 // we have no clue so we render all characters:
76 for (int i = offs0; i <= offs1; i++) {
77 Rectangle pc = mapper.modelToView(c, i);
78 g.drawLine(pc.x, pc.y + pc.height, pc.x + pc.width, pc.y + pc.height);
79 }
80 }
81
82 } catch (BadLocationException e) {
83 // can't render
84 }
85 }
86
87 }
|