|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 8232243 |
| 26 | + * @key headful |
| 27 | + * @summary Verifies caret position in multiline JTextPane |
| 28 | + * in hidpi mode should be in sync with mouse press position. |
| 29 | + * @run main/othervm -Dsun.java2d.uiScale=2 TestCaretPositionJTextPane |
| 30 | + */ |
| 31 | + |
| 32 | +importjavax.swing.JComboBox; |
| 33 | +importjavax.swing.JTextPane; |
| 34 | +importjavax.swing.JFrame; |
| 35 | +importjavax.swing.JScrollPane; |
| 36 | +importjavax.swing.SwingUtilities; |
| 37 | +importjavax.swing.text.BadLocationException; |
| 38 | +importjavax.swing.text.Caret; |
| 39 | +importjava.awt.Font; |
| 40 | +importjava.awt.BorderLayout; |
| 41 | +importjava.awt.Point; |
| 42 | +importjava.awt.Robot; |
| 43 | +importjava.awt.event.InputEvent; |
| 44 | +importjava.awt.event.MouseEvent; |
| 45 | +importjava.awt.event.MouseListener; |
| 46 | +importjava.awt.geom.Rectangle2D; |
| 47 | + |
| 48 | +publicclassTestCaretPositionJTextPane { |
| 49 | +privatestaticJTextPanetextPane; |
| 50 | +privatestaticJFramef; |
| 51 | + |
| 52 | +privatestaticvoidcreateUI() { |
| 53 | +f = newJFrame("Test Cursor/Caret with Java 9"); |
| 54 | + |
| 55 | +textPane = newJTextPane(); |
| 56 | +textPane.setFont(newjava.awt.Font("Dialog", Font.PLAIN, 12)); |
| 57 | + |
| 58 | +fillTextPane(textPane); |
| 59 | + |
| 60 | +textPane.addMouseListener(newMouseListener() { |
| 61 | +@Override |
| 62 | +publicvoidmouseClicked(MouseEvente) {} |
| 63 | + |
| 64 | +@Override |
| 65 | +publicvoidmousePressed(MouseEvente) { |
| 66 | +try { |
| 67 | +Caretcaret = textPane.getCaret(); |
| 68 | +Rectangle2Drect = textPane.modelToView2D(caret.getDot()); |
| 69 | + |
| 70 | +if (Math.abs(e.getPoint().x - rect.getX()) > 5) { |
| 71 | +System.out.println("mouse point " + e.getPoint()); |
| 72 | +System.out.println("caret position " + rect); |
| 73 | +thrownewRuntimeException(" Wrong caret position"); |
| 74 | + } |
| 75 | + } catch (BadLocationExceptionex) {} |
| 76 | + } |
| 77 | + |
| 78 | +@Override |
| 79 | +publicvoidmouseReleased(MouseEvente) {} |
| 80 | + |
| 81 | +@Override |
| 82 | +publicvoidmouseEntered(MouseEvente) {} |
| 83 | + |
| 84 | +@Override |
| 85 | +publicvoidmouseExited(MouseEvente) {} |
| 86 | + }); |
| 87 | +f.add(newJScrollPane(textPane), BorderLayout.CENTER); |
| 88 | +f.pack(); |
| 89 | +f.setVisible(true); |
| 90 | + } |
| 91 | + |
| 92 | +publicstaticvoidmain(Stringargs[]) throwsException { |
| 93 | +try { |
| 94 | +Robotrobot = newRobot(); |
| 95 | +robot.setAutoDelay(200); |
| 96 | + |
| 97 | +SwingUtilities.invokeAndWait(() -> createUI()); |
| 98 | + |
| 99 | +robot.waitForIdle(); |
| 100 | +Pointp = textPane.getLocationOnScreen(); |
| 101 | +robot.mouseMove(p.x+ 480, p.y+6); |
| 102 | +robot.waitForIdle(); |
| 103 | +robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 104 | +robot.waitForIdle(); |
| 105 | +robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 106 | +robot.waitForIdle(); |
| 107 | + } finally { |
| 108 | +SwingUtilities.invokeAndWait(() -> f.dispose()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +privatestaticvoidfillTextPane(JTextPanetextPane) { |
| 113 | +StringBuilderbuf = newStringBuilder(); |
| 114 | + |
| 115 | +for (inti = 0; i < 30; i++) { |
| 116 | +StringBuilderrow = newStringBuilder(); |
| 117 | +for (intj = 0; j < 50; j++) { |
| 118 | +row.append(j); |
| 119 | +if (j % 5 == 0) { |
| 120 | +row.append(" "); |
| 121 | + } |
| 122 | + } |
| 123 | +buf.append(row).append(System.lineSeparator()); |
| 124 | + } |
| 125 | +textPane.setText(buf.toString()); |
| 126 | +textPane.setCaretPosition(0); |
| 127 | + } |
| 128 | +} |
0 commit comments