Skip to content

Commit 99c05d5

Browse files
authored
Merge pull request #2061 from SAP/pr-jdk-17.0.17+7
Merge to tag jdk-17.0.17+7
2 parents 4ef1f15 + 3758e58 commit 99c05d5

File tree

8 files changed

+550
-106
lines changed

8 files changed

+550
-106
lines changed

src/java.desktop/share/classes/com/sun/java/swing/SwingUtilities3.java

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,11 +32,20 @@
3232
import java.util.Map;
3333
import java.util.WeakHashMap;
3434
import java.applet.Applet;
35+
import java.awt.Color;
3536
import java.awt.Component;
3637
import java.awt.Container;
38+
import java.awt.Graphics;
39+
import java.awt.Insets;
40+
import java.awt.Rectangle;
3741
import java.awt.Window;
42+
import javax.swing.ButtonModel;
43+
import javax.swing.Icon;
3844
import javax.swing.JComponent;
45+
import javax.swing.JMenu;
3946
import javax.swing.RepaintManager;
47+
import sun.swing.MenuItemLayoutHelper;
48+
import sun.swing.SwingUtilities2;
4049

4150
/**
4251
* A collection of utility methods for Swing.
@@ -57,6 +66,10 @@ public class SwingUtilities3 {
5766
private static final Object DELEGATE_REPAINT_MANAGER_KEY =
5867
new StringBuilder("DelegateRepaintManagerKey");
5968

69+
private static Color disabledForeground;
70+
private static Color acceleratorSelectionForeground;
71+
private static Color acceleratorForeground;
72+
6073
/**
6174
* Registers delegate RepaintManager for {@code JComponent}.
6275
*/
@@ -111,6 +124,128 @@ public static boolean isVsyncRequested(Container rootContainer) {
111124
return Boolean.TRUE == vsyncedMap.get(rootContainer);
112125
}
113126

