--- old/src/java.desktop/share/classes/java/awt/TextField.java 2015-10-14 19:00:22.357987043 +0530
+++ new/src/java.desktop/share/classes/java/awt/TextField.java 2015-10-14 19:00:21.909987043 +0530
@@ -198,7 +198,7 @@
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public TextField(String text, int columns) throws HeadlessException {
- super(text);
+ super(text, true);
this.columns = (columns >= 0) ? columns : 0;
}
--- old/src/java.desktop/share/classes/java/awt/TextComponent.java 2015-10-14 19:00:23.225987043 +0530
+++ new/src/java.desktop/share/classes/java/awt/TextComponent.java 2015-10-14 19:00:22.993987043 +0530
@@ -81,6 +81,15 @@
boolean editable = true;
/**
+ * A boolean indicating whether EOL characters present in the input
+ * text should be replaced by a space character.
+ * This variable is set to true
for TextField
.
+ * If set to true
EOL will be replaced with space.
+ * If set to false
text remains unchanged.
+ */
+ boolean replaceEOL;
+
+ /**
* The selection refers to the selected text, and the
* selectionStart
is the start position
* of the selected text.
@@ -136,6 +145,45 @@
setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
+ /**
+ * Constructs a new text component. Replaces EOL character from the input
+ * text with space character if the flag replaceEOL is true
+ * and also sets the value of the cursor to Cursor.TEXT_CURSOR
.
+ * @param text the text to be displayed; if
+ * text
is null
, the empty
+ * string ""
will be displayed
+ * @param replaceEOL boolean flag to decide whether to replace
+ * EOL or not.
+ * @exception HeadlessException if
+ * GraphicsEnvironment.isHeadless
+ * returns true
+ * @see java.awt.GraphicsEnvironment#isHeadless
+ * @see java.awt.Cursor
+ */
+ TextComponent(String text, boolean replaceEOL) throws HeadlessException {
+ GraphicsEnvironment.checkHeadless();
+ this.text = (text != null) ? text : "";
+ if (replaceEOL) {
+ this.replaceEOL = replaceEOL;
+ if (this.text != "") {
+ replaceEOL();
+ }
+ }
+ setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
+ }
+
+ /**
+ * Replaces EOL characters from the text variable with a space character.
+ */
+ private void replaceEOL() {
+ String[] strEOLs = {System.getProperty("line.separator"), "\n"};
+ for (String eol : strEOLs) {
+ if (text.contains(eol)) {
+ text = text.replace(eol, " ");
+ }
+ }
+ }
+
private void enableInputMethodsIfNecessary() {
if (checkForEnableIM) {
checkForEnableIM = false;
@@ -232,6 +280,9 @@
boolean skipTextEvent = (text == null || text.isEmpty())
&& (t == null || t.isEmpty());
text = (t != null) ? t : "";
+ if (replaceEOL && text != "") {
+ replaceEOL();
+ }
TextComponentPeer peer = (TextComponentPeer)this.peer;
// Please note that we do not want to post an event
// if TextArea.setText() or TextField.setText() replaces an empty text
--- /dev/null 2015-10-14 15:40:12.544320000 +0530
+++ new/test/java/awt/TextField/EOLTest/EOLTest.java 2015-10-14 19:00:23.729987043 +0530
@@ -0,0 +1,129 @@
+/*
+ * 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 8055197
+ @summary TextField should replace EOL character with space character
+ @run main EOLTest
+ */
+
+import java.awt.Frame;
+import java.awt.TextField;
+
+public class EOLTest {
+
+ private Frame mainFrame;
+ private TextField textField;
+ private String testStrEOL;
+ private boolean isTestFail;
+ private int testFailCount;
+ StringBuilder testFailMessage;
+ private String expectedString = "Row1 Row2";
+
+ public EOLTest() {
+ mainFrame = new Frame();
+ mainFrame.setSize(200, 200);
+ mainFrame.setVisible(true);
+ testFailMessage = new StringBuilder();
+ testStrEOL = "Row1" + System.getProperty("line.separator") + "Row2";
+ }
+
+ private void testConstructor1() {
+ textField = new TextField(testStrEOL);
+ textField.setSize(200, 100);
+ mainFrame.add(textField);
+ checkTest();
+ mainFrame.remove(textField);
+ textField = null;
+ }
+
+ private void testConstructor2() {
+ textField = new TextField(30);
+ textField.setSize(200, 100);
+ mainFrame.add(textField);
+ textField.setText(testStrEOL);
+ checkTest();
+ mainFrame.remove(textField);
+ textField = null;
+ }
+
+ private void testConstructor3() {
+ textField = new TextField(testStrEOL, 30);
+ textField.setSize(200, 100);
+ mainFrame.add(textField);
+ checkTest();
+ mainFrame.remove(textField);
+ textField = null;
+ }
+
+ private void testSetText() {
+ textField = new TextField();
+ textField.setSize(200, 100);
+ textField.setText(testStrEOL);
+ mainFrame.add(textField);
+ checkTest();
+ mainFrame.remove(textField);
+ textField = null;
+ }
+
+ private void checkTest() {
+ if (!textField.getText().equals(expectedString)) {
+ testFailMessage.append("TestFail line : ");
+ testFailMessage.append(Thread.currentThread().getStackTrace()[2].
+ getLineNumber());
+ testFailMessage.append(" TextField.getText() : \"");
+ testFailMessage.append(textField.getText());
+ testFailMessage.append("\" does not match expected string : \"");
+ testFailMessage.append(expectedString).append("\"");
+ testFailMessage.append(System.getProperty("line.separator"));
+ testFailCount++;
+ isTestFail = true;
+ }
+ }
+
+ private void checkFailures() {
+ if (isTestFail) {
+ testFailMessage.insert(0, "Test Fail count : " + testFailCount
+ + System.getProperty("line.separator"));
+ dispose();
+ throw new RuntimeException(testFailMessage.toString());
+ }
+ }
+
+ private void dispose() {
+ if (mainFrame != null) {
+ mainFrame.dispose();
+ }
+ }
+
+ public static void main(String[] args) {
+ EOLTest testEOL = new EOLTest();
+ testEOL.testConstructor1();
+ testEOL.testConstructor2();
+ testEOL.testConstructor3();
+ testEOL.testSetText();
+ testEOL.checkFailures();
+ testEOL.dispose();
+ }
+}