ref #9541 new referencing objects implementation in TaxEditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / TextHelper.java
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 package eu.etaxonomy.taxeditor.model;
10
11 import org.eclipse.swt.graphics.GC;
12 import org.eclipse.swt.widgets.Control;
13
14 /**
15 * @author n.hoffmann
16 * @created Mar 30, 2010
17 */
18 public class TextHelper {
19
20 /** Constant <code>ELLIPSIS="..."</code> */
21 public static final String ELLIPSIS = "...";
22
23 /**
24 * <p>shortenText</p>
25 *
26 * @param textValue a {@link java.lang.String} object.
27 * @param control a {@link org.eclipse.swt.widgets.Control} object.
28 * @see org.eclipse.jface.dialogs.Dialog#shortenText(String, Control)
29 * @return a {@link java.lang.String} object.
30 */
31 public static String shortenText(String textValue, Control control) {
32 if (textValue == null) {
33 return null;
34 }
35 GC gc = new GC(control);
36 int maxWidth = control.getBounds().width;
37 int maxExtent = gc.textExtent(textValue).x;
38 if (maxExtent < maxWidth) {
39 gc.dispose();
40 return textValue;
41 }
42 int length = textValue.length();
43 int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent)));
44
45 int end = length - charsToClip;
46 while (end > 0) {
47 String s1 = textValue.substring(0, end);
48 String s = s1 + ELLIPSIS;
49 int l = gc.textExtent(s).x;
50 if (l < maxWidth) {
51 gc.dispose();
52 return s;
53 }
54 end--;
55 }
56 gc.dispose();
57 return textValue;
58
59 }
60
61 public static String deproxyClassName(Class clazz){
62 String name = clazz.getSimpleName();
63 if (name.indexOf("_$$")>-1){
64 return name.substring(0, name.indexOf("_$$"));
65 }else{
66 return name;
67 }
68 }
69 }