1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: public class DefaultHighlighter extends LayeredHighlighter
48: {
49: public static class DefaultHighlightPainter
50: extends LayerPainter
51: {
52: private Color color;
53:
54: public DefaultHighlightPainter(Color c)
55: {
56: super();
57: color = c;
58: }
59:
60: public Color getColor()
61: {
62: return color;
63: }
64:
65: private void paintHighlight(Graphics g, Rectangle rect)
66: {
67: g.fillRect(rect.x, rect.y, rect.width, rect.height);
68: }
69:
70: public void paint(Graphics g, int p0, int p1, Shape bounds,
71: JTextComponent c)
72: {
73: Rectangle r0 = null;
74: Rectangle r1 = null;
75: Rectangle rect = bounds.getBounds();
76:
77: try
78: {
79: r0 = c.modelToView(p0);
80: r1 = c.modelToView(p1);
81: }
82: catch (BadLocationException e)
83: {
84:
85: return;
86: }
87:
88: if (r0 == null || r1 == null)
89: return;
90:
91: if (color == null)
92: g.setColor(c.getSelectionColor());
93: else
94: g.setColor(color);
95:
96:
97: if (r0.y == r1.y)
98: {
99: r0.width = r1.x - r0.x;
100: paintHighlight(g, r0);
101: return;
102: }
103:
104:
105: r0.width = rect.x + rect.width - r0.x;
106: paintHighlight(g, r0);
107:
108:
109:
110: r0.y += r0.height;
111: r0.x = rect.x;
112:
113: while (r0.y < r1.y)
114: {
115: paintHighlight(g, r0);
116: r0.y += r0.height;
117: }
118:
119:
120: paintHighlight(g, r1);
121: }
122:
123: public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
124: JTextComponent c, View view)
125: {
126: throw new InternalError();
127: }
128: }
129:
130: private class HighlightEntry
131: {
132: int p0;
133: int p1;
134: Highlighter.HighlightPainter painter;
135:
136: public HighlightEntry(int p0, int p1, Highlighter.HighlightPainter painter)
137: {
138: this.p0 = p0;
139: this.p1 = p1;
140: this.painter = painter;
141: }
142:
143: public int getStartPosition()
144: {
145: return p0;
146: }
147:
148: public int getEndPosition()
149: {
150: return p1;
151: }
152:
153: public Highlighter.HighlightPainter getPainter()
154: {
155: return painter;
156: }
157: }
158:
159:
162: public static final LayeredHighlighter.LayerPainter DefaultPainter =
163: new DefaultHighlightPainter(null);
164:
165: private JTextComponent textComponent;
166: private Vector highlights = new Vector();
167: private boolean drawsLayeredHighlights = true;
168:
169: public DefaultHighlighter()
170: {
171:
172: }
173:
174: public boolean getDrawsLayeredHighlights()
175: {
176: return drawsLayeredHighlights;
177: }
178:
179: public void setDrawsLayeredHighlights(boolean newValue)
180: {
181: drawsLayeredHighlights = newValue;
182: }
183:
184: private void checkPositions(int p0, int p1)
185: throws BadLocationException
186: {
187: if (p0 < 0)
188: throw new BadLocationException("DefaultHighlighter", p0);
189:
190: if (p1 < p0)
191: throw new BadLocationException("DefaultHighlighter", p1);
192: }
193:
194: public void install(JTextComponent c)
195: {
196: textComponent = c;
197: removeAllHighlights();
198: }
199:
200: public void deinstall(JTextComponent c)
201: {
202: textComponent = null;
203: }
204:
205: public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter painter)
206: throws BadLocationException
207: {
208: checkPositions(p0, p1);
209: HighlightEntry entry = new HighlightEntry(p0, p1, painter);
210: highlights.add(entry);
211: return entry;
212: }
213:
214: public void removeHighlight(Object tag)
215: {
216: highlights.remove(tag);
217: }
218:
219: public void removeAllHighlights()
220: {
221: highlights.clear();
222: }
223:
224: public Highlighter.Highlight[] getHighlights()
225: {
226: return null;
227: }
228:
229: public void changeHighlight(Object tag, int p0, int p1)
230: throws BadLocationException
231: {
232: checkPositions(p0, p1);
233: HighlightEntry entry = (HighlightEntry) tag;
234: entry.p0 = p0;
235: entry.p1 = p1;
236: }
237:
238: public void paintLayeredHighlights(Graphics g, int p0, int p1,
239: Shape viewBounds, JTextComponent editor,
240: View view)
241: {
242:
243: }
244:
245: public void paint(Graphics g)
246: {
247:
248: if (highlights.size() == 0)
249: return;
250:
251: Shape bounds = textComponent.getBounds();
252:
253: for (int index = 0; index < highlights.size(); ++index)
254: {
255: HighlightEntry entry = (HighlightEntry) highlights.get(index);
256: entry.painter.paint(g, entry.p0, entry.p1, bounds, textComponent);
257: }
258: }
259: }