View | Details | Raw Unified | Return to bug 281246 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/draw2d/PolylineConnection.java (+9 lines)
Lines 278-283 Link Here
278
}
278
}
279
279
280
/**
280
/**
281
 * Sets the decoration to be used along the {@link Connection}.
282
 * @param dec the new decoration
283
 * @since 3.5
284
 */
285
public void setDecoration(RotatableDecoration dec) {
286
	add(dec, new PolylineLabelLocator(this));
287
}
288
289
/**
281
 * Sets the anchor to be used at the end of the polyline connection. Removes this listener 
290
 * Sets the anchor to be used at the end of the polyline connection. Removes this listener 
282
 * from the old anchor and adds it to the new anchor.
291
 * from the old anchor and adds it to the new anchor.
283
 * @param anchor the new target anchor
292
 * @param anchor the new target anchor
(-)src/org/eclipse/draw2d/PolylineLabelLocator.java (+50 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2005 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
 *	   Manuel Selva - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d;
12
13
import org.eclipse.draw2d.geometry.PointList;
14
15
/**
16
 * Locator used to place a {@link PolylineLabelDecoration} on a
17
 * {@link Connection}.
18
 * 
19
 * @since 3.5
20
 */
21
public class PolylineLabelLocator implements Locator {
22
23
	private Connection connection;
24
25
	/**
26
	 * Constructs a LabelLocator associated with passed connection.
27
	 * 
28
	 * @param connection
29
	 *            The connection associated with the locator
30
	 */
31
	public PolylineLabelLocator(Connection connection) {
32
		this.connection = connection;
33
	}
34
35
	/**
36
	 * Relocates the passed in figure (which must be a
37
	 * {@link PolylineLabelDecoration}) at the start of the connection.
38
	 * 
39
	 * @param target
40
	 *            The PolylineLabelDecoration to relocate
41
	 */
42
	public void relocate(IFigure target) {
43
44
		PointList points = connection.getPoints();
45
		PolylineLabelDecoration dec = (PolylineLabelDecoration) target;
46
47
		dec.setLocation(points.getPoint(1));
48
		dec.setReferencePoint(points.getPoint(0));
49
	}
50
}
(-)src/org/eclipse/draw2d/PolylineLabelDecoration.java (+160 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2005 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
 *	   Manuel Selva - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d;
12
13
import org.eclipse.swt.graphics.Image;
14
15
import org.eclipse.draw2d.geometry.Dimension;
16
import org.eclipse.draw2d.geometry.Point;
17
import org.eclipse.draw2d.geometry.Rectangle;
18
19
/**
20
 * A decorative label Figure intended to be placed on a {@link PolylineConnection}.
21
 * 
22
 * @since 3.5
23
 */
24
public class PolylineLabelDecoration extends Label implements
25
		RotatableDecoration {
26
27
	private double angle;
28
	private Point location = new Point();
29
	private Point refPoint = new Point();
30
31
	public PolylineLabelDecoration() {
32
		super();
33
	}
34
	
35
	public PolylineLabelDecoration(Image i) {
36
		super(i);
37
	}
38
39
	public PolylineLabelDecoration(String s) {
40
		super(s);
41
	}
42
43
	public PolylineLabelDecoration(String s, Image i) {
44
		super(s, i);
45
	}
46
47
	public Rectangle getBounds() {
48
		if (bounds == null) {
49
			int xDiff = refPoint.x - location.x;
50
			int yDiff = refPoint.y - location.y;
51
			int x, y, width, height;
52
			if (xDiff >= 0) {
53
				width = xDiff;
54
				x = location.x;
55
			} else {
56
				width = -xDiff;
57
				x = refPoint.x;
58
			}
59
			if (yDiff >= 0) {
60
				height = yDiff;
61
				y = location.y;
62
			} else {
63
				height = -yDiff;
64
				y = refPoint.y;
65
			}
66
			bounds = new Rectangle(x, y, width, height);
67
		}
68
		return bounds;
69
	}
70
71
	public void setLocation(Point p) {
72
		bounds = null;
73
		location.setLocation(p);
74
	}
75
76
	public void setReferencePoint(Point ref) {
77
		Point pt = Point.SINGLETON;
78
		refPoint.setLocation(ref);
79
		pt.setLocation(ref);
80
		pt.negate().translate(location);
81
		setRotation(Math.atan2(pt.y, pt.x));
82
	}
83
84
	private void setRotation(double angle) {
85
		bounds = null;
86
		this.angle = Math.toDegrees(angle);
87
	}
88
89
	protected Point getIconLocation() {
90
		double linkLength = Math.sqrt(bounds.width * bounds.width
91
				+ bounds.height * bounds.height);
92
		int y = 0;//-getIconSize().height;
93
		int x;
94
		switch (getTextAlignment()) {
95
		case LEFT:
96
			x = 0;
97
			break;
98
		case RIGHT:
99
			x = (int) (linkLength - getIconSize().width);
100
			break;
101
		default:
102
			x = (int) (linkLength / 2 - getIconSize().width / 2);
103
		}
104
		if (x < 0) {
105
			x = 0;
106
		}
107
		return new Point(x, y);
108
	}
109
	
110
	protected Point getTextLocation() {
111
		Dimension textDimension = getTextSize();
112
		double linkLength = Math.sqrt((bounds.width + getIconSize().width) * 2
113
				+ (bounds.height + getIconSize().height) * 2);
114
		int y = -textDimension.height;
115
		int x;
116
		switch (getTextAlignment()) {
117
		case LEFT:
118
			x = 0;
119
			break;
120
		case RIGHT:
121
			x = (int) (linkLength - textDimension.width);
122
			break;
123
		default:
124
			x = (int) (linkLength / 2 - textDimension.width / 2);
125
		}
126
		if (x <  getIconSize().width + 5) {
127
			x =  getIconSize().width + 5;
128
		}
129
		return new Point(x, y);
130
	}
131
132
	protected void paintFigure(Graphics graphics) {
133
134
		// Draws the text
135
		graphics.pushState();
136
		graphics.setClip(graphics.getClip(new Rectangle()).expand(50, 50));
137
		graphics.translate(bounds.x, bounds.y);
138
		if (angle < 0) {
139
			graphics.translate(0, getBounds().height);
140
		}
141
		graphics.rotate((float) angle);
142
143
		if (isOpaque())
144
			graphics.fillRectangle(getBounds());
145
		if (getBorder() instanceof AbstractBackground)
146
			((AbstractBackground) getBorder()).paintBackground(this, graphics,
147
					NO_INSETS);
148
		if (getIcon() != null)
149
			graphics.drawImage(getIcon(), getIconLocation());
150
		if (!isEnabled()) {
151
			graphics.translate(1, 1);
152
			graphics.setForegroundColor(ColorConstants.buttonLightest);
153
			graphics.drawText(getSubStringText(), getTextLocation());
154
			graphics.translate(-1, -1);
155
			graphics.setForegroundColor(ColorConstants.buttonDarker);
156
		}
157
		graphics.drawText(getSubStringText(), getTextLocation());
158
		graphics.popState();
159
	}
160
}

Return to bug 281246