Skip to content

Commit ea83ae0

Browse files
author
duke
committedSep 19, 2023
Automatic merge of jdk:master into master
2 parents 2a7e32a + 0c97246 commit ea83ae0

File tree

4 files changed

+197
-68
lines changed

4 files changed

+197
-68
lines changed
 

‎src/java.desktop/windows/classes/sun/awt/windows/WMenuItemPeer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, 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
@@ -75,9 +75,9 @@ private void readShortcutLabel() {
7575
public void setLabel(String label) {
7676
//Fix for 6288578: PIT. Windows: Shortcuts displayed for the menuitems in a popup menu
7777
readShortcutLabel();
78-
_setLabel(label);
78+
_setLabel();
7979
}
80-
public native void _setLabel(String label);
80+
public native void _setLabel();
8181

8282
// Toolkit & peer internals
8383

‎src/java.desktop/windows/native/libawt/windows/awt_MenuItem.cpp

+6-63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, 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
@@ -48,7 +48,6 @@
4848
// struct for _SetLabel() method
4949
struct SetLabelStruct {
5050
jobject menuitem;
51-
jstring label;
5251
};
5352
// struct for _SetEnable() method
5453
struct SetEnableStruct {
@@ -685,7 +684,7 @@ void AwtMenuItem::DoCommand()
685684
}
686685
}
687686

