Search in sources :

Example 1 with ButtonUI

use of javax.swing.plaf.ButtonUI in project intellij-community by JetBrains.

the class UIUtil method getCheckBoxTextHorizontalOffset.

public static int getCheckBoxTextHorizontalOffset(@NotNull JCheckBox cb) {
    // logic copied from javax.swing.plaf.basic.BasicRadioButtonUI.paint
    ButtonUI ui = cb.getUI();
    String text = cb.getText();
    Icon buttonIcon = cb.getIcon();
    if (buttonIcon == null && ui != null) {
        if (ui instanceof BasicRadioButtonUI) {
            buttonIcon = ((BasicRadioButtonUI) ui).getDefaultIcon();
        } else if (isUnderAquaLookAndFeel()) {
            // inheritors of AquaButtonToggleUI
            Ref<Method> cached = ourDefaultIconMethodsCache.get(ui.getClass());
            if (cached == null) {
                cached = Ref.create(ReflectionUtil.findMethod(Arrays.asList(ui.getClass().getMethods()), "getDefaultIcon", JComponent.class));
                ourDefaultIconMethodsCache.put(ui.getClass(), cached);
                if (!cached.isNull()) {
                    cached.get().setAccessible(true);
                }
            }
            Method method = cached.get();
            if (method != null) {
                try {
                    buttonIcon = (Icon) method.invoke(ui, cb);
                } catch (Exception e) {
                    cached.set(null);
                }
            }
        }
    }
    Dimension size = new Dimension();
    Rectangle viewRect = new Rectangle();
    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Insets i = cb.getInsets();
    size = cb.getSize(size);
    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = size.width - (i.right + viewRect.x);
    viewRect.height = size.height - (i.bottom + viewRect.y);
    iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
    textRect.x = textRect.y = textRect.width = textRect.height = 0;
    SwingUtilities.layoutCompoundLabel(cb, cb.getFontMetrics(cb.getFont()), text, buttonIcon, cb.getVerticalAlignment(), cb.getHorizontalAlignment(), cb.getVerticalTextPosition(), cb.getHorizontalTextPosition(), viewRect, iconRect, textRect, text == null ? 0 : cb.getIconTextGap());
    return textRect.x;
}
Also used : ButtonUI(javax.swing.plaf.ButtonUI) BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI)

Example 2 with ButtonUI

use of javax.swing.plaf.ButtonUI in project intellij-community by JetBrains.

the class JBCheckBox method setTextIcon.

/**
   * Sets given icon to display between checkbox icon and text.
   *
   * @return true in case of success and false otherwise
   */
public boolean setTextIcon(@NotNull Icon icon) {
    if (UIUtil.isUnderDarcula() || UIUtil.isUnderIntelliJLaF()) {
        return false;
    }
    ButtonUI ui = getUI();
    if (ui instanceof BasicRadioButtonUI) {
        Icon defaultIcon = ((BasicRadioButtonUI) ui).getDefaultIcon();
        if (defaultIcon != null) {
            MergedIcon mergedIcon = new MergedIcon(defaultIcon, 10, icon);
            setIcon(mergedIcon);
            return true;
        }
    }
    return false;
}
Also used : BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI) ButtonUI(javax.swing.plaf.ButtonUI) BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI)

Example 3 with ButtonUI

use of javax.swing.plaf.ButtonUI in project azure-tools-for-java by Microsoft.

the class SurveyPopUpDialog method renderUiByTheme.

private void renderUiByTheme() {
    // Use default ui setting for mac
    if (SystemUtils.IS_OS_MAC) {
        return;
    }
    final UIManager.LookAndFeelInfo theme = LafManager.getInstance().getCurrentLookAndFeel();
    if (StringUtils.containsIgnoreCase(theme.getName(), "light")) {
        setPanelBackGroundColor(contentPane, JBColor.WHITE);
        final ButtonUI buttonUI = new MetalButtonUI();
        giveFeedbackButton.setUI(buttonUI);
        giveFeedbackButton.setForeground(Gray._255);
        giveFeedbackButton.setBackground(new Color(0, 114, 198));
        notNowButton.setUI(buttonUI);
        notNowButton.setForeground(Gray._255);
        notNowButton.setBackground(Gray._105);
        buttonOnHoverColor = JBColor.LIGHT_GRAY;
    } else {
        setPanelBackGroundColor(contentPane, null);
        final ButtonUI buttonUI = new JButton().getUI();
        giveFeedbackButton.setForeground(null);
        giveFeedbackButton.setBackground(null);
        giveFeedbackButton.setUI(buttonUI);
        notNowButton.setForeground(null);
        notNowButton.setBackground(null);
        notNowButton.setUI(buttonUI);
        buttonOnHoverColor = JBColor.WHITE;
    }
    giveFeedbackButton.setBorderPainted(false);
    setButtonHoverListener(giveFeedbackButton);
    notNowButton.setBorderPainted(false);
    setButtonHoverListener(notNowButton);
}
Also used : JBColor(com.intellij.ui.JBColor) ButtonUI(javax.swing.plaf.ButtonUI) MetalButtonUI(javax.swing.plaf.metal.MetalButtonUI) MetalButtonUI(javax.swing.plaf.metal.MetalButtonUI)

