Bug 196363 - [CTabFolder] CTabItem.shortenText() from the left
Summary: [CTabFolder] CTabItem.shortenText() from the left
Status: RESOLVED DUPLICATE of bug 194759
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Duong Nguyen CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-12 14:35 EDT by Peter Centgraf CLA
Modified: 2007-08-14 15:08 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Centgraf CLA 2007-07-12 14:35:27 EDT
I have a use case where the most distinguishing characters of a CTabItem title are at the end of the string, rather than the beginning.  Therefore, I want to elide the title on the left, rather than the right.

Current behavior:

"My long tab name #1" -> "My long tab n..."
"My long tab name #2" -> "My long tab n..."

Desired behavior:

"My long tab name #1" -> "...g tab name #1"
"My long tab name #2" -> "...g tab name #2"

This can be accomplished quite easily with a few lines of code in CTabItem.shortenText().  I'd be happy to contribute a patch, but I am uncertain of the code style the SWT committers would prefer.  Should the parameter be set as a new constructor style flag, or as a JavaBean-style property?  Should it be set on each CTabItem, or on the CTabFolder?  If the latter, how should this choice be communicated to the CTabItems?  If someone could answer those questions, I'll whip up the code promptly.
Comment 1 Peter Centgraf CLA 2007-07-12 15:02:04 EDT
Here's the logic that will do the job, once we figure out how to set the new clipTail flag:

String shortenText(GC gc, String text, int width, String ellipses, boolean clipTail) {
	if (gc.textExtent(text, FLAGS).x <= width) return text;
	int ellipseWidth = gc.textExtent(ellipses, FLAGS).x;
	int length = text.length();
	
	if (clipTail) {
		int end = length - 1;
		while (end > 0) {
			text = text.substring(0, end);
			int l = gc.textExtent(text, FLAGS).x;
			if (l + ellipseWidth <= width) {
				return text + ellipses;
			}
			end--;
		}
	}
	else {
		while (text.length() > 1) {
			text = text.substring(1);
			int l = gc.textExtent(text, FLAGS).x;
			if (l + ellipseWidth <= width) {
				return ellipses + text;
			}
		}
	}
	return text;
}
Comment 2 Duong Nguyen CLA 2007-08-13 11:49:27 EDT

*** This bug has been marked as a duplicate of bug 194759 ***
Comment 3 Peter Centgraf CLA 2007-08-14 15:08:14 EDT
FYI, CLabel implements a variant of this idea that shortens the text in the middle, instead of the side.  That version does not support the ellipses parameter but is very similar otherwise.