Skip to content

Commit f4d7f43

Browse files
author
Daniel D. Daugherty
committed
8299123: [BACKOUT] 4512626 Non-editable JTextArea provides no visual indication of keyboard focus
Reviewed-by: tschatzl
1 parent 03afec1 commit f4d7f43

File tree

4 files changed

+160
-157
lines changed

4 files changed

+160
-157
lines changed

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

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, 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,8 +366,6 @@ protected void moveCaret(MouseEvent e) {
366366
}
367367
}
368368

369-
private int savedBlinkRate = 0;
370-
private boolean isBlinkRateSaved = false;
371369
// --- FocusListener methods --------------------------
372370

373371
/**
@@ -381,21 +379,8 @@ protected void moveCaret(MouseEvent e) {
381379
public void focusGained(FocusEvent e) {
382380
if (component.isEnabled()) {
383381
if (component.isEditable()) {
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-
}
382+
setVisible(true);
397383
}
398-
setVisible(true);
399384
setSelectionVisible(true);
400385
updateSystemSelection();
401386
}
@@ -1046,34 +1031,17 @@ public void setVisible(boolean e) {
10461031
* @see Caret#setBlinkRate
10471032
*/
10481033
public void setBlinkRate(int rate) {
1049-
if (rate < 0) {
1050-
throw new IllegalArgumentException("Invalid blink rate: " + rate);
1051-
}
10521034
if (rate != 0) {
1053-
if (component.isEditable()) {
1054-
if (flasher == null) {
1055-
flasher = new Timer(rate, handler);
1056-
}
1057-
flasher.setDelay(rate);
1058-
if (!flasher.isRunning()){
1059-
flasher.restart();
1060-
}
1061-
} else {
1062-
savedBlinkRate = rate;
1063-
isBlinkRateSaved = true;
1035+
if (flasher == null) {
1036+
flasher = new Timer(rate, handler);
10641037
}
1038+
flasher.setDelay(rate);
10651039
} else {
10661040
if (flasher != null) {
10671041
flasher.stop();
10681042
flasher.removeActionListener(handler);
10691043
flasher = null;
10701044
}
1071-
if (component.isEditable()) {
1072-
if (isBlinkRateSaved) {
1073-
savedBlinkRate = 0;
1074-
isBlinkRateSaved = false;
1075-
}
1076-
}
10771045
}
10781046
}
10791047

@@ -1085,9 +1053,6 @@ public void setBlinkRate(int rate) {
10851053
* @see Caret#getBlinkRate
10861054
*/
10871055
public int getBlinkRate() {
1088-
if (isBlinkRateSaved) {
1089-
return savedBlinkRate;
1090-
}
10911056
return (flasher == null) ? 0 : flasher.getDelay();
10921057
}
10931058

test/jdk/ProblemList.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ javax/swing/JPopupMenu/6800513/bug6800513.java 7184956 macosx-all
657657
javax/swing/JTabbedPane/8007563/Test8007563.java 8051591 generic-all
658658
javax/swing/JTabbedPane/4624207/bug4624207.java 8064922 macosx-all
659659
javax/swing/SwingUtilities/TestBadBreak/TestBadBreak.java 8160720 generic-all
660+
javax/swing/text/DefaultCaret/HidingSelection/HidingSelectionTest.java 8194048 windows-all
661+
javax/swing/text/DefaultCaret/HidingSelection/MultiSelectionTest.java 8213562 linux-all
660662
javax/swing/JFileChooser/6798062/bug6798062.java 8146446 windows-all
661663
javax/swing/JPopupMenu/4870644/bug4870644.java 8194130 macosx-all,linux-all
662664
javax/swing/dnd/8139050/NativeErrorsInTableDnD.java 8202765 macosx-all,linux-all

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

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

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;
24+
import javax.swing.*;
25+
import java.awt.*;
3426
import java.awt.event.InputEvent;
27+
import java.awt.image.BufferedImage;
3528

3629
/**
3730
* @test
@@ -46,6 +39,7 @@ public class HidingSelectionTest {
4639
private static JTextField field1;
4740
private static JTextField field2;
4841
private static JFrame frame;
42+
private static Rectangle bounds;
4943
private static JMenu menu;
5044
private static JTextField anotherWindow;
5145
private static Point menuLoc;
@@ -73,9 +67,17 @@ public static void main(String[] args) throws Exception {
7367
Robot robot = new Robot();
7468
robot.waitForIdle();
7569
robot.delay(200);
70+
SwingUtilities.invokeAndWait(() -> {
71+
bounds = field2.getBounds();
72+
bounds.setLocation(field2.getLocationOnScreen());
73+
});
74+
BufferedImage nosel = robot.createScreenCapture(bounds);
7675

7776
SwingUtilities.invokeAndWait(field2::requestFocus);
7877
SwingUtilities.invokeAndWait(field2::selectAll);
78+
robot.waitForIdle();
79+
robot.delay(200);
80+
BufferedImage sel = robot.createScreenCapture(bounds);
7981

8082
SwingUtilities.invokeAndWait(() -> {
8183
menuLoc = menu.getLocationOnScreen();
@@ -87,7 +89,7 @@ public static void main(String[] args) throws Exception {
8789
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
8890
robot.waitForIdle();
8991
robot.delay(200);
90-
if (!field2.getCaret().isSelectionVisible()) {
92+
if (!biEqual(robot.createScreenCapture(bounds), sel)) {
9193
throw new RuntimeException("Test fails: menu hides selection");
9294
}
9395

@@ -96,7 +98,7 @@ public static void main(String[] args) throws Exception {
9698
SwingUtilities.invokeAndWait(field1::requestFocus);
9799
robot.waitForIdle();
98100
robot.delay(200);
99-
if (field2.getCaret().isSelectionVisible()) {
101+
if (!biEqual(robot.createScreenCapture(bounds), nosel)) {
100102
throw new RuntimeException(
101103
"Test fails: focus lost doesn't hide selection");
102104
}
@@ -117,12 +119,35 @@ public static void main(String[] args) throws Exception {
117119
SwingUtilities.invokeAndWait(anotherWindow::requestFocus);
118120
robot.waitForIdle();
119121
robot.delay(200);
120-
if (!field2.getCaret().isSelectionVisible()) {
122+
if (biEqual(robot.createScreenCapture(bounds), nosel)) {
121123
throw new RuntimeException(
122124
"Test fails: switch window hides selection");
123125
}
124126

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+
125135
SwingUtilities.invokeLater(frame2::dispose);
126136
SwingUtilities.invokeLater(frame::dispose);
127137
}
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+
}
128153
}

0 commit comments

Comments
 (0)