Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 333508
Collapse All | Expand All

(-)src/org/eclipse/nebula/widgets/grid/GridCellRenderer.java (+30 lines)
Lines 49-54 Link Here
49
    private boolean wordWrap = false;
49
    private boolean wordWrap = false;
50
    
50
    
51
    private boolean dragging = false;
51
    private boolean dragging = false;
52
    
53
    private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE;
54
55
	/** Truncate at the end */
56
	public static final int TRUNCATE_END=3;
57
58
	/** Truncate in the middle (equal number of chars before/after the truncation) */
59
	public static final int TRUNCATE_MIDDLE=2;
60
61
	/** Truncate at the start */
62
	public static final int TRUNCATE_START=1;
52
63
53
    /**
64
    /**
54
     * @return Returns the row.
65
     * @return Returns the row.
Lines 261-264 Link Here
261
    {
272
    {
262
    	this.dragging = dragging;
273
    	this.dragging = dragging;
263
    }
274
    }
275
276
    /**
277
     * Get the truncation style
278
     * @return .
279
     */
280
	public int getTruncationStyle() {
281
		return truncationStyle;
282
	}
283
284
	/**
285
	 * Set the truncation style to use when cell content is too large.
286
	 * @see GridCellRenderer#TRUNCATE_END
287
	 * @see GridCellRenderer#TRUNCATE_MIDDLE
288
	 * @see GridCellRenderer#TRUNCATE_START
289
	 * @param truncationStyle
290
	 */
291
	public void setTruncationStyle(int truncationStyle) {
292
		this.truncationStyle = truncationStyle;
293
	}
264
}
294
}
(-)src/org/eclipse/nebula/widgets/grid/GridFooterRenderer.java (+21 lines)
Lines 24-29 Link Here
24
 */
24
 */
25
public abstract class GridFooterRenderer extends AbstractInternalWidget
25
public abstract class GridFooterRenderer extends AbstractInternalWidget
26
{
26
{
27
    private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE;
28
27
    /**
29
    /**
28
     * Returns the bounds of the text in the cell.  This is used when displaying in-place tooltips.
30
     * Returns the bounds of the text in the cell.  This is used when displaying in-place tooltips.
29
     * If <code>null</code> is returned here, in-place tooltips will not be displayed.  If the 
31
     * If <code>null</code> is returned here, in-place tooltips will not be displayed.  If the 
Lines 39-42 Link Here
39
    {
41
    {
40
        return null;
42
        return null;
41
    }
43
    }
44
45
    /**
46
     * Get the truncation style
47
     * @return .
48
     */
49
	public int getTruncationStyle() {
50
		return truncationStyle;
51
	}
52
53
	/**
54
	 * Set the truncation style to use when cell content is too large.
55
	 * @see GridCellRenderer#TRUNCATE_END
56
	 * @see GridCellRenderer#TRUNCATE_MIDDLE
57
	 * @see GridCellRenderer#TRUNCATE_START
58
	 * @param truncationStyle
59
	 */
60
	public void setTruncationStyle(int truncationStyle) {
61
		this.truncationStyle = truncationStyle;
62
	}
42
}
63
}
(-)src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java (-1 / +23 lines)
Lines 28-33 Link Here
28
{
28
{
29
    private boolean wordWrap = false;
29
    private boolean wordWrap = false;
30
    
30
    
31
    private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE;
32
31
    /**
33
    /**
32
     * Returns the bounds of the text in the cell.  This is used when displaying in-place tooltips.
34
     * Returns the bounds of the text in the cell.  This is used when displaying in-place tooltips.
33
     * If <code>null</code> is returned here, in-place tooltips will not be displayed.  If the 
35
     * If <code>null</code> is returned here, in-place tooltips will not be displayed.  If the 
Lines 79-83 Link Here
79
  public void setWordWrap(boolean wordWrap)
81
  public void setWordWrap(boolean wordWrap)
80
  {
82
  {
81
      this.wordWrap = wordWrap;
83
      this.wordWrap = wordWrap;
82
  }	
84
  }
85
  
86
  /**
87
   * Get the truncation style
88
   * @return .
89
   */
90
	public int getTruncationStyle() {
91
		return truncationStyle;
92
	}
93
94
	/**
95
	 * Set the truncation style to use when cell content is too large.
96
	 * @see GridCellRenderer#TRUNCATE_END
97
	 * @see GridCellRenderer#TRUNCATE_MIDDLE
98
	 * @see GridCellRenderer#TRUNCATE_START
99
	 * @param truncationStyle
100
	 */
101
	public void setTruncationStyle(int truncationStyle) {
102
		this.truncationStyle = truncationStyle;
103
	}
104
83
}
105
}
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnGroupHeaderRenderer.java (-1 / +1 lines)
Lines 87-93 Link Here
87
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
87
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
88
        if (!isWordWrap())
