DocumentMarkerPainter.java
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.Color;
16 import java.awt.Graphics;
17 import java.awt.Rectangle;
18 import java.awt.Shape;
19 
20 import javax.swing.plaf.TextUI;
21 import javax.swing.text.BadLocationException;
22 import javax.swing.text.Highlighter;
23 import javax.swing.text.JTextComponent;
24 
25 /**
26  * Paint marked text blocks in another color. Precondition is an installed
27  {@link org.qedeq.gui.se.util.CurrentLineHighlighterUtility} for that text component.
28  *
29  @author  Santhosh Kumar T
30  @author  Michael Meyling
31  */
32 public class DocumentMarkerPainter implements Highlighter.HighlightPainter {
33 
34     /** Background color. */
35     private Color color;
36 
37     /**
38      * Constructor.
39      *
40      @param   color   Background color.
41      */
42     public DocumentMarkerPainter(final Color color) {
43         this.color = color;
44     }
45 
46     public void paint(final Graphics g, final int p0, final int p1, final Shape bounds,
47             final JTextComponent c) {
48         specialPaint(g, p0 - 1, p1 + 1, bounds, c)// trick
49     }
50 
51     private void specialPaint(final Graphics g, final int offs0, final int offs1,
52             final Shape bounds, final JTextComponent c) {
53         final Rectangle alloc = bounds.getBounds();
54         try {
55             // --- determine locations ---
56             TextUI mapper = c.getUI();
57             Rectangle p0 = mapper.modelToView(c, offs0);
58             Rectangle p1 = mapper.modelToView(c, offs1);
59 
60             // --- render ---
61             g.setColor(color);
62             if (p0.y == p1.y) {
63                 // same line, render a rectangle
64                 Rectangle r = p0.union(p1);
65                 g.fillRect(r.x, r.y, r.width, r.height);
66             else {
67                 // different lines
68                 int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
69                 g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
70                 if ((p0.y + p0.height!= p1.y) {
71                     g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height));
72                 }
73                 g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
74             }
75 
76 // TODO mime 20080417: this code dosn't work; we want to higlight the current line even if it has
77 //                     text markers in it
78 ////          paint highlighter, Q & D
79 //            try {
80 //                // cursor line within: highlight again with other color:
81 //                if (c instanceof JTextArea) {
82 //                    final JTextArea ta = (JTextArea) c;
83 //                    final int caretLine = ta.getLineOfOffset(c.getCaretPosition());
84 //                    if (ta.getLineOfOffset(offs0) <= caretLine
85 //                            && caretLine <= ta.getLineOfOffset(offs1)) {
86 //                        GuiHelper.paintCurrentLineBackground(g, c,
87 //                            GuiHelper.getCurrentAndMarkedBackgroundColor());
88 //                    }
89 //                }
90 //            } catch (Exception e) {
91 //                e.printStackTrace();
92 //            }
93 
94         catch (BadLocationException e) {
95             // can't render
96         }
97     }
98 
99 }