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