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 |
} |