Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 58712ab9b80d6613f49ec72ee31628b2eb5e592f (plain) (blame)
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
/*****************************************************************************
 * Copyright (c) 2009, 2014 CEA LIST, Atos Origin, and others.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Atos Origin - Enable extending with a composite figure, by adding overrideable methods.
 *  Christian W. Damus (CEA) - bug 392301
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.figure.node;

import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.graphics.ColorRegistry;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.GradientStyle;
import org.eclipse.papyrus.infra.gmfdiag.common.figure.node.IPapyrusNodeFigure;
import org.eclipse.swt.graphics.Color;

/**
 * Common node figure. In charge of background, font, gradient, foreground,
 * border, shadow
 */
public class PapyrusNodeFigure extends NodeFigure implements IPapyrusNodeFigure {

	/**
	 * The border color.
	 */
	private Color borderColor = ColorConstants.black;

	/**
	 * The shadow
	 */
	private boolean shadow = true;

	/** Default custom dash values */
	public static final int[] DEFAULT_CUSTOM_DASH = { 5, 5 };

	/** Table used to draw figure border as dashed line. */
	protected int[] customDash = null;

	/** Get custom dash values */
	public int[] getCustomDash() {
		if (customDash == null) {
			// Initialize dash property for dashed border representation.
			customDash = DEFAULT_CUSTOM_DASH;
		}
		return customDash;
	}

	/** Set custom dash values */
	public void setCustomDash(int[] dash) {
		this.customDash = dash;
	}

	protected LineBorder shadowborder;

	public PapyrusNodeFigure() {
		super();
		createCompositeFigureStructure();

		shadowborder = new RectangularShadowBorder(3, getForegroundColor());
		setBorder(getBorderedFigure(), shadowborder);
	}

	/**
	 * Create the composite structure of this figure, by adding it its necessary
	 * children. Children should override and implement this method in case they
	 * have a composite figure, to add children forming the overall structure.
	 */
	protected void createCompositeFigureStructure() {
		// By default, do nothing : the figure is not composite
	}

	/**
	 * Get the figure on which the border must be drawn. Children should
	 * override and implement this method in case the border must not be drawn
	 * on the overall figure. The returned figure shall be created in the method {@link #createCompositeFigureStructure()}.
	 *
	 * @return the figure to draw the border on
	 * @see #createCompositeFigureStructure()
	 */
	protected IFigure getBorderedFigure() {
		// by default, border is drawn on the overall figure
		return this;
	}

	/**
	 * Sets the border to the bordered figure. This method is not intended to be
	 * overridden. Override {@link #getBorderedFigure()} instead.
	 *
	 * @param borderedFigure
	 *            the figure on which the border shall be drawn (or null for
	 *            this figure)
	 * @param border
	 *            The new border
	 * @see IFigure#setBorder(Border)
	 */
	protected void setBorder(IFigure borderedFigure, Border border) {
		if (borderedFigure == null) {
			super.setBorder(border);
		} else {
			borderedFigure.setBorder(border);
		}
	}

	@Override
	public boolean isShadow() {
		return shadow;
	}

	@Override
	public void setShadow(boolean shadow) {
		this.shadow = shadow;
		if (shadow == true) {
			setBorder(getBorderedFigure(), shadowborder);
		} else {
			setBorder(getBorderedFigure(), getDefaultBorder(null));
		}
	}

	/**
	 * Sets the border color.
	 *
	 * @param borderColor
	 *            the border color
	 */
	@Override
	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
		setBorder(getBorderedFigure(), getDefaultBorder(borderColor));
	}

	/**
	 * Get the default non shadow border to use. Children can override and
	 * implement this method if necessary, taking care not to fail if
	 * borderColor is null
	 *
	 * @param borderColor
	 *            the color of the border to take if possible or null
	 * @return a non shadow border
	 */
	protected Border getDefaultBorder(Color borderColor) {

		// Default border is line border
		LineBorder lineBorder = null;
		if (borderColor != null) {
			lineBorder = new LineBorder(borderColor);
		} else {
			lineBorder = new LineBorder();
		}

		// Set border style and width
		lineBorder.setStyle(getLineStyle());
		lineBorder.setWidth(getLineWidth());

		return lineBorder;
	}

	/**
	 * Gets the border color.
	 *
	 * @return the border color
	 */
	@Override
	public Color getBorderColor() {
		return this.borderColor;
	}

	/**
	 * Paint figure.
	 *
	 * @param graphics
	 *            the graphics
	 */
	@Override
	public void paintFigure(Graphics graphics) {
		super.paintFigure(graphics);

		paintBackground(graphics, getBounds());

		shadowborder.setColor(getForegroundColor());

	}

	/**
	 * Paint the background of the figure. If this figure uses gradient, then it
	 * will paint the background with the gradient informations. Otherwise it
	 * will use the background color.
	 *
	 * @param graphics
	 *            the graphics
	 * @param rectangle
	 *            the rectangle where the background needs to be fill.
	 */
	protected void paintBackground(Graphics graphics, Rectangle rectangle) {
		if (isUsingGradient()) {
			applyTransparency(graphics);
			boolean isVertical = (getGradientStyle() == GradientStyle.VERTICAL) ? true : false;
			graphics.setBackgroundColor(ColorRegistry.getInstance().getColor(getGradientColor1()));
			graphics.setForegroundColor(ColorRegistry.getInstance().getColor(getGradientColor2()));
			graphics.fillGradient(rectangle, isVertical);
		} else {
			graphics.setBackgroundColor(getBackgroundColor());
			graphics.setForegroundColor(getForegroundColor());
			graphics.fillRectangle(rectangle);
		}
	}

	/**
	 * <pre>
	 * This figure manages the border representation with custom dashes.
	 *
	 * {@inheritDoc}
	 * </pre>
	 */
	@Override
	protected void paintBorder(Graphics graphics) {

		if (getLineStyle() == Graphics.LINE_CUSTOM) {
			graphics.setLineDash(getCustomDash());
		}
		super.paintBorder(graphics);
	}

	/**
	 * <pre>
	 * This method propagates the new line style to the border
	 *
	 * @see org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure#setLineStyle(int)
	 * </pre>
	 *
	 * @param s
	 *            the new line style value
	 */
	@Override
	public void setLineStyle(int s) {
		if ((getBorder() != null) && (getBorder() instanceof LineBorder)) {
			((LineBorder) getBorder()).setStyle(s);
		}

		super.setLineStyle(s);
	}

	/**
	 * <pre>
	 * This method propagates the new line width to the border
	 *
	 * @see org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure#setLineWidth(int)
	 * </pre>
	 *
	 * @param w
	 *            the new line width value
	 */
	@Override
	public void setLineWidth(int w) {
		if ((getBorder() != null) && (getBorder() instanceof LineBorder)) {
			((LineBorder) getBorder()).setWidth(w);
		}

		super.setLineWidth(w);
	}
}

Back to the top