688-
void AwtMenuItem::SetLabel(LPCTSTR sb)
687+
void AwtMenuItem::SetLabel()
689688
{
690689
AwtMenu* menu = GetMenuContainer();
691690
/* Fix for bug 4257944 by ssi@sparc.spb.su
@@ -704,12 +703,11 @@ void AwtMenuItem::SetLabel(LPCTSTR sb)
704703
memset(&mii, 0, sizeof(MENUITEMINFO));
705704
mii.cbSize = sizeof(MENUITEMINFO);
706705
mii.fMask = MIIM_CHECKMARKS | MIIM_DATA | MIIM_ID
707-
| MIIM_STATE | MIIM_SUBMENU | MIIM_TYPE;
706+
| MIIM_STATE | MIIM_SUBMENU | MIIM_FTYPE;
708707

709708
::GetMenuItemInfo(hMenu, GetID(), FALSE, &mii);
710709

711710
mii.fType = MFT_OWNERDRAW;
712-
mii.dwTypeData = (LPTSTR)(*sb);
713711

714712
// find index by menu item id
715713
int nMenuItemCount = ::GetMenuItemCount(hMenu);
@@ -784,72 +782,19 @@ void AwtMenuItem::_SetLabel(void *param) {
784782

785783
SetLabelStruct *sls = (SetLabelStruct *)param;
786784
jobject self = sls->menuitem;
787-
jstring label = sls->label;
788785

789786
int badAlloc = 0;
790787
AwtMenuItem *m = NULL;
791788

792789
PDATA pData;
793790
JNI_CHECK_PEER_GOTO(self, ret);
794791
m = (AwtMenuItem *)pData;
795-
// if (::IsWindow(m->GetOwnerHWnd()))
796-
{
797-
// fix for bug 4251036 MenuItem setLabel(null/"") behaves differently
798-
// under Win32 and Solaris
799-
jstring empty = NULL;
800-
if (JNU_IsNull(env, label))
801-
{
802-
empty = JNU_NewStringPlatform(env, TEXT(""));
803-
}
804-
if (env->ExceptionCheck()) {
805-
badAlloc = 1;
806-
goto ret;
807-
}
808-
LPCTSTR labelPtr;
809-
if (empty != NULL)
810-
{
811-
labelPtr = JNU_GetStringPlatformChars(env, empty, 0);
812-
}
813-
else
814-
{
815-
labelPtr = JNU_GetStringPlatformChars(env, label, 0);
816-
}
817-
if (labelPtr == NULL)
818-
{
819-
badAlloc = 1;
820-
}
821-
else
822-
{
823-
DASSERT(!IsBadStringPtr(labelPtr, 20));
824-
m->SetLabel(labelPtr);
825-
if (empty != NULL)
826-
{
827-
JNU_ReleaseStringPlatformChars(env, empty, labelPtr);
828-
}
829-
else
830-
{
831-
JNU_ReleaseStringPlatformChars(env, label, labelPtr);
832-
}
833-
}
834-
if (empty != NULL)
835-
{
836-
env->DeleteLocalRef(empty);
837-
}
838-
}
792+
m->SetLabel();
839793

840794
ret:
841795
env->DeleteGlobalRef(self);
842-
if (label != NULL)
843-
{
844-
env->DeleteGlobalRef(label);
845-
}
846796

847797
delete sls;
848-
849-
if (badAlloc)
850-
{
851-
throw std::bad_alloc();
852-
}
853798
} else {
854799
AwtToolkit::GetInstance().InvokeFunction(AwtMenuItem::_SetLabel, param);
855800
}
@@ -1031,17 +976,15 @@ Java_sun_awt_windows_WMenuItemPeer_initIDs(JNIEnv *env, jclass cls)
1031976
/*
1032977
* Class: sun_awt_windows_WMenuItemPeer
1033978
* Method: _setLabel
1034-
* Signature: (Ljava/lang/String;)V
979+
* Signature: ()V
1035980
*/
1036981
JNIEXPORT void JNICALL
1037-
Java_sun_awt_windows_WMenuItemPeer__1setLabel(JNIEnv *env, jobject self,
1038-
jstring label)
982+
Java_sun_awt_windows_WMenuItemPeer__1setLabel(JNIEnv *env, jobject self)
1039983
{
1040984
TRY;
1041985

1042986
SetLabelStruct *sls = new SetLabelStruct;
1043987
sls->menuitem = env->NewGlobalRef(self);
1044-
sls->label = (label == NULL) ? NULL : (jstring)env->NewGlobalRef(label);
1045988

1046989
AwtToolkit::GetInstance().SyncCall(AwtMenuItem::_SetLabel, sls);
1047990
// global refs and sls are deleted in _SetLabel

‎src/java.desktop/windows/native/libawt/windows/awt_MenuItem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, 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
@@ -133,7 +133,7 @@ class AwtMenuItem : public AwtObject {
133133
virtual BOOL IsTopMenu();
134134
void DrawCheck(HDC hDC, RECT rect);
135135

136-
void SetLabel(LPCTSTR sb);
136+
void SetLabel();
137137
virtual void Enable(BOOL isEnabled);
138138
virtual void UpdateContainerLayout();
139139
virtual void RedrawMenuBar();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Menu;
27+
import java.awt.MenuBar;
28+
import java.awt.MenuItem;
29+
import java.awt.Point;
30+
import java.awt.PopupMenu;
31+
import java.awt.Robot;
32+
import java.awt.event.InputEvent;
33+
import java.awt.event.MouseAdapter;
34+
import java.awt.event.MouseEvent;
35+
import java.awt.image.BufferedImage;
36+
import java.io.File;
37+
import java.io.IOException;
38+
import javax.imageio.ImageIO;
39+
40+
/*
41+
* @test
42+
* @key headful
43+
* @bug 4234266
44+
* @summary To test setLabel functionality on Windows.
45+
* @requires (os.family == "windows")
46+
*/
47+
48+
public class SetLabelTest {
49+
private static Robot robot;
50+
private static Frame frame;
51+
private static MenuBar mb;
52+
private static PopupMenu pm;
53+
private static Point frameLoc;
54+
private static final StringBuffer errorLog = new StringBuffer();
55+
56+
public static void main(String[] args) throws Exception {
57+
try {
58+
robot = new Robot();
59+
60+
EventQueue.invokeAndWait(() -> createTestUI());
61+
robot.delay(1000);
62+
EventQueue.invokeAndWait(() -> frameLoc = frame.getLocationOnScreen());
63+
64+
checkMenu();
65+
checkPopupMenu();
66+
robot.delay(200);
67+
if (!errorLog.isEmpty()) {
68+
throw new RuntimeException("Before & After screenshots are same." +
69+
" Test fails for the following case(s):\n" + errorLog);
70+
}
71+
} finally {
72+
EventQueue.invokeAndWait(() -> {
73+
if (frame != null) {
74+
frame.dispose();
75+
}
76+
});
77+
}
78+
}
79+
80+
private static void createTestUI() {
81+
frame = new Frame("Menu SetLabel Test");
82+
frame.setUndecorated(true);
83+
mb = new MenuBar();
84+
85+
//Menu 1
86+
Menu menu1 = new Menu("Menu");
87+
MenuItem mi1 = new MenuItem("Item-1");
88+
menu1.add(mi1);
89+
menu1.addSeparator();
90+
MenuItem mi2 = new MenuItem("Item-2");
91+
menu1.add(mi2);
92+
mb.add(menu1);
93+
94+
//Popup menu
95+
pm = new PopupMenu("Popup");
96+
MenuItem pm1 = new MenuItem("Item-1");
97+
pm.add(pm1);
98+
pm.addSeparator();
99+
MenuItem pm2 = new MenuItem("Item-2");
100+
pm.add(pm2);
101+
frame.add(pm);
102+
103+
frame.addMouseListener(new MouseAdapter() {
104+
@Override
105+
public void mousePressed(MouseEvent e) {
106+
showPopup(e);
107+
}
108+
109+
@Override
110+
public void mouseReleased(MouseEvent e) {
111+
showPopup(e);
112+
}
113+
114+
private void showPopup(MouseEvent e) {
115+
if (e.isPopupTrigger()) {
116+
pm.show(e.getComponent(),
117+
e.getX(), e.getY());
118+
}
119+
}
120+
});
121+
frame.setMenuBar(mb);
122+
frame.setSize(300, 200);
123+
frame.setLocation(500,500);
124+
frame.setVisible(true);
125+
}
126+
127+
private static void checkMenu() throws IOException {
128+
robot.mouseMove(frameLoc.x + 8, frameLoc.y + 8);
129+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
130+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
131+
robot.delay(200);
132+
BufferedImage beforeImgMenu = robot.createScreenCapture(frame.getBounds());
133+
134+
//Change Menu & MenuItem label
135+
Menu m1 = mb.getMenu(0);
136+
m1.setLabel("New Menu");
137+
m1.getItem(0).setLabel("New Item-1");
138+
m1.getItem(2).setLabel("New Item-2");
139+
robot.delay(200);
140+
BufferedImage afterImgMenu = robot.createScreenCapture(frame.getBounds());
141+
142+
if (compareImages(beforeImgMenu, afterImgMenu)) {
143+
errorLog.append("Menu case\n");
144+
ImageIO.write(beforeImgMenu, "png", new File("MenuBefore.png"));
145+
ImageIO.write(afterImgMenu, "png", new File("MenuAfter.png"));
146+
}
147+
}
148+
149+
private static void checkPopupMenu() throws IOException {
150+
robot.mouseMove(frameLoc.x + (frame.getWidth() / 2),
151+
frameLoc.y + (frame.getHeight() /2));
152+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
153+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
154+
robot.delay(200);
155+
BufferedImage beforeImgPopup = robot.createScreenCapture(frame.getBounds());
156+
157+
//Change popup menu label
158+
pm.setLabel("Changed Popup");
159+
pm.getItem(0).setLabel("New Item-1");
160+
pm.getItem(2).setLabel("New Item-2");
161+
robot.delay(200);
162+
BufferedImage afterImgPopup = robot.createScreenCapture(frame.getBounds());
163+
robot.mouseMove(frameLoc.x + (frame.getWidth() - 10),
164+
frameLoc.y + (frame.getHeight() - 10));
165+
robot.delay(100);
166+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
167+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
168+
169+
if (compareImages(beforeImgPopup, afterImgPopup)) {
170+
errorLog.append("Popup case\n");
171+
ImageIO.write(beforeImgPopup, "png", new File("PopupBefore.png"));
172+
ImageIO.write(afterImgPopup, "png", new File("PopupAfter.png"));
173+
}
174+
}
175+
176+
private static boolean compareImages(BufferedImage beforeImg, BufferedImage afterImg) {
177+
for (int x = 0; x < beforeImg.getWidth(); x++) {
178+
for (int y = 0; y < beforeImg.getHeight(); y++) {
179+
if (beforeImg.getRGB(x, y) != afterImg.getRGB(x, y)) {
180+
return false;
181+
}
182+
}
183+
}
184+
return true;
185+
}
186+
}

0 commit comments

Comments
 (0)
Failed to load comments.