# HG changeset patch # User alexsch # Date 1441977136 -14400 # Node ID 369c16c9c4d29b9bbb32d3f85f853d963264b7ba # Parent 7da13611c26eacb6731c7dab1f05ac8d710aad79 8025082: The behaviour of the highlight will be lost after clicking the set button Reviewed-by: serb, alexsch Contributed-by: Rajeev Chamyal diff -r 7da13611c26e -r 369c16c9c4d2 src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java --- a/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java Fri Sep 11 15:03:53 2015 +0300 +++ b/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java Fri Sep 11 17:12:16 2015 +0400 @@ -860,6 +860,7 @@ Highlighter.HighlightPainter p = getSelectionPainter(); try { selectionTag = h.addHighlight(p0, p1, p); + updateOwnsSelection(); } catch (BadLocationException bl) { selectionTag = null; } @@ -870,6 +871,7 @@ Highlighter h = component.getHighlighter(); h.removeHighlight(selectionTag); selectionTag = null; + updateOwnsSelection(); } } } @@ -1110,6 +1112,7 @@ if (selectionTag != null) { h.removeHighlight(selectionTag); selectionTag = null; + updateOwnsSelection(); } // otherwise, change or add the highlight } else { @@ -1120,6 +1123,7 @@ Highlighter.HighlightPainter p = getSelectionPainter(); selectionTag = h.addHighlight(p0, p1, p); } + updateOwnsSelection(); } catch (BadLocationException e) { throw new StateInvariantError("Bad caret position"); } @@ -1170,6 +1174,7 @@ if (this.dot != dot || this.dotBias != dotBias || selectionTag != null || forceCaretPositionChange) { changeCaretPosition(dot, dotBias); + updateOwnsSelection(); } this.markBias = this.dotBias; this.markLTR = dotLTR; @@ -1177,6 +1182,7 @@ if ((h != null) && (selectionTag != null)) { h.removeHighlight(selectionTag); selectionTag = null; + updateOwnsSelection(); } } @@ -1925,6 +1931,13 @@ } } + /** + * Updates ownsSelection based on text selection in the caret. + */ + private void updateOwnsSelection() { + ownsSelection = (selectionTag != null) + && SwingUtilities2.canAccessSystemClipboard(); + } private class DefaultFilterBypass extends NavigationFilter.FilterBypass { public Caret getCaret() { diff -r 7da13611c26e -r 369c16c9c4d2 test/javax/swing/JTextPane/bug8025082.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/javax/swing/JTextPane/bug8025082.java Fri Sep 11 17:12:16 2015 +0400 @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2015, 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. + */ + +/* + * @test + * @bug 8025082 + * @summary The behaviour of the highlight will be lost after clicking the set + * button. + * @run main bug8025082 + */ +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import javax.swing.*; + +public class bug8025082 { + + private static JButton button; + private static JFrame frame; + + public static void main(String[] args) throws Exception { + Robot robo = new Robot(); + robo.delay(500); + + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + createUI(); + } + }); + + robo.waitForIdle(); + Point point = getButtonLocationOnScreen(); + robo.mouseMove(point.x, point.y); + robo.mousePress(InputEvent.BUTTON1_MASK); + robo.mouseRelease(InputEvent.BUTTON1_MASK); + robo.waitForIdle(); + + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + frame.dispose(); + } + }); + } + + private static void createUI() { + frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(500, 500); + JTextPane textpane = new JTextPane(); + textpane.setText("Select Me"); + textpane.selectAll(); + + JPanel panel = new JPanel(new BorderLayout()); + panel.add(textpane, BorderLayout.CENTER); + button = new JButton("Press Me"); + panel.add(button, BorderLayout.SOUTH); + + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (!textpane.getCaret().isSelectionVisible()) { + throw new RuntimeException("Highlight removed after " + + "button click"); + } + } + }); + + frame.getContentPane().add(panel); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private static Point getButtonLocationOnScreen() throws Exception { + final Point[] result = new Point[1]; + + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + Point point = button.getLocationOnScreen(); + point.x += button.getWidth() / 2; + point.y += button.getHeight() / 2; + result[0] = point; + } + }); + return result[0]; + } +}