-
Notifications
You must be signed in to change notification settings - Fork 98
/
DefaultRowHeaderRenderer.java
319 lines (263 loc) · 10.1 KB
/
DefaultRowHeaderRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* chris.gross@us.ibm.com - initial API and implementation
* Chuck.Mastrandrea@sas.com - wordwrapping in bug 222280
* smcduff@hotmail.com - wordwrapping in bug 222280
*******************************************************************************/
package org.eclipse.nebula.widgets.grid.internal;
import org.eclipse.nebula.widgets.grid.AbstractRenderer;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;
/**
* The row header renderer.
*
* @author chris.gross@us.ibm.com
* @since 2.0.0
*/
public class DefaultRowHeaderRenderer extends AbstractRenderer
{
int leftMargin = 6;
int rightMargin = 8;
int topMargin = 3;
int bottomMargin = 3;
private TextLayout textLayout;
private int truncationStyle =SWT.CENTER;
/**
* {@inheritDoc}
*/
@Override
public void paint(GC gc, Object value)
{
GridItem item = (GridItem) value;
String text = getHeaderText(item);
if (getHeaderFont(item) == null) {
gc.setFont(item.getParent().getFont());
} else {
gc.setFont(getHeaderFont(item));
}
Color background = getHeaderBackground(item);
if( background == null ) {
background = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
}
gc.setBackground(background);
if (isSelected() && item.getParent().getCellSelectionEnabled())
{
gc.setBackground(item.getParent().getCellHeaderSelectionBackground());
}
gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height + 1);
if (!item.getParent().getCellSelectionEnabled())
{
if (isSelected())
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
}
else
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
}
gc.drawLine(getBounds().x, getBounds().y, getBounds().x + getBounds().width - 1,
getBounds().y);
gc.drawLine(getBounds().x, getBounds().y, getBounds().x, getBounds().y + getBounds().height
- 1);
if (!isSelected())
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
gc.drawLine(getBounds().x + 1, getBounds().y + 1,
getBounds().x + getBounds().width - 2, getBounds().y + 1);
gc.drawLine(getBounds().x + 1, getBounds().y + 1, getBounds().x + 1,
getBounds().y + getBounds().height - 2);
}
if (isSelected())
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
}
else
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
}
gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x
+ getBounds().width - 1,
getBounds().y + getBounds().height - 1);
gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1, getBounds().x
+ getBounds().width - 1,
getBounds().y + getBounds().height - 1);
if (!isSelected())
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
gc.drawLine(getBounds().x + getBounds().width - 2, getBounds().y + 1,
getBounds().x + getBounds().width - 2, getBounds().y + getBounds().height
- 2);
gc.drawLine(getBounds().x + 1, getBounds().y + getBounds().height - 2,
getBounds().x + getBounds().width - 2, getBounds().y + getBounds().height
- 2);
}
}
else
{
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x
+ getBounds().width - 1,
getBounds().y + getBounds().height - 1);
gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1, getBounds().x
+ getBounds().width - 1,
getBounds().y + getBounds().height - 1);
}
int x = leftMargin;
Image image = getHeaderImage(item);
if( image != null ) {
if( isSelected() && !item.getParent().getCellSelectionEnabled() ) {
gc.drawImage(image, x + 1, getBounds().y + 1 + (getBounds().height - image.getBounds().height)/2);
x += 1;
} else {
gc.drawImage(image, x, getBounds().y + (getBounds().height - image.getBounds().height)/2);
}
x += image.getBounds().width + 5;
}
int width = getBounds().width - x;
width -= rightMargin;
Color foreground = getHeaderForeground(item);
if( foreground == null ) {
foreground = getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND);
}
gc.setForeground(foreground);
int y = getBounds().y;
int selectionOffset = 0;
if (isSelected() && !item.getParent().getCellSelectionEnabled())
{
selectionOffset = 1;
}
if (!item.getParent().isWordWrapHeader())
{
y += (getBounds().height - gc.stringExtent(text).y) / 2;
gc.drawString(TextUtils.getShortStr(gc, text, width, truncationStyle), getBounds().x + x + selectionOffset, y + selectionOffset, true);
}
else
{
getTextLayout(gc, item);
textLayout.setWidth(width < 1 ? 1 : width);
textLayout.setText(text);
if (item.getParent().isAutoHeight())
{
// Look through all columns to get the max height needed for this item
int columnCount = item.getParent().getColumnCount();
int maxHeight = textLayout.getBounds().height + topMargin + bottomMargin;
for (int i=0; i<columnCount; i++)
{
GridColumn column = item.getParent().getColumn(i);
if (column.getWordWrap())
{
int height = column.getCellRenderer().computeSize(gc, column.getWidth(), SWT.DEFAULT, item).y;
maxHeight = Math.max(maxHeight, height);
}
}
if (maxHeight != item.getHeight())
{
item.setHeight(maxHeight);
}
}
textLayout.draw(gc, getBounds().x + x + selectionOffset, y + selectionOffset);
}
}
private Font getHeaderFont(GridItem item){
return item.getHeaderFont();
}
/**
* {@inheritDoc}
*/
@Override
public Point computeSize(GC gc, int wHint, int hHint, Object value)
{
GridItem item = (GridItem) value;
String text = getHeaderText(item);
Image image = getHeaderImage(item);
Font previousFont = gc.getFont();
if (getHeaderFont(item) == null) {
gc.setFont(item.getParent().getFont());
} else {
gc.setFont(getHeaderFont(item));
}
int x = leftMargin;
if( image != null ) {
x += image.getBounds().width + 5;
}
x += gc.stringExtent(text).x + rightMargin;
int y = topMargin;
if( image != null ) {
y += Math.max(gc.getFontMetrics().getHeight(),image.getBounds().height);
} else {
y += gc.getFontMetrics().getHeight();
}
y += bottomMargin;
gc.setFont(previousFont);
return new Point(x, y);
}
private Image getHeaderImage(GridItem item) {
return item.getHeaderImage();
}
private String getHeaderText(GridItem item)
{
String text = item.getHeaderText();
if (text == null)
{
text = (item.getRowIndex() + 1) + "";
}
return text;
}
private Color getHeaderBackground(GridItem item) {
return item.getHeaderBackground();
}
private Color getHeaderForeground(GridItem item) {
return item.getHeaderForeground();
}
private void getTextLayout(GC gc, GridItem gridItem)
{
if (textLayout == null)
{
textLayout = new TextLayout(gc.getDevice());
textLayout.setFont(gc.getFont());
gridItem.getParent().addDisposeListener(new DisposeListener()
{
@Override
public void widgetDisposed(DisposeEvent e)
{
textLayout.dispose();
}
});
}
}
/**
* Get the truncation style
* @return the truncation style.
*/
public int getTruncationStyle() {
return truncationStyle;
}
/**
* Set the truncation style to use when cell content is too large.
* @see SWT#LEFT
* @see SWT#CENTER
* @see SWT#RIGHT
* @param truncationStyle
*/
public void setTruncationStyle(int truncationStyle) {
this.truncationStyle = truncationStyle;
}
}