From 3cb008ffed2c09affbaaa26e8e5113a78e4abdc2 Mon Sep 17 00:00:00 2001 From: serb Date: Tue, 1 Oct 2013 04:29:50 +0400 Subject: [PATCH] 7150100: [macosx] "0123456789" is selected in the TextField Reviewed-by: anthony, art --- .../sun/lwawt/LWTextComponentPeer.java | 3 +- .../classes/sun/awt/X11/XTextAreaPeer.java | 44 ++++++------- .../classes/sun/awt/X11/XTextFieldPeer.java | 41 ++++++------ .../SelectionVisible/SelectionVisible.html | 42 +++++++++++++ .../SelectionVisible/SelectionVisible.java | 62 ++++++++++++++++++ .../SelectionVisible/SelectionVisible.html | 42 +++++++++++++ .../SelectionVisible/SelectionVisible.java | 63 +++++++++++++++++++ 7 files changed, 247 insertions(+), 50 deletions(-) create mode 100644 test/java/awt/TextArea/SelectionVisible/SelectionVisible.html create mode 100644 test/java/awt/TextArea/SelectionVisible/SelectionVisible.java create mode 100644 test/java/awt/TextField/SelectionVisible/SelectionVisible.html create mode 100644 test/java/awt/TextField/SelectionVisible/SelectionVisible.java diff --git a/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java b/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java index 860aa5466..8616992e8 100644 --- a/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java +++ b/src/macosx/classes/sun/lwawt/LWTextComponentPeer.java @@ -71,13 +71,14 @@ abstract class LWTextComponentPeer start) { + // Should be called after setText() and setCaretPosition() select(start, end); } - setCaretPosition(getTarget().getCaretPosition()); firstChangeSkipped = true; } diff --git a/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java b/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java index 27465df84..7ada27c8c 100644 --- a/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java +++ b/src/solaris/classes/sun/awt/X11/XTextAreaPeer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2013, 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 @@ -64,16 +64,14 @@ import sun.awt.AWTAccessor; import sun.awt.SunToolkit; -class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { - boolean editable; +final class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { - AWTTextPane textPane; - AWTTextArea jtext; + private final AWTTextPane textPane; + private final AWTTextArea jtext; + private final boolean firstChangeSkipped; - boolean firstChangeSkipped; - - private final JavaMouseEventHandler javaMouseEventHandler - = new JavaMouseEventHandler( this ); + private final JavaMouseEventHandler javaMouseEventHandler = + new JavaMouseEventHandler(this); /* FIXME */ @@ -98,7 +96,7 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { * Create a Text area. */ XTextAreaPeer(TextArea target) { - super( target ); + super(target); // some initializations require that target be set even // though init(target) has not been called @@ -106,8 +104,7 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { //ComponentAccessor.enableEvents(target,AWTEvent.MOUSE_WHEEL_EVENT_MASK); - firstChangeSkipped = false; - String text = ((TextArea)target).getText(); + String text = target.getText(); jtext = new AWTTextArea(text, this); jtext.setWrapStyleWord(true); jtext.getDocument().addDocumentListener(jtext); @@ -143,25 +140,22 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { setFont(font); + // set the text of this object to the text of its target + setTextImpl(target.getText()); //?? should this be setText + int start = target.getSelectionStart(); int end = target.getSelectionEnd(); - - if (end > start) { - select(start, end); - } // Fix for 5100200 // Restoring Motif behaviour // Since the end position of the selected text can be greater then the length of the text, // so we should set caret to max position of the text - int caretPosition = Math.min(end, text.length()); - setCaretPosition(caretPosition); - + setCaretPosition(Math.min(end, text.length())); + if (end > start) { + // Should be called after setText() and setCaretPosition() + select(start, end); + } setEditable(target.isEditable()); - setScrollBarVisibility(); - // set the text of this object to the text of its target - setTextImpl(target.getText()); //?? should this be setText - // After this line we should not change the component's text firstChangeSkipped = true; } @@ -408,7 +402,6 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { * @see java.awt.peer.TextComponentPeer */ public void setEditable(boolean editable) { - this.editable = editable; if (jtext != null) jtext.setEditable(editable); repaintText(); } @@ -461,7 +454,7 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { repaintText(); } - protected boolean setTextImpl(String txt) { + private void setTextImpl(String txt) { if (jtext != null) { // JTextArea.setText() posts two different events (remove & insert). // Since we make no differences between text events, @@ -474,7 +467,6 @@ class XTextAreaPeer extends XComponentPeer implements TextAreaPeer { } jtext.getDocument().addDocumentListener(jtext); } - return true; } /** diff --git a/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java b/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java index be948e2a7..8bb745599 100644 --- a/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java +++ b/src/solaris/classes/sun/awt/X11/XTextFieldPeer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2013, 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 @@ -57,46 +57,41 @@ import sun.util.logging.PlatformLogger; import sun.awt.CausedFocusEvent; import sun.awt.AWTAccessor; -public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { +final class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XTextField"); - String text; - XAWTTextField xtext; + private String text; + private final XAWTTextField xtext; + private final boolean firstChangeSkipped; - boolean firstChangeSkipped; - - public XTextFieldPeer(TextField target) { + XTextFieldPeer(TextField target) { super(target); - int start, end; - firstChangeSkipped = false; text = target.getText(); xtext = new XAWTTextField(text,this, target.getParent()); xtext.getDocument().addDocumentListener(xtext); xtext.setCursor(target.getCursor()); XToolkit.specialPeerMap.put(xtext,this); - TextField txt = (TextField) target; initTextField(); - setText(txt.getText()); - if (txt.echoCharIsSet()) { - setEchoChar(txt.getEchoChar()); + setText(target.getText()); + if (target.echoCharIsSet()) { + setEchoChar(target.getEchoChar()); } else setEchoChar((char)0); - start = txt.getSelectionStart(); - end = txt.getSelectionEnd(); - - if (end > start) { - select(start, end); - } + int start = target.getSelectionStart(); + int end = target.getSelectionEnd(); // Fix for 5100200 // Restoring Motif behaviour // Since the end position of the selected text can be greater then the length of the text, // so we should set caret to max position of the text - int caretPosition = Math.min(end, text.length()); - setCaretPosition(caretPosition); + setCaretPosition(Math.min(end, text.length())); + if (end > start) { + // Should be called after setText() and setCaretPosition() + select(start, end); + } - setEditable(txt.isEditable()); + setEditable(target.isEditable()); // After this line we should not change the component's text firstChangeSkipped = true; @@ -219,7 +214,7 @@ public class XTextFieldPeer extends XComponentPeer implements TextFieldPeer { repaint(); } - protected boolean setXAWTTextField(String txt) { + private boolean setXAWTTextField(String txt) { text = txt; if (xtext != null) { // JTextField.setText() posts two different events (remove & insert). diff --git a/test/java/awt/TextArea/SelectionVisible/SelectionVisible.html b/test/java/awt/TextArea/SelectionVisible/SelectionVisible.html new file mode 100644 index 000000000..efa092ba9 --- /dev/null +++ b/test/java/awt/TextArea/SelectionVisible/SelectionVisible.html @@ -0,0 +1,42 @@ + + + + + + SelectionVisible + + + +

SelectionVisible
Bugid: 4082144

+ + + + diff --git a/test/java/awt/TextArea/SelectionVisible/SelectionVisible.java b/test/java/awt/TextArea/SelectionVisible/SelectionVisible.java new file mode 100644 index 000000000..c34f75f60 --- /dev/null +++ b/test/java/awt/TextArea/SelectionVisible/SelectionVisible.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1999, 2013, 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. + */ + + +import java.applet.Applet; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Panel; +import java.awt.TextArea; + +public final class SelectionVisible extends Applet { + + TextArea tf; + + @Override + public void init() { + tf = new TextArea(3, 20); + tf.setText("0123456789"); + tf.select(0, 6); + + final TextArea ta = new TextArea("INSTRUCTIONS:\n" + + "The text 012345 should be selected in the TextArea.\n" + + "If this is what you observe, then the test passes.\n" + + "Otherwise, the test fails.", 40, 5, + TextArea.SCROLLBARS_NONE); + ta.setEditable(false); + ta.setPreferredSize(new Dimension(300, 70)); + final Panel panel = new Panel(); + panel.setLayout(new FlowLayout()); + panel.add(tf); + setLayout(new BorderLayout()); + add(ta, BorderLayout.CENTER); + add(panel, BorderLayout.PAGE_END); + } + + @Override + public void start() { + setVisible(true); + tf.requestFocus(); + } +} diff --git a/test/java/awt/TextField/SelectionVisible/SelectionVisible.html b/test/java/awt/TextField/SelectionVisible/SelectionVisible.html new file mode 100644 index 000000000..31490d16f --- /dev/null +++ b/test/java/awt/TextField/SelectionVisible/SelectionVisible.html @@ -0,0 +1,42 @@ + + + + + + SelectionVisible + + + +

SelectionVisible
Bugid: 4082144

+ + + + diff --git a/test/java/awt/TextField/SelectionVisible/SelectionVisible.java b/test/java/awt/TextField/SelectionVisible/SelectionVisible.java new file mode 100644 index 000000000..cccec8eb3 --- /dev/null +++ b/test/java/awt/TextField/SelectionVisible/SelectionVisible.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1999, 2013, 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. + */ + + +import java.applet.Applet; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Panel; +import java.awt.TextArea; +import java.awt.TextField; + +public final class SelectionVisible extends Applet { + + TextField tf; + + @Override + public void init() { + tf = new TextField(20); + tf.setText("0123456789"); + tf.select(0, 6); + + final TextArea ta = new TextArea("INSTRUCTIONS:\n" + + "The text 012345 should be selected in the TextField.\n" + + "If this is what you observe, then the test passes.\n" + + "Otherwise, the test fails.", 40, 5, + TextArea.SCROLLBARS_NONE); + ta.setEditable(false); + ta.setPreferredSize(new Dimension(300, 70)); + final Panel panel = new Panel(); + panel.setLayout(new FlowLayout()); + panel.add(tf); + setLayout(new BorderLayout()); + add(ta, BorderLayout.CENTER); + add(panel, BorderLayout.PAGE_END); + } + + @Override + public void start() { + setVisible(true); + tf.requestFocus(); + } +} -- GitLab