public Rectangle getVisualBounds(JComponent c, int type, int width, int height) { Rectangle bounds = new Rectangle(0, 0, width, height); if (type == VisuallyLayoutable.CLIP_BOUNDS) { return bounds; } AbstractButton b = (AbstractButton) c; if (type == VisuallyLayoutable.COMPONENT_BOUNDS && b.getBorder() != null && b.isBorderPainted()) { Border border = b.getBorder(); if (border instanceof BackgroundBorder) { border = ((BackgroundBorder) border).getBackgroundBorder(); if (border instanceof VisualMargin) { InsetsUtil.subtractInto(((VisualMargin) border).getVisualMargin(c), bounds); } else if (border instanceof QuaquaButtonBorder) { InsetsUtil.subtractInto(((QuaquaButtonBorder) border).getVisualMargin(c), bounds); } } return bounds; } String text = b.getText(); boolean isEmpty = (text == null || text.length() == 0); if (isEmpty) { text = " "; } Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon(); if ((icon == null) && (text == null)) { return null; } FontMetrics fm = c.getFontMetrics(c.getFont()); Insets insets = c.getInsets(viewInsets); viewR.x = insets.left; viewR.y = insets.top; viewR.width = width - (insets.left + insets.right); viewR.height = height - (insets.top + insets.bottom); iconR.x = iconR.y = iconR.width = iconR.height = 0; textR.x = textR.y = textR.width = textR.height = 0; String clippedText = layoutCL(b, fm, text, icon, viewR, iconR, textR); Rectangle textBounds = Fonts.getPerceivedBounds(text, c.getFont(), c); if (isEmpty) { textBounds.width = 0; } int ascent = fm.getAscent(); textR.x += textBounds.x; textR.width = textBounds.width; textR.y += ascent + textBounds.y; textR.height -= fm.getHeight() - textBounds.height; bounds.setBounds(textR); return bounds; }
public static void center(JComponent c, Rectangle r, boolean withInsets) { Rectangle visible = c.getVisibleRect(); visible.x = r.x - (visible.width - r.width) / 2; visible.y = r.y - (visible.height - r.height) / 2; Rectangle bounds = c.getBounds(); Insets i = withInsets ? new Insets(0, 0, 0, 0) : c.getInsets(); bounds.x = i.left; bounds.y = i.top; bounds.width -= i.left + i.right; bounds.height -= i.top + i.bottom; if (visible.x < bounds.x) visible.x = bounds.x; if (visible.x + visible.width > bounds.x + bounds.width) visible.x = bounds.x + bounds.width - visible.width; if (visible.y < bounds.y) visible.y = bounds.y; if (visible.y + visible.height > bounds.y + bounds.height) visible.y = bounds.y + bounds.height - visible.height; c.scrollRectToVisible(visible); }
private static void resetRects(JComponent c, int height) { Insets insets = c.getInsets(); viewRect.x = insets.left; viewRect.y = insets.top; viewRect.width = c.getWidth() - (insets.right + viewRect.x); viewRect.height = height - (insets.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; }
@Override public void paint(Graphics g, JComponent c) { JButton button = (JButton) c; String text = button.getText(); Icon icon = (button.isEnabled()) ? button.getIcon() : button.getDisabledIcon(); if ((icon == null) && (text == null)) { return; } FontMetrics fm = g.getFontMetrics(); paintViewInsets = c.getInsets(paintViewInsets); paintViewR.x = paintViewInsets.left; paintViewR.y = paintViewInsets.top; // Use inverted height & width paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right); paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom); paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0; paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0; Graphics2D g2 = (Graphics2D) g; AffineTransform tr = g2.getTransform(); if (angle == 90) { g2.rotate(Math.PI / 2); g2.translate(0, -c.getWidth()); paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2; paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2; } else if (angle == 270) { g2.rotate(-Math.PI / 2); g2.translate(-c.getHeight(), 0); paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2; paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2; } if (icon != null) { icon.paintIcon(c, g, paintIconR.x, paintIconR.y); } if (text != null) { int textX = paintTextR.x; int textY = paintTextR.y + fm.getAscent(); if (button.isEnabled()) { paintText(g, c, new Rectangle(paintViewR.x, paintViewR.y, textX, textY), text); } else { paintText(g, c, new Rectangle(paintViewR.x, paintViewR.y, textX, textY), text); } } g2.setTransform(tr); }
public static Window setSize(JComponent content, final Dimension size) { final Window popupWindow = SwingUtilities.windowForComponent(content); final Point location = popupWindow.getLocation(); popupWindow.setLocation(location.x, location.y); Insets insets = content.getInsets(); if (insets != null) { size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; } content.setPreferredSize(size); popupWindow.pack(); return popupWindow; }
/** The minumum size is the size of the display area plus insets plus the button. */ public Dimension getMinimumSize(JComponent c) { if (!isMinimumSizeDirty) { return new Dimension(cachedMinimumSize); } Dimension size = getDisplaySize(); Insets insets = getInsets(); size.height += insets.top + insets.bottom; if (comboBox.isEditable()) { Insets editorBorderInsets = UIManager.getInsets("ComboBox.editorBorderInsets"); size.width += editorBorderInsets.left + editorBorderInsets.right; // size.height += editorBorderInsets.top + editorBorderInsets.bottom; // The combo editor benefits from extra space for the caret. // To make editable and non-editable equally wide, // we always add 1 pixel. size.width += 1; } else if (arrowButton != null) { Insets arrowButtonInsets = arrowButton.getInsets(); size.width += arrowButtonInsets.left; } int buttonWidth = getEditableButtonWidth(); size.width += insets.left + insets.right + buttonWidth; // Honor corrections made in #paintCurrentValue ListCellRenderer renderer = comboBox.getRenderer(); if (renderer instanceof JComponent) { JComponent component = (JComponent) renderer; Insets rendererInsets = component.getInsets(); Insets editorInsets = UIManager.getInsets("ComboBox.editorInsets"); int offsetLeft = Math.max(0, editorInsets.left - rendererInsets.left); int offsetRight = Math.max(0, editorInsets.right - rendererInsets.right); // int offsetTop = Math.max(0, editorInsets.top - rendererInsets.top); // int offsetBottom = Math.max(0, editorInsets.bottom - rendererInsets.bottom); size.width += offsetLeft + offsetRight; // size.height += offsetTop + offsetBottom; } // The height is oriented on the JTextField height Dimension textFieldSize = PHANTOM.getMinimumSize(); size.height = Math.max(textFieldSize.height, size.height); cachedMinimumSize.setSize(size.width, size.height); isMinimumSizeDirty = false; return new Dimension(size); }
@Override public void paint(Graphics g, JComponent c) { String style = (String) c.getClientProperty("Quaqua.Button.style"); if (style != null && style.equals("help")) { Insets insets = c.getInsets(); UIManager.getIcon("Button.helpIcon").paintIcon(c, g, insets.left, insets.top); return; } Object oldHints = QuaquaUtilities.beginGraphics((Graphics2D) g); if (((AbstractButton) c).isBorderPainted()) { Border b = c.getBorder(); if (b != null && b instanceof BackgroundBorder) { ((BackgroundBorder) b) .getBackgroundBorder() .paintBorder(c, g, 0, 0, c.getWidth(), c.getHeight()); } } super.paint(g, c); QuaquaUtilities.endGraphics((Graphics2D) g, oldHints); Debug.paint(g, c, this); }
public void paint(final Graphics g, final JComponent c) { final AnchoredButton button = (AnchoredButton) c; final String text = button.getText(); final Icon icon = (button.isEnabled()) ? button.getIcon() : button.getDisabledIcon(); if ((icon == null) && (text == null)) { return; } final FontMetrics fm = button.getFontMetrics(button.getFont()); ourViewInsets = c.getInsets(ourViewInsets); ourViewRect.x = ourViewInsets.left; ourViewRect.y = ourViewInsets.top; final ToolWindowAnchor anchor = button.getAnchor(); // Use inverted height & width if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) { ourViewRect.height = c.getWidth() - (ourViewInsets.left + ourViewInsets.right); ourViewRect.width = c.getHeight() - (ourViewInsets.top + ourViewInsets.bottom); } else { ourViewRect.height = c.getHeight() - (ourViewInsets.left + ourViewInsets.right); ourViewRect.width = c.getWidth() - (ourViewInsets.top + ourViewInsets.bottom); } ourIconRect.x = ourIconRect.y = ourIconRect.width = ourIconRect.height = 0; ourTextRect.x = ourTextRect.y = ourTextRect.width = ourTextRect.height = 0; final String clippedText = SwingUtilities.layoutCompoundLabel( c, fm, text, icon, button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), ourViewRect, ourIconRect, ourTextRect, button.getText() == null ? 0 : button.getIconTextGap()); // Paint button's background final Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); final ButtonModel model = button.getModel(); final Color background = button.getBackground(); Color toBorder = model.isRollover() ? new Color(0, 0, 0, 50) : null; final boolean vertical = anchor == ToolWindowAnchor.LEFT || anchor == ToolWindowAnchor.RIGHT; if (model.isArmed() && model.isPressed() || model.isSelected()) { g2.setColor(new Color(0, 0, 0, 30)); g2.fillRect(3, 3, button.getWidth() - (vertical ? 6 : 5), button.getHeight() - 6); g2.setColor(new Color(0, 0, 0, 120)); g2.drawLine(2, 2, 3 + button.getWidth() - (vertical ? 7 : 6), 2); g2.drawLine(2, 3, 2, 3 + button.getHeight() - 7); g2.setColor(new Color(0, 0, 0, 40)); g2.drawRect(3, 3, button.getWidth() - (vertical ? 7 : 6), button.getHeight() - 7); g2.setColor(new Color(255, 255, 255, 110)); g2.drawLine( 3, button.getHeight() - 3, 3 + button.getWidth() - (vertical ? 6 : 5), button.getHeight() - 3); g2.drawLine( 3 + button.getWidth() - (vertical ? 6 : 5), 2, 3 + button.getWidth() - (vertical ? 6 : 5), 3 + button.getHeight() - 7); toBorder = null; } if (toBorder != null) { g.setColor(toBorder); g.drawRect(2, 2, button.getWidth() - (vertical ? 6 : 5), button.getHeight() - 6); } AffineTransform tr = null; if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) { tr = g2.getTransform(); if (ToolWindowAnchor.RIGHT == anchor) { if (icon != null) { // do not rotate icon icon.paintIcon(c, g2, ourIconRect.y, ourIconRect.x); } g2.rotate(Math.PI / 2); g2.translate(0, -c.getWidth()); } else { if (icon != null) { // do not rotate icon icon.paintIcon( c, g2, ourIconRect.y, c.getHeight() - ourIconRect.x - icon.getIconHeight()); } g2.rotate(-Math.PI / 2); g2.translate(-c.getHeight(), 0); } } else { if (icon != null) { icon.paintIcon(c, g2, ourIconRect.x, ourIconRect.y); } } // paint text if (text != null) { if (model.isEnabled()) { if (model.isArmed() && model.isPressed() || model.isSelected()) { g.setColor(background); } else { g.setColor(button.getForeground()); } } else { g.setColor(background.darker()); } /* Draw the Text */ if (model.isEnabled()) { /** * paint the text normally */ g.setColor(button.getForeground()); BasicGraphicsUtils.drawString( g, clippedText, button.getMnemonic2(), ourTextRect.x, ourTextRect.y + fm.getAscent()); } else { /** * paint the text disabled ** */ if (model.isSelected()) { g.setColor(c.getBackground()); } else { g.setColor(getDisabledTextColor()); } BasicGraphicsUtils.drawString( g, clippedText, button.getMnemonic2(), ourTextRect.x, ourTextRect.y + fm.getAscent()); } } if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) { g2.setTransform(tr); } }
public static void main(String args[]) { JComponent ch = new JComponent() {}; ch.getAccessibleContext(); ch.isFocusTraversable(); ch.setEnabled(false); ch.setEnabled(true); ch.requestFocus(); ch.requestFocusInWindow(); ch.getPreferredSize(); ch.getMaximumSize(); ch.getMinimumSize(); ch.contains(1, 2); Component c1 = ch.add(new Component() {}); Component c2 = ch.add(new Component() {}); Component c3 = ch.add(new Component() {}); Insets ins = ch.getInsets(); ch.getAlignmentY(); ch.getAlignmentX(); ch.getGraphics(); ch.setVisible(false); ch.setVisible(true); ch.setForeground(Color.red); ch.setBackground(Color.red); for (String font : Toolkit.getDefaultToolkit().getFontList()) { for (int j = 8; j < 17; j++) { Font f1 = new Font(font, Font.PLAIN, j); Font f2 = new Font(font, Font.BOLD, j); Font f3 = new Font(font, Font.ITALIC, j); Font f4 = new Font(font, Font.BOLD | Font.ITALIC, j); ch.setFont(f1); ch.setFont(f2); ch.setFont(f3); ch.setFont(f4); ch.getFontMetrics(f1); ch.getFontMetrics(f2); ch.getFontMetrics(f3); ch.getFontMetrics(f4); } } ch.enable(); ch.disable(); ch.reshape(10, 10, 10, 10); ch.getBounds(new Rectangle(1, 1, 1, 1)); ch.getSize(new Dimension(1, 2)); ch.getLocation(new Point(1, 2)); ch.getX(); ch.getY(); ch.getWidth(); ch.getHeight(); ch.isOpaque(); ch.isValidateRoot(); ch.isOptimizedDrawingEnabled(); ch.isDoubleBuffered(); ch.getComponentCount(); ch.countComponents(); ch.getComponent(1); ch.getComponent(2); Component[] cs = ch.getComponents(); ch.getLayout(); ch.setLayout(new FlowLayout()); ch.doLayout(); ch.layout(); ch.invalidate(); ch.validate(); ch.remove(0); ch.remove(c2); ch.removeAll(); ch.preferredSize(); ch.minimumSize(); ch.getComponentAt(1, 2); ch.locate(1, 2); ch.getComponentAt(new Point(1, 2)); ch.isFocusCycleRoot(new Container()); ch.transferFocusBackward(); ch.setName("goober"); ch.getName(); ch.getParent(); ch.getGraphicsConfiguration(); ch.getTreeLock(); ch.getToolkit(); ch.isValid(); ch.isDisplayable(); ch.isVisible(); ch.isShowing(); ch.isEnabled(); ch.enable(false); ch.enable(true); ch.enableInputMethods(false); ch.enableInputMethods(true); ch.show(); ch.show(false); ch.show(true); ch.hide(); ch.getForeground(); ch.isForegroundSet(); ch.getBackground(); ch.isBackgroundSet(); ch.getFont(); ch.isFontSet(); Container c = new Container(); c.add(ch); ch.getLocale(); for (Locale locale : Locale.getAvailableLocales()) ch.setLocale(locale); ch.getColorModel(); ch.getLocation(); boolean exceptions = false; try { ch.getLocationOnScreen(); } catch (IllegalComponentStateException e) { exceptions = true; } if (!exceptions) throw new RuntimeException("IllegalComponentStateException did not occur when expected"); ch.location(); ch.setLocation(1, 2); ch.move(1, 2); ch.setLocation(new Point(1, 2)); ch.getSize(); ch.size(); ch.setSize(1, 32); ch.resize(1, 32); ch.setSize(new Dimension(1, 32)); ch.resize(new Dimension(1, 32)); ch.getBounds(); ch.bounds(); ch.setBounds(10, 10, 10, 10); ch.setBounds(new Rectangle(10, 10, 10, 10)); ch.isLightweight(); ch.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); ch.getCursor(); ch.isCursorSet(); ch.inside(1, 2); ch.contains(new Point(1, 2)); ch.isFocusable(); ch.setFocusable(true); ch.setFocusable(false); ch.transferFocus(); ch.getFocusCycleRootAncestor(); ch.nextFocus(); ch.transferFocusUpCycle(); ch.hasFocus(); ch.isFocusOwner(); ch.toString(); ch.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); ch.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); ch.setComponentOrientation(ComponentOrientation.UNKNOWN); ch.getComponentOrientation(); }
public void paint(Graphics g, JComponent comp) { UIUtil.applyRenderingHints(g); JMenu jMenu = (JMenu) comp; ButtonModel buttonmodel = jMenu.getModel(); int mnemonicIndex = jMenu.getDisplayedMnemonicIndex(); Icon icon = getIcon(); Icon allowedIcon = getAllowedIcon(); Insets insets = comp.getInsets(); resetRects(); ourViewRect.setBounds(0, 0, jMenu.getWidth(), jMenu.getHeight()); ourViewRect.x += insets.left; ourViewRect.y += insets.top; ourViewRect.width -= insets.right + ourViewRect.x; ourViewRect.height -= insets.bottom + ourViewRect.y; Font font = g.getFont(); Font font1 = comp.getFont(); g.setFont(font1); FontMetrics fontmetrics = g.getFontMetrics(font1); String s1 = layoutMenuItem( fontmetrics, jMenu.getText(), icon, allowedIcon, arrowIcon, jMenu.getVerticalAlignment(), jMenu.getHorizontalAlignment(), jMenu.getVerticalTextPosition(), jMenu.getHorizontalTextPosition(), ourViewRect, ourIconRect, ourTextRect, ourAcceleratorRect, ourCheckIconRect, ourArrowIconRect, jMenu.getText() != null ? defaultTextIconGap : 0, defaultTextIconGap); Color color2 = g.getColor(); if (comp.isOpaque()) { g.setColor(jMenu.getBackground()); g.fillRect(0, 0, jMenu.getWidth(), jMenu.getHeight()); if (buttonmodel.isArmed() || buttonmodel.isSelected()) { if (UIUtil.isUnderAquaLookAndFeel()) { myAquaSelectedBackgroundPainter.paintBorder( comp, g, 0, 0, jMenu.getWidth(), jMenu.getHeight()); } else { g.setColor(selectionBackground); if (allowedIcon != null) { g.fillRect(k, 0, jMenu.getWidth() - k, jMenu.getHeight()); } else { g.fillRect(0, 0, jMenu.getWidth(), jMenu.getHeight()); g.setColor(selectionBackground); } } } g.setColor(color2); } if (allowedIcon != null) { if (buttonmodel.isArmed() || buttonmodel.isSelected()) { g.setColor(selectionForeground); } else { g.setColor(jMenu.getForeground()); } if (useCheckAndArrow()) { allowedIcon.paintIcon(comp, g, ourCheckIconRect.x, ourCheckIconRect.y); } g.setColor(color2); if (menuItem.isArmed()) { drawIconBorder(g); } } if (icon != null) { if (!buttonmodel.isEnabled()) { icon = jMenu.getDisabledIcon(); } else if (buttonmodel.isPressed() && buttonmodel.isArmed()) { icon = jMenu.getPressedIcon(); if (icon == null) { icon = jMenu.getIcon(); } } if (icon != null) { icon.paintIcon(comp, g, ourIconRect.x, ourIconRect.y); } } if (s1 != null && s1.length() > 0) { if (buttonmodel.isEnabled()) { if (buttonmodel.isArmed() || buttonmodel.isSelected()) { g.setColor(selectionForeground); } else { g.setColor(jMenu.getForeground()); } BasicGraphicsUtils.drawStringUnderlineCharAt( g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent()); } else { final Object disabledForeground = UIUtil.getMenuItemDisabledForeground(); if (disabledForeground instanceof Color) { g.setColor((Color) disabledForeground); BasicGraphicsUtils.drawStringUnderlineCharAt( g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent()); } else { g.setColor(jMenu.getBackground().brighter()); BasicGraphicsUtils.drawStringUnderlineCharAt( g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent()); g.setColor(jMenu.getBackground().darker()); BasicGraphicsUtils.drawStringUnderlineCharAt( g, s1, mnemonicIndex, ourTextRect.x - 1, (ourTextRect.y + fontmetrics.getAscent()) - 1); } } } if (arrowIcon != null) { if (buttonmodel.isArmed() || buttonmodel.isSelected()) { g.setColor(selectionForeground); } if (useCheckAndArrow()) { try { if (SystemInfo.isMac && myAquaInvertedArrowIcon != null && (buttonmodel.isArmed() || buttonmodel.isSelected()) && UIUtil.isUnderAquaLookAndFeel()) { myAquaInvertedArrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y); } else if (SystemInfo.isMac && myAquaDisabledArrowIcon != null && !buttonmodel.isEnabled() && UIUtil.isUnderAquaLookAndFeel()) { myAquaDisabledArrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y); } else arrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y); } catch (NullPointerException npe) { // GTKIconFactory$MenuArrowIcon.paintIcon since it doesn't expect to be given a null // instead of SynthContext // http://www.jetbrains.net/jira/browse/IDEADEV-22360 } } } g.setColor(color2); g.setFont(font); }