KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > viewer > MessageGui


1 package org.coach.tracing.server.viewer;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.geom.*;
6
7 public class MessageGui extends JComponent {
8     public final static Integer JavaDoc NW_SE = new Integer JavaDoc(0);
9     public final static Integer JavaDoc NE_SW = new Integer JavaDoc(1);
10     public final static Integer JavaDoc SE_NW = new Integer JavaDoc(2);
11     public final static Integer JavaDoc SW_NE = new Integer JavaDoc(3);
12     public final static Integer JavaDoc UP = new Integer JavaDoc(4);
13     public final static Integer JavaDoc DOWN = new Integer JavaDoc(5);
14     public final static int arrow_size = 6;
15     public final static int delta = 2;
16     //the thickness of the message line when calculating the polygon
17
public final static Color normal_color = Color.blue;
18     public final static Color trail_color = Color.magenta;
19     public final static Color text_color = Color.black;
20     private static Diagram view;
21     private static ScaleManager sm;
22
23     private EventGui start;
24     private EventGui end;
25     private Integer JavaDoc orientation = null;
26     private Point[] arrow_head = new Point[3];
27     //array of 3 points
28
private Polygon poly = null;
29     private Dimension text_dimension = null;
30     private Point text_origin = null;
31     private String JavaDoc text = "";
32     private int identityGuiWidth;
33
34     public MessageGui(Diagram v, EventGui e1, EventGui e2, int identityGuiWidth) {
35         super();
36         if (e1.getPosition() < e2.getPosition()) {
37             start = e1;
38             end = e2;
39         } else {
40             start = e2;
41             end = e1;
42         }
43         this.identityGuiWidth = identityGuiWidth;
44         if (view == null) {
45             view = v;
46             sm = view.getScaleManager();
47         }
48         text = start.getOpName();
49         setLocation(calcOrigin(start.getCenter(), end.getCenter(), start.getRadius()));
50         setSize(calcDimension(start.getCenter(), end.getCenter(), start.getRadius()));
51         orientation = calcOrientation(start.getCenter(), end.getCenter());
52         poly = calcPolygon(start.getCenter(), end.getCenter());
53         calcArrow(arrow_head);
54         calcText();
55         ToolTipManager.sharedInstance().registerComponent(this);
56         setToolTipText(text);
57     }
58
59     public void paint(Graphics g) {
60         //paint the event
61
super.paint(g);
62         float scale = sm.getScale();
63         Graphics2D g2 = (Graphics2D)g;
64         g2.setRenderingHints(sm.getHints());
65
66         //set dashed stroke if the message is a return one
67
if (start.isReturn()) {
68             g2.setStroke(sm.getStrokeDashed());
69         } else {
70             g2.setStroke(sm.getStroke());
71         //draw the lines
72
}
73         int x1 = 0;
74         int y1 = 0;
75         int x2 = 0;
76         int y2 = 0;
77
78         if ((orientation == NW_SE) || (orientation == SE_NW)) {
79             x2 = getWidth() - 1;
80             y2 = getH() - 1;
81         }
82
83         if ((orientation == NE_SW) || (orientation == SW_NE)) {
84             y1 = getH() - 1;
85             x2 = getWidth() - 1;
86         }
87
88         // color the message if the start and end event is also colored
89
if (start.getColor() != null && end.getColor() != null) {
90             g2.setColor(start.getColor());
91         } else {
92             g2.setColor(normal_color);
93         }
94
95         if ((orientation == UP) || (orientation == DOWN)) {
96             //draw the line to self
97
g2.draw(new Line2D.Float(0, 0, (getWidth() - 1) / 2, 0));
98             g2.draw(new Line2D.Float((getWidth() - 1) / 2, 0, (getWidth() - 1) / 2, getH() - 1));
99             g2.draw(new Line2D.Float(0, getH() - 1, (getWidth() - 1) / 2, getH() - 1));
100         } else {
101             //draw the line
102
g2.draw(new Line2D.Float(x1, y1, x2, y2));
103         }
104         g2.setStroke(sm.getStroke());
105
106         //draw the arrow head
107
if (arrow_head[0] != null) {
108             g2.draw(new Line2D.Float(arrow_head[0].x, arrow_head[0].y, arrow_head[1].x, arrow_head[1].y));
109             g2.draw(new Line2D.Float(arrow_head[0].x, arrow_head[0].y, arrow_head[2].x, arrow_head[2].y));
110         }
111         //draw the message text
112
g2.setFont(sm.getFont());
113         g2.setColor(Color.white);
114         String JavaDoc t = text;
115         t = getTrimmedText(text, g2);
116         g2.setColor(text_color);
117         g2.drawString(t, text_origin.x, text_origin.y);
118     }
119
120     private void calcText() {
121         double W = getWidth();
122         double H = getH();
123         text_dimension = sm.getStringDimension(text + "_", this);
124         double x = (W - text_dimension.width) / 2;
125
126         if (orientation == DOWN || orientation == UP) {
127             x = 0;
128         }
129         double y = H / 2 + text_dimension.height / 3;
130
131         //the y is at the base line of the string
132
if (x < 0) {
133             x = 0;
134         }
135         text_origin = new Point((int)x, (int)y);
136     }
137
138     private String JavaDoc getTrimmedText(String JavaDoc s, Graphics2D g) {
139         Dimension td = sm.getStringDimension(s, g);
140         double W = getWidth();
141
142         if (td.width > W && (s.length() > 0)) {
143             while (td.width > W && (s.length() > 0)) {
144                 s = s.substring(0, s.length() - 1);
145                 //
146
td = sm.getStringDimension(s + "____", this);
147             //using '_' character, because something goes wrong with the '.' when measuring !!!
148
}
149             s += "...";
150         }
151         return s;
152     }
153
154     private void calcArrow(Point[] pl) {
155         float scale = sm.getScale();
156         double px, py;
157         //head
158
double x1, y1;
159         //-a
160
double x2, y2;
161         //+a
162
double W = getWidth();
163         double H = getH();
164         double Z = Math.sqrt(W * W + H * H);
165         double sin_a = H / Z;
166         double cos_a = W / Z;
167         double sin_30 = 0.5;
168         double cos_30 = Math.sqrt(3) / 2;
169         //sin(x)cos(y)-cos(x)sin(y)
170
double sin_a_minus_30 = sin_a * cos_30 - cos_a * sin_30;
171         //cos(x)cos(y)+sin(x)sin(y)
172
double cos_a_minus_30 = cos_a * cos_30 + sin_a * sin_30;
173         //sin(x)cos(y)+cos(x)sin(y)
174
double sin_a_plus_30 = sin_a * cos_30 + cos_a * sin_30;
175         //cos(x)cos(y)-sin(x)sin(y)
176
double cos_a_plus_30 = cos_a * cos_30 + sin_a * sin_30;
177         x1 = (arrow_size + 1) * scale * cos_a_minus_30;
178         y1 = (arrow_size + 1) * scale * sin_a_minus_30;
179         x2 = arrow_size * scale * cos_a_plus_30;
180         y2 = arrow_size * scale * sin_a_plus_30;
181
182         if (orientation == NW_SE) {
183             pl[0] = new Point((int)(W - 1), (int)(H - 1));
184             pl[1] = new Point((int)(W - x1 - 1), (int)(H - y1 - 1));
185             pl[2] = new Point((int)(W - x2 - 1), (int)(H - y2 - 1));
186         }
187
188         if (orientation == NE_SW) {
189             pl[0] = new Point(0, (int)(H - 1));
190             pl[1] = new Point((int)(x1), (int)(H - y1 - 1));
191             pl[2] = new Point((int)(x2), (int)(H - y2 - 1));
192         }
193
194         if (orientation == DOWN) {
195             pl[0] = new Point(0, (int)(H - 1));
196             pl[1] = new Point((int)((scale * arrow_size * Math.sqrt(3) / 2)), (int)(H + (scale * arrow_size / 2) - 1));
197             pl[2] = new Point((int)((scale * arrow_size * Math.sqrt(3) / 2)), (int)(H - (scale * arrow_size / 2) - 1));
198         }
199
200         //rare, probably wrong cases, we do not show arrows at all for them
201
if (orientation == UP) {
202             pl[0] = null;
203             pl[1] = null;
204             pl[2] = null;
205         }
206
207         if (orientation == SW_NE) {
208             pl[0] = null;
209             pl[1] = null;
210             pl[2] = null;
211         }
212
213         if (orientation == SE_NW) {
214             pl[0] = null;
215             pl[1] = null;
216             pl[2] = null;
217         }
218     }
219
220     private Point calcOrigin(Point p1, Point p2, int r) {
221         //calculate the origin
222
double W = Math.abs(p2.x - p1.x);
223
224         if (W < 3) {
225             return new Point((int)(Math.min(p1.x, p2.x) + r), (int)Math.min(p1.y, p2.y));
226         } else {
227             double H = Math.abs(p2.y - p1.y);
228             double Z = Math.sqrt(W * W + H * H);
229             double rx = r * W / Z;
230             double ry = r * H / Z;
231             return new Point((int)(Math.min(p1.x, p2.x) + rx + 2), (int)(Math.min(p1.y, p2.y) + ry));
232         }
233     }
234
235     private Dimension calcDimension(Point p1, Point p2, int r) {
236         //calculate the size
237
float scale = sm.getScale();
238         double W = Math.abs(p2.x - p1.x);
239
240         if (W < 3) {
241 // int ww = (int)((view.getIdentityGui(start.getEventKey()).getWidth()));
242

243             int ww = identityGuiWidth;
244             return new Dimension(ww, (int)(Math.abs(p1.y - p2.y) + (scale * arrow_size / 2 + 1)));
245         // return new Dimension((int)Math.abs(p1.y - p2.y), (int)(Math.abs(p1.y - p2.y) + (scale * arrow_size / 2 + 1)));
246
} else {
247             double H = Math.abs(p2.y - p1.y);
248             double Z = Math.sqrt(W * W + H * H);
249             double rx = r * W / Z;
250             double ry = r * H / Z;
251             //adjustment for the arrow head
252
return new Dimension((int)(W - (2 * rx)), (int)((H - 2 * ry) + (scale * arrow_size / 2 + 1)));
253         }
254     }
255
256     private Integer JavaDoc calcOrientation(Point p1, Point p2) {
257
258         //calculate the arrow orientation
259
if (p1.x < p2.x) {
260
261             //W - E
262
if (p1.y < p2.y) {
263                 //N - S
264
return NW_SE;
265             } else {
266                 //S - N
267
return SW_NE;
268             }
269         } else {
270
271             if ((p1.x - p2.x) < 3) //checking for self arrows
272
{
273
274                 //we have an arrow to self
275
if (p1.y < p2.y) {
276                     //UP
277
return DOWN;
278                 } else {
279                     //DOWN
280
return UP;
281                 }
282             }
283             {
284
285                 //E - W
286
if (p1.y < p2.y) {
287                     //N - S
288
return NE_SW;
289                 } else {
290                     //S - N
291
return SE_NW;
292                 }
293             }
294         }
295     }
296
297     private Polygon calcPolygon(Point p1, Point p2) {
298         Polygon p = new Polygon();
299         //draw the lines
300
int x1 = 0;
301         int y1 = 0;
302         int x2 = 0;
303         int y2 = 0;
304
305         if ((orientation == NW_SE) || (orientation == SE_NW)) {
306             x2 = getWidth() - 1;
307             y2 = getH() - 1;
308         }
309
310         if ((orientation == NE_SW) || (orientation == SW_NE)) {
311             y1 = getH() - 1;
312             x2 = getWidth() - 1;
313         }
314
315         if ((orientation == UP) || (orientation == DOWN)) {
316             p.addPoint(0, -delta);
317             p.addPoint((getWidth() - 1) / 2 + delta, -delta);
318             p.addPoint((getWidth() - 1) / 2 + delta, getH() - 1 + delta);
319             p.addPoint(0, getH() - 1 + delta);
320             p.addPoint(0, getH() - 1 - delta);
321             p.addPoint((getWidth() - 1) / 2 - delta, getH() - 1 - delta);
322             p.addPoint((getWidth() - 1) / 2 - delta, +delta);
323             p.addPoint(0, +delta);
324         } else {
325             float scale = sm.getScale();
326             double W = getWidth() - 1;
327             double H = getH() - 1;
328             double Z = Math.sqrt(W * W + H * H);
329             double sin_a = H / Z;
330             double cos_a = W / Z;
331             double dx = (delta * scale) * sin_a;
332             double dy = (delta * scale) * cos_a;
333
334             if ((orientation == NW_SE) || (orientation == SE_NW)) {
335                 p.addPoint((int)(x1 - dx), (int)(y1 + dy));
336                 p.addPoint((int)(x1 + dx), (int)(y1 - dy));
337                 p.addPoint((int)(x2 + dx), (int)(y2 - dy));
338                 p.addPoint((int)(x2 - dx), (int)(y2 + dy));
339             }
340
341             if ((orientation == NE_SW) || (orientation == SW_NE)) {
342                 p.addPoint((int)(x1 - dx), (int)(y1 - dy));
343                 p.addPoint((int)(x1 + dx), (int)(y1 + dy));
344                 p.addPoint((int)(x2 + dx), (int)(y2 + dy));
345                 p.addPoint((int)(x2 - dx), (int)(y2 - dy));
346             }
347         }
348         return p;
349     }
350
351     public int getH() {
352         float scale = sm.getScale();
353         return (int)(getHeight() - (scale * arrow_size / 2));
354     }
355 }
356
Popular Tags