DefaultHighlighter.java 14.1 KB
Newer Older
1
/* DefaultHighlighter.java -- The default highlight for Swing
2
   Copyright (C) 2004, 2006  Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.text;

import java.awt.Color;
import java.awt.Graphics;
43
import java.awt.Insets;
Tom Tromey committed
44 45
import java.awt.Rectangle;
import java.awt.Shape;
46
import java.util.ArrayList;
47
import java.util.Iterator;
48

49
import javax.swing.SwingUtilities;
50
import javax.swing.plaf.TextUI;
Tom Tromey committed
51

52 53
/**
 * The default highlight for Swing text components. It highlights text
54
 * by filling the background with a rectangle.
55
 */
Tom Tromey committed
56 57 58 59 60 61
public class DefaultHighlighter extends LayeredHighlighter
{
  public static class DefaultHighlightPainter
    extends LayerPainter
  {
    private Color color;
62

Tom Tromey committed
63 64 65 66 67 68 69 70 71 72 73 74
    public DefaultHighlightPainter(Color c)
    {
      super();
      color = c;
    }

    public Color getColor()
    {
      return color;
    }

    public void paint(Graphics g, int p0, int p1, Shape bounds,
75
                      JTextComponent t)
Tom Tromey committed
76
    {
77 78
      if (p0 == p1)
        return;
79

80
      Rectangle rect = bounds.getBounds();
Tom Tromey committed
81

82 83 84 85
      Color col = getColor();
      if (col == null)
        col = t.getSelectionColor();
      g.setColor(col);
86 87

      TextUI ui = t.getUI();
88

89
      try
90 91 92 93
        {

          Rectangle l0 = ui.modelToView(t, p0, null);
          Rectangle l1 = ui.modelToView(t, p1, null);
94

95 96 97 98
          // Note: The computed locations may lie outside of the allocation
          // area if the text is scrolled.

          if (l0.y == l1.y)
99 100 101 102
          {
            SwingUtilities.computeUnion(l0.x, l0.y, l0.width, l0.height, l1);

            // Paint only inside the allocation area.
103
            SwingUtilities.computeIntersection(rect.x, rect.y, rect.width,
104 105
                                               rect.height, l1);

106
            g.fillRect(l1.x, l1.y, l1.width, l1.height);
107 108 109 110 111 112 113 114 115 116
          }
        else
          {
            // 1. The line of p0 is painted from the position of p0
            // to the right border.
            // 2. All lines between the ones where p0 and p1 lie on
            // are completely highlighted. The allocation area is used to find
            // out the bounds.
            // 3. The final line is painted from the left border to the
            // position of p1.
117

118 119 120 121 122 123 124 125 126
            int firstLineWidth = rect.x + rect.width - l0.x;
            g.fillRect(l0.x, l0.y, firstLineWidth, l0.height);
            if (l0.y + l0.height != l1.y)
              {
                g.fillRect(rect.x, l0.y + l0.height, rect.width,
                           l1.y - l0.y - l0.height);
              }
            g.fillRect(rect.x, l1.y, l1.x - rect.x, l1.height);
          }
127 128 129
      }
    catch (BadLocationException ex)
      {
130 131
        // Can't render. Comment out for debugging.
        // ex.printStackTrace();
132
      }
Tom Tromey committed
133 134 135
    }

    public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
136
                            JTextComponent c, View view)
Tom Tromey committed
137
    {
138 139
      Color col = getColor();
      if (col == null)
140
        col = c.getSelectionColor();
141 142 143 144
      g.setColor(col);

      Rectangle rect = null;
      if (p0 == view.getStartOffset() && p1 == view.getEndOffset())
145 146
        {
          // Paint complete bounds region.
147
          rect = bounds instanceof Rectangle ? (Rectangle) bounds
148 149
                                             : bounds.getBounds();
        }
150
      else
151 152
        {
          // Only partly inside the view.
153
          try
154
            {
155
              Shape s = view.modelToView(p0, Position.Bias.Forward,
156 157 158 159 160 161 162 163 164
                                         p1, Position.Bias.Backward,
                                         bounds);
              rect = s instanceof Rectangle ? (Rectangle) s : s.getBounds();
            }
          catch (BadLocationException ex)
            {
              // Can't render the highlight.
            }
        }
165 166

      if (rect != null)
167
        {
168
          g.fillRect(rect.x, rect.y, rect.width, rect.height);
169
        }
170
      return rect;
Tom Tromey committed
171 172
    }
  }
173

174
  private class HighlightEntry implements Highlighter.Highlight
