1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52:
53: public class DefaultHighlighter extends LayeredHighlighter
54: {
55: public static class DefaultHighlightPainter
56: extends LayerPainter
57: {
58: private Color color;
59:
60: public DefaultHighlightPainter(Color c)
61: {
62: super();
63: color = c;
64: }
65:
66: public Color getColor()
67: {
68: return color;
69: }
70:
71: private void paintHighlight(Graphics g, Rectangle rect)
72: {
73: g.fillRect(rect.x, rect.y, rect.width, rect.height);
74: }
75:
76: public void paint(Graphics g, int p0, int p1, Shape bounds,
77: JTextComponent t)
78: {
79: if (p0 == p1)
80: return;
81:
82: Rectangle rect = bounds.getBounds();
83:
84: if (color == null)
85: g.setColor(t.getSelectionColor());
86: else
87: g.setColor(color);
88:
89: TextUI ui = t.getUI();
90:
91: try
92: {
93:
94: Rectangle l0 = ui.modelToView(t, p0, null);
95: Rectangle l1 = ui.modelToView(t, p1, null);
96:
97:
98:
99:
100: if (l0.y == l1.y)
101: {
102: SwingUtilities.computeUnion(l0.x, l0.y, l0.width, l0.height, l1);
103:
104:
105: SwingUtilities.computeIntersection(rect.x, rect.y, rect.width, rect.height, l1);
106:
107: paintHighlight(g, l1);
108: }
109: else
110: {
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122: l0.x -= rect.x;
123: l0.width = rect.width - l0.x - rect.x;
124:
125: paintHighlight(g, l0);
126:
127: int posBelow = Utilities.getPositionBelow(t, p0, l0.x);
128: int p1RowStart = Utilities.getRowStart(t, p1);
129: if (posBelow != -1
130: && posBelow != p0
131: && Utilities.getRowStart(t, posBelow)
132: != p1RowStart)
133: {
134: Rectangle grow = ui.modelToView(t, posBelow);
135: grow.x = rect.x;
136: grow.width = rect.width;
137:
138:
139: int nextPosBelow = posBelow;
140: while (nextPosBelow != -1
141: && Utilities.getRowStart(t, nextPosBelow) != p1RowStart)
142: {
143: posBelow = nextPosBelow;
144: nextPosBelow = Utilities.getPositionBelow(t, posBelow, l0.x);
145:
146: if (nextPosBelow == posBelow)
147: break;
148: }
149:
150:
151:
152:
153:
154:
155: Rectangle end = ui.modelToView(t, posBelow);
156: grow.height = end.y + end.height - grow.y;
157:
158: paintHighlight(g, grow);
159: }
160:
161:
162: l1.width = l1.x + l1.width - rect.x;
163: l1.x = rect.x;
164: paintHighlight(g, l1);
165: }
166: }
167: catch (BadLocationException ex)
168: {
169: AssertionError err = new AssertionError("Unexpected bad location exception");
170: err.initCause(ex);
171: throw err;
172: }
173: }
174:
175: public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
176: JTextComponent c, View view)
177: {
178: throw new InternalError();
179: }
180: }
181:
182: private class HighlightEntry implements Highlighter.Highlight
183: {
184: int p0;
185: int p1;
186: Highlighter.HighlightPainter painter;
187:
188: public HighlightEntry(int p0, int p1, Highlighter.HighlightPainter painter)
189: {
190: this.p0 = p0;
191: this.p1 = p1;
192: this.painter = painter;
193: }
194:
195: public int getStartOffset()
196: {
197: return p0;
198: }
199:
200: public int getEndOffset()
201: {
202: return p1;
203: }
204:
205: public Highlighter.HighlightPainter getPainter()
206: {
207: return painter;
208: }
209: }
210:
211:
214: public static final LayeredHighlighter.LayerPainter DefaultPainter =
215: new DefaultHighlightPainter(null);
216:
217: private JTextComponent textComponent;
218: private ArrayList highlights = new ArrayList();
219: private boolean drawsLayeredHighlights = true;
220:
221: public DefaultHighlighter()
222: {
223:
224: }
225:
226: public boolean getDrawsLayeredHighlights()
227: {
228: return drawsLayeredHighlights;
229: }
230:
231: public void setDrawsLayeredHighlights(boolean newValue)
232: {
233: drawsLayeredHighlights = newValue;
234: }
235:
236: private void checkPositions(int p0, int p1)
237: throws BadLocationException
238: {
239: if (p0 < 0)
240: throw new BadLocationException("DefaultHighlighter", p0);
241:
242: if (p1 < p0)
243: throw new BadLocationException("DefaultHighlighter", p1);
244: }
245:
246: public void install(JTextComponent c)
247: {
248: textComponent = c;
249: removeAllHighlights();
250: }
251:
252: public void deinstall(JTextComponent c)
253: {
254: textComponent = null;
255: }
256:
257: public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter painter)
258: throws BadLocationException
259: {
260: checkPositions(p0, p1);
261: HighlightEntry entry = new HighlightEntry(p0, p1, painter);
262: highlights.add(entry);
263:
264: textComponent.getUI().damageRange(textComponent, p0, p1);
265:
266: return entry;
267: }
268:
269: public void removeHighlight(Object tag)
270: {
271: highlights.remove(tag);
272:
273: HighlightEntry entry = (HighlightEntry) tag;
274: textComponent.getUI().damageRange(textComponent,
275: entry.p0,
276: entry.p1);
277: }
278:
279: public void removeAllHighlights()
280: {
281: highlights.clear();
282: }
283:
284: public Highlighter.Highlight[] getHighlights()
285: {
286: return (Highlighter.Highlight[])
287: highlights.toArray(new Highlighter.Highlight[highlights.size()]);
288: }
289:
290: public void changeHighlight(Object tag, int n0, int n1)
291: throws BadLocationException
292: {
293: int o0, o1;
294:
295: checkPositions(n0, n1);
296: HighlightEntry entry = (HighlightEntry) tag;
297: o0 = entry.p0;
298: o1 = entry.p1;
299:
300:
301: if (o0 == n0 && o1 == n1)
302: return;
303:
304: entry.p0 = n0;
305: entry.p1 = n1;
306:
307: TextUI ui = textComponent.getUI();
308:
309:
310: if (n0 == n1)
311: ui.damageRange(textComponent, o0, o1);
312:
313: else if ((o1 > n0 && o1 <= n1)
314: || (n1 > o0 && n1 <= o1))
315: {
316:
317:
318: int fds, sds;
319: int fde, sde;
320:
321:
322: if(o0 < n0)
323: {
324:
325:
326: fds = o0;
327: fde = n0;
328: }
329: else
330: {
331:
332:
333: fds = n0;
334: fde = o0;
335: }
336:
337: if (o1 < n1)
338: {
339:
340:
341: sds = o1;
342: sde = n1;
343: }
344: else
345: {
346:
347:
348: sds = n1;
349: sde = o1;
350: }
351:
352:
353:
354: if (fde == sds)
355: ui.damageRange(textComponent, fds, sde);
356: else
357: {
358: if (fds != fde)
359: ui.damageRange(textComponent, fds, fde);
360:
361: if (sds != sde)
362: ui.damageRange(textComponent, sds, sde);
363: }
364: }
365: else
366: {
367:
368:
369: ui.damageRange(textComponent, o0, o1);
370: ui.damageRange(textComponent, n0, n1);
371: }
372:
373: }
374:
375: public void paintLayeredHighlights(Graphics g, int p0, int p1,
376: Shape viewBounds, JTextComponent editor,
377: View view)
378: throws NotImplementedException
379: {
380:
381: }
382:
383: public void paint(Graphics g)
384: {
385: int size = highlights.size();
386:
387:
388: if (size == 0)
389: return;
390:
391:
392: Insets insets = textComponent.getInsets();
393: Shape bounds =
394: new Rectangle(insets.left,
395: insets.top,
396: textComponent.getWidth() - insets.left - insets.right,
397: textComponent.getHeight() - insets.top - insets.bottom);
398:
399: for (int index = 0; index < size; ++index)
400: {
401: HighlightEntry entry = (HighlightEntry) highlights.get(index);
402: entry.painter.paint(g, entry.p0, entry.p1, bounds, textComponent);
403: }
404: }
405: }