public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { // determine the text (will be clipped and appended with "..." if too // long) and its location g.setFont(FONT); FontMetrics metrics = g.getFontMetrics(); VIEWRECT.x = x + 3; VIEWRECT.y = y; VIEWRECT.width = width - 6; VIEWRECT.height = INSETS.top; ICONRECT.setBounds(0, 0, 0, 0); TEXTRECT.setBounds(0, 0, 0, 0); String text = SwingUtilities.layoutCompoundLabel(metrics, heading, null, SwingConstants.CENTER, SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.LEFT, VIEWRECT, ICONRECT, TEXTRECT, 0); // draw the background g.setColor(background); g.fillRect(x, y, width, INSETS.top); // draw the text g.setColor(foreground); g.drawString(text, TEXTRECT.x, TEXTRECT.y + TEXT_Y_OFFSET); } }
public static Rectangle drawStringInRectClipped( Graphics g, FontMetrics fm, String strText, Rectangle rcClip, int iHorzAlign, int iVertAlign, boolean bReturnBounds ) { RCTEXT.setBounds( 0, 0, 0, 0 ); MessageDisplay.SCRATCH_RECT.setBounds( 0, 0, 0, 0 ); fm = fm == null ? g.getFontMetrics() : fm; String str = SwingUtilities.layoutCompoundLabel( fm, strText, null, // no icon iVertAlign, // vertical alignment iHorzAlign, // horizontal alignment BOTTOM, // vertical text pos relative to icon RIGHT, // horizontal text pos relative to icon rcClip, MessageDisplay.SCRATCH_RECT, RCTEXT, 0 ); // text/icon gap if( str != null && !str.equals( "" ) ) { g.drawString( str, RCTEXT.x, RCTEXT.y + fm.getAscent() ); } return bReturnBounds ? new Rectangle( RCTEXT ) : null; }
public void run() { if (text == null) { repaint(dirty); return; } if (fm == null) return; adjustText(text); SwingUtilities.layoutCompoundLabel(fm, text, null, SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT, SplashComponentPreview.this.view, new Rectangle(), rect, 0); //textDragItem.setRectangle(rect); textDragItem.setRectangle(SplashComponentPreview.this.view); dirty = dirty.union(rect); // update screen (assume repaint manager optimizes unions;) // repaint(dirty); repaint(); dirty = new Rectangle(rect); } });
@Override public Dimension getMinimumSize(JComponent c) { JRibbonTaskToggleButton b = (JRibbonTaskToggleButton) c; Icon icon = b.getIcon(); String text = "Www"; Font font = b.getFont(); FontMetrics fm = b.getFontMetrics(font); Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); SwingUtilities.layoutCompoundLabel(b, fm, text, icon, SwingUtilities.CENTER, b.getHorizontalAlignment(), SwingUtilities.CENTER, SwingUtilities.CENTER, viewR, iconR, textR, (text == null ? 0 : 6)); Rectangle r = iconR.union(textR); Insets insets = b.getInsets(); r.width += 4; r.height += insets.top + insets.bottom; return r.getSize(); } }
@Override protected Rectangle getTabsTextBoundsAt(int tabIndex) { Rectangle tabRect = _tabPane.getBoundsAt(tabIndex); Rectangle iconRect = new Rectangle(), textRect = new Rectangle(); String title = _tabPane.getDisplayTitleAt(tabIndex); Icon icon = _tabPane.getIconForTab(tabIndex); SwingUtilities.layoutCompoundLabel(_tabPane, _tabPane.getGraphics() .getFontMetrics(_tabPane.getFont()), title, icon, SwingUtilities.CENTER, SwingUtilities.CENTER, SwingUtilities.CENTER, SwingUtilities.TRAILING, tabRect, iconRect, textRect, icon == null ? 0 : _textIconGap); if (_tabPane.getTabPlacement() == JideTabbedPane.TOP || _tabPane.getTabPlacement() == JideTabbedPane.BOTTOM) { iconRect.x = tabRect.x + _iconMarginHorizon; textRect.x = (icon != null ? iconRect.x + iconRect.width + _textIconGap : tabRect.x + _textPadding); } else { iconRect.y = tabRect.y + _iconMarginVertical; textRect.y = (icon != null ? iconRect.y + iconRect.height + _textIconGap : tabRect.y + _textPadding); iconRect.x = tabRect.x + 2; textRect.x = tabRect.x + 2; } return textRect; }
@Override public Dimension getPreferredSize(JComponent c) { JRibbonTaskToggleButton b = (JRibbonTaskToggleButton) c; Icon icon = b.getIcon(); String text = b.getText(); Font font = b.getFont(); FontMetrics fm = b.getFontMetrics(font); Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); SwingUtilities.layoutCompoundLabel(b, fm, text, icon, SwingUtilities.CENTER, b.getHorizontalAlignment(), SwingUtilities.CENTER, SwingUtilities.CENTER, viewR, iconR, textR, (text == null ? 0 : 6)); Rectangle r = iconR.union(textR); Insets insets = b.getInsets(); r.width += insets.left + insets.right; r.height += insets.top + insets.bottom; return r.getSize(); }
private int getLabelBaseline(JLabel label, int height) { Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); FontMetrics fm = label.getFontMetrics(label.getFont()); resetRects(label, height); SwingUtilities.layoutCompoundLabel(label, fm, "a", icon, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(), viewRect, iconRect, textRect, label.getIconTextGap()); return textRect.y + fm.getAscent(); }
private static int getLabelBaseline(JLabel label, int height) { Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon(); FontMetrics fm = label.getFontMetrics(label.getFont()); resetRects(label, height); SwingUtilities.layoutCompoundLabel(label, fm, "a", icon, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(), viewRect, iconRect, textRect, label.getIconTextGap()); return textRect.y + fm.getAscent(); }
/** * {@inheritDoc} <p> * * Paints a diagonal cross over the text if the comp is of type JLabel, * does nothing otherwise. */ @Override protected void doPaint(Graphics2D g, JComponent comp, int width, int height) { if (!(comp instanceof JLabel)) return; JLabel label = (JLabel) comp; Insets insets = label.getInsets(insetss); paintViewR.x = insets.left; paintViewR.y = insets.top; paintViewR.width = width - (insets.left + insets.right); paintViewR.height = height - (insets.top + insets.bottom); paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; SwingUtilities.layoutCompoundLabel(label, label.getFontMetrics(label.getFont()), label.getText(), null, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(), paintViewR, paintIconR, paintTextR, label.getIconTextGap()); doPaint(g, paintTextR); }
/** * {@inheritDoc} <p> * * Paints a diagonal cross over the text if the comp is of type JLabel, * does nothing otherwise. */ @Override protected void doPaint(Graphics2D g, JComponent comp, int width, int height) { if (!(comp instanceof JLabel)) return; JLabel label = (JLabel) comp; Insets insets = label.getInsets(insetss); paintViewR.x = insets.left; paintViewR.y = insets.top; paintViewR.width = width - (insets.left + insets.right); paintViewR.height = height - (insets.top + insets.bottom); paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; SwingUtilities.layoutCompoundLabel(label, label.getFontMetrics(label.getFont()), label.getText(), null, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(), label.getHorizontalTextPosition(), paintViewR, paintIconR, paintTextR, label.getIconTextGap()); doPaint(g, paintTextR); }
private void paintWithForegroundPainterWithoutFilters(Graphics2D g2d) { paintWithoutForegroundPainter(g2d); if (getText() != null && !getText().isEmpty()) { Insets i = getInsets(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = getWidth() - (i.right + viewRect.x); viewRect.height = getHeight() - (i.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; // layout the text and icon String text = SwingUtilities.layoutCompoundLabel( this, g2d.getFontMetrics(), getText(), getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), viewRect, iconRect, textRect, getText() == null ? 0 : getIconTextGap()); if (!isPaintBorderInsets()) { g2d.translate(i.left, i.top); } g2d.setPaint(fgPaint); BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, text, getDisplayedMnemonicIndex(), textRect.x, textRect.y + g2d.getFontMetrics().getAscent()); } }
private void paintWithForegroundPainterWithoutFilters(Graphics2D g2d) { paintWithoutForegroundPainter(g2d); if (getText() != null && !getText().isEmpty()) { Insets i = getInsets(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = getWidth() - (i.right + viewRect.x); viewRect.height = getHeight() - (i.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; // layout the text and icon String text = SwingUtilities.layoutCompoundLabel( this, g2d.getFontMetrics(), getText(), getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), viewRect, iconRect, textRect, getText() == null ? 0 : getIconTextGap()); if (!isPaintBorderInsets()) { g2d.translate(i.left, i.top); } g2d.setPaint(fgPaint); BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, text, getDisplayedMnemonicIndex(), textRect.x, textRect.y + g2d.getFontMetrics().getAscent()); } }
/** * Returns the baseline for buttons. */ private int getButtonBaseline(AbstractButton button, int height) { FontMetrics fm = button.getFontMetrics(button.getFont()); resetRects(button, height); // NOTE: that we use "a" here to make sure we get a valid value, if // we were to pass in an empty string or null we would not get // back the right thing. SwingUtilities.layoutCompoundLabel( button, fm, "a", button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), viewRect, iconRect, textRect, button.getText() == null ? 0 : button.getIconTextGap()); return textRect.y + fm.getAscent() + 1; }
/** * Returns the baseline for buttons. */ private int getCheckBoxBaseline(AbstractButton button, int height) { FontMetrics fm = button.getFontMetrics(button.getFont()); resetRects(button, height); // NOTE: that we use "a" here to make sure we get a valid value, if // we were to pass in an empty string or null we would not get // back the right thing. SwingUtilities.layoutCompoundLabel( button, fm, "a", button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), viewRect, iconRect, textRect, button.getText() == null ? 0 : button.getIconTextGap()); return textRect.y + fm.getAscent(); } /**
@Override public void paint(Graphics g) { String fullText = super.getText(); shortText = SwingUtilities.layoutCompoundLabel( this, g.getFontMetrics(), fullText, getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), getItemRect(itemRect), iconRect, textRect, getIconTextGap()); /** TODO: setText is more heavyweight than we want in this * situation. Make JLabel.text protected instead of private. */ try { inpainting = true; // TODO JW: don't - override getText to return the short version // during painting setText(shortText); // temporarily truncate text super.paint(g); } finally { inpainting = false; setText(fullText); // restore full text } }
private String layout(AbstractButton b, FontMetrics fm, int width, int height) { Insets i = b.getInsets(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = width - (i.right + viewRect.x); viewRect.height = height - (i.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; // layout the text and icon return SwingUtilities.layoutCompoundLabel(b, fm, b.getText(), b .getIcon(), b.getVerticalAlignment(), b .getHorizontalAlignment(), b.getVerticalTextPosition(), b .getHorizontalTextPosition(), viewRect, iconRect, textRect, b .getText() == null ? 0 : b.getIconTextGap()); }
@Override public void paint(Graphics g) { String fullText = super.getText(); shortText = SwingUtilities.layoutCompoundLabel( this, g.getFontMetrics(), fullText, getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), getItemRect(itemRect), iconRect, textRect, getIconTextGap()); /** TODO: setText is more heavyweight than we want in this * situation. Make JLabel.text protected instead of private. */ try { inpainting = true; // TODO JW: don't - override getText to return the short version // during painting setText(shortText); // temporarily truncate text super.paint(g); } finally { inpainting = false; setText(fullText); // restore full text } }
@Override public void paint(Graphics g) { String fullText = super.getText(); shortText = SwingUtilities.layoutCompoundLabel( this, g.getFontMetrics(), fullText, getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), getItemRect(itemRect), iconRect, textRect, getIconTextGap()); /** TODO: setText is more heavyweight than we want in this * situation. Make JLabel.text protected instead of private. */ try { inpainting = true; // TODO JW: don't - override getText to return the short version // during painting setText(shortText); // temporarily truncate text super.paint(g); } finally { inpainting = false; setText(fullText); // restore full text } }
@Override public void paint(Graphics g) { String fullText = super.getText(); shortText = SwingUtilities.layoutCompoundLabel( this, g.getFontMetrics(), fullText, getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), getItemRect(itemRect), iconRect, textRect, getIconTextGap()); /** TODO: setText is more heavyweight than we want in this * situation. Make JLabel.text protected instead of private. */ try { inpainting = true; // TODO JW: don't - override getText to return the short version // during painting setText(shortText); // temporarily truncate text super.paint(g); } finally { inpainting = false; setText(fullText); // restore full text } }
@Override public void paint(Graphics g) { String fullText = super.getText(); shortText = SwingUtilities.layoutCompoundLabel( this, g.getFontMetrics(), fullText, getIcon(), getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), getItemRect(itemRect), iconRect, textRect, getIconTextGap()); /** TODO: setText is more heavyweight than we want in this * situation. Make JLabel.text protected instead of private. */ try { inpainting = true; // TODO JW: don't - override getText to return the short version // during painting setText(shortText); // temporarily truncate text super.paint(g); } finally { inpainting = false; setText(fullText); // restore full text } }