Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2012 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 50-56
Link Here
|
50 |
/** Left and right margins */ |
50 |
/** Left and right margins */ |
51 |
private static final int DEFAULT_MARGIN = 3; |
51 |
private static final int DEFAULT_MARGIN = 3; |
52 |
/** a string inserted in the middle of text that has been shortened */ |
52 |
/** a string inserted in the middle of text that has been shortened */ |
53 |
private static final String ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" |
53 |
private static final String DEFAULT_ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" |
54 |
/** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/ |
54 |
/** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/ |
55 |
private int align = SWT.LEFT; |
55 |
private int align = SWT.LEFT; |
56 |
private int leftMargin = DEFAULT_MARGIN; |
56 |
private int leftMargin = DEFAULT_MARGIN; |
Lines 75-80
Link Here
|
75 |
private boolean gradientVertical; |
75 |
private boolean gradientVertical; |
76 |
private Color background; |
76 |
private Color background; |
77 |
|
77 |
|
|
|
78 |
private String ellipsis = DEFAULT_ELLIPSIS; |
79 |
private int ellipsisAlign = SWT.CENTER; |
80 |
|
78 |
private static int DRAW_FLAGS = SWT.DRAW_MNEMONIC | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER; |
81 |
private static int DRAW_FLAGS = SWT.DRAW_MNEMONIC | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER; |
79 |
|
82 |
|
80 |
/** |
83 |
/** |
Lines 222-227
Link Here
|
222 |
return bottomMargin; |
225 |
return bottomMargin; |
223 |
} |
226 |
} |
224 |
/** |
227 |
/** |
|
|
228 |
* Return the CLabel's ellipsis. |
229 |
* |
230 |
* @return the current ellipsis |
231 |
* |
232 |
* @since TDB |
233 |
*/ |
234 |
public String getEllipsis() { |
235 |
return ellipsis; |
236 |
} |
237 |
/** |
238 |
* Return the CLabel's ellipsis alignment. |
239 |
* |
240 |
* @return the current ellipsis alignment |
241 |
* |
242 |
* @since TDB |
243 |
*/ |
244 |
public int getEllipsisAligment() { |
245 |
return ellipsisAlign; |
246 |
} |
247 |
/** |
225 |
* Return the CLabel's image or <code>null</code>. |
248 |
* Return the CLabel's image or <code>null</code>. |
226 |
* |
249 |
* |
227 |
* @return the image of the label or null |
250 |
* @return the image of the label or null |
Lines 800-805
Link Here
|
800 |
this.bottomMargin = bottomMargin; |
823 |
this.bottomMargin = bottomMargin; |
801 |
redraw(); |
824 |
redraw(); |
802 |
} |
825 |
} |
|
|
826 |
/** |
827 |
* Set the label's ellipsis |
828 |
* |
829 |
* @param ellipsis the ellipsis to use instead of the {@link #DEFAULT_ELLIPSIS} |
830 |
* |
831 |
* @exception SWTException <ul> |
832 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
833 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
834 |
* <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> |
835 |
* </ul> |
836 |
* |
837 |
* @since TBD |
838 |
*/ |
839 |
public void setEllipsis (String ellipsis) { |
840 |
checkWidget (); |
841 |
this.ellipsis = ellipsis; |
842 |
redraw(); |
843 |
} |
844 |
/** |
845 |
* Set the alignment of the ellipsis. |
846 |
* <p>Use the values LEFT, CENTER, RIGHT, BEGINNING or END to align the ellipsis in the text.</p> |
847 |
* <p>BEGINNING and END are mapped to LEFT and RIGHT according to the current LEFT_TO_RIGHT and RIGHT_TO_LEFT style.</p> |
848 |
* |
849 |
* @param align the alignment style of LEFT, RIGHT, CENTER, BEGINNING or END |
850 |
* |
851 |
* @exception SWTException <ul> |
852 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
853 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
854 |
* <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> |
855 |
* </ul> |
856 |
* |
857 |
* @since TBD |
858 |
*/ |
859 |
public void setEllipsisAlignment(int ellipsisAlign) { |
860 |
checkWidget(); |
861 |
if (ellipsisAlign != SWT.LEFT && ellipsisAlign != SWT.RIGHT && ellipsisAlign != SWT.CENTER && ellipsisAlign != SWT.BEGINNING && ellipsisAlign != SWT.END) { |
862 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
863 |
} |
864 |
|
865 |
// correct left/right |
866 |
if ( ellipsisAlign == SWT.BEGINNING ) { |
867 |
ellipsisAlign = ( getStyle () & SWT.RIGHT_TO_LEFT ) != 0 ? SWT.RIGHT : SWT.LEFT; |
868 |
} |
869 |
else if ( ellipsisAlign == SWT.END ) { |
870 |
ellipsisAlign = ( getStyle () & SWT.RIGHT_TO_LEFT ) != 0 ? SWT.LEFT : SWT.RIGHT; |
871 |
} |
872 |
|
873 |
if (this.ellipsisAlign != ellipsisAlign) { |
874 |
this.ellipsisAlign = ellipsisAlign; |
875 |
redraw(); |
876 |
} |
877 |
} |
803 |
public void setFont(Font font) { |
878 |
public void setFont(Font font) { |
804 |
super.setFont(font); |
879 |
super.setFont(font); |
805 |
redraw(); |
880 |
redraw(); |
Lines 944-983
Link Here
|
944 |
*/ |
1019 |
*/ |
945 |
protected String shortenText(GC gc, String t, int width) { |
1020 |
protected String shortenText(GC gc, String t, int width) { |
946 |
if (t == null) return null; |
1021 |
if (t == null) return null; |
947 |
int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; |
1022 |
int w = gc.textExtent(this.ellipsis, DRAW_FLAGS).x; |
948 |
if (width<=w) return t; |
1023 |
if (width<=w) return t; |
949 |
int l = t.length(); |
1024 |
switch ( ellipsisAlign ) { |
950 |
int max = l/2; |
1025 |
case SWT.LEFT: |
951 |
int min = 0; |
1026 |
return beginShortenText(gc, t, width, w); |
952 |
int mid = (max+min)/2 - 1; |
1027 |
case SWT.RIGHT: |
953 |
if (mid <= 0) return t; |
1028 |
return endShortenText(gc, t, width, w); |
954 |
TextLayout layout = new TextLayout (getDisplay()); |
1029 |
case SWT.CENTER: |
955 |
layout.setText(t); |
1030 |
//$FALL-THROUGH$ |
956 |
mid = validateOffset(layout, mid); |
1031 |
default: |
957 |
while (min < mid && mid < max) { |
1032 |
return centerShortenText(gc, t, width, w); |
958 |
String s1 = t.substring(0, mid); |
1033 |
} |
959 |
String s2 = t.substring(validateOffset(layout, l-mid), l); |
1034 |
} |
960 |
int l1 = gc.textExtent(s1, DRAW_FLAGS).x; |
1035 |
protected String beginShortenText(GC gc, String t, int width, int w) { |
961 |
int l2 = gc.textExtent(s2, DRAW_FLAGS).x; |
1036 |
TextLayout layout = new TextLayout (getDisplay()); |
962 |
if (l1+w+l2 > width) { |
1037 |
layout.setText(t); |
963 |
max = mid; |
1038 |
|
964 |
mid = validateOffset(layout, (max+min)/2); |
1039 |
String result = t; |
965 |
} else if (l1+w+l2 < width) { |
1040 |
int len = gc.textExtent(result, DRAW_FLAGS).x; |
966 |
min = mid; |
1041 |
while ( len + w > width && result.length () > 0 ) { |
967 |
mid = validateOffset(layout, (max+min)/2); |
1042 |
result = result.substring ( validateOffsetInverse ( layout, 1 ) ); |
968 |
} else { |
1043 |
len = gc.textExtent(result, DRAW_FLAGS).x; |
969 |
min = max; |
1044 |
} |
970 |
} |
1045 |
layout.dispose(); |
971 |
} |
1046 |
return ellipsis + result; |
972 |
String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l-mid), l); |
1047 |
} |
973 |
layout.dispose(); |
1048 |
protected String endShortenText(GC gc, String t, int width, int w) { |
974 |
return result; |
1049 |
TextLayout layout = new TextLayout (getDisplay()); |
|
|
1050 |
layout.setText(t); |
1051 |
|
1052 |
String result = t; |
1053 |
int len = gc.textExtent(result, DRAW_FLAGS).x; |
1054 |
while ( len + w > width && result.length () > 0 ) { |
1055 |
result = result.substring ( 0, validateOffset ( layout, result.length () - 1 ) ); |
1056 |
len = gc.textExtent(result, DRAW_FLAGS).x; |
1057 |
} |
1058 |
layout.dispose(); |
1059 |
return result + ellipsis; |
1060 |
} |
1061 |
protected String centerShortenText(GC gc, String t, int width, int w) { |
1062 |
int l = t.length(); |
1063 |
int max = l/2; |
1064 |
int min = 0; |
1065 |
int mid = (max+min)/2 - 1; |
1066 |
if (mid <= 0) return t; |
1067 |
TextLayout layout = new TextLayout (getDisplay()); |
1068 |
layout.setText(t); |
1069 |
mid = validateOffset(layout, mid); |
1070 |
while (min < mid && mid < max) { |
1071 |
String s1 = t.substring(0, mid); |
1072 |
String s2 = t.substring(validateOffset(layout, l-mid), l); |
1073 |
int l1 = gc.textExtent(s1, DRAW_FLAGS).x; |
1074 |
int l2 = gc.textExtent(s2, DRAW_FLAGS).x; |
1075 |
if (l1+w+l2 > width) { |
1076 |
max = mid; |
1077 |
mid = validateOffset(layout, (max+min)/2); |
1078 |
} else if (l1+w+l2 < width) { |
1079 |
min = mid; |
1080 |
mid = validateOffset(layout, (max+min)/2); |
1081 |
} else { |
1082 |
min = max; |
1083 |
} |
1084 |
} |
1085 |
String result = mid == 0 ? t : t.substring(0, mid) + this.ellipsis + t.substring(validateOffset(layout, l-mid), l); |
1086 |
layout.dispose(); |
1087 |
return result; |
975 |
} |
1088 |
} |
976 |
int validateOffset(TextLayout layout, int offset) { |
1089 |
int validateOffset(TextLayout layout, int offset) { |
977 |
int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); |
1090 |
int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); |
978 |
if (nextOffset != offset) return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER); |
1091 |
if (nextOffset != offset) return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER); |
979 |
return offset; |
1092 |
return offset; |
980 |
} |
1093 |
} |
|
|
1094 |
int validateOffsetInverse(TextLayout layout, int offset) { |
1095 |
int previousOffset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); |
1096 |
if (previousOffset != offset) return layout.getNextOffset(previousOffset, SWT.MOVEMENT_CLUSTER); |
1097 |
return offset; |
1098 |
} |
981 |
private String[] splitString(String text) { |
1099 |
private String[] splitString(String text) { |
982 |
String[] lines = new String[1]; |
1100 |
String[] lines = new String[1]; |
983 |
int start = 0, pos; |
1101 |
int start = 0, pos; |