Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0e6c45e69dff57700857bb549b96851914ccb11 (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.timing.custom.figures;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.GradientStyle;
import org.eclipse.gmf.tooling.runtime.draw2d.CenterLayout;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.common.figure.node.PapyrusNodeFigure;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.widgets.Display;

public class CompactStateFigure extends PapyrusNodeFigure implements IPapyrusUMLElementFigure {

	public static final int PREFERRED_HEIGHT = 32;

	private final WrappingLabel label;

	private boolean infinite;

	/**
	 * @param infinite
	 *        Whether the state has an end cap (the last state doesn't end)
	 */
	public void setInfinite(final boolean infinite) {
		this.infinite = infinite;
	}

	public boolean isInfinite() {
		return this.infinite;
	}

	public CompactStateFigure() {
		super();
		setLayoutManager(new CenterLayout());
		this.label = new WrappingLabel();
		this.label.setAlignment(PositionConstants.CENTER);
		add(this.label);
	}

	@Override
	public void paintFigure(final Graphics g) {
		// super.paintFigure(g);

		g.pushState();
		final int height = Math.min(PREFERRED_HEIGHT, this.bounds.height);
		final int middleY = this.bounds.y + this.bounds.height / 2;
		final int x = this.bounds.x;
		final int w = this.bounds.width - 1;
		final int y = middleY - height / 2;
		final int h = height - 1;

		g.setLineWidth(1);

		final int cornerSize = Math.min(h / 3, w / 3);

		final int[] polygon;
		if(this.infinite) {
			polygon = new int[]{ x, middleY, x + cornerSize, y, x + w + 1, y, x + w + 1, y + h, x + cornerSize, y + h };
		} else {
			polygon = new int[]{ x, middleY, x + cornerSize, y, x + w - cornerSize, y, x + w, middleY, x + w - cornerSize, y + h, x + cornerSize, y + h };
		}
		g.setAntialias(SWT.ON);
		if(isUsingGradient()) {
			applyTransparency(g);
			final Pattern pattern;
			// apparently, patterns are not scaled automatically, so we need to scale the pattern so that it fits the
			// current zoom level
			final double scale = g.getAbsoluteScale();
			final int xScaled = (int)(this.bounds.x * scale);
			final int yScaled = (int)(this.bounds.y * scale);
			final int widthScaled = (int)(this.bounds.width * scale);
			final int heightScaled = (int)(height * scale);
			if(getGradientStyle() == GradientStyle.VERTICAL) {
				pattern = new Pattern(Display.getCurrent(), xScaled, yScaled, xScaled, yScaled + heightScaled, FigureUtilities.integerToColor(Integer.valueOf(getGradientColor2())), FigureUtilities.integerToColor(Integer.valueOf(getGradientColor1())));
			} else {
				pattern = new Pattern(Display.getCurrent(), xScaled, yScaled, xScaled + widthScaled, this.bounds.y, FigureUtilities.integerToColor(Integer.valueOf(getGradientColor2())), FigureUtilities.integerToColor(Integer.valueOf(getGradientColor1())));
			}
			g.setBackgroundPattern(pattern);
			g.fillPolygon(polygon);
			g.drawPolygon(polygon);
			pattern.dispose();
		} else {
			g.setBackgroundColor(getBackgroundColor());
			g.setForegroundColor(getForegroundColor());
			// FIXME: the figure is not filled because the default fill color is black
			// g.fillPolygon(polygon);
			g.drawPolygon(polygon);
		}
		g.popState();
		// displayDebugFigure(g);
	}

	// private void displayDebugFigure(final Graphics g) {
	// g.pushState();
	// g.setForegroundColor(ColorConstants.red);
	// for (int x = 0; x < this.bounds.width - 1; x++) {
	// final int y = computeHalfHeightAtAbscissa(x, this.bounds.width, this.bounds.height, this.infinite);
	// g.drawRectangle(this.bounds.x + x, this.bounds.y + this.bounds.height / 2 - y, 1, 1);
	// g.drawRectangle(this.bounds.x + x, this.bounds.y + this.bounds.height / 2 + y - 1, 1, 1);
	// }
	// g.popState();
	// }

	/**
	 * Compute the half height of a CompactStateFigure with the given width and height, at the given abscissa.
	 * <p>
	 * This function is useful in order to place an OccurrenceSpecification along the border of a StateInvariant.
	 * 
	 * @param x
	 *        the x coordinate at which to compute the height
	 * @param figureWidth
	 *        the width of the CompactStateFigure
	 * @param figureHeight
	 *        the height of the CompactStateFigure
	 * @param infinite
	 *        whether the figure ends with a cap (not infinite) or not (infinite)
	 * @return the half height at the given position
	 */
	public static int computeHalfHeightAtAbscissa(final int x, final int figureWidth, final int figureHeight, final boolean infinite) {
		final int height = Math.min(PREFERRED_HEIGHT, figureHeight);
		final int width = figureWidth;
		final int halfHeight = figureHeight / 2;
		final int cornerSize = Math.min(height / 3, width / 3);

		final int resultY;
		if(x < cornerSize) {
			// on the left corner
			final double slope = (height / 2.0) / cornerSize;
			resultY = (int)(x * slope);
		} else if(infinite) {
			// on the straight border
			resultY = halfHeight;
		} else if(x < width - cornerSize) {
			// on the straight border
			resultY = halfHeight;
		} else {
			// on the right corner
			final double slope = (double)halfHeight / cornerSize;
			resultY = (int)(halfHeight - ((x - width + cornerSize) * slope));
		}
		return resultY;
	}

	public WrappingLabel getCompactStateInvariantLabel() {
		return this.label;
	}

	@Override
	public Dimension getPreferredSize(final int wHint, final int hHint) {
		return new Dimension(-1, PREFERRED_HEIGHT);
	}

	@Override
	protected void paintBorder(final Graphics graphics) {
		// no border
	}

	public void setStereotypeDisplay(final String stereotypes, final Image image) {
		// TODO Auto-generated method stub

	}
}

Back to the top