Tom Tromey committed
175
  {
176 177
    Position p0;
    Position p1;
Tom Tromey committed
178 179
    Highlighter.HighlightPainter painter;

180 181
    public HighlightEntry(Position p0, Position p1,
                          Highlighter.HighlightPainter painter)
Tom Tromey committed
182 183 184 185 186 187
    {
      this.p0 = p0;
      this.p1 = p1;
      this.painter = painter;
    }

188
    public int getStartOffset()
Tom Tromey committed
189
    {
190
      return p0.getOffset();
Tom Tromey committed
191 192
    }

193
    public int getEndOffset()
Tom Tromey committed
194
    {
195
      return p1.getOffset();
Tom Tromey committed
196 197 198 199 200 201 202 203 204
    }

    public Highlighter.HighlightPainter getPainter()
    {
      return painter;
    }
  }

  /**
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
   * A HighlightEntry that is used for LayerPainter painters. In addition
   * to the info maintained by the HighlightEntry, this class maintains
   * a painting rectangle. This is used as repaint region when the
   * highlight changes and the text component needs repainting.
   */
  private class LayerHighlightEntry
    extends HighlightEntry
  {

    /**
     * The paint rectangle.
     */
    Rectangle paintRect = new Rectangle();

    LayerHighlightEntry(Position p0, Position p1,
                        Highlighter.HighlightPainter p)
    {
      super(p0, p1, p);
    }

    /**
     * Paints the highlight by calling the LayerPainter. This
     * restricts the area to be painted by startOffset and endOffset
     * and manages the paint rectangle.
     */
    void paintLayeredHighlight(Graphics g, int p0, int p1, Shape bounds,
231
                               JTextComponent tc, View view)
232 233 234 235 236 237
    {
      p0 = Math.max(getStartOffset(), p0);
      p1 = Math.min(getEndOffset(), p1);

      Highlighter.HighlightPainter painter = getPainter();
      if (painter instanceof LayerPainter)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        {
          LayerPainter layerPainter = (LayerPainter) painter;
          Shape area = layerPainter.paintLayer(g, p0, p1, bounds, tc, view);
          Rectangle rect;
          if (area instanceof Rectangle && paintRect != null)
            rect = (Rectangle) area;
          else
            rect = area.getBounds();

          if (paintRect.width == 0 || paintRect.height == 0)
            paintRect = rect.getBounds();
          else
            paintRect = SwingUtilities.computeUnion(rect.x, rect.y, rect.width,
                                                    rect.height, paintRect);
        }
253 254 255 256
    }
  }

  /**
Tom Tromey committed
257 258 259 260
   * @specnote final as of 1.4
   */
  public static final LayeredHighlighter.LayerPainter DefaultPainter =
    new DefaultHighlightPainter(null);
261

Tom Tromey committed
262
  private JTextComponent textComponent;
263
  private ArrayList highlights = new ArrayList();
Tom Tromey committed
264
  private boolean drawsLayeredHighlights = true;
265

Tom Tromey committed
266 267
  public DefaultHighlighter()
  {
268
    // Nothing to do here.
Tom Tromey committed
269 270 271 272 273 274 275 276 277 278 279
  }

  public boolean getDrawsLayeredHighlights()
  {
    return drawsLayeredHighlights;
  }

  public void setDrawsLayeredHighlights(boolean newValue)
  {
    drawsLayeredHighlights = newValue;
  }
280

Tom Tromey committed
281 282 283 284 285
  private void checkPositions(int p0, int p1)
    throws BadLocationException
  {
    if (p0 < 0)
      throw new BadLocationException("DefaultHighlighter", p0);
286

Tom Tromey committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    if (p1 < p0)
      throw new BadLocationException("DefaultHighlighter", p1);
  }

  public void install(JTextComponent c)
  {
    textComponent = c;
    removeAllHighlights();
  }

  public void deinstall(JTextComponent c)
  {
    textComponent = null;
  }

302 303
  public Object addHighlight(int p0, int p1,
                             Highlighter.HighlightPainter painter)
