001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * The main author is Santhosh Kumar T. Visit his great home page under:
004 * http://www.jroller.com/santhosh/
005 *
006 * The original source for this file is:
007 * http://www.jroller.com/santhosh/entry/document_guard
008 *
009 * It is distributed under the GNU Lesser General Public License:
010 * http://www.gnu.org/copyleft/lesser.html
011 */
012
013 package org.qedeq.gui.se.util;
014
015 import java.awt.Color;
016 import java.awt.Graphics;
017 import java.awt.Rectangle;
018 import java.awt.Shape;
019
020 import javax.swing.plaf.TextUI;
021 import javax.swing.text.BadLocationException;
022 import javax.swing.text.Highlighter;
023 import javax.swing.text.JTextComponent;
024
025 /**
026 * Paint marked text blocks in another color. Precondition is an installed
027 * {@link org.qedeq.gui.se.util.CurrentLineHighlighterUtility} for that text component.
028 *
029 * @version $Revision: 1.1 $
030 * @author Santhosh Kumar T
031 * @author Michael Meyling
032 */
033 public class DocumentMarkerPainter implements Highlighter.HighlightPainter {
034
035 /** Background color. */
036 private Color color;
037
038 /**
039 * Constructor.
040 *
041 * @param color Background color.
042 */
043 public DocumentMarkerPainter(final Color color) {
044 this.color = color;
045 }
046
047 public void paint(final Graphics g, final int p0, final int p1, final Shape bounds,
048 final JTextComponent c) {
049 specialPaint(g, p0 - 1, p1 + 1, bounds, c); // trick
050 }
051
052 private void specialPaint(final Graphics g, final int offs0, final int offs1,
053 final Shape bounds, final JTextComponent c) {
054 final Rectangle alloc = bounds.getBounds();
055 try {
056 // --- determine locations ---
057 TextUI mapper = c.getUI();
058 Rectangle p0 = mapper.modelToView(c, offs0);
059 Rectangle p1 = mapper.modelToView(c, offs1);
060
061 // --- render ---
062 g.setColor(color);
063 if (p0.y == p1.y) {
064 // same line, render a rectangle
065 Rectangle r = p0.union(p1);
066 g.fillRect(r.x, r.y, r.width, r.height);
067 } else {
068 // different lines
069 int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
070 g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
071 if ((p0.y + p0.height) != p1.y) {
072 g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height));
073 }
074 g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
075 }
076
077 // TODO mime 20080417: this code dosn't work; we want to higlight the current line even if it has
078 // text markers in it
079 //// paint highlighter, Q & D
080 // try {
081 // // cursor line within: highlight again with other color:
082 // if (c instanceof JTextArea) {
083 // final JTextArea ta = (JTextArea) c;
084 // final int caretLine = ta.getLineOfOffset(c.getCaretPosition());
085 // if (ta.getLineOfOffset(offs0) <= caretLine
086 // && caretLine <= ta.getLineOfOffset(offs1)) {
087 // GuiHelper.paintCurrentLineBackground(g, c,
088 // GuiHelper.getCurrentAndMarkedBackgroundColor());
089 // }
090 // }
091 // } catch (Exception e) {
092 // e.printStackTrace();
093 // }
094
095 } catch (BadLocationException e) {
096 // can't render
097 }
098 }
099
100 }
|