Skip to content

Package: TextUtils

TextUtils

nameinstructionbranchcomplexitylinemethod
getShortStr(GC, String, int)
M: 135 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%
getShortString(GC, String, int)
M: 125 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
getShortText(GC, String, int)
M: 111 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - Copied when updating from Nebula 2.1 to 2.2
13: ******************************************************************************/
14: // REUSED CLASS
15: /*******************************************************************************
16: * Copyright (c) 2006 IBM Corporation and others.
17: * All rights reserved. This program and the accompanying materials
18: * are made available under the terms of the Eclipse Public License v1.0
19: * which accompanies this distribution, and is available at
20: * http://www.eclipse.org/legal/epl-v10.html
21: *
22: * Contributors:
23: * chris.gross@us.ibm.com - initial API and implementation
24: *******************************************************************************/
25: package org.eclipse.emf.ecp.view.internal.table.nebula.grid;
26:
27: import org.eclipse.swt.graphics.GC;
28:
29: /**
30: * Utility class to provide common operations on strings not supported by the
31: * base java API.
32: *
33: * @author chris.gross@us.ibm.com
34: * @author Mirko Paturzo <mirko.paturzo@exeura.eu>
35: * @author gongguangyong@live.cn
36: *
37: * Mirko modified the pivot calculation for improve short text provider performance.
38: * The pivot number is calculate starting from the size of the cell provided
39: *
40: * @since 2.0.0
41: */
42: public class TextUtils {
43:
44:         /**
45:          * Shortens a supplied string so that it fits within the area specified by
46:          * the width argument. Strings that have been shorted have an "..." attached
47:          * to the end of the string. The width is computed using the
48:          * {@link GC#textExtent(String)}.
49:          *
50:          * @param gc GC used to perform calculation.
51:          * @param t text to modify.
52:          * @param width Pixels to display.
53:          * @return shortened string that fits in area specified.
54:          * @deprecated when text is large, performance is poor, possible occur OOM exception. suggest use
55:          * {@link #getShortStr(GC, String, int)}
56:          */
57:         @Deprecated
58:         public static String getShortText(GC gc, String t, int width) {
59:•                if (t == null) {
60:                         return null;
61:                 }
62:
63:•                if (t.equals("")) { //$NON-NLS-1$
64:                         return ""; //$NON-NLS-1$
65:                 }
66:
67:•                if (width >= gc.textExtent(t).x) {
68:                         return t;
69:                 }
70:
71:                 final int w = gc.textExtent("...").x; //$NON-NLS-1$
72:                 String text = t;
73:                 final int l = text.length();
74:                 final int pivot = l / 2;
75:                 int s = pivot;
76:                 int e = pivot + 1;
77:
78:•                while (s >= 0 && e < l) {
79:                         final String s1 = text.substring(0, s);
80:                         final String s2 = text.substring(e, l);
81:                         final int l1 = gc.textExtent(s1).x;
82:                         final int l2 = gc.textExtent(s2).x;
83:•                        if (l1 + w + l2 < width) {
84:                                 text = s1 + "..." + s2; //$NON-NLS-1$
85:                                 break;
86:                         }
87:                         s--;
88:                         e++;
89:                 }
90:
91:•                if (s == 0 || e == l) {
92:                         text = text.substring(0, 1) + "..." + text.substring(l - 1, l); //$NON-NLS-1$
93:                 }
94:
95:                 return text;
96:         }
97:
98:         /**
99:          * Shortens a supplied string so that it fits within the area specified by
100:          * the width argument. Strings that have been shorted have an "..." attached
101:          * to the end of the string. The width is computed using the
102:          * {@link GC#stringExtent(String)}.
103:          *
104:          * @param gc GC used to perform calculation.
105:          * @param t text to modify.
106:          * @param width Pixels to display.
107:          * @return shortened string that fits in area specified.
108:          * @deprecated when text is large, performance is poor, possible occur OOM exception. suggest use
109:          * {@link #getShortStr(GC, String, int)}
110:          */
111:         @Deprecated
112:         public static String getShortString(GC gc, String t, int width) {
113:•                if (t == null) {
114:                         return null;
115:                 }
116:
117:•                if (t.equals("")) { //$NON-NLS-1$
118:                         return ""; //$NON-NLS-1$
119:                 }
120:
121:                 final int textWidth = gc.stringExtent(t).x;
122:•                if (width >= textWidth) {
123:                         return t;
124:                 }
125:                 String text = t;
126:                 final int l = text.length();
127:                 final int w = gc.stringExtent("...").x; //$NON-NLS-1$
128:                 final double midChar = (double) textWidth / l;
129:                 final int pivot = (int) (width / midChar / 2);
130:                 int s = pivot;
131:                 int e = l - pivot + 1;
132:
133:•                while (s >= 0 && e < l) {
134:                         final String s1 = text.substring(0, s);
135:                         final String s2 = text.substring(e, l);
136:                         final int l1 = gc.stringExtent(s1).x;
137:                         final int l2 = gc.stringExtent(s2).x;
138:•                        if (l1 + w + l2 < width) {
139:                                 text = s1 + "..." + s2; //$NON-NLS-1$
140:                                 break;
141:                         }
142:                         s--;
143:                         e++;
144:                 }
145:
146:•                if (s == 0 || e == l) {
147:                         text = text.substring(0, 1) + "..." + text.substring(l - 1, l); //$NON-NLS-1$
148:                 }
149:
150:                 return text;
151:         }
152:
153:         /**
154:          * Shortens a supplied string so that it fits within the area specified by
155:          * the width argument. Strings that have been shorted have an "..." attached
156:          * to the end of the string. The width is computed using the
157:          * {@link GC#getCharWidth(char)}.
158:          *
159:          * @param gc GC used to perform calculation.
160:          * @param t text to modify.
161:          * @param width Pixels to display.
162:          * @return shortened string that fits in area specified.
163:          */
164:         public static String getShortStr(GC gc, String t, int width) {
165:•                if (t == null || t.equals("")) { //$NON-NLS-1$
166:                         return t;
167:                 }
168:
169:•                if (width >= gc.stringExtent(t).x) {
170:                         return t;
171:                 }
172:
173:                 final char[] chars = t.toCharArray();
174:                 final int length = chars.length;
175:                 int left = 0;
176:                 int right = length - 1;
177:                 int calcWidth = gc.stringExtent("...").x; //$NON-NLS-1$
178:
179:•                while (left < right) {
180:                         int step = gc.getCharWidth(chars[left]);
181:                         calcWidth += step;
182:•                        if (calcWidth >= width) {
183:                                 break;
184:                         }
185:                         left++;
186:
187:                         step = gc.getCharWidth(chars[right]);
188:                         calcWidth += step;
189:•                        if (calcWidth >= width) {
190:                                 break;
191:                         }
192:                         right--;
193:                 }
194:•                if (left >= right) {
195:                         return t;
196:                 }
197:                 final StringBuilder builder = new StringBuilder(left + length - right + 4);
198:•                if (left == 0 || right == length - 1) {
199:                         builder.append(chars[0]).append("...").append(chars[length - 1]); //$NON-NLS-1$
200:                 } else {
201:•                        final int leftLen = left == 1 ? left : left - 1;
202:                         builder.append(chars, 0, leftLen).append("...").append(chars, right + 1, length - right - 1); //$NON-NLS-1$
203:                 }
204:                 return builder.toString();
205:         }
206:
207:         /**
208:          * private constructor to prevent instantiation.
209:          */
210:         private TextUtils() {
211:                 // is empty
212:         }
213: }