127+
public static void applyInsets(Rectangle rect, Insets insets) {
128+
if (insets != null) {
129+
rect.x += insets.left;
130+
rect.y += insets.top;
131+
rect.width -= (insets.right + rect.x);
132+
rect.height -= (insets.bottom + rect.y);
133+
}
134+
}
135+
136+
public static void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh,
137+
MenuItemLayoutHelper.LayoutResult lr,
138+
Color holdc, Color foreground) {
139+
if (lh.getCheckIcon() != null) {
140+
ButtonModel model = lh.getMenuItem().getModel();
141+
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
142+
&& model.isSelected())) {
143+
g.setColor(foreground);
144+
} else {
145+
g.setColor(holdc);
146+
}
147+
if (lh.useCheckAndArrow()) {
148+
lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
149+
lr.getCheckRect().x, lr.getCheckRect().y);
150+
}
151+
g.setColor(holdc);
152+
}
153+
}
154+
155+
public static void paintIcon(Graphics g, MenuItemLayoutHelper lh,
156+
MenuItemLayoutHelper.LayoutResult lr, Color holdc) {
157+
if (lh.getIcon() != null) {
158+
Icon icon;
159+
ButtonModel model = lh.getMenuItem().getModel();
160+
if (!model.isEnabled()) {
161+
icon = lh.getMenuItem().getDisabledIcon();
162+
} else if (model.isPressed() && model.isArmed()) {
163+
icon = lh.getMenuItem().getPressedIcon();
164+
if (icon == null) {
165+
// Use default icon
166+
icon = lh.getMenuItem().getIcon();
167+
}
168+
} else {
169+
icon = lh.getMenuItem().getIcon();
170+
}
171+
172+
if (icon != null) {
173+
icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x,
174+
lr.getIconRect().y);
175+
g.setColor(holdc);
176+
}
177+
}
178+
}
179+
180+
181+
public static void paintAccText(Graphics g, MenuItemLayoutHelper lh,
182+
MenuItemLayoutHelper.LayoutResult lr) {
183+
if (!lh.getAccText().isEmpty()) {
184+
ButtonModel model = lh.getMenuItem().getModel();
185+
g.setFont(lh.getAccFontMetrics().getFont());
186+
if (!model.isEnabled()) {
187+
188+
// paint the accText disabled
189+
if (disabledForeground != null) {
190+
g.setColor(disabledForeground);
191+
SwingUtilities2.drawString(lh.getMenuItem(), g,
192+
lh.getAccText(), lr.getAccRect().x,
193+
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
194+
} else {
195+
g.setColor(lh.getMenuItem().getBackground().brighter());
196+
SwingUtilities2.drawString(lh.getMenuItem(), g,
197+
lh.getAccText(), lr.getAccRect().x,
198+
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
199+
g.setColor(lh.getMenuItem().getBackground().darker());
200+
SwingUtilities2.drawString(lh.getMenuItem(), g,
201+
lh.getAccText(), lr.getAccRect().x - 1,
202+
lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
203+
}
204+
} else {
205+
206+
// paint the accText normally
207+
if (model.isArmed()
208+
|| (lh.getMenuItem() instanceof JMenu
209+
&& model.isSelected())) {
210+
g.setColor(acceleratorSelectionForeground);
211+
} else {
212+
g.setColor(acceleratorForeground);
213+
}
214+
SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
215+
lr.getAccRect().x, lr.getAccRect().y +
216+
lh.getAccFontMetrics().getAscent());
217+
}
218+
}
219+
}
220+
221+
public static void setDisabledForeground(Color disabledFg) {
222+
disabledForeground = disabledFg;
223+
}
224+
225+
public static void setAcceleratorSelectionForeground(Color acceleratorSelectionFg) {
226+
acceleratorSelectionForeground = acceleratorSelectionFg;
227+
}
228+
229+
public static void setAcceleratorForeground(Color acceleratorFg) {
230+
acceleratorForeground = acceleratorFg;
231+
}
232+
233+
public static void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
234+
MenuItemLayoutHelper.LayoutResult lr,
235+
Color foreground) {
236+
if (lh.getArrowIcon() != null) {
237+
ButtonModel model = lh.getMenuItem().getModel();
238+
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
239+
&& model.isSelected())) {
240+
g.setColor(foreground);
241+
}
242+
if (lh.useCheckAndArrow()) {
243+
lh.getArrowIcon().paintIcon(lh.getMenuItem(), g,
244+
lr.getArrowRect().x, lr.getArrowRect().y);
245+
}
246+
}
247+
}
248+
114249
/**
115250
* Returns delegate {@code RepaintManager} for {@code component} hierarchy.
116251
*/

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicMenuItemUI.java

Lines changed: 51 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,17 +25,52 @@
2525

2626
package javax.swing.plaf.basic;
2727

28-
import java.awt.*;
29-
import java.awt.event.*;
28+
import java.awt.Color;
29+
import java.awt.Component;
30+
import java.awt.Dimension;
31+
import java.awt.Font;
32+
import java.awt.FontMetrics;
33+
import java.awt.Graphics;
34+
import java.awt.Insets;
35+
import java.awt.Point;
36+
import java.awt.Rectangle;
37+
import java.awt.event.ActionEvent;
38+
import java.awt.event.InputEvent;
39+
import java.awt.event.MouseEvent;
3040
import java.beans.PropertyChangeEvent;
3141
import java.beans.PropertyChangeListener;
3242