Example 4 with ButtonUI

use of javax.swing.plaf.ButtonUI in project intellij-community by JetBrains.

the class NotificationsManagerImpl method createButtons.

@Nullable
private static JPanel createButtons(@NotNull Notification notification, @NotNull final JPanel content, @Nullable HyperlinkListener listener) {
    if (notification instanceof NotificationActionProvider) {
        JPanel buttons = new JPanel(new HorizontalLayout(5));
        buttons.setOpaque(false);
        content.add(BorderLayout.SOUTH, buttons);
        final Ref<JButton> defaultButton = new Ref<>();
        NotificationActionProvider provider = (NotificationActionProvider) notification;
        for (NotificationActionProvider.Action action : provider.getActions(listener)) {
            JButton button = new JButton(action) {

                @Override
                public void setUI(ButtonUI ui) {
                    boolean isDarcula = ui instanceof DarculaButtonUI && UIUtil.isUnderDarcula();
                    if (isDarcula) {
                        ui = new DarculaButtonUI() {

                            @Override
                            protected Color getButtonColor1() {
                                return new ColorUIResource(0x464b4c);
                            }

                            @Override
                            protected Color getButtonColor2() {
                                return new ColorUIResource(0x383c3d);
                            }
                        };
                    }
                    super.setUI(ui);
                    if (isDarcula) {
                        setBorder(new DarculaButtonPainter() {

                            @Override
                            protected Color getBorderColor() {
                                return new ColorUIResource(0x616263);
                            }
                        });
                    }
                }
            };
            button.setOpaque(false);
            if (action.isDefaultAction()) {
                defaultButton.setIfNull(button);
            }
            buttons.add(HorizontalLayout.RIGHT, button);
        }
        if (!defaultButton.isNull()) {
            UIUtil.addParentChangeListener(content, new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent event) {
                    if (event.getOldValue() == null && event.getNewValue() != null) {
                        UIUtil.removeParentChangeListener(content, this);
                        JRootPane rootPane = UIUtil.getRootPane(content);
                        if (rootPane != null) {
                            rootPane.setDefaultButton(defaultButton.get());
                        }
                    }
                }
            });
        }
        return buttons;
    }
    return null;
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) DarculaButtonPainter(com.intellij.ide.ui.laf.darcula.ui.DarculaButtonPainter) ButtonUI(javax.swing.plaf.ButtonUI) DarculaButtonUI(com.intellij.ide.ui.laf.darcula.ui.DarculaButtonUI) ColorUIResource(javax.swing.plaf.ColorUIResource) HorizontalLayout(com.intellij.ui.components.panels.HorizontalLayout) Ref(com.intellij.openapi.util.Ref) DarculaButtonUI(com.intellij.ide.ui.laf.darcula.ui.DarculaButtonUI) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ButtonUI

use of javax.swing.plaf.ButtonUI in project intellij-community by JetBrains.

the class UIUtil method getTextAlignBorder.

public static EmptyBorder getTextAlignBorder(@NotNull JToggleButton alignSource) {
    ButtonUI ui = alignSource.getUI();
    int leftGap = alignSource.getIconTextGap();
    Border border = alignSource.getBorder();
    if (border != null) {
        leftGap += border.getBorderInsets(alignSource).left;
    }
    if (ui instanceof BasicRadioButtonUI) {
        leftGap += ((BasicRadioButtonUI) alignSource.getUI()).getDefaultIcon().getIconWidth();
    } else {
        Method method = ReflectionUtil.getMethod(ui.getClass(), "getDefaultIcon", JComponent.class);
        if (method != null) {
            try {
                Object o = method.invoke(ui, alignSource);
                if (o instanceof Icon) {
                    leftGap += ((Icon) o).getIconWidth();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    return new EmptyBorder(0, leftGap, 0, 0);
}
Also used : ButtonUI(javax.swing.plaf.ButtonUI) BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI) Method(java.lang.reflect.Method) EmptyBorder(javax.swing.border.EmptyBorder) Border(javax.swing.border.Border) EmptyBorder(javax.swing.border.EmptyBorder) LineBorder(javax.swing.border.LineBorder) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasicRadioButtonUI(javax.swing.plaf.basic.BasicRadioButtonUI)

Aggregations

ButtonUI (javax.swing.plaf.ButtonUI)6 BasicRadioButtonUI (javax.swing.plaf.basic.BasicRadioButtonUI)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 DarculaButtonPainter (com.intellij.ide.ui.laf.darcula.ui.DarculaButtonPainter)1 DarculaButtonUI (com.intellij.ide.ui.laf.darcula.ui.DarculaButtonUI)1 Ref (com.intellij.openapi.util.Ref)1 JBColor (com.intellij.ui.JBColor)1 HorizontalLayout (com.intellij.ui.components.panels.HorizontalLayout)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 IOException (java.io.IOException)1 Border (javax.swing.border.Border)1 EmptyBorder (javax.swing.border.EmptyBorder)1 LineBorder (javax.swing.border.LineBorder)1 ColorUIResource (javax.swing.plaf.ColorUIResource)1 MetalButtonUI (javax.swing.plaf.metal.MetalButtonUI)1 Nullable (org.jetbrains.annotations.Nullable)1