public int maxWidthTrue() { int max = 0; if (_popupUtil != null && _popupUtil.getFixedWidth() != 0) { max = _popupUtil.getFixedWidth(); max += _popupUtil.getBorderSize() * 2; if (max < PositionablePopupUtil.MIN_SIZE) { // don't let item disappear _popupUtil.setFixedWidth(PositionablePopupUtil.MIN_SIZE); max = PositionablePopupUtil.MIN_SIZE; } } else { if (_text && getText() != null) { if (getText().trim().length() == 0) { // show width of 1 blank character if (getFont() != null) { max = getFontMetrics(getFont()).stringWidth("0"); } } else { max = getFontMetrics(getFont()).stringWidth(getText()); } } if (_icon && _namedIcon != null) { max = Math.max(_namedIcon.getIconWidth(), max); } if (_text && _popupUtil != null) { max += _popupUtil.getMargin() * 2; max += _popupUtil.getBorderSize() * 2; } if (max < PositionablePopupUtil.MIN_SIZE) { // don't let item disappear max = PositionablePopupUtil.MIN_SIZE; } } if (debug) { log.trace("maxWidth= " + max + " preferred width= " + getPreferredSize().width); } return max; }
/** * create a text image whose bit map can be rotated * * @param text * @return */ private NamedIcon makeTextIcon(String text) { if (text == null || text.equals("")) { text = " "; } int width = getFontMetrics(getFont()).stringWidth(text); int height = getFontMetrics(getFont()).getHeight(); int hOffset = 0; int vOffset = getFontMetrics(getFont()).getAscent(); if (_popupUtil != null) { if (_popupUtil.getFixedWidth() != 0) { switch (_popupUtil.getJustification()) { case PositionablePopupUtil.LEFT: hOffset = _popupUtil.getBorderSize(); break; case PositionablePopupUtil.RIGHT: hOffset = _popupUtil.getFixedWidth() - width; hOffset += _popupUtil.getBorderSize(); break; default: hOffset = Math.max((_popupUtil.getFixedWidth() - width) / 2, 0); hOffset += _popupUtil.getBorderSize(); break; } width = _popupUtil.getFixedWidth() + 2 * _popupUtil.getBorderSize(); } else { width += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize()); hOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize(); } if (_popupUtil.getFixedHeight() != 0) { vOffset = Math.max(vOffset + (_popupUtil.getFixedHeight() - height) / 2, 0); vOffset += _popupUtil.getBorderSize(); height = _popupUtil.getFixedHeight() + 2 * _popupUtil.getBorderSize(); } else { height += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize()); vOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize(); } } BufferedImage bufIm = new BufferedImage(width + 2, height + 2, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bufIm.createGraphics(); g2d.setFont(getFont()); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); if (_popupUtil != null) { if (_popupUtil.hasBackground()) { g2d.setColor(_popupUtil.getBackground()); g2d.fillRect(0, 0, width, height); } if (_popupUtil.getBorderSize() != 0) { g2d.setColor(_popupUtil.getBorderColor()); g2d.setStroke(new java.awt.BasicStroke(2 * _popupUtil.getBorderSize())); g2d.drawRect(0, 0, width, height); } } g2d.setColor(getForeground()); g2d.drawString(text, hOffset, vOffset); NamedIcon icon = new NamedIcon(bufIm); g2d.dispose(); return icon; }
/** Create an image of icon with text overlaid */ protected NamedIcon makeTextOverlaidIcon(String text, NamedIcon ic) { String url = ic.getURL(); NamedIcon icon = new NamedIcon(url, url); int textWidth = getFontMetrics(getFont()).stringWidth(text); int iconWidth = icon.getIconWidth(); int textHeight = getFontMetrics(getFont()).getHeight(); int iconHeight = icon.getIconHeight(); int width = Math.max(textWidth, iconWidth); int height = Math.max(textHeight, iconHeight); int hOffset = Math.max((textWidth - iconWidth) / 2, 0); int vOffset = Math.max((textHeight - iconHeight) / 2, 0); if (_popupUtil != null) { if (_popupUtil.getFixedWidth() != 0) { switch (_popupUtil.getJustification()) { case PositionablePopupUtil.LEFT: hOffset = _popupUtil.getBorderSize(); break; case PositionablePopupUtil.RIGHT: hOffset = _popupUtil.getFixedWidth() - width; hOffset += _popupUtil.getBorderSize(); break; default: hOffset = Math.max((_popupUtil.getFixedWidth() - width) / 2, 0); hOffset += _popupUtil.getBorderSize(); break; } width = _popupUtil.getFixedWidth() + 2 * _popupUtil.getBorderSize(); } else { width += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize()); hOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize(); } if (_popupUtil.getFixedHeight() != 0) { vOffset = Math.max(vOffset + (_popupUtil.getFixedHeight() - height) / 2, 0); vOffset += _popupUtil.getBorderSize(); height = _popupUtil.getFixedHeight() + 2 * _popupUtil.getBorderSize(); } else { height += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize()); vOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize(); } } BufferedImage bufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bufIm.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); if (_popupUtil != null) { if (_popupUtil.hasBackground()) { g2d.setColor(_popupUtil.getBackground()); g2d.fillRect(0, 0, width, height); } if (_popupUtil.getBorderSize() != 0) { g2d.setColor(_popupUtil.getBorderColor()); g2d.setStroke(new java.awt.BasicStroke(2 * _popupUtil.getBorderSize())); g2d.drawRect(0, 0, width, height); } } g2d.drawImage( icon.getImage(), AffineTransform.getTranslateInstance(hOffset, vOffset + 1), this); g2d.setFont(getFont()); hOffset = Math.max((width - textWidth) / 2, 0); vOffset = Math.max((height - textHeight) / 2, 0) + getFontMetrics(getFont()).getAscent(); g2d.setColor(getForeground()); g2d.drawString(text, hOffset, vOffset); icon = new NamedIcon(bufIm); g2d.dispose(); icon.setURL(url); return icon; }