|
Lines 10-16
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.nebula.widgets.grid.internal; |
11 |
package org.eclipse.nebula.widgets.grid.internal; |
| 12 |
|
12 |
|
|
|
13 |
import org.eclipse.nebula.widgets.grid.GridCellRenderer; |
| 13 |
import org.eclipse.swt.graphics.GC; |
14 |
import org.eclipse.swt.graphics.GC; |
|
|
15 |
import org.eclipse.swt.graphics.Point; |
| 14 |
|
16 |
|
| 15 |
/** |
17 |
/** |
| 16 |
* Utility class to provide common operations on strings not supported by the |
18 |
* Utility class to provide common operations on strings not supported by the |
|
Lines 21-56
Link Here
|
| 21 |
*/ |
23 |
*/ |
| 22 |
public class TextUtils |
24 |
public class TextUtils |
| 23 |
{ |
25 |
{ |
|
|
26 |
/** |
| 27 |
* Shortens a supplied string so that it fits within the area specified by |
| 28 |
* the width argument. Strings that have been shorted have an "..." attached |
| 29 |
* to the end of the string. The width is computed using the |
| 30 |
* {@link GC#textExtent(String)}. |
| 31 |
* |
| 32 |
* @param gc GC used to perform calculation. |
| 33 |
* @param t text to modify. |
| 34 |
* @param width Pixels to display. |
| 35 |
* @param style truncation style. see {@link GridCellRenderer#TRUNCATE_END}, {@link GridCellRenderer#TRUNCATE_MIDDLE}, {@link GridCellRenderer#TRUNCATE_START} |
| 36 |
* @return shortened string that fits in area specified. |
| 37 |
*/ |
| 38 |
public static String getShortText(GC gc, String t, int width, int style) |
| 39 |
{ |
| 40 |
return getShortX(gc, t, width, style, false); |
| 41 |
} |
| 24 |
|
42 |
|
| 25 |
/** |
43 |
/** |
| 26 |
* Shortens a supplied string so that it fits within the area specified by |
44 |
* Shortens a supplied string so that it fits within the area specified by |
| 27 |
* the width argument. Strings that have been shorted have an "..." attached |
45 |
* the width argument. Strings that have been shorted have an "..." attached |
| 28 |
* to the end of the string. The width is computed using the |
46 |
* to the end of the string. The width is computed using the |
| 29 |
* {@link GC#textExtent(String)}. |
47 |
* {@link GC#stringExtent(String)}. |
| 30 |
* |
48 |
* |
| 31 |
* @param gc GC used to perform calculation. |
49 |
* @param gc GC used to perform calculation. |
| 32 |
* @param t text to modify. |
50 |
* @param t text to modify. |
| 33 |
* @param width Pixels to display. |
51 |
* @param width Pixels to display. |
|
|
52 |
* @param style truncation style. see {@link GridCellRenderer#TRUNCATE_END}, {@link GridCellRenderer#TRUNCATE_MIDDLE}, {@link GridCellRenderer#TRUNCATE_START} |
| 34 |
* @return shortened string that fits in area specified. |
53 |
* @return shortened string that fits in area specified. |
| 35 |
*/ |
54 |
*/ |
| 36 |
public static String getShortText(GC gc, String t, int width) |
55 |
public static String getShortString(GC gc, String t, int width, int style) |
| 37 |
{ |
56 |
{ |
|
|
57 |
return getShortX(gc, t, width, style, true); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* This is the actiual handler for the text shortening. |
| 62 |
* @param gc |
| 63 |
* @param t |
| 64 |
* @param width |
| 65 |
* @param style |
| 66 |
* @param exTx see {@link #getMeasure(GC, String, boolean)} |
| 67 |
* @return . |
| 68 |
*/ |
| 69 |
private static String getShortX(GC gc, String t, int width, int style, boolean exTx){ |
| 38 |
if (t == null) |
70 |
if (t == null) |
| 39 |
{ |
71 |
{ |
| 40 |
return null; |
72 |
return null; |
| 41 |
} |
73 |
} |
| 42 |
|
74 |
|
| 43 |
if (t.equals("")) |
75 |
if (t.length()==0) |
| 44 |
{ |
76 |
{ |
| 45 |
return ""; |
77 |
return ""; |
| 46 |
} |
78 |
} |
| 47 |
|
79 |
|
| 48 |
if (width >= gc.textExtent(t).x) |
80 |
if (width >= getMeasure(gc,t,exTx).x) |
| 49 |
{ |
81 |
{ |
| 50 |
return t; |
82 |
return t; |
| 51 |
} |
83 |
} |
|
|
84 |
|
| 85 |
switch(style){ |
| 86 |
case GridCellRenderer.TRUNCATE_MIDDLE: |
| 87 |
return getShortXMid(gc, t, width, exTx); |
| 88 |
case GridCellRenderer.TRUNCATE_END: |
| 89 |
return getShortXEnd(gc, t, width, exTx); |
| 90 |
case GridCellRenderer.TRUNCATE_START: |
| 91 |
return getShortXStart(gc, t, width, exTx); |
| 92 |
default: |
| 93 |
throw new IllegalArgumentException("Unknown truncation style '"+style+"'! Please use one of the TRUNCATION_* constants."); |
| 94 |
} |
| 95 |
} |
| 52 |
|
96 |
|
| 53 |
int w = gc.textExtent("...").x; |
97 |
private static String getShortXMid(GC gc, String t, int width, boolean exTx){ |
|
|
98 |
int w = getMeasure(gc,"...",exTx).x; |
| 54 |
String text = t; |
99 |
String text = t; |
| 55 |
int l = text.length(); |
100 |
int l = text.length(); |
| 56 |
int pivot = l / 2; |
101 |
int pivot = l / 2; |
|
Lines 61-68
Link Here
|
| 61 |
{ |
106 |
{ |
| 62 |
String s1 = text.substring(0, s); |
107 |
String s1 = text.substring(0, s); |
| 63 |
String s2 = text.substring(e, l); |
108 |
String s2 = text.substring(e, l); |
| 64 |
int l1 = gc.textExtent(s1).x; |
109 |
int l1 = getMeasure(gc,s1,exTx).x; |
| 65 |
int l2 = gc.textExtent(s2).x; |
110 |
int l2 = getMeasure(gc,s2,exTx).x; |
| 66 |
if (l1 + w + l2 < width) |
111 |
if (l1 + w + l2 < width) |
| 67 |
{ |
112 |
{ |
| 68 |
text = s1 + "..." + s2; |
113 |
text = s1 + "..." + s2; |
|
Lines 80-147
Link Here
|
| 80 |
return text; |
125 |
return text; |
| 81 |
} |
126 |
} |
| 82 |
|
127 |
|
| 83 |
/** |
128 |
private static String getShortXEnd(GC gc, String t, int width, boolean exTx){ |
| 84 |
* Shortens a supplied string so that it fits within the area specified by |
129 |
int w = getMeasure(gc,"...",exTx).x; |
| 85 |
* the width argument. Strings that have been shorted have an "..." attached |
|
|
| 86 |
* to the end of the string. The width is computed using the |
| 87 |
* {@link GC#stringExtent(String)}. |
| 88 |
* |
| 89 |
* @param gc GC used to perform calculation. |
| 90 |
* @param t text to modify. |
| 91 |
* @param width Pixels to display. |
| 92 |
* @return shortened string that fits in area specified. |
| 93 |
*/ |
| 94 |
public static String getShortString(GC gc, String t, int width) |
| 95 |
{ |
| 96 |
|
130 |
|
| 97 |
if (t == null) |
131 |
String text = t; |
| 98 |
{ |
132 |
int l = text.length(); |
| 99 |
return null; |
|
|
| 100 |
} |
| 101 |
|
133 |
|
| 102 |
if (t.equals("")) |
134 |
for(int i=l; i>0; i--){ |
| 103 |
{ |
135 |
String s1 = text.substring(0, i); |
| 104 |
return ""; |
136 |
int l1 = getMeasure(gc,s1,exTx).x; |
| 105 |
} |
|
|
| 106 |
|
137 |
|
| 107 |
if (width >= gc.stringExtent(t).x) |
138 |
if (l1 + w < width) |
| 108 |
{ |
139 |
{ |
| 109 |
return t; |
140 |
text = s1 + "..."; |
|
|
141 |
break; |
| 142 |
} |
| 110 |
} |
143 |
} |
| 111 |
|
144 |
|
| 112 |
int w = gc.stringExtent("...").x; |
145 |
return text; |
|
|
146 |
} |
| 147 |
|
| 148 |
private static String getShortXStart(GC gc, String t, int width, boolean exTx){ |
| 149 |
int w = getMeasure(gc,"...",exTx).x; |
| 150 |
|
| 113 |
String text = t; |
151 |
String text = t; |
| 114 |
int l = text.length(); |
152 |
int l = text.length(); |
| 115 |
int pivot = l / 2; |
153 |
|
| 116 |
int s = pivot; |
154 |
for(int i=l; i>0; i--){ |
| 117 |
int e = pivot + 1; |
155 |
String s1 = text.substring(l-i, l); |
| 118 |
while (s >= 0 && e < l) |
156 |
int l1 = getMeasure(gc,s1,exTx).x; |
| 119 |
{ |
157 |
|
| 120 |
String s1 = text.substring(0, s); |
158 |
if (l1 + w < width) |
| 121 |
String s2 = text.substring(e, l); |
|
|
| 122 |
int l1 = gc.stringExtent(s1).x; |
| 123 |
int l2 = gc.stringExtent(s2).x; |
| 124 |
if (l1 + w + l2 < width) |
| 125 |
{ |
159 |
{ |
| 126 |
text = s1 + "..." + s2; |
160 |
text = "..."+s1; |
| 127 |
break; |
161 |
break; |
| 128 |
} |
162 |
} |
| 129 |
s--; |
|
|
| 130 |
e++; |
| 131 |
} |
| 132 |
|
| 133 |
if (s == 0 || e == l) |
| 134 |
{ |
| 135 |
text = text.substring(0, 1) + "..." + text.substring(l - 1, l); |
| 136 |
} |
163 |
} |
| 137 |
|
164 |
|
| 138 |
return text; |
165 |
return text; |
| 139 |
} |
166 |
} |
| 140 |
|
167 |
|
| 141 |
/** |
168 |
/** |
|
|
169 |
* Calls gc.stringExtent() or gc.textExtent() |
| 170 |
* @param gc |
| 171 |
* @param t |
| 172 |
* @param exTx true => stringExtent, false => textExtent |
| 173 |
* @return . |
| 174 |
*/ |
| 175 |
private static Point getMeasure(GC gc, String t, boolean exTx){ |
| 176 |
if (exTx) |
| 177 |
return gc.stringExtent(t); |
| 178 |
return gc.textExtent(t); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 142 |
* Protected constructor to prevent instantiation. |
182 |
* Protected constructor to prevent instantiation. |
| 143 |
*/ |
183 |
*/ |
| 144 |
protected TextUtils() |
184 |
protected TextUtils() |
| 145 |
{ |
185 |
{ |
|
|
186 |
// nothing |
| 146 |
} |
187 |
} |
| 147 |
} |
188 |
} |