|
1 | 1 | /* |
2 | | - * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
36 | 36 | import java.applet.Applet; |
37 | 37 | import java.awt.AWTEvent; |
38 | 38 | import java.awt.EventQueue; |
| 39 | +import java.awt.Color; |
39 | 40 | import java.awt.Component; |
40 | 41 | import java.awt.Container; |
| 42 | +import java.awt.Graphics; |
| 43 | +import java.awt.Insets; |
| 44 | +import java.awt.Rectangle; |
41 | 45 | import java.awt.Window; |
| 46 | +import javax.swing.ButtonModel; |
| 47 | +import javax.swing.Icon; |
42 | 48 | import javax.swing.JComponent; |
| 49 | +import javax.swing.JMenu; |
43 | 50 | import javax.swing.RepaintManager; |
| 51 | +import sun.swing.MenuItemLayoutHelper; |
| 52 | +import sun.swing.SwingUtilities2; |
44 | 53 |
|
45 | 54 | /** |
46 | 55 | * A collection of utility methods for Swing. |
@@ -113,6 +122,119 @@ public static boolean isVsyncRequested(Container rootContainer) { |
113 | 122 | return Boolean.TRUE == vsyncedMap.get(rootContainer); |
114 | 123 | } |
115 | 124 |
|
| 125 | + public static void applyInsets(Rectangle rect, Insets insets) { |
| 126 | + if (insets != null) { |
| 127 | + rect.x += insets.left; |
| 128 | + rect.y += insets.top; |
| 129 | + rect.width -= (insets.right + rect.x); |
| 130 | + rect.height -= (insets.bottom + rect.y); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public static void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh, |
| 135 | + MenuItemLayoutHelper.LayoutResult lr, |
| 136 | + Color holdc, Color foreground) { |
| 137 | + if (lh.getCheckIcon() != null) { |
| 138 | + ButtonModel model = lh.getMenuItem().getModel(); |
| 139 | + if (model.isArmed() || (lh.getMenuItem() instanceof JMenu |
| 140 | + && model.isSelected())) { |
| 141 | + g.setColor(foreground); |
| 142 | + } else { |
| 143 | + g.setColor(holdc); |
| 144 | + } |
| 145 | + if (lh.useCheckAndArrow()) { |
| 146 | + lh.getCheckIcon().paintIcon(lh.getMenuItem(), g, |
| 147 | + lr.getCheckRect().x, lr.getCheckRect().y); |
| 148 | + } |
| 149 | + g.setColor(holdc); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static void paintIcon(Graphics g, MenuItemLayoutHelper lh, |
| 154 | + MenuItemLayoutHelper.LayoutResult lr, Color holdc) { |
| 155 | + if (lh.getIcon() != null) { |
| 156 | + Icon icon; |
| 157 | + ButtonModel model = lh.getMenuItem().getModel(); |
| 158 | + if (!model.isEnabled()) { |
| 159 | + icon = lh.getMenuItem().getDisabledIcon(); |
| 160 | + } else if (model.isPressed() && model.isArmed()) { |
| 161 | + icon = lh.getMenuItem().getPressedIcon(); |
| 162 | + if (icon == null) { |
| 163 | + // Use default icon |
| 164 | + icon = lh.getMenuItem().getIcon(); |
| 165 | + } |
| 166 | + } else { |
| 167 | + icon = lh.getMenuItem().getIcon(); |
| 168 | + } |
| 169 | + |
| 170 | + if (icon != null) { |
| 171 | + icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x, |
| 172 | + lr.getIconRect().y); |
| 173 | + g.setColor(holdc); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + public static void paintAccText(Graphics g, MenuItemLayoutHelper lh, |
| 180 | + MenuItemLayoutHelper.LayoutResult lr, |
| 181 | + Color disabledForeground, |
| 182 | + Color acceleratorSelectionForeground, |
| 183 | + Color acceleratorForeground) { |
| 184 | + if (!lh.getAccText().isEmpty()) { |
| 185 | + ButtonModel model = lh.getMenuItem().getModel(); |
| 186 | + g.setFont(lh.getAccFontMetrics().getFont()); |
| 187 | + if (!model.isEnabled()) { |
| 188 | + |
| 189 | + // paint the accText disabled |
| 190 | + if (disabledForeground != null) { |
| 191 | + g.setColor(disabledForeground); |
| 192 | + SwingUtilities2.drawString(lh.getMenuItem(), g, |
| 193 | + lh.getAccText(), lr.getAccRect().x, |
| 194 | + lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); |
| 195 | + } else { |
| 196 | + g.setColor(lh.getMenuItem().getBackground().brighter()); |
| 197 | + SwingUtilities2.drawString(lh.getMenuItem(), g, |
| 198 | + lh.getAccText(), lr.getAccRect().x, |
| 199 | + lr.getAccRect().y + lh.getAccFontMetrics().getAscent()); |
| 200 | + g.setColor(lh.getMenuItem().getBackground().darker()); |
| 201 | + SwingUtilities2.drawString(lh.getMenuItem(), g, |
| 202 | + lh.getAccText(), lr.getAccRect().x - 1, |
| 203 | + lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1); |
| 204 | + } |
| 205 | + } else { |
| 206 | + |
| 207 | + // paint the accText normally |
| 208 | + if (model.isArmed() |
| 209 | + || (lh.getMenuItem() instanceof JMenu |
| 210 | + && model.isSelected())) { |
| 211 | + g.setColor(acceleratorSelectionForeground); |
| 212 | + } else { |
| 213 | + g.setColor(acceleratorForeground); |
| 214 | + } |
| 215 | + SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(), |
| 216 | + lr.getAccRect().x, lr.getAccRect().y + |
| 217 | + lh.getAccFontMetrics().getAscent()); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + public static void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh, |
| 223 | + MenuItemLayoutHelper.LayoutResult lr, |
| 224 | + Color foreground) { |
| 225 | + if (lh.getArrowIcon() != null) { |
| 226 | + ButtonModel model = lh.getMenuItem().getModel(); |
| 227 | + if (model.isArmed() || (lh.getMenuItem() instanceof JMenu |
| 228 | + && model.isSelected())) { |
| 229 | + g.setColor(foreground); |
| 230 | + } |
| 231 | + if (lh.useCheckAndArrow()) { |
| 232 | + lh.getArrowIcon().paintIcon(lh.getMenuItem(), g, |
| 233 | + lr.getArrowRect().x, lr.getArrowRect().y); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
116 | 238 | /** |
117 | 239 | * Returns delegate {@code RepaintManager} for {@code component} hierarchy. |
118 | 240 | */ |
|
0 commit comments