Project

General

Profile

Download (1.56 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.model;
12

    
13
import org.eclipse.swt.graphics.GC;
14
import org.eclipse.swt.widgets.Control;
15

    
16
/**
17
 * <p>TextHelper class.</p>
18
 *
19
 * @author n.hoffmann
20
 * @created Mar 30, 2010
21
 * @version 1.0
22
 */
23
public class TextHelper {
24
	
25
	/** Constant <code>ELLIPSIS="..."</code> */
26
	public static final String ELLIPSIS = "...";
27
		
28
	/**
29
	 * <p>shortenText</p>
30
	 *
31
	 * @param textValue a {@link java.lang.String} object.
32
	 * @param control a {@link org.eclipse.swt.widgets.Control} object.
33
	 * @see org.eclipse.jface.dialogs.Dialog#shortenText(String, Control)
34
	 * @return a {@link java.lang.String} object.
35
	 */
36
	public static String shortenText(String textValue, Control control) {
37
		if (textValue == null) {
38
			return null;
39
		}
40
		GC gc = new GC(control);
41
		int maxWidth = control.getBounds().width;
42
		int maxExtent = gc.textExtent(textValue).x;
43
		if (maxExtent < maxWidth) {
44
			gc.dispose();
45
			return textValue;
46
		}
47
		int length = textValue.length();
48
		int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent)));
49
		
50
		int end = length - charsToClip;
51
		while (end > 0) {
52
			String s1 = textValue.substring(0, end);
53
			String s = s1 + ELLIPSIS;
54
			int l = gc.textExtent(s).x;
55
			if (l < maxWidth) {
56
				gc.dispose();
57
				return s;
58
			}
59
			end--;
60
		}
61
		gc.dispose();
62
		return textValue;
63
		
64
	}
65
}
(28-28/29)