Skip to content

Commit c1a9356

Browse files
committed
8299077: [REDO] JDK-4512626 Non-editable JTextArea provides no visual indication of keyboard focus
8299127: [REDO] JDK-8194048 Regression automated test '/open/test/jdk/javax/swing/text/DefaultCaret/HidingSelection/HidingSelectionTest.java' fails 8299128: [REDO] JDK-8213562 Test javax/swing/text/DefaultCaret/HidingSelection/MultiSelectionTest.java fails Backport-of: fef70d78baec9ce11d50b9a4c1fb26a1b854ccbf
1 parent 63aa076 commit c1a9356

File tree

4 files changed

+152
-160
lines changed

4 files changed

+152
-160
lines changed

src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java

Lines changed: 35 additions & 5 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, 2022, 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
@@ -366,6 +366,8 @@ protected void moveCaret(MouseEvent e) {
366366
}
367367
}
368368

369+
private int savedBlinkRate = 0;
370+
private boolean isBlinkRateSaved = false;
369371
// --- FocusListener methods --------------------------
370372

371373
/**
@@ -379,8 +381,21 @@ protected void moveCaret(MouseEvent e) {
379381
public void focusGained(FocusEvent e) {
380382
if (component.isEnabled()) {
381383
if (component.isEditable()) {
382-
setVisible(true);
384+
if (isBlinkRateSaved) {
385+
setBlinkRate(savedBlinkRate);
386+
savedBlinkRate = 0;
387+
isBlinkRateSaved = false;
388+
}
389+
} else {
390+
if (getBlinkRate() != 0) {
391+
if (!isBlinkRateSaved) {
392+
savedBlinkRate = getBlinkRate();
393+
isBlinkRateSaved = true;
394+
}
395+
setBlinkRate(0);
396+
}
383397
}
398+
setVisible(true);
384399
setSelectionVisible(true);
385400
updateSystemSelection();
386401
}
@@ -1031,17 +1046,29 @@ public void setVisible(boolean e) {
10311046
* @see Caret#setBlinkRate
10321047
*/
10331048
public void setBlinkRate(int rate) {
1049+
if (rate < 0) {
1050+
throw new IllegalArgumentException("Invalid blink rate: " + rate);
1051+
}
10341052
if (rate != 0) {
1035-
if (flasher == null) {
1036-
flasher = new Timer(rate, handler);
1053+
if (component.isEditable()) {
1054+
if (flasher == null) {
1055+
flasher = new Timer(rate, handler);
1056+
}
1057+
flasher.setDelay(rate);
1058+
} else {
1059+
savedBlinkRate = rate;
1060+
isBlinkRateSaved = true;
10371061
}
1038-
flasher.setDelay(rate);
10391062
} else {
10401063
if (flasher != null) {
10411064
flasher.stop();
10421065
flasher.removeActionListener(handler);
10431066
flasher = null;
10441067
}
1068+
if (component.isEditable() && isBlinkRateSaved) {
1069+
savedBlinkRate = 0;
1070+
isBlinkRateSaved = false;
1071+
}
10451072
}
10461073
}
10471074

@@ -1053,6 +1080,9 @@ public void setBlinkRate(int rate) {
10531080
* @see Caret#getBlinkRate
10541081
*/
10551082
public int getBlinkRate() {
1083+
if (isBlinkRateSaved) {
1084+
return savedBlinkRate;
1085+
}
10561086
return (flasher == null) ? 0 : flasher.getDelay();
10571087
}
10581088

test/jdk/ProblemList.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,6 @@ javax/swing/JPopupMenu/6800513/bug6800513.java 7184956 macosx-all
714714
javax/swing/JTabbedPane/8007563/Test8007563.java 8051591 generic-all
715715
javax/swing/JTabbedPane/4624207/bug4624207.java 8064922 macosx-all
716716
javax/swing/SwingUtilities/TestBadBreak/TestBadBreak.java 8160720 generic-all
717-
javax/swing/text/DefaultCaret/HidingSelection/HidingSelectionTest.java 8194048 windows-all
718-
javax/swing/text/DefaultCaret/HidingSelection/MultiSelectionTest.java 8213562 linux-all
719717
javax/swing/JFileChooser/6798062/bug6798062.java 8146446 windows-all
720718
javax/swing/JComboBox/8182031/ComboPopupTest.java 8196465 linux-all,macosx-all
721719
javax/swing/JFileChooser/6738668/bug6738668.java 8194946 generic-all

test/jdk/javax/swing/text/DefaultCaret/HidingSelection/HidingSelectionTest.java

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, 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
@@ -21,10 +21,17 @@
2121
* questions.
2222
*/
2323

24-
import javax.swing.*;
25-
import java.awt.*;
24+
import javax.swing.JFrame;
25+
import javax.swing.JMenu;
26+
import javax.swing.JMenuBar;
27+
import javax.swing.JMenuItem;
28+
import javax.swing.JTextField;
29+
import javax.swing.MenuSelectionManager;
30+
import javax.swing.SwingUtilities;
31+
import java.awt.FlowLayout;
32+
import java.awt.Point;
33+
import java.awt.Robot;
2634
import java.awt.event.InputEvent;
27-
import java.awt.image.BufferedImage;
2835

2936
/**
3037
* @test
@@ -39,7 +46,6 @@ public class HidingSelectionTest {
3946
private static JTextField field1;
4047
private static JTextField field2;
4148
private static JFrame frame;
42-
private static Rectangle bounds;
4349
private static JMenu menu;
4450
private static JTextField anotherWindow;
4551
private static Point menuLoc;
@@ -67,17 +73,9 @@ public static void main(String[] args) throws Exception {
6773
Robot robot = new Robot();
6874
robot.waitForIdle();
6975
robot.delay(200);
70-
SwingUtilities.invokeAndWait(() -> {
71-
bounds = field2.getBounds();
72-
bounds.setLocation(field2.getLocationOnScreen());
73-
});
74-
BufferedImage nosel = robot.createScreenCapture(bounds);
7576

7677
SwingUtilities.invokeAndWait(field2::requestFocus);
7778
SwingUtilities.invokeAndWait(field2::selectAll);
78-
robot.waitForIdle();
79-
robot.delay(200);
80-
BufferedImage sel = robot.createScreenCapture(bounds);
8179

8280
SwingUtilities.invokeAndWait(() -> {
8381
menuLoc = menu.getLocationOnScreen();
@@ -89,7 +87,7 @@ public static void main(String[] args) throws Exception {
8987
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
9088
robot.waitForIdle();
9189
robot.delay(200);
92-
if (!biEqual(robot.createScreenCapture(bounds), sel)) {
90+
if (!field2.getCaret().isSelectionVisible()) {
9391
throw new RuntimeException("Test fails: menu hides selection");
9492
}
9593

@@ -98,7 +96,7 @@ public static void main(String[] args) throws Exception {
9896
SwingUtilities.invokeAndWait(field1::requestFocus);
9997
robot.waitForIdle();
10098
robot.delay(200);
101-
if (!biEqual(robot.createScreenCapture(bounds), nosel)) {
99+
if (field2.getCaret().isSelectionVisible()) {
102100
throw new RuntimeException(
103101
"Test fails: focus lost doesn't hide selection");
104102
}
@@ -119,35 +117,12 @@ public static void main(String[] args) throws Exception {
119117
SwingUtilities.invokeAndWait(anotherWindow::requestFocus);
120118
robot.waitForIdle();
121119
robot.delay(200);
122-
if (biEqual(robot.createScreenCapture(bounds), nosel)) {
120+
if (!field2.getCaret().isSelectionVisible()) {
123121
throw new RuntimeException(
124122
"Test fails: switch window hides selection");
125123
}
126124

127-
SwingUtilities.invokeAndWait(anotherWindow::selectAll);
128-
robot.waitForIdle();
129-
robot.delay(200);
130-
if (biEqual(robot.createScreenCapture(bounds), sel)) {
131-
throw new RuntimeException(
132-
"Test fails: selection ownership is lost selection is shown");
133-
}
134-
135125
SwingUtilities.invokeLater(frame2::dispose);
136126
SwingUtilities.invokeLater(frame::dispose);
137127
}
138-
139-
static boolean biEqual(BufferedImage i1, BufferedImage i2) {
140-
if (i1.getWidth() == i2.getWidth() &&
141-
i1.getHeight() == i2.getHeight()) {
142-
for (int x = 0; x < i1.getWidth(); x++) {
143-
for (int y = 0; y < i1.getHeight(); y++) {
144-
if (i1.getRGB(x, y) != i2.getRGB(x, y)) {
145-
return false;
146-
}
147-
}
148-
}
149-
return true;
150-
}
151-
return false;
152-
}
153128
}

0 commit comments

Comments
 (0)