1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
import org.eclipse.swt.graphics.Color;
|
14
|
|
15
|
import org.eclipse.draw2d.geometry.Insets;
|
16
|
import org.eclipse.draw2d.geometry.Rectangle;
|
17
|
|
18
|
/**
|
19
|
* SchemeBorder allows the creation of borders based on
|
20
|
* {@link SchemeBorder.Scheme Schemes}. A <i>Scheme</i> is a class whose only
|
21
|
* purpose is to carry border specific information. SchemeBorder renders the
|
22
|
* border based on the information given by the <i>Scheme</i> set to it.
|
23
|
*/
|
24
|
public class SchemeBorder extends AbstractBorder implements ColorConstants {
|
25
|
|
26
|
/** The {@link SchemeBorder.Scheme} associated with this SchemeBorder **/
|
27
|
protected Scheme scheme = null;
|
28
|
|
29
|
/** Arrays of Colors, used for shadow or highlight effects **/
|
30
|
protected static final Color DARKEST_DARKER[] = new Color[] {
|
31
|
buttonDarkest, buttonDarker }, LIGHTER_DARKER[] = new Color[] {
|
32
|
buttonLightest, buttonDarker }, DARKER_LIGHTER[] = new Color[] {
|
33
|
buttonDarker, buttonLightest };
|
34
|
|
35
|
/**
|
36
|
* Holds a set of information about a border, which can be changed to create
|
37
|
* a wide range of schemes. Provides support for border opacity, size,
|
38
|
* highlight side and shadow side colors.
|
39
|
*/
|
40
|
public static class Scheme {
|
41
|
private Insets insets;
|
42
|
private boolean isOpaque = false;
|
43
|
|
44
|
/** Arrays of Colors, used for highlight and shadow effecsts */
|
45
|
protected Color highlight[], shadow[];
|
46
|
|
47
|
/**
|
48
|
* Constructs a default border Scheme with no border sides.
|
49
|
*
|
50
|
* @since 2.0
|
51
|
*/
|
52
|
protected Scheme() {
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Constructs a border Scheme with the specified highlight and shadow
|
57
|
* colors. The size of the border depends on the number of colors passed
|
58
|
* in for each parameter. Hightlight colors are used in the top and left
|
59
|
* sides of the border, and Shadow colors are used in the bottom and
|
60
|
* right sides of the border.
|
61
|
*
|
62
|
* @param highlight
|
63
|
* the hightlight colors
|
64
|
* @param shadow
|
65
|
* the shadow colors
|
66
|
* @since 2.0
|
67
|
*/
|
68
|
public Scheme(Color[] highlight, Color[] shadow) {
|
69
|
this.highlight = highlight;
|
70
|
this.shadow = shadow;
|
71
|
init();
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Constructs a border scheme with the specified colors. The input
|
76
|
* colors serve as both highlight and shadow colors. The size of the
|
77
|
* border is the number of colors passed in as input. Hightlight colors
|
78
|
* are used in the top and left sides of the border, and Shadow colors
|
79
|
* are used in the bottom and right sides of the border.
|
80
|
*
|
81
|
* @param colors
|
82
|
* the colors to be used for the border
|
83
|
* @since 2.0
|
84
|
*/
|
85
|
public Scheme(Color[] colors) {
|
86
|
highlight = shadow = colors;
|
87
|
init();
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Calculates and returns the Insets for this border Scheme. The
|
92
|
* calculations depend on the number of colors passed in as input.
|
93
|
*
|
94
|
* @return the Insets used by this border
|
95
|
* @since 2.0
|
96
|
*/
|
97
|
protected Insets calculateInsets() {
|
98
|
int tl = getHighlight().length;
|
99
|
int br = getShadow().length;
|
100
|
return new Insets(tl, tl, br, br);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Calculates and retuns the opaque state of this border scheme. Returns
|
105
|
* <code>false</code> if any of the border colors are <code>null</code>.
|
106
|
* This is done to prevent the appearance of underlying pixels since the
|
107
|
* border color is <code>null</code>.
|
108
|
*
|
109
|
* @return <code>true</code> if this border is opaque
|
110
|
* @since 2.0
|
111
|
*/
|
112
|
protected boolean calculateOpaque() {
|
113
|
Color colors[] = getHighlight();
|
114
|
for (int i = 0; i < colors.length; i++)
|
115
|
if (colors[i] == null)
|
116
|
return false;
|
117
|
colors = getShadow();
|
118
|
for (int i = 0; i < colors.length; i++)
|
119
|
if (colors[i] == null)
|
120
|
return false;
|
121
|
return true;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Returns the highlight colors of this border scheme as an array of
|
126
|
* Colors.
|
127
|
*
|
128
|
* @return the highlight colors
|
129
|
* @since 2.0
|
130
|
*/
|
131
|
protected Color[] getHighlight() {
|
132
|
return highlight;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Returns the Insets required by this Scheme.
|
137
|
*
|
138
|
* @return the Insets
|
139
|
* @since 2.0
|
140
|
*/
|
141
|
protected Insets getInsets() {
|
142
|
return insets;
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Returns the shadow colors of this border scheme as an array of
|
147
|
* Colors.
|
148
|
*
|
149
|
* @return the shadow colors
|
150
|
* @since 2.0
|
151
|
*/
|
152
|
protected Color[] getShadow() {
|
153
|
return shadow;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Calculates and initializes the properties of this border scheme.
|
158
|
*
|
159
|
* @since 2.0
|
160
|
*/
|
161
|
protected void init() {
|
162
|
insets = calculateInsets();
|
163
|
isOpaque = calculateOpaque();
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Returns whether this border should be opaque or not.
|
168
|
*
|
169
|
* @return <code>true</code> if this border is opaque
|
170
|
* @since 2.0
|
171
|
*/
|
172
|
protected boolean isOpaque() {
|
173
|
return isOpaque;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Interface which defines some commonly used schemes for the border. These
|
179
|
* schemes can be given as input to the {@link SchemeBorder SchemeBorder} to
|
180
|
* generate appropriate borders.
|
181
|
*/
|
182
|
public static interface SCHEMES {
|
183
|
|
184
|
/** Schemes used for shadow and highlight effects **/
|
185
|
Scheme BUTTON_CONTRAST = new Scheme(new Color[] { button,
|
186
|
buttonLightest }, DARKEST_DARKER), BUTTON_RAISED = new Scheme(
|
187
|
new Color[] { buttonLightest }, DARKEST_DARKER),
|
188
|
BUTTON_PRESSED = new Scheme(DARKEST_DARKER,
|
189
|
new Color[] { buttonLightest }), RAISED = new Scheme(
|
190
|
new Color[] { buttonLightest },
|
191
|
new Color[] { buttonDarkest }), LOWERED = new Scheme(
|
192
|
new Color[] { buttonDarkest },
|
193
|
new Color[] { buttonLightest }), RIDGED = new Scheme(
|
194
|
LIGHTER_DARKER, DARKER_LIGHTER), ETCHED = new Scheme(
|
195
|
DARKER_LIGHTER, LIGHTER_DARKER);
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Constructs a default SchemeBorder with no scheme defined.
|
200
|
*
|
201
|
* @since 2.0
|
202
|
*/
|
203
|
protected SchemeBorder() {
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Constructs a SchemeBorder with the Scheme given as input.
|
208
|
*
|
209
|
* @param scheme
|
210
|
* the Scheme to be used by this border
|
211
|
* @since 2.0
|
212
|
*/
|
213
|
public SchemeBorder(Scheme scheme) {
|
214
|
setScheme(scheme);
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* @see Border#getInsets(IFigure)
|
219
|
*/
|
220
|
public Insets getInsets(IFigure figure) {
|
221
|
return getScheme().getInsets();
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Returns the scheme used by this border.
|
226
|
*
|
227
|
* @return the Scheme used by this border
|
228
|
* @since 2.0
|
229
|
*/
|
230
|
protected Scheme getScheme() {
|
231
|
return scheme;
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* Returns the opaque state of this border. Returns <code>true</code>
|
236
|
* indicating that this will fill in the area enclosed by the border.
|
237
|
*
|
238
|
* @see Border#isOpaque()
|
239
|
*/
|
240
|
public boolean isOpaque() {
|
241
|
return true;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* Sets the Scheme for this border to the Scheme given as input.
|
246
|
*
|
247
|
* @param scheme
|
248
|
* the Scheme for this border
|
249
|
* @since 2.0
|
250
|
*/
|
251
|
protected void setScheme(Scheme scheme) {
|
252
|
this.scheme = scheme;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* @see Border#paint(IFigure, Graphics, Insets)
|
257
|
*/
|
258
|
public void paint(IFigure figure, Graphics g, Insets insets) {
|
259
|
Color[] tl = scheme.getHighlight();
|
260
|
Color[] br = scheme.getShadow();
|
261
|
|
262
|
paint(g, figure, insets, tl, br);
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* Paints the border using the information in the set Scheme and the inputs
|
267
|
* given. Side widths are determined by the number of colors in the Scheme
|
268
|
* for each side.
|
269
|
*
|
270
|
* @param graphics
|
271
|
* the graphics object
|
272
|
* @param fig
|
273
|
* the figure this border belongs to
|
274
|
* @param insets
|
275
|
* the insets
|
276
|
* @param tl
|
277
|
* the highlight (top/left) colors
|
278
|
* @param br
|
279
|
* the shadow (bottom/right) colors
|
280
|
*/
|
281
|
protected void paint(Graphics graphics, IFigure fig, Insets insets,
|
282
|
Color[] tl, Color[] br) {
|
283
|
graphics.setLineWidth(1);
|
284
|
graphics.setLineStyle(Graphics.LINE_SOLID);
|
285
|
graphics.setXORMode(false);
|
286
|
|
287
|
Rectangle rect = getPaintRectangle(fig, insets);
|
288
|
|
289
|
int top = rect.y;
|
290
|
int left = rect.x;
|
291
|
int bottom = rect.bottom() - 1;
|
292
|
int right = rect.right() - 1;
|
293
|
Color color;
|
294
|
|
295
|
for (int i = 0; i < br.length; i++) {
|
296
|
color = br[i];
|
297
|
graphics.setForegroundColor(color);
|
298
|
graphics.drawLine(right - i, bottom - i, right - i, top + i);
|
299
|
graphics.drawLine(right - i, bottom - i, left + i, bottom - i);
|
300
|
}
|
301
|
|
302
|
right--;
|
303
|
bottom--;
|
304
|
|
305
|
for (int i = 0; i < tl.length; i++) {
|
306
|
color = tl[i];
|
307
|
graphics.setForegroundColor(color);
|
308
|
graphics.drawLine(left + i, top + i, right - i, top + i);
|
309
|
graphics.drawLine(left + i, top + i, left + i, bottom - i);
|
310
|
}
|
311
|
}
|
312
|
|
313
|
}
|