Пример #1
0
  /**
   * Create a line number component for a text component.
   *
   * @param component the related text component
   * @param minimumDisplayDigits the number of digits used to calculate the minimum width of the
   *     component
   */
  public TextLineNumber(JTextComponent component, int minimumDisplayDigits) {
    this.component = component;

    setFont(component.getFont());

    setBorderGap(5);
    setCurrentLineForeground(Color.RED);
    setDigitAlignment(RIGHT);
    setMinimumDisplayDigits(minimumDisplayDigits);

    component.getDocument().addDocumentListener(this);
    component.addCaretListener(this);
    component.addPropertyChangeListener("font", this);
  }
Пример #2
0
  /** Draw the line numbers */
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //	Determine the width of the space available to draw the line number

    FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
    Insets insets = getInsets();
    int availableWidth = getSize().width - insets.left - insets.right;

    //  Determine the rows to draw within the clipped bounds.

    Rectangle clip = g.getClipBounds();
    int rowStartOffset = component.viewToModel(new Point(0, clip.y));
    int endOffset = component.viewToModel(new Point(0, clip.y + clip.height));

    while (rowStartOffset <= endOffset) {
      try {
        if (isCurrentLine(rowStartOffset)) g.setColor(getCurrentLineForeground());
        else g.setColor(getForeground());

        //  Get the line number as a string and then determine the
        //  "X" and "Y" offsets for drawing the string.

        String lineNumber = getTextLineNumber(rowStartOffset);
        int stringWidth = fontMetrics.stringWidth(lineNumber);
        int x = getOffsetX(availableWidth, stringWidth) + insets.left;
        int y = getOffsetY(rowStartOffset, fontMetrics);
        g.drawString(lineNumber, x, y);

        //  Move to the next row

        rowStartOffset = Utilities.getRowEnd(component, rowStartOffset) + 1;
      } catch (Exception e) {
      }
    }
  }