1 | /*
|
---|
2 | * 10/01/2009
|
---|
3 | *
|
---|
4 | * MarkOccurrencesHighlightPainter.java - Renders "marked occurrences."
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea;
|
---|
10 |
|
---|
11 | import java.awt.Color;
|
---|
12 | import java.awt.Graphics;
|
---|
13 | import java.awt.Graphics2D;
|
---|
14 | import java.awt.Rectangle;
|
---|
15 | import java.awt.Shape;
|
---|
16 | import javax.swing.text.BadLocationException;
|
---|
17 | import javax.swing.text.JTextComponent;
|
---|
18 | import javax.swing.text.Position;
|
---|
19 | import javax.swing.text.View;
|
---|
20 |
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Highlight painter that renders "mark occurrences."
|
---|
24 | *
|
---|
25 | * @author Robert Futrell
|
---|
26 | * @version 1.0
|
---|
27 | */
|
---|
28 | /*
|
---|
29 | * NOTE: This implementation is a "hack" so typing at the "end" of the highlight
|
---|
30 | * does not extend it to include the newly-typed chars, which is the standard
|
---|
31 | * behavior of Swing Highlights.
|
---|
32 | */
|
---|
33 | class MarkOccurrencesHighlightPainter extends ChangeableColorHighlightPainter {
|
---|
34 |
|
---|
35 | private Color borderColor;
|
---|
36 | private boolean paintBorder;
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates a highlight painter that defaults to blue.
|
---|
41 | */
|
---|
42 | public MarkOccurrencesHighlightPainter() {
|
---|
43 | super(Color.BLUE);
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Returns whether a border is painted around marked occurrences.
|
---|
49 | *
|
---|
50 | * @return Whether a border is painted.
|
---|
51 | * @see #setPaintBorder(boolean)
|
---|
52 | * @see #getColor()
|
---|
53 | */
|
---|
54 | public boolean getPaintBorder() {
|
---|
55 | return paintBorder;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * {@inheritDoc}
|
---|
61 | */
|
---|
62 | public Shape paintLayer(Graphics g, int p0, int p1, Shape viewBounds,
|
---|
63 | JTextComponent c, View view) {
|
---|
64 |
|
---|
65 | g.setColor(getColor());
|
---|
66 | p1++; // Workaround for Java Highlight issues.
|
---|
67 |
|
---|
68 | // This special case isn't needed for most standard Swing Views (which
|
---|
69 | // always return a width of 1 for modelToView() calls), but it is
|
---|
70 | // needed for RSTA views, which actually return the width of chars for
|
---|
71 | // modelToView calls. But this should be faster anyway, as we
|
---|
72 | // short-circuit and do only one modelToView() for one offset.
|
---|
73 | if (p0==p1) {
|
---|
74 | try {
|
---|
75 | Shape s = view.modelToView(p0, viewBounds,
|
---|
76 | Position.Bias.Forward);
|
---|
77 | Rectangle r = s.getBounds();
|
---|
78 | g.drawLine(r.x, r.y, r.x, r.y+r.height);
|
---|
79 | return r;
|
---|
80 | } catch (BadLocationException ble) {
|
---|
81 | ble.printStackTrace(); // Never happens
|
---|
82 | return null;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (p0 == view.getStartOffset() && p1 == view.getEndOffset()) {
|
---|
87 | // Contained in view, can just use bounds.
|
---|
88 | Rectangle alloc;
|
---|
89 | if (viewBounds instanceof Rectangle) {
|
---|
90 | alloc = (Rectangle) viewBounds;
|
---|
91 | } else {
|
---|
92 | alloc = viewBounds.getBounds();
|
---|
93 | }
|
---|
94 | g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
|
---|
95 | return alloc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Should only render part of View.
|
---|
99 | Graphics2D g2d = (Graphics2D)g;
|
---|
100 | try {
|
---|
101 | // --- determine locations ---
|
---|
102 | Shape shape = view.modelToView(p0, Position.Bias.Forward, p1,
|
---|
103 | Position.Bias.Backward, viewBounds);
|
---|
104 | Rectangle r = (shape instanceof Rectangle) ? (Rectangle) shape
|
---|
105 | : shape.getBounds();
|
---|
106 | g2d.fillRect(r.x, r.y, r.width, r.height);
|
---|
107 | if (paintBorder) {
|
---|
108 | g2d.setColor(borderColor);
|
---|
109 | g2d.drawRect(r.x,r.y, r.width-1,r.height-1);
|
---|
110 | }
|
---|
111 | return r;
|
---|
112 | } catch (BadLocationException e) { // Never happens
|
---|
113 | e.printStackTrace();
|
---|
114 | return null;
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * {@inheritDoc}
|
---|
122 | */
|
---|
123 | public void setColor(Color c) {
|
---|
124 | super.setColor(c);
|
---|
125 | borderColor = c.darker();
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Toggles whether a border is painted around highlights.
|
---|
131 | *
|
---|
132 | * @param paint Whether to paint a border.
|
---|
133 | * @see #getPaintBorder()
|
---|
134 | * @see #setColor(Color)
|
---|
135 | */
|
---|
136 | public void setPaintBorder(boolean paint) {
|
---|
137 | this.paintBorder = paint;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | } |
---|