7129742: Unable to view focus in Non-Editable TextArea
authorzhouyx
Fri, 20 Apr 2012 10:34:40 +0800
changeset 12523 a3c0565476da
parent 12418 f749f1ed92ca
child 12524 8745ce952a63
7129742: Unable to view focus in Non-Editable TextArea Summary: Make sure the cursor is visible by setVisible(true) Reviewed-by: rupashka, alexp
jdk/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java
jdk/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java
jdk/test/java/awt/TextArea/TextAreaCaretVisibilityTest/bug7129742.java
--- a/jdk/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java	Wed Apr 18 21:08:38 2012 +0400
+++ b/jdk/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java	Fri Apr 20 10:34:40 2012 +0800
@@ -657,10 +657,13 @@
     }
 
 
-    // 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();
         }
 
--- a/jdk/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java	Wed Apr 18 21:08:38 2012 +0400
+++ b/jdk/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java	Fri Apr 20 10:34:40 2012 +0800
@@ -578,31 +578,7 @@
         }
 
         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();
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/TextArea/TextAreaCaretVisibilityTest/bug7129742.java	Fri Apr 20 10:34:40 2012 +0800
@@ -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();
+                }
+            }
+        });
+    }
+
+}