Tom Tromey committed
304 305 306
    throws BadLocationException
  {
    checkPositions(p0, p1);
307 308 309 310 311 312 313 314
    HighlightEntry entry;
    Document doc = textComponent.getDocument();
    Position pos0 = doc.createPosition(p0);
    Position pos1 = doc.createPosition(p1);
    if (getDrawsLayeredHighlights() && painter instanceof LayerPainter)
      entry = new LayerHighlightEntry(pos0, pos1, painter);
    else
      entry = new HighlightEntry(pos0, pos1, painter);
Tom Tromey committed
315
    highlights.add(entry);
316

317
    textComponent.getUI().damageRange(textComponent, p0, p1);
318

Tom Tromey committed
319 320 321 322 323
    return entry;
  }

  public void removeHighlight(Object tag)
  {
324 325 326
    HighlightEntry entry = (HighlightEntry) tag;
    if (entry instanceof LayerHighlightEntry)
      {
327 328 329 330
        LayerHighlightEntry lEntry = (LayerHighlightEntry) entry;
        Rectangle paintRect = lEntry.paintRect;
        textComponent.repaint(paintRect.x, paintRect.y, paintRect.width,
                              paintRect.height);
331 332 333
      }
    else
      {
334 335 336
        textComponent.getUI().damageRange(textComponent,
                                          entry.getStartOffset(),
                                          entry.getEndOffset());
337
      }
Tom Tromey committed
338
    highlights.remove(tag);
339

Tom Tromey committed
340 341 342 343
  }

  public void removeAllHighlights()
  {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    // Repaint damaged region.
    int minX = 0;
    int maxX = 0;
    int minY = 0;
    int maxY = 0;
    int p0 = -1;
    int p1 = -1;
    for (Iterator i = highlights.iterator(); i.hasNext();)
      {
        HighlightEntry e = (HighlightEntry) i.next();
        if (e instanceof LayerHighlightEntry)
          {
            LayerHighlightEntry le = (LayerHighlightEntry) e;
            Rectangle r = le.paintRect;
            minX = Math.min(r.x, minX);
            maxX = Math.max(r.x + r.width, maxX);
            minY = Math.min(r.y, minY);
            maxY = Math.max(r.y + r.height, maxY);
          }
        else
          {
            if (p0 == -1 || p1 == -1)
              {
                p0 = e.getStartOffset();
                p1 = e.getEndOffset();
              }
            else
              {
                p0 = Math.min(p0, e.getStartOffset());
                p1 = Math.max(p1, e.getEndOffset());
              }
          }
        if (minX != maxX && minY != maxY)
          textComponent.repaint(minX, minY, maxX - minX, maxY - minY);
        if (p0 != -1 && p1 != -1)
          {
            TextUI ui = textComponent.getUI();
            ui.damageRange(textComponent, p0, p1);
          }
383

384
      }
Tom Tromey committed
385 386 387 388 389
    highlights.clear();
  }

  public Highlighter.Highlight[] getHighlights()
  {
390
    return (Highlighter.Highlight[])
391
      highlights.toArray(new Highlighter.Highlight[highlights.size()]);
Tom Tromey committed
392 393
  }

394
  public void changeHighlight(Object tag, int n0, int n1)
Tom Tromey committed
395 396
    throws BadLocationException
  {
397
    Document doc = textComponent.getDocument();
398
    TextUI ui = textComponent.getUI();
399
    if (tag instanceof LayerHighlightEntry)
400
      {
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        LayerHighlightEntry le = (LayerHighlightEntry) tag;
        Rectangle r = le.paintRect;
        if (r.width > 0 && r.height > 0)
          textComponent.repaint(r.x, r.y, r.width, r.height);
        r.width = 0;
        r.height = 0;
        le.p0 = doc.createPosition(n0);
        le.p1 = doc.createPosition(n1);
        ui.damageRange(textComponent, Math.min(n0, n1), Math.max(n0, n1));
      }
    else if (tag instanceof HighlightEntry)
      {
        HighlightEntry e = (HighlightEntry) tag;
        int p0 = e.getStartOffset();
        int p1 = e.getEndOffset();
        if (p0 == n0)
417
          {
418 419
            ui.damageRange(textComponent, Math.min(p1, n1),
                           Math.max(p1, n1));
420
          }
421
        else if (n1 == p1)
422
          {
423 424
            ui.damageRange(textComponent, Math.min(p0, n0),
                           Math.max(p0, n0));
425 426 427
          }
        else
          {
428 429
            ui.damageRange(textComponent, p0, p1);
            ui.damageRange(textComponent, n0, n1);
430
          }
431 432
        e.p0 = doc.createPosition(n0);
        e.p1 = doc.createPosition(n1);
433
      }
Tom Tromey committed
434 435 436 437 438 439
  }

  public void paintLayeredHighlights(Graphics g, int p0, int p1,
                                     Shape viewBounds, JTextComponent editor,
                                     View view)
  {
440 441
    for (Iterator i = highlights.iterator(); i.hasNext();)
      {
442 443 444 445 446 447 448 449 450
        Object o = i.next();
        if (o instanceof LayerHighlightEntry)
          {
            LayerHighlightEntry entry = (LayerHighlightEntry) o;
            int start = entry.getStartOffset();
            int end = entry.getEndOffset();
            if ((p0 < start && p1 > start) || (p0 >= start && p0 < end))
              entry.paintLayeredHighlight(g, p0, p1, viewBounds, editor, view);
          }
451
      }
Tom Tromey committed
452 453 454 455
  }

  public void paint(Graphics g)
  {
456
    int size = highlights.size();
457

Tom Tromey committed
458
    // Check if there are any highlights.
459
    if (size == 0)
Tom Tromey committed
460
      return;
461 462 463 464 465 466 467 468

    // Prepares the rectangle of the inner drawing area.
    Insets insets = textComponent.getInsets();
    Shape bounds =
      new Rectangle(insets.left,
                    insets.top,
                    textComponent.getWidth() - insets.left - insets.right,
                    textComponent.getHeight() - insets.top - insets.bottom);
469

470
    for (int index = 0; index < size; ++index)
Tom Tromey committed
471
      {
472 473
        HighlightEntry entry = (HighlightEntry) highlights.get(index);
        if (! (entry instanceof LayerHighlightEntry))
474 475
          entry.painter.paint(g, entry.getStartOffset(), entry.getEndOffset(),
                              bounds, textComponent);
Tom Tromey committed
476 477 478
      }
  }
}