Ejemplo n.º 1
0
  static void drawStringInRect(
      JComponent c,
      Graphics g,
      String aString,
      int x,
      int y,
      int width,
      int height,
      int justification) {
    FontMetrics fontMetrics;
    int drawWidth, startX, startY, delta;

    if (g.getFont() == null) {
      //            throw new InconsistencyException("No font set");
      return;
    }
    fontMetrics = SwingUtilities2.getFontMetrics(c, g);
    if (fontMetrics == null) {
      //            throw new InconsistencyException("No metrics for Font " + font());
      return;
    }

    if (justification == CENTER) {
      drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
      if (drawWidth > width) {
        drawWidth = width;
      }
      startX = x + (width - drawWidth) / 2;
    } else if (justification == RIGHT) {
      drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
      if (drawWidth > width) {
        drawWidth = width;
      }
      startX = x + width - drawWidth;
    } else {
      startX = x;
    }

    delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
    if (delta < 0) {
      delta = 0;
    }

    startY = y + height - delta - fontMetrics.getDescent();

    SwingUtilities2.drawString(c, g, aString, startX, startY);
  }
Ejemplo n.º 2
0
    /**
     * Paints the border for the specified component with the specified position and size.
     *
     * @param c the component for which this border is being painted
     * @param g the paint graphics
     * @param x the x position of the painted border
     * @param y the y position of the painted border
     * @param width the width of the painted border
     * @param height the height of the painted border
     */
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
      if (!(c instanceof JPopupMenu)) {
        return;
      }

      Font origFont = g.getFont();
      Color origColor = g.getColor();
      JPopupMenu popup = (JPopupMenu) c;

      String title = popup.getLabel();
      if (title == null) {
        return;
      }

      g.setFont(font);

      FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
      int fontHeight = fm.getHeight();
      int descent = fm.getDescent();
      int ascent = fm.getAscent();
      Point textLoc = new Point();
      int stringWidth = SwingUtilities2.stringWidth(popup, fm, title);

      textLoc.y = y + ascent + TEXT_SPACING;
      textLoc.x = x + ((width - stringWidth) / 2);

      g.setColor(background);
      g.fillRect(
          textLoc.x - TEXT_SPACING,
          textLoc.y - (fontHeight - descent),
          stringWidth + (2 * TEXT_SPACING),
          fontHeight - descent);
      g.setColor(foreground);
      SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

      MotifGraphicsUtils.drawGroove(
          g, x, textLoc.y + TEXT_SPACING, width, GROOVE_HEIGHT, shadowColor, highlightColor);

      g.setFont(origFont);
      g.setColor(origColor);
    }
Ejemplo n.º 3
0
  /**
   * Compute and return the location of the icons origin, the location of origin of the text
   * baseline, and a possibly clipped version of the compound labels string. Locations are computed
   * relative to the viewR rectangle.
   */
  private static String layoutMenuItem(
      JComponent c,
      FontMetrics fm,
      String text,
      FontMetrics fmAccel,
      String acceleratorText,
      Icon icon,
      Icon checkIcon,
      Icon arrowIcon,
      int verticalAlignment,
      int horizontalAlignment,
      int verticalTextPosition,
      int horizontalTextPosition,
      Rectangle viewR,
      Rectangle iconR,
      Rectangle textR,
      Rectangle acceleratorR,
      Rectangle checkIconR,
      Rectangle arrowIconR,
      int textIconGap,
      int menuItemGap) {

    SwingUtilities.layoutCompoundLabel(
        c,
        fm,
        text,
        icon,
        verticalAlignment,
        horizontalAlignment,
        verticalTextPosition,
        horizontalTextPosition,
        viewR,
        iconR,
        textR,
        textIconGap);

    /* Initialize the acceelratorText bounds rectangle textR.  If a null
     * or and empty String was specified we substitute "" here
     * and use 0,0,0,0 for acceleratorTextR.
     */
    if ((acceleratorText == null) || acceleratorText.equals("")) {
      acceleratorR.width = acceleratorR.height = 0;
      acceleratorText = "";
    } else {
      acceleratorR.width = SwingUtilities2.stringWidth(c, fmAccel, acceleratorText);
      acceleratorR.height = fmAccel.getHeight();
    }

    /* Initialize the checkIcon bounds rectangle checkIconR.
     */

    if (checkIcon != null) {
      checkIconR.width = checkIcon.getIconWidth();
      checkIconR.height = checkIcon.getIconHeight();
    } else {
      checkIconR.width = checkIconR.height = 0;
    }

    /* Initialize the arrowIcon bounds rectangle arrowIconR.
     */

    if (arrowIcon != null) {
      arrowIconR.width = arrowIcon.getIconWidth();
      arrowIconR.height = arrowIcon.getIconHeight();
    } else {
      arrowIconR.width = arrowIconR.height = 0;
    }

    Rectangle labelR = iconR.union(textR);
    if (MotifGraphicsUtils.isLeftToRight(c)) {
      textR.x += checkIconR.width + menuItemGap;
      iconR.x += checkIconR.width + menuItemGap;

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + viewR.width - arrowIconR.width - menuItemGap - acceleratorR.width;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x;
      arrowIconR.x = viewR.x + viewR.width - menuItemGap - arrowIconR.width;
    } else {
      textR.x -= (checkIconR.width + menuItemGap);
      iconR.x -= (checkIconR.width + menuItemGap);

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + arrowIconR.width + menuItemGap;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x + viewR.width - checkIconR.width;
      arrowIconR.x = viewR.x + menuItemGap;
    }

    // Align the accelertor text and the check and arrow icons vertically
    // with the center of the label rect.
    acceleratorR.y = labelR.y + (labelR.height / 2) - (acceleratorR.height / 2);
    arrowIconR.y = labelR.y + (labelR.height / 2) - (arrowIconR.height / 2);
    checkIconR.y = labelR.y + (labelR.height / 2) - (checkIconR.height / 2);

    /*
    System.out.println("Layout: v=" +viewR+"  c="+checkIconR+" i="+
    iconR+" t="+textR+" acc="+acceleratorR+" a="+arrowIconR);
    */
    return text;
  }