/**
   * Adds a highlight to the view. Returns a tag that can be used to refer to the highlight.
   *
   * @param p0 the start offset of the range to highlight >= 0
   * @param p1 the end offset of the range to highlight >= p0
   * @param p the painter to use to actually render the highlight
   * @return an object that can be used as a tag to refer to the highlight
   * @exception BadLocationException if the specified location is invalid
   */
  public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p)
      throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid start offset", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end offset", p1);
    }

    Document doc = component.getDocument();
    HighlightInfo i =
        (getDrawsLayeredHighlights() && (p instanceof LayeredHighlighter.LayerPainter))
            ? new LayeredHighlightInfo()
            : new HighlightInfo();
    i.painter = p;
    i.p0 = doc.createPosition(p0);
    i.p1 = doc.createPosition(p1);
    highlights.addElement(i);
    safeDamageRange(p0, p1);
    return i;
  }
  /**
   * Changes a highlight.
   *
   * @param tag the highlight tag
   * @param p0 the beginning of the range &gt;= 0
   * @param p1 the end of the range &gt;= p0
   * @exception BadLocationException if the specified location is invalid
   */
  public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
    if (p0 < 0) {
      throw new BadLocationException("Invalid beginning of the range", p0);
    }

    if (p1 < p0) {
      throw new BadLocationException("Invalid end of the range", p1);
    }

    Document doc = component.getDocument();
    if (tag instanceof LayeredHighlightInfo) {
      LayeredHighlightInfo lhi = (LayeredHighlightInfo) tag;
      if (lhi.width > 0 && lhi.height > 0) {
        component.repaint(lhi.x, lhi.y, lhi.width, lhi.height);
      }
      // Mark the highlights region as invalid, it will reset itself
      // next time asked to paint.
      lhi.width = lhi.height = 0;
      lhi.p0 = doc.createPosition(p0);
      lhi.p1 = doc.createPosition(p1);
      safeDamageRange(Math.min(p0, p1), Math.max(p0, p1));
    } else {
      HighlightInfo info = (HighlightInfo) tag;
      int oldP0 = info.p0.getOffset();
      int oldP1 = info.p1.getOffset();
      if (p0 == oldP0) {
        safeDamageRange(Math.min(oldP1, p1), Math.max(oldP1, p1));
      } else if (p1 == oldP1) {
        safeDamageRange(Math.min(p0, oldP0), Math.max(p0, oldP0));
      } else {
        safeDamageRange(oldP0, oldP1);
        safeDamageRange(p0, p1);
      }
      info.p0 = doc.createPosition(p0);
      info.p1 = doc.createPosition(p1);
    }
  }