/** Paints one view that corresponds to a line (or multiple lines if folding takes effect). */
  private void paintView(View view, Graphics g, int yBase) {
    JTextComponent component = editorUI.getComponent();
    if (component == null) return;
    BaseTextUI textUI = (BaseTextUI) component.getUI();

    Element rootElem = textUI.getRootView(component).getElement();
    int line = rootElem.getElementIndex(view.getStartOffset());

    String annotation = ""; // NOI18N
    AnnotateLine al = null;
    if (!elementAnnotations.isEmpty()) {
      al = getAnnotateLine(line);
      if (al != null) {
        annotation = getDisplayName(al); // NOI18N
      }
    } else {
      annotation = elementAnnotationsSubstitute;
    }

    if (al != null && al.getRevision().equals(recentRevision)) {
      g.setColor(selectedColor());
    } else {
      g.setColor(foregroundColor());
    }
    int texty = yBase + editorUI.getLineAscent();
    int textx = 2;
    g.drawString(annotation, textx, texty);
  }
 /** GlyphGutter copy pasted utility method. */
 private int getLineFromMouseEvent(MouseEvent e) {
   int line = -1;
   if (editorUI != null) {
     try {
       JTextComponent component = editorUI.getComponent();
       BaseTextUI textUI = (BaseTextUI) component.getUI();
       int clickOffset = textUI.viewToModel(component, new Point(0, e.getY()));
       line = Utilities.getLineOffset(doc, clickOffset);
     } catch (BadLocationException ble) {
     }
   }
   return line;
 }
  /**
   * GlyphGutter copy pasted bolerplate method. It invokes {@link #paintView} that contains actual
   * business logic.
   */
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Rectangle clip = g.getClipBounds();

    JTextComponent component = editorUI.getComponent();
    if (component == null) return;

    BaseTextUI textUI = (BaseTextUI) component.getUI();
    View rootView = Utilities.getDocumentView(component);
    if (rootView == null) return;

    g.setColor(backgroundColor());
    g.fillRect(clip.x, clip.y, clip.width, clip.height);

    AbstractDocument doc = (AbstractDocument) component.getDocument();
    doc.readLock();
    try {
      foldHierarchy.lock();
      try {
        int startPos = textUI.getPosFromY(clip.y);
        int startViewIndex = rootView.getViewIndex(startPos, Position.Bias.Forward);
        int rootViewCount = rootView.getViewCount();

        if (startViewIndex >= 0 && startViewIndex < rootViewCount) {
          int clipEndY = clip.y + clip.height;
          for (int i = startViewIndex; i < rootViewCount; i++) {
            View view = rootView.getView(i);
            Rectangle rec = component.modelToView(view.getStartOffset());
            if (rec == null) {
              break;
            }
            int y = rec.y;
            paintView(view, g, y);
            if (y >= clipEndY) {
              break;
            }
          }
        }

      } finally {
        foldHierarchy.unlock();
      }
    } catch (BadLocationException ble) {
      Mercurial.LOG.log(Level.WARNING, null, ble);
    } finally {
      doc.readUnlock();
    }
  }