1 /* 2 * Copyright (c) 2018, 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 * @test 25 * @bug 8199441 26 * @key headful 27 * @summary Verifies caret position in multiline line-wrapped text component 28 * in hidpi mode should be in sync with mouse press position. 29 * @run main/othervm -Dsun.java2d.uiScale=1.5 TestCaretPosition 30 */ 31 32 import javax.swing.JTextArea; 33 import javax.swing.JFrame; 34 import javax.swing.JScrollPane; 35 import javax.swing.SwingUtilities; 36 import javax.swing.text.BadLocationException; 37 import javax.swing.text.Caret; 38 import java.awt.Font; 39 import java.awt.BorderLayout; 40 import java.awt.Point; 41 import java.awt.Robot; 42 import java.awt.event.InputEvent; 43 import java.awt.event.MouseEvent; 44 import java.awt.event.MouseListener; 45 import java.awt.geom.Rectangle2D; 46 47 public class TestCaretPosition { 48 private static JTextArea jTextArea1; 49 private static JFrame f; 50 51 private static void createUI() { 52 jTextArea1 = new JTextArea(5, 80); 53 f = new JFrame(); 54 jTextArea1.setFont(new java.awt.Font("Arial", Font.PLAIN, 12)); 55 56 fillTextArea(jTextArea1); 57 jTextArea1.setLineWrap(true); 58 jTextArea1.addMouseListener(new MouseListener() { 59 @Override 60 public void mouseClicked(MouseEvent e) {} 61 62 @Override 63 public void mousePressed(MouseEvent e) { 64 try { 65 Caret caret = jTextArea1.getCaret(); 66 Rectangle2D rect = jTextArea1.modelToView2D(caret.getDot()); 67 68 if (Math.abs(e.getPoint().x - rect.getX()) > 5) { 69 System.out.println("mouse point " + e.getPoint()); 70 System.out.println("caret position " + rect); 71 throw new RuntimeException(" Wrong caret position"); 72 } 73 } catch (BadLocationException ex) {} 74 } 75 76 @Override 77 public void mouseReleased(MouseEvent e) {} 78 79 @Override 80 public void mouseEntered(MouseEvent e) {} 81 82 @Override 83 public void mouseExited(MouseEvent e) {} 84 }); 85 f.add(new JScrollPane(jTextArea1), BorderLayout.CENTER); 86 f.pack(); 87 f.setVisible(true); 88 } 89 90 public static void main(String args[]) throws Exception { 91 try { 92 93 SwingUtilities.invokeAndWait(() -> createUI()); 94 95 Point p = jTextArea1.getLocationOnScreen(); 96 Robot robot = new Robot(); 97 robot.setAutoDelay(200); 98 robot.mouseMove(p.x+ 480, p.y+6); 99 robot.waitForIdle(); 100 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 101 robot.waitForIdle(); 102 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 103 robot.waitForIdle(); 104 } finally { 105 SwingUtilities.invokeAndWait(() -> f.dispose()); 106 } 107 } 108 109 private static void fillTextArea(JTextArea area) { 110 StringBuilder buf = new StringBuilder(); 111 112 for (int i = 0; i < 3; i++) { 113 StringBuilder row = new StringBuilder(); 114 for (int j = 0; j < 50; j++) { 115 row.append(j); 116 if (j % 5 == 0) { 117 row.append(" "); 118 } 119 } 120 buf.append(row).append(System.lineSeparator()); 121 } 122 area.setText(buf.toString()); 123 area.setCaretPosition(0); 124 } 125 }