コード例 #1
0
ファイル: ActionUtils.java プロジェクト: njwhite/jsyntaxpane
 /**
  * Get the closest position within the document of the component that has given line and column.
  *
  * @param line the first being 1
  * @param column the first being 1
  * @return the closest position for the text component at given line and column
  */
 public static int getDocumentPosition(JTextComponent editor, int line, int column) {
   int lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
   int charWidth = editor.getFontMetrics(editor.getFont()).charWidth('m');
   int y = line * lineHeight;
   int x = column * charWidth;
   Point pt = new Point(x, y);
   int pos = editor.viewToModel(pt);
   return pos;
 }
コード例 #2
0
 /** {@inheritDoc} */
 public void paint(Graphics g) {
   if (isVisible() && overwriteMode) {
     Rectangle r = null;
     int pos = editor.getCaretPosition();
     try {
       r = editor.modelToView(pos);
     } catch (BadLocationException e) {
     }
     if (r != null && (r.width != 0 || r.height != 0)) {
       if ((x != r.x) || (y != r.y)) {
         repaint();
         x = r.x;
         y = r.y;
         height = r.height;
         width = editor.getFontMetrics(editor.getFont()).charWidth('W') + 1;
       } else {
         g.setColor(editor.getCaretColor());
         g.setXORMode(editor.getBackground());
         g.fillRect(x, y, width, height);
       }
     }
   } else {
     super.paint(g);
   }
 }
コード例 #3
0
 /* (non-Javadoc)
  * @see org.xamjwg.html.renderer.BaseInputControl#getPreferredWidthImpl(int, int)
  */
 protected int getPreferredWidthImpl(int availWidth, int availHeight) {
   int size = this.size;
   if (size == -1) {
     return 100;
   } else {
     JTextComponent widget = this.widget;
     FontMetrics fm = widget.getFontMetrics(widget.getFont());
     Insets insets = widget.getInsets();
     return insets.left + insets.right + fm.charWidth('0') * size;
   }
 }
コード例 #4
0
ファイル: Baseline.java プロジェクト: benbac20/jrdesktop
 /** Returns the baseline for single line text components, like <code>JTextField</code>. */
 private static int getSingleLineTextBaseline(JTextComponent textComponent, int h) {
   View rootView = textComponent.getUI().getRootView(textComponent);
   if (rootView.getViewCount() > 0) {
     Insets insets = textComponent.getInsets();
     int height = h - insets.top - insets.bottom;
     int y = insets.top;
     View fieldView = rootView.getView(0);
     int vspan = (int) fieldView.getPreferredSpan(View.Y_AXIS);
     if (height != vspan) {
       int slop = height - vspan;
       y += slop / 2;
     }
     FontMetrics fm = textComponent.getFontMetrics(textComponent.getFont());
     y += fm.getAscent();
     return y;
   }
   return -1;
 }
コード例 #5
0
ファイル: TextLineNumber.java プロジェクト: dejlek/jlib
  /*
   *  Determine the Y offset for the current row
   */
  private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    //  Get the bounding rectangle of the row

    Rectangle r = component.modelToView(rowStartOffset);
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;

    //  The text needs to be positioned above the bottom of the bounding
    //  rectangle based on the descent of the font(s) contained on the row.
    if (r.height == lineHeight) {
      // default font is being used
      descent = fontMetrics.getDescent();
    } else {
      // We need to check all the attributes for font changes
      if (fonts == null) {
        fonts = new HashMap<String, FontMetrics>();
      }

      Element root = component.getDocument().getDefaultRootElement();
      int index = root.getElementIndex(rowStartOffset);
      Element line = root.getElement(index);

      for (int i = 0; i < line.getElementCount(); i++) {
        Element child = line.getElement(i);
        AttributeSet as = child.getAttributes();
        String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
        Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
        String key = fontFamily + fontSize;

        FontMetrics fm = fonts.get(key);

        if (fm == null) {
          Font font = new Font(fontFamily, Font.PLAIN, fontSize);
          fm = component.getFontMetrics(font);
          fonts.put(key, fm);
        }

        descent = Math.max(descent, fm.getDescent());
      }
    }

    return y - descent;
  }
コード例 #6
0
ファイル: TextLineNumber.java プロジェクト: Horopter/Vignya
  /**
   * Draw the line numbers
   *
   * @param g
   */
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    ((Graphics2D) g)
        .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //	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) {
        break;
      }
    }
  }