public Dimension getPreferredSize(JComponent c) {
    Dimension size;
    Insets border = progressBar.getInsets();
    FontMetrics fontSizer = progressBar.getFontMetrics(progressBar.getFont());

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      size = new Dimension(getPreferredInnerHorizontal());
      // Ensure that the progress string will fit
      if (progressBar.isStringPainted()) {
        // I'm doing this for completeness.
        String progString = progressBar.getString();
        int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progString);
        if (stringWidth > size.width) {
          size.width = stringWidth;
        }
        // This uses both Height and Descent to be sure that
        // there is more than enough room in the progress bar
        // for everything.
        // This does have a strange dependency on
        // getStringPlacememnt() in a funny way.
        int stringHeight = fontSizer.getHeight() + fontSizer.getDescent();
        if (stringHeight > size.height) {
          size.height = stringHeight;
        }
      }
    } else {
      size = new Dimension(getPreferredInnerVertical());
      // Ensure that the progress string will fit.
      if (progressBar.isStringPainted()) {
        String progString = progressBar.getString();
        int stringHeight = fontSizer.getHeight() + fontSizer.getDescent();
        if (stringHeight > size.width) {
          size.width = stringHeight;
        }
        // This is also for completeness.
        int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progString);
        if (stringWidth > size.height) {
          size.height = stringWidth;
        }
      }
    }

    size.width += border.left + border.right;
    size.height += border.top + border.bottom;
    return size;
  }
Esempio n. 2
0
  /*
   *  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;
  }
  /**
   * Designate the place where the progress string will be painted. This implementation places it at
   * the center of the progress bar (in both x and y). Override this if you want to right, left,
   * top, or bottom align the progress string or if you need to nudge it around for any reason.
   */
  protected Point getStringPlacement(
      Graphics g, String progressString, int x, int y, int width, int height) {
    FontMetrics fontSizer = SwingUtilities2.getFontMetrics(progressBar, g, progressBar.getFont());
    int stringWidth = SwingUtilities2.stringWidth(progressBar, fontSizer, progressString);

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      return new Point(
          x + Math.round(width / 2 - stringWidth / 2),
          y
              + ((height + fontSizer.getAscent() - fontSizer.getLeading() - fontSizer.getDescent())
                  / 2));
    } else { // VERTICAL
      return new Point(
          x
              + ((width - fontSizer.getAscent() + fontSizer.getLeading() + fontSizer.getDescent())
                  / 2),
          y + Math.round(height / 2 - stringWidth / 2));
    }
  }
 /**
  * Returns the baseline.
  *
  * @throws NullPointerException {@inheritDoc}
  * @throws IllegalArgumentException {@inheritDoc}
  * @see javax.swing.JComponent#getBaseline(int, int)
  * @since 1.6
  */
 public int getBaseline(JComponent c, int width, int height) {
   super.getBaseline(c, width, height);
   if (progressBar.isStringPainted() && progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
     FontMetrics metrics = progressBar.getFontMetrics(progressBar.getFont());
     Insets insets = progressBar.getInsets();
     int y = insets.top;
     height = height - insets.top - insets.bottom;
     return y + (height + metrics.getAscent() - metrics.getLeading() - metrics.getDescent()) / 2;
   }
   return -1;
 }