-
Notifications
You must be signed in to change notification settings - Fork 96
/
DefaultColumnFooterRenderer.java
197 lines (145 loc) · 4.96 KB
/
DefaultColumnFooterRenderer.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
/*******************************************************************************
* 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
* Marty Jones<martybjones@gmail.com> - custom header/footer font in bug 293743
*******************************************************************************/
package org.eclipse.nebula.widgets.grid.internal;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridFooterRenderer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
/**
* The column footer renderer.
*
* @author Tom Schindl - tom.schindl@bestsolution.at
* @since 2.0.0
*/
public class DefaultColumnFooterRenderer extends GridFooterRenderer
{
int leftMargin = 6;
int rightMargin = 6;
int topMargin = 3;
int bottomMargin = 3;
int arrowMargin = 6;
int imageSpacing = 3;
/**
* {@inheritDoc}
*/
public Point computeSize(GC gc, int wHint, int hHint, Object value)
{
GridColumn column = (GridColumn)value;
gc.setFont(column.getFooterFont());
int x = 0;
x += leftMargin;
x += gc.stringExtent(column.getText()).x + rightMargin;
int y = 0;
y += topMargin;
y += gc.getFontMetrics().getHeight();
y += bottomMargin;
if (column.getFooterImage() != null)
{
x += column.getFooterImage().getBounds().width + imageSpacing;
y = Math.max(y, topMargin + column.getFooterImage().getBounds().height + bottomMargin);
}
return new Point(x, y);
}
/**
* {@inheritDoc}
*/
public void paint(GC gc, Object value)
{
GridColumn column = (GridColumn)value;
// set the font to be used to display the text.
gc.setFont(column.getFooterFont());
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width,
getBounds().height);
gc.drawLine(getBounds().x, getBounds().y, getBounds().x
+ getBounds().width, getBounds().y);
int x = leftMargin;
if (column.getFooterImage() != null)
{
gc.drawImage(column.getFooterImage(), getBounds().x + x,
getBounds().y + getBounds().height - bottomMargin - column.getFooterImage().getBounds().height);
x += column.getFooterImage().getBounds().width + imageSpacing;
}
int width = getBounds().width - x;
if (column.getSort() == SWT.NONE)
{
width -= rightMargin;
}
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
int y = getBounds().y + getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
String text = TextUtils.getShortStr(gc, column.getFooterText(), width, truncationStyle);
if (column.getAlignment() == SWT.RIGHT)
{
int len = gc.stringExtent(text).x;
if (len < width)
{
x += width - len;
}
}
else if (column.getAlignment() == SWT.CENTER)
{
int len = gc.stringExtent(text).x;
if (len < width)
{
x += (width - len) / 2;
}
}
gc.drawString(text, getBounds().x + x,
y,true);
}
/**
* {@inheritDoc}
*/
public boolean notify(int event, Point point, Object value)
{
return false;
}
/**
* {@inheritDoc}
*/
public Rectangle getTextBounds(Object value, boolean preferred)
{
GridColumn column = (GridColumn)value;
int x = leftMargin;
if (column.getImage() != null)
{
x += column.getImage().getBounds().width + imageSpacing;
}
GC gc = new GC(column.getParent());
gc.setFont(column.getFooterFont());
int y = getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
Rectangle bounds = new Rectangle(x,y,0,0);
Point p = gc.stringExtent(column.getText());
bounds.height = p.y;
if (preferred)
{
bounds.width = p.x;
}
else
{
int width = getBounds().width - x;
if (column.getSort() == SWT.NONE)
{
width -= rightMargin;
}
bounds.width = width;
}
gc.dispose();
return bounds;
}
}