mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-23 20:44:41 +02:00
7129742: Unable to view focus in Non-Editable TextArea
Make sure the cursor is visible by setVisible(true) Reviewed-by: rupashka, alexp
This commit is contained in:
parent
846b62bbd6
commit
2f21d39ae5
3 changed files with 119 additions and 27 deletions
|
@ -657,10 +657,13 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer {
|
|||
}
|
||||
|
||||
|
||||
// TODO : fix this duplicate code
|
||||
class XAWTCaret extends DefaultCaret {
|
||||
static class XAWTCaret extends DefaultCaret {
|
||||
public void focusGained(FocusEvent e) {
|
||||
super.focusGained(e);
|
||||
if (getComponent().isEnabled()){
|
||||
// Make sure the cursor is visible in case of non-editable TextArea
|
||||
super.setVisible(true);
|
||||
}
|
||||
getComponent().repaint();
|
||||
}
|
||||
|
||||
|
|
|
@ -578,31 +578,7 @@ public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer {
|
|||
}
|
||||
|
||||
protected Caret createCaret() {
|
||||
return new XAWTCaret();
|
||||
}
|
||||
}
|
||||
|
||||
class XAWTCaret extends DefaultCaret {
|
||||
public void focusGained(FocusEvent e) {
|
||||
super.focusGained(e);
|
||||
getComponent().repaint();
|
||||
}
|
||||
|
||||
public void focusLost(FocusEvent e) {
|
||||
super.focusLost(e);
|
||||
getComponent().repaint();
|
||||
}
|
||||
|
||||
// Fix for 5100950: textarea.getSelectedText() returns the de-selected text, on XToolkit
|
||||
// Restoring Motif behaviour
|
||||
// If the text is unhighlighted then we should sets the selection range to zero
|
||||
public void setSelectionVisible(boolean vis) {
|
||||
if (vis){
|
||||
super.setSelectionVisible(vis);
|
||||
}else{
|
||||
// In order to de-select the selection
|
||||
setDot(getDot());
|
||||
}
|
||||
return new XTextAreaPeer.XAWTCaret();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Portions Copyright (c) 2012 IBM Corporation
|
||||
*/
|
||||
|
||||
|
||||
/* @test
|
||||
* @bug 7129742
|
||||
* @summary Focus in non-editable TextArea is not shown on Linux.
|
||||
* @author Sean Chou
|
||||
*/
|
||||
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.TextArea;
|
||||
import java.awt.Toolkit;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.DefaultCaret;
|
||||
|
||||
import sun.awt.SunToolkit;
|
||||
|
||||
public class bug7129742 {
|
||||
|
||||
public static DefaultCaret caret = null;
|
||||
public static JFrame frame = null;
|
||||
public static boolean fastreturn = false;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
|
||||
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
frame = new JFrame("Test");
|
||||
TextArea textArea = new TextArea("Non-editable textArea");
|
||||
textArea.setEditable(false);
|
||||
frame.setLayout(new FlowLayout());
|
||||
frame.add(textArea);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
||||
try {
|
||||
Class XTextAreaPeerClzz = textArea.getPeer().getClass();
|
||||
System.out.println(XTextAreaPeerClzz.getName());
|
||||
if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) {
|
||||
fastreturn = true;
|
||||
return;
|
||||
}
|
||||
|
||||
Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext");
|
||||
jtextField.setAccessible(true);
|
||||
JTextArea jtext = (JTextArea)jtextField.get(textArea.getPeer());
|
||||
caret = (DefaultCaret) jtext.getCaret();
|
||||
|
||||
textArea.requestFocusInWindow();
|
||||
} catch (NoSuchFieldException | SecurityException
|
||||
| IllegalArgumentException | IllegalAccessException e) {
|
||||
/* These exceptions mean the implementation of XTextAreaPeer is
|
||||
* changed, this testcase is not valid any more, fix it or remove.
|
||||
*/
|
||||
frame.dispose();
|
||||
throw new RuntimeException("This testcase is not valid any more!");
|
||||
}
|
||||
}
|
||||
});
|
||||
toolkit.realSync();
|
||||
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
if (fastreturn) {
|
||||
return;
|
||||
}
|
||||
boolean passed = caret.isActive();
|
||||
System.out.println("is caret visible : " + passed);
|
||||
|
||||
if (!passed) {
|
||||
throw new RuntimeException("The test for bug 71297422 failed");
|
||||
}
|
||||
} finally {
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue