1 /* DefaultHighlighter.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.awt.Color;
42 import java.awt.Graphics;
43 import java.awt.Rectangle;
44 import java.awt.Shape;
45 import java.util.Vector;
47 public class DefaultHighlighter extends LayeredHighlighter
49 public static class DefaultHighlightPainter
54 public DefaultHighlightPainter(Color c)
60 public Color getColor()
65 private void paintHighlight(Graphics g, Rectangle rect)
67 g.fillRect(rect.x, rect.y, rect.width, rect.height);
70 public void paint(Graphics g, int p0, int p1, Shape bounds,
75 Rectangle rect = bounds.getBounds();
79 r0 = c.modelToView(p0);
80 r1 = c.modelToView(p1);
82 catch (BadLocationException e)
84 // This should never occur.
88 if (r0 == null || r1 == null)
92 g.setColor(c.getSelectionColor());
96 // Check if only one line to highlight.
99 r0.width = r1.x - r0.x;
100 paintHighlight(g, r0);
104 // First line, from p0 to end-of-line.
105 r0.width = rect.x + rect.width - r0.x;
106 paintHighlight(g, r0);
108 // FIXME: All the full lines in between, if any (assumes that all lines
109 // have the same height -- not a good assumption with JEditorPane/JTextPane).
115 paintHighlight(g, r0);
119 // Last line, from beginnin-of-line to p1.
120 paintHighlight(g, r1);
123 public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
124 JTextComponent c, View view)
126 throw new InternalError();
130 private class HighlightEntry
134 Highlighter.HighlightPainter painter;
136 public HighlightEntry(int p0, int p1, Highlighter.HighlightPainter painter)
140 this.painter = painter;
143 public int getStartPosition()
148 public int getEndPosition()
153 public Highlighter.HighlightPainter getPainter()
160 * @specnote final as of 1.4
162 public static final LayeredHighlighter.LayerPainter DefaultPainter =
163 new DefaultHighlightPainter(null);
165 private JTextComponent textComponent;
166 private Vector highlights = new Vector();
167 private boolean drawsLayeredHighlights = true;
169 public DefaultHighlighter()
173 public boolean getDrawsLayeredHighlights()
175 return drawsLayeredHighlights;
178 public void setDrawsLayeredHighlights(boolean newValue)
180 drawsLayeredHighlights = newValue;
183 private void checkPositions(int p0, int p1)
184 throws BadLocationException
187 throw new BadLocationException("DefaultHighlighter", p0);
190 throw new BadLocationException("DefaultHighlighter", p1);
193 public void install(JTextComponent c)
196 removeAllHighlights();
199 public void deinstall(JTextComponent c)
201 textComponent = null;
204 public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter painter)
205 throws BadLocationException
207 checkPositions(p0, p1);
208 HighlightEntry entry = new HighlightEntry(p0, p1, painter);
209 highlights.add(entry);
213 public void removeHighlight(Object tag)
215 highlights.remove(tag);
218 public void removeAllHighlights()
223 public Highlighter.Highlight[] getHighlights()
228 public void changeHighlight(Object tag, int p0, int p1)
229 throws BadLocationException
231 checkPositions(p0, p1);
232 HighlightEntry entry = (HighlightEntry) tag;
237 public void paintLayeredHighlights(Graphics g, int p0, int p1,
238 Shape viewBounds, JTextComponent editor,
243 public void paint(Graphics g)
245 // Check if there are any highlights.
246 if (highlights.size() == 0)
249 Shape bounds = textComponent.getBounds();
251 for (int index = 0; index < highlights.size(); ++index)
253 HighlightEntry entry = (HighlightEntry) highlights.get(index);
254 entry.painter.paint(g, entry.p0, entry.p1, bounds, textComponent);