33-
import javax.swing.*;
34-
import javax.swing.event.*;
35-
import javax.swing.plaf.*;
43+
import javax.swing.ButtonModel;
44+
import javax.swing.Icon;
45+
import javax.swing.InputMap;
46+
import javax.swing.JCheckBoxMenuItem;
47+
import javax.swing.JComponent;
48+
import javax.swing.JMenu;
49+
import javax.swing.JMenuItem;
50+
import javax.swing.JRadioButtonMenuItem;
51+
import javax.swing.KeyStroke;
52+
import javax.swing.LookAndFeel;
53+
import javax.swing.MenuElement;
54+
import javax.swing.MenuSelectionManager;
55+
import javax.swing.SwingUtilities;
56+
import javax.swing.UIManager;
57+
import javax.swing.event.MenuDragMouseEvent;
58+
import javax.swing.event.MenuDragMouseListener;
59+
import javax.swing.event.MenuKeyListener;
60+
61+
import javax.swing.event.MouseInputListener;
62+
import javax.swing.plaf.ComponentInputMapUIResource;
63+
import javax.swing.plaf.ComponentUI;
64+
import javax.swing.plaf.MenuItemUI;
65+
import javax.swing.plaf.UIResource;
3666
import javax.swing.text.View;
3767

38-
import sun.swing.*;
68+
import com.sun.java.swing.SwingUtilities3;
69+
import sun.swing.MenuItemCheckIconFactory;
70+
import sun.swing.MenuItemLayoutHelper;
71+
import sun.swing.SwingUtilities2;
72+
import sun.swing.UIAction;
73+
3974

