Bug 433817 - Shell#setText with newlines (\n) causes the shell title to disappear
Summary: Shell#setText with newlines (\n) causes the shell title to disappear
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.4   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 4.5 RC2   Edit
Assignee: Niraj Modi CLA
QA Contact:
URL:
Whiteboard:
Keywords: Documentation
Depends on:
Blocks:
 
Reported: 2014-04-30 03:53 EDT by Dani Megert CLA
Modified: 2015-05-20 09:10 EDT (History)
5 users (show)

See Also:


Attachments
Works in my setup (11.04 KB, image/png)
2014-04-30 05:42 EDT, Markus Keller CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2014-04-30 03:53:29 EDT
4.4 M6, but also in older releases.

When calling Shell#setText with a string that contains newlines (\n), the title moves up the more newlines there are until it becomes unreadable.


import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ShellTitleBug {
	public static void main(String[] args) {
		final Display display= new Display();
		Shell shell= new Shell(display);
		shell.setText("This\nBug\nis\nugly");
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
Comment 1 Markus Keller CLA 2014-04-30 05:42:06 EDT
Created attachment 242528 [details]
Works in my setup

Seems to depend on OS settings, since this works for me: The lines are just concatenated, which is one of two good solutions. The other would be to just cut at the first \n, like TreeItem#setText(..).
Comment 2 Niraj Modi CLA 2014-05-30 11:43:04 EDT
This issue is not seen on Windows7, but can be seen on Linux Ubuntu.

Proposed Solution:
To address this issue, we will keep Windows7 behavior as reference & fix the Shell.setText() behavior on Linux.
Comment 3 Dani Megert CLA 2014-05-30 15:42:20 EDT
(In reply to Niraj Modi from comment #2)
> This issue is not seen on Windows7, but can be seen on Linux Ubuntu.

Yes, it is seen on Windows 7, that's where I saw it and filed the bug.
Comment 4 Niraj Modi CLA 2014-09-09 10:29:44 EDT
On Windows7, I am not able to figure out which OS level setting is causing the shell title to split on new line character. Tried with few of the OS themes as well, but all worked for me.

Anyways, if we attempt to fix this problem on SWT end by removing the \ n & \r in Shell.setText() method then we would need to keep track of original string to be returned via Shell.getText() method.

For now we can update the current JavaDoc for Shell.setText() method stating that, if newline char is used in Shell title, the behavior is platform dependent.
Comment 5 Markus Keller CLA 2014-09-12 09:00:56 EDT
Note that multi-line handling is inconsistent in many #setText(String) methods across different widgets and platforms.

The problem could be mentioned in Javadocs, but we do that, then we should not only do it for Shell#setText(String).

package snip;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class ShellTitleBug {
	private static final String MULTILINE = "This\nBug\nis\nugly";

	public static void main(String[] args) {
		final Display display= new Display();
		Shell shell= new Shell(display);
		shell.setText(MULTILINE);
		
		shell.setLayout(new GridLayout());
		
		new Label(shell, SWT.NONE).setText(MULTILINE);
		new Text(shell, SWT.SINGLE | SWT.BORDER).setText(MULTILINE);
		new Group(shell, SWT.NONE).setText(MULTILINE);
		
		Tree tree = new Tree(shell, SWT.BORDER);
		TreeItem treeItem = new TreeItem(tree, SWT.NONE);
		treeItem.setText(MULTILINE);
		new TreeItem(treeItem, SWT.NONE).setText(MULTILINE);
		treeItem.setExpanded(true);
		
		Table table= new Table(shell, SWT.BORDER);
		new TableItem(table, SWT.NONE).setText(MULTILINE);
		
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
Comment 6 Niraj Modi CLA 2015-05-14 04:57:12 EDT
(In reply to Markus Keller from comment #5)
> Note that multi-line handling is inconsistent in many #setText(String)
> methods across different widgets and platforms.
> 
> The problem could be mentioned in Javadocs, but we do that, then we should
> not only do it for Shell#setText(String).

Added a generic JavaDoc note w.r.t. to usage/behavior of escape characters like '\n', '\t' etc.. in below widgets for #setText() and List,ComboList#add() methods:
- Button
- Combo
- Decorations(parent of Shell)
- Group
- Label
- List
- TableItem
- Text
- TreeItem

Updated Windows files only(assuming changes will be copied over to other platform files via JavaDoc-Basher tool) via below git commit:
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=eef9aae31d613bddb04ee66908f2982f5fa891a0
Comment 7 Niraj Modi CLA 2015-05-14 06:18:33 EDT
Updated few more classes including custom widgets for the JavaDoc note w.r.t. to usage/behavior of escape characters in #setText() method:
- CCombo
- CLabel
- Item
- ToolBar

Refer git commit: http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=2421fb3ae62d819c660c8a65ad2613d75ea756c6

Resolving this bug now.
Comment 8 Sravan Kumar Lakkimsetti CLA 2015-05-14 08:23:17 EDT
These java doc doc changes are not available on build I20150513-2000 on Linux. I tested by importing the swt source. These java doc changes are not available on in the linux source package
Comment 9 Niraj Modi CLA 2015-05-14 08:39:38 EDT
(In reply to Sravan Kumar Lakkimsetti from comment #8)
> These java doc doc changes are not available on build I20150513-2000 on
> Linux. I tested by importing the swt source. These java doc changes are not
> available on in the linux source package

Hi Sravan,
Today only I made these JavaDoc change to win32 source..
These changes would be copied over to other platform files only when JavaDoc-Basher tool is run, which is yet to be done for Mars.
Comment 10 Sravan Kumar Lakkimsetti CLA 2015-05-14 08:56:47 EDT
I guess I am too early for the test. Actually I am looking for CLabel and CCombo javadocs. These are common across all platforms
Comment 11 Niraj Modi CLA 2015-05-15 09:34:41 EDT
Updated JavaDoc for CCombo#add() methods too, via below git commit: http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=e3cc9c35f8a2e43dc795766aca1e50b8d3e0b65b
Comment 12 Markus Keller CLA 2015-05-18 05:17:50 EDT
These characters are not called escape characters. They are control characters.
Comment 13 Niraj Modi CLA 2015-05-18 06:01:34 EDT
(In reply to Markus Keller from comment #12)
> These characters are not called escape characters. They are control
> characters.

Hi Markus,
Not sure if these two terms are synonyms, but Oracle docs usually refer these characters as Escape sequences:
Spec: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6
Tutorials: https://docs.oracle.com/javase/tutorial/java/data/characters.html

Stackoverflow: http://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters-in-java
Comment 14 Niraj Modi CLA 2015-05-19 08:48:31 EDT
In general programming terms, escape/control characters/sequences are usually used interchangeably.. all referring to some non-graphics, non-printing character set.

Java precisely refer these as "escape sequences", updated my JavaDocs accordingly:
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=d6e9a5cd1a69c0f96157482d1b5fd29f162cf11d
Comment 15 Markus Keller CLA 2015-05-19 11:50:37 EDT
(In reply to Niraj Modi from comment #14)
> In general programming terms, escape/control characters/sequences are
> usually used interchangeably.. all referring to some non-graphics,
> non-printing character set.

Definitely not. And that some people constantly mix up terms is not a justification for us to continue the confusion.

These APIs are not about the Java language, but about String objects that are passed by a client. References to the JLS are out of context. The String objects can just as well be constructed by other means (read from a file, compiled in a non-Java language, etc.) Most escape sequences in a String literal are handled perfectly well and are not platform-dependent. E.g. these are all fine: \"\'\\\u2603

The characters that are problematic are the control characters.
Comment 16 Niraj Modi CLA 2015-05-20 09:10:42 EDT
(In reply to Markus Keller from comment #15)
> (In reply to Niraj Modi from comment #14)
> > In general programming terms, escape/control characters/sequences are
> > usually used interchangeably.. all referring to some non-graphics,
> > non-printing character set.
> 
> Definitely not. And that some people constantly mix up terms is not a
> justification for us to continue the confusion.
> 
> These APIs are not about the Java language, but about String objects that
> are passed by a client. References to the JLS are out of context. The String
> objects can just as well be constructed by other means (read from a file,
> compiled in a non-Java language, etc.) Most escape sequences in a String
> literal are handled perfectly well and are not platform-dependent. E.g.
> these are all fine: \"\'\\\u2603
> 
> The characters that are problematic are the control characters.

You didn't reverted on comment 13, so I assumed you were OK with JLS terminology. Anyways, Thanks for this explanation on control characters, I have updated my JavaDocs accordingly: http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=20db836c37f4f983653ccdf81fb29ed3aa1c1494