88
        if (!isWordWrap())
89
        {
89
        {
90
          gc.drawString(TextUtils.getShortString(gc, group.getText(), width), getBounds().x + x,
90
          gc.drawString(TextUtils.getShortString(gc, group.getText(), width, getTruncationStyle()), getBounds().x + x,
91
              getBounds().y + topMargin);
91
              getBounds().y + topMargin);
92
        }
92
        }
93
        else
93
        else
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java (-1 / +1 lines)
Lines 169-175 Link Here
169
169
170
        if (!isWordWrap())
170
        if (!isWordWrap())
171
        {
171
        {
172
          text = TextUtils.getShortString(gc, text, width);
172
          text = TextUtils.getShortString(gc, text, width, getTruncationStyle());
173
            //y -= gc.getFontMetrics().getHeight();
173
            //y -= gc.getFontMetrics().getHeight();
174
        }
174
        }
175
175
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultCellRenderer.java (-1 / +1 lines)
Lines 201-207 Link Here
201
201
202
        if (!isWordWrap())
202
        if (!isWordWrap())
203
        {
203
        {
204
            String text = TextUtils.getShortString(gc, item.getText(getColumn()), width);
204
            String text = TextUtils.getShortString(gc, item.getText(getColumn()), width, getTruncationStyle());
205
205
206
            if (getAlignment() == SWT.RIGHT)
206
            if (getAlignment() == SWT.RIGHT)
207
            {
207
            {
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnFooterRenderer.java (-1 / +1 lines)
Lines 112-118 Link Here
112
112
113
        int y = getBounds().y + getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
113
        int y = getBounds().y + getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
114
114
115
        String text = TextUtils.getShortString(gc, column.getFooterText(), width);
115
        String text = TextUtils.getShortString(gc, column.getFooterText(), width, getTruncationStyle());
116
116
117
        if (column.getAlignment() == SWT.RIGHT)
117
        if (column.getAlignment() == SWT.RIGHT)
118
        {
118
        {
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultRowHeaderRenderer.java (-2 / +25 lines)
Lines 13-18 Link Here
13
package org.eclipse.nebula.widgets.grid.internal;
13
package org.eclipse.nebula.widgets.grid.internal;
14
14
15
import org.eclipse.nebula.widgets.grid.AbstractRenderer;
15
import org.eclipse.nebula.widgets.grid.AbstractRenderer;
16
import org.eclipse.nebula.widgets.grid.GridCellRenderer;
16
import org.eclipse.nebula.widgets.grid.GridColumn;
17
import org.eclipse.nebula.widgets.grid.GridColumn;
17
import org.eclipse.nebula.widgets.grid.GridItem;
18
import org.eclipse.nebula.widgets.grid.GridItem;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
Lines 42-48 Link Here
42
    int bottomMargin = 3;
43
    int bottomMargin = 3;
43
44
44
    private TextLayout textLayout;
45
    private TextLayout textLayout;
45
    
46
47
    private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE;
48
46
    /**
49
    /**
47
     * {@inheritDoc}
50
     * {@inheritDoc}
48
     */
51
     */
Lines 168-174 Link Here
168
        if (!item.getParent().isWordWrapHeader())
171
        if (!item.getParent().isWordWrapHeader())
169
        {
172
        {
170
            y += (getBounds().height - gc.stringExtent(text).y) / 2;
173
            y += (getBounds().height - gc.stringExtent(text).y) / 2;
171
            gc.drawString(TextUtils.getShortString(gc, text, width), getBounds().x + x + selectionOffset, y + selectionOffset, true);
174
            gc.drawString(TextUtils.getShortString(gc, text, width, this.truncationStyle), getBounds().x + x + selectionOffset, y + selectionOffset, true);
172
        }
175
        }
173
        else
176
        else
174
        {
177
        {
Lines 275-278 Link Here
275
            });
278
            });
276
        }
279
        }
277
    }
280
    }
281
282
    /**
283
     * Get the truncation style
284
     * @return .
285
     */
286
	public int getTruncationStyle() {
287
		return truncationStyle;
288
	}
289
290
	/**
291
	 * Set the truncation style to use when cell content is too large.
292
	 * @see GridCellRenderer#TRUNCATE_END
293
	 * @see GridCellRenderer#TRUNCATE_MIDDLE
294
	 * @see GridCellRenderer#TRUNCATE_START
295
	 * @param truncationStyle
296
	 */
297
	public void setTruncationStyle(int truncationStyle) {
298
		this.truncationStyle = truncationStyle;
299
	}
300
278
}
301
}
(-)src/org/eclipse/nebula/widgets/grid/internal/TextUtils.java (-50 / +91 lines)
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
}

Return to bug 333508