1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.model;
|
11
|
|
12
|
import org.eclipse.swt.graphics.GC;
|
13
|
import org.eclipse.swt.widgets.Control;
|
14
|
|
15
|
/**
|
16
|
* <p>TextHelper class.</p>
|
17
|
*
|
18
|
* @author n.hoffmann
|
19
|
* @created Mar 30, 2010
|
20
|
* @version 1.0
|
21
|
*/
|
22
|
public class TextHelper {
|
23
|
|
24
|
/** Constant <code>ELLIPSIS="..."</code> */
|
25
|
public static final String ELLIPSIS = "...";
|
26
|
|
27
|
/**
|
28
|
* <p>shortenText</p>
|
29
|
*
|
30
|
* @param textValue a {@link java.lang.String} object.
|
31
|
* @param control a {@link org.eclipse.swt.widgets.Control} object.
|
32
|
* @see org.eclipse.jface.dialogs.Dialog#shortenText(String, Control)
|
33
|
* @return a {@link java.lang.String} object.
|
34
|
*/
|
35
|
public static String shortenText(String textValue, Control control) {
|
36
|
if (textValue == null) {
|
37
|
return null;
|
38
|
}
|
39
|
GC gc = new GC(control);
|
40
|
int maxWidth = control.getBounds().width;
|
41
|
int maxExtent = gc.textExtent(textValue).x;
|
42
|
if (maxExtent < maxWidth) {
|
43
|
gc.dispose();
|
44
|
return textValue;
|
45
|
}
|
46
|
int length = textValue.length();
|
47
|
int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent)));
|
48
|
|
49
|
int end = length - charsToClip;
|
50
|
while (end > 0) {
|
51
|
String s1 = textValue.substring(0, end);
|
52
|
String s = s1 + ELLIPSIS;
|
53
|
int l = gc.textExtent(s).x;
|
54
|
if (l < maxWidth) {
|
55
|
gc.dispose();
|
56
|
return s;
|
57
|
}
|
58
|
end--;
|
59
|
}
|
60
|
gc.dispose();
|
61
|
return textValue;
|
62
|
|
63
|
}
|
64
|
|
65
|
public static String deproxyClassName(Class clazz){
|
66
|
String name = clazz.getSimpleName();
|
67
|
if (name.indexOf("_$$")>-1){
|
68
|
return name.substring(0, name.indexOf("_$$"));
|
69
|
}else{
|
70
|
return name;
|
71
|
}
|
72
|
}
|
73
|
}
|