Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 216555 Details for
Bug 381142
CLabel should allow customization of ellipsis
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Changes as unified diff
CLabel.patch (text/plain), 7.87 KB, created by
Jens Reimann
on 2012-05-31 04:26:40 EDT
(
hide
)
Description:
Changes as unified diff
Filename:
MIME Type:
Creator:
Jens Reimann
Created:
2012-05-31 04:26:40 EDT
Size:
7.87 KB
patch
obsolete
>--- CLabel.java 2012-02-01 15:39:40.000000000 +0100 >+++ CLabel.ellipsis.java 2012-05-31 10:25:31.620676907 +0200 >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2010 IBM Corporation and others. >+ * Copyright (c) 2000, 2012 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -50,7 +50,7 @@ > /** Left and right margins */ > private static final int DEFAULT_MARGIN = 3; > /** a string inserted in the middle of text that has been shortened */ >- private static final String ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" >+ private static final String DEFAULT_ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" > /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/ > private int align = SWT.LEFT; > private int leftMargin = DEFAULT_MARGIN; >@@ -75,6 +75,9 @@ > private boolean gradientVertical; > private Color background; > >+ private String ellipsis = DEFAULT_ELLIPSIS; >+ private int ellipsisAlign = SWT.CENTER; >+ > private static int DRAW_FLAGS = SWT.DRAW_MNEMONIC | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER; > > /** >@@ -222,6 +225,26 @@ > return bottomMargin; > } > /** >+ * Return the CLabel's ellipsis. >+ * >+ * @return the current ellipsis >+ * >+ * @since TDB >+ */ >+public String getEllipsis() { >+ return ellipsis; >+} >+/** >+ * Return the CLabel's ellipsis alignment. >+ * >+ * @return the current ellipsis alignment >+ * >+ * @since TDB >+ */ >+public int getEllipsisAligment() { >+ return ellipsisAlign; >+} >+/** > * Return the CLabel's image or <code>null</code>. > * > * @return the image of the label or null >@@ -800,6 +823,58 @@ > this.bottomMargin = bottomMargin; > redraw(); > } >+/** >+ * Set the label's ellipsis >+ * >+ * @param ellipsis the ellipsis to use instead of the {@link #DEFAULT_ELLIPSIS} >+ * >+ * @exception SWTException <ul> >+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> >+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> >+ * <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> >+ * </ul> >+ * >+ * @since TBD >+ */ >+public void setEllipsis (String ellipsis) { >+ checkWidget (); >+ this.ellipsis = ellipsis; >+ redraw(); >+} >+/** >+ * Set the alignment of the ellipsis. >+ * <p>Use the values LEFT, CENTER, RIGHT, BEGINNING or END to align the ellipsis in the text.</p> >+ * <p>BEGINNING and END are mapped to LEFT and RIGHT according to the current LEFT_TO_RIGHT and RIGHT_TO_LEFT style.</p> >+ * >+ * @param align the alignment style of LEFT, RIGHT, CENTER, BEGINNING or END >+ * >+ * @exception SWTException <ul> >+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> >+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> >+ * <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> >+ * </ul> >+ * >+ * @since TBD >+ */ >+public void setEllipsisAlignment(int ellipsisAlign) { >+ checkWidget(); >+ if (ellipsisAlign != SWT.LEFT && ellipsisAlign != SWT.RIGHT && ellipsisAlign != SWT.CENTER && ellipsisAlign != SWT.BEGINNING && ellipsisAlign != SWT.END) { >+ SWT.error(SWT.ERROR_INVALID_ARGUMENT); >+ } >+ >+ // correct left/right >+ if ( ellipsisAlign == SWT.BEGINNING ) { >+ ellipsisAlign = ( getStyle () & SWT.RIGHT_TO_LEFT ) != 0 ? SWT.RIGHT : SWT.LEFT; >+ } >+ else if ( ellipsisAlign == SWT.END ) { >+ ellipsisAlign = ( getStyle () & SWT.RIGHT_TO_LEFT ) != 0 ? SWT.LEFT : SWT.RIGHT; >+ } >+ >+ if (this.ellipsisAlign != ellipsisAlign) { >+ this.ellipsisAlign = ellipsisAlign; >+ redraw(); >+ } >+} > public void setFont(Font font) { > super.setFont(font); > redraw(); >@@ -944,40 +1019,83 @@ > */ > protected String shortenText(GC gc, String t, int width) { > if (t == null) return null; >- int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; >+ int w = gc.textExtent(this.ellipsis, DRAW_FLAGS).x; > if (width<=w) return t; >- int l = t.length(); >- int max = l/2; >- int min = 0; >- int mid = (max+min)/2 - 1; >- if (mid <= 0) return t; >- TextLayout layout = new TextLayout (getDisplay()); >- layout.setText(t); >- mid = validateOffset(layout, mid); >- while (min < mid && mid < max) { >- String s1 = t.substring(0, mid); >- String s2 = t.substring(validateOffset(layout, l-mid), l); >- int l1 = gc.textExtent(s1, DRAW_FLAGS).x; >- int l2 = gc.textExtent(s2, DRAW_FLAGS).x; >- if (l1+w+l2 > width) { >- max = mid; >- mid = validateOffset(layout, (max+min)/2); >- } else if (l1+w+l2 < width) { >- min = mid; >- mid = validateOffset(layout, (max+min)/2); >- } else { >- min = max; >- } >- } >- String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l-mid), l); >- layout.dispose(); >- return result; >+ switch ( ellipsisAlign ) { >+ case SWT.LEFT: >+ return beginShortenText(gc, t, width, w); >+ case SWT.RIGHT: >+ return endShortenText(gc, t, width, w); >+ case SWT.CENTER: >+ //$FALL-THROUGH$ >+ default: >+ return centerShortenText(gc, t, width, w); >+ } >+} >+protected String beginShortenText(GC gc, String t, int width, int w) { >+ TextLayout layout = new TextLayout (getDisplay()); >+ layout.setText(t); >+ >+ String result = t; >+ int len = gc.textExtent(result, DRAW_FLAGS).x; >+ while ( len + w > width && result.length () > 0 ) { >+ result = result.substring ( validateOffsetInverse ( layout, 1 ) ); >+ len = gc.textExtent(result, DRAW_FLAGS).x; >+ } >+ layout.dispose(); >+ return ellipsis + result; >+} >+protected String endShortenText(GC gc, String t, int width, int w) { >+ TextLayout layout = new TextLayout (getDisplay()); >+ layout.setText(t); >+ >+ String result = t; >+ int len = gc.textExtent(result, DRAW_FLAGS).x; >+ while ( len + w > width && result.length () > 0 ) { >+ result = result.substring ( 0, validateOffset ( layout, result.length () - 1 ) ); >+ len = gc.textExtent(result, DRAW_FLAGS).x; >+ } >+ layout.dispose(); >+ return result + ellipsis; >+} >+protected String centerShortenText(GC gc, String t, int width, int w) { >+ int l = t.length(); >+ int max = l/2; >+ int min = 0; >+ int mid = (max+min)/2 - 1; >+ if (mid <= 0) return t; >+ TextLayout layout = new TextLayout (getDisplay()); >+ layout.setText(t); >+ mid = validateOffset(layout, mid); >+ while (min < mid && mid < max) { >+ String s1 = t.substring(0, mid); >+ String s2 = t.substring(validateOffset(layout, l-mid), l); >+ int l1 = gc.textExtent(s1, DRAW_FLAGS).x; >+ int l2 = gc.textExtent(s2, DRAW_FLAGS).x; >+ if (l1+w+l2 > width) { >+ max = mid; >+ mid = validateOffset(layout, (max+min)/2); >+ } else if (l1+w+l2 < width) { >+ min = mid; >+ mid = validateOffset(layout, (max+min)/2); >+ } else { >+ min = max; >+ } >+ } >+ String result = mid == 0 ? t : t.substring(0, mid) + this.ellipsis + t.substring(validateOffset(layout, l-mid), l); >+ layout.dispose(); >+ return result; > } > int validateOffset(TextLayout layout, int offset) { > int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); > if (nextOffset != offset) return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER); > return offset; > } >+int validateOffsetInverse(TextLayout layout, int offset) { >+ int previousOffset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); >+ if (previousOffset != offset) return layout.getNextOffset(previousOffset, SWT.MOVEMENT_CLUSTER); >+ return offset; >+} > private String[] splitString(String text) { > String[] lines = new String[1]; > int start = 0, pos;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 381142
:
216554
| 216555