4075
/**
4176
* BasicMenuItem implementation
@@ -669,84 +704,22 @@ protected void paintMenuItem(Graphics g, JComponent c,
669704

670705
private void paintIcon(Graphics g, MenuItemLayoutHelper lh,
671706
MenuItemLayoutHelper.LayoutResult lr, Color holdc) {
672-
if (lh.getIcon() != null) {
673-
Icon icon;
674-
ButtonModel model = lh.getMenuItem().getModel();
675-
if (!model.isEnabled()) {
676-
icon = lh.getMenuItem().getDisabledIcon();
677-
} else if (model.isPressed() && model.isArmed()) {
678-
icon = lh.getMenuItem().getPressedIcon();
679-
if (icon == null) {
680-
// Use default icon
681-
icon = lh.getMenuItem().getIcon();
682-
}
683-
} else {
684-
icon = lh.getMenuItem().getIcon();
685-
}
686-
687-
if (icon != null) {
688-
icon.paintIcon(lh.getMenuItem(), g, lr.getIconRect().x,
689-
lr.getIconRect().y);
690-
g.setColor(holdc);
691-
}
692-
}
707+
SwingUtilities3.paintIcon(g, lh, lr, holdc);
693708
}
694709

695710
private void paintCheckIcon(Graphics g, MenuItemLayoutHelper lh,
696711
MenuItemLayoutHelper.LayoutResult lr,
697712
Color holdc, Color foreground) {
698-
if (lh.getCheckIcon() != null) {
699-
ButtonModel model = lh.getMenuItem().getModel();
700-
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
701-
&& model.isSelected())) {
702-
g.setColor(foreground);
703-
} else {
704-
g.setColor(holdc);
705-
}
706-
if (lh.useCheckAndArrow()) {
707-
lh.getCheckIcon().paintIcon(lh.getMenuItem(), g,
708-
lr.getCheckRect().x, lr.getCheckRect().y);
709-
}
710-
g.setColor(holdc);
711-
}
713+
SwingUtilities3.paintCheckIcon(g, lh, lr, holdc, foreground);
712714
}
713715

714716
private void paintAccText(Graphics g, MenuItemLayoutHelper lh,
715717
MenuItemLayoutHelper.LayoutResult lr) {
716-
if (!lh.getAccText().isEmpty()) {
717-
ButtonModel model = lh.getMenuItem().getModel();
718-
g.setFont(lh.getAccFontMetrics().getFont());
719-
if (!model.isEnabled()) {
720-
// *** paint the accText disabled
721-
if (disabledForeground != null) {
722-
g.setColor(disabledForeground);
723-
SwingUtilities2.drawString(lh.getMenuItem(), g,
724-
lh.getAccText(), lr.getAccRect().x,
725-
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
726-
} else {
727-
g.setColor(lh.getMenuItem().getBackground().brighter());
728-
SwingUtilities2.drawString(lh.getMenuItem(), g,
729-
lh.getAccText(), lr.getAccRect().x,
730-
lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
731-
g.setColor(lh.getMenuItem().getBackground().darker());
732-
SwingUtilities2.drawString(lh.getMenuItem(), g,
733-
lh.getAccText(), lr.getAccRect().x - 1,
734-
lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
735-
}
736-
} else {
737-
// *** paint the accText normally
738-
if (model.isArmed()
739-
|| (lh.getMenuItem() instanceof JMenu
740-
&& model.isSelected())) {
741-
g.setColor(acceleratorSelectionForeground);
742-
} else {
743-
g.setColor(acceleratorForeground);
744-
}
745-
SwingUtilities2.drawString(lh.getMenuItem(), g, lh.getAccText(),
746-
lr.getAccRect().x, lr.getAccRect().y +
747-
lh.getAccFontMetrics().getAscent());
748-
}
749-
}
718+
SwingUtilities3.setDisabledForeground(disabledForeground);
719+
SwingUtilities3.setAcceleratorSelectionForeground(
720+
acceleratorSelectionForeground);
721+
SwingUtilities3.setAcceleratorForeground(acceleratorForeground);
722+
SwingUtilities3.paintAccText(g, lh, lr);
750723
}
751724

752725
private void paintText(Graphics g, MenuItemLayoutHelper lh,
@@ -765,26 +738,11 @@ private void paintText(Graphics g, MenuItemLayoutHelper lh,
765738
private void paintArrowIcon(Graphics g, MenuItemLayoutHelper lh,
766739
MenuItemLayoutHelper.LayoutResult lr,
767740
Color foreground) {
768-
if (lh.getArrowIcon() != null) {
769-
ButtonModel model = lh.getMenuItem().getModel();
770-
if (model.isArmed() || (lh.getMenuItem() instanceof JMenu
771-
&& model.isSelected())) {
772-
g.setColor(foreground);
773-
}
774-
if (lh.useCheckAndArrow()) {
775-
lh.getArrowIcon().paintIcon(lh.getMenuItem(), g,
776-
lr.getArrowRect().x, lr.getArrowRect().y);
777-
}
778-
}
741+
SwingUtilities3.paintArrowIcon(g, lh, lr, foreground);
779742
}
780743

781744
private void applyInsets(Rectangle rect, Insets insets) {
782-
if(insets != null) {
783-
rect.x += insets.left;
784-
rect.y += insets.top;
785-
rect.width -= (insets.right + rect.x);
786-
rect.height -= (insets.bottom + rect.y);
787-
}
745+
SwingUtilities3.applyInsets(rect, insets);
788746
}
789747

790748
/**

src/java.desktop/windows/classes/com/sun/java/swing/plaf/windows/WindowsCheckBoxMenuItemUI.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
import java.awt.Rectangle;
3131

3232
import javax.swing.ButtonModel;
33+
import javax.swing.Icon;
3334
import javax.swing.JComponent;
3435
import javax.swing.JMenuItem;
3536
import javax.swing.plaf.ComponentUI;
@@ -71,6 +72,24 @@ protected void paintBackground(Graphics g, JMenuItem menuItem,
7172
}
7273
super.paintBackground(g, menuItem, bgColor);
7374
}
75+
76+
/**
77+
* Paint MenuItem.
78+
*/
79+
protected void paintMenuItem(Graphics g, JComponent c,
80+
Icon checkIcon, Icon arrowIcon,
81+
Color background, Color foreground,
82+
int defaultTextIconGap) {
83+
if (WindowsMenuItemUI.isVistaPainting()) {
84+
WindowsMenuItemUI.paintMenuItem(accessor, g, c, checkIcon,
85+
arrowIcon, background, foreground, defaultTextIconGap,
86+
menuItem, getPropertyPrefix());
87+
return;
88+
}
89+
super.paintMenuItem(g, c, checkIcon, arrowIcon, background,
90+
foreground, defaultTextIconGap);
91+
}
92+
7493
/**
7594
* Method which renders the text of the current menu item.
7695
*

0 commit comments

Comments
 (0)