Fix static analysis warnings for UML2SD
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / core / AsyncMessage.java
CommitLineData
73005152 1/**********************************************************************
df0b8ff4 2 * Copyright (c) 2005, 2008 IBM Corporation and others.
73005152
BH
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
73005152
BH
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12package org.eclipse.linuxtools.tmf.ui.views.uml2sd.core;
13
14import java.util.Comparator;
15
4df4581d 16import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b 17import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
73005152 18import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
df0b8ff4 19import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
73005152
BH
20import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncForBackward;
21import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncMessageComparator;
22
23/**
24 * A AsyncMessage is a asynchronous message which appear at two different event occurrences on each lifeline ends (sender
25 * and receiver).<br>
26 * <br>
27 * <br>
28 * Usage example:
29 *
30 * <pre>
31 * Frame frame;
32 * Lifeline lifeLine1;
33 * Lifeline lifeLine2;
34 *
35 * AsyncMessage message = new AsyncMessage();
36 * // Create a new event occurrence on each lifeline
37 * lifeline1.getNewOccurrenceIndex();
38 * lifeline2.getNewOccurrenceIndex();
39 * // Set the message sender and receiver
40 * message.setStartLifeline(lifeLine1);
41 * message.setEndLifline(lifeline2);
42 * message.setName(&quot;Message label&quot;);
43 * // add the message to the frame
44 * frame.addMessage(message);
45 * </pre>
46 *
47 * @see Lifeline Lifeline for more event occurence details
df0b8ff4 48 * @version 1.0
73005152
BH
49 * @author sveyrier
50 *
51 */
52public class AsyncMessage extends BaseMessage implements ITimeRange {
53
df0b8ff4
BH
54 // ------------------------------------------------------------------------
55 // Constants
56 // ------------------------------------------------------------------------
57 /**
58 * The grahNode ID constant
59 */
60 public static final String ASYNC_MESS_TAG = "AsyncMessage"; //$NON-NLS-1$
61
62 // ------------------------------------------------------------------------
63 // Attributes
64 // ------------------------------------------------------------------------
65 /**
66 * Flag whether message has time information or not.
67 */
eb63f5ff 68 protected boolean fHasTime = false;
73005152
BH
69 /**
70 * The time when the message begin
71 */
eb63f5ff 72 protected ITmfTimestamp fEndTime = new TmfTimestamp();
73005152
BH
73 /**
74 * The time when the message end
75 */
eb63f5ff 76 protected ITmfTimestamp fStartTime = new TmfTimestamp();
73005152
BH
77 /**
78 * The associated message.
79 */
eb63f5ff 80 protected AsyncMessageReturn fMessageReturn = null;
73005152 81
df0b8ff4
BH
82 // ------------------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------------------
85 /**
86 * Default constructor
87 */
73005152 88 public AsyncMessage() {
eb63f5ff 89 fPrefId = ISDPreferences.PREF_ASYNC_MESS;
73005152
BH
90 }
91
df0b8ff4
BH
92 // ------------------------------------------------------------------------
93 // Methods
94 // ------------------------------------------------------------------------
95 /*
96 * (non-Javadoc)
97 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getX()
98 */
73005152
BH
99 @Override
100 public int getX() {
101 int x = super.getX(true);
102 int activationWidth = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
eb63f5ff 103 if ((fStartLifeline != null) && (fEndLifeline != null) && (fStartLifeline.getX() > fEndLifeline.getX())) {
73005152
BH
104 activationWidth = -activationWidth;
105 }
106
eb63f5ff 107 if (isMessageStartInActivation(fStartEventOccurrence)) {
73005152
BH
108 x = x + activationWidth;
109 }
110 return x;
111 }
df0b8ff4
BH
112
113 /*
114 * (non-Javadoc)
115 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getY()
116 */
73005152
BH
117 @Override
118 public int getY() {
eb63f5ff
BH
119 if ((fStartLifeline != null) && (fEndLifeline != null)) {
120 return fEndLifeline.getY() + fEndLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * fStartEventOccurrence;
73005152
BH
121 }
122 return super.getY();
123 }
124
df0b8ff4
BH
125 /*
126 * (non-Javadoc)
127 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getWidth()
128 */
73005152
BH
129 @Override
130 public int getWidth() {
131 int width = super.getWidth(true);
132 int activationWidth = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
eb63f5ff 133 if ((fStartLifeline != null) && (fEndLifeline != null) && (fStartLifeline.getX() > fEndLifeline.getX())) {
73005152
BH
134 activationWidth = -activationWidth;
135 }
136
eb63f5ff 137 if (isMessageStartInActivation(fStartEventOccurrence)) {
73005152 138 width = width - activationWidth;
df0b8ff4 139 }
73005152 140
eb63f5ff 141 if (isMessageEndInActivation(fEndEventOccurrence)) {
73005152 142 width = width - activationWidth;
df0b8ff4 143 }
73005152
BH
144
145 return width;
146 }
147
df0b8ff4
BH
148 /*
149 * (non-Javadoc)
150 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getHeight()
151 */
73005152
BH
152 @Override
153 public int getHeight() {
eb63f5ff
BH
154 if ((fStartLifeline != null) && (fEndLifeline != null)) {
155 return (fEndLifeline.getY() + fEndLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * fEndEventOccurrence) - getY();
73005152
BH
156 }
157 return super.getHeight();
158 }
159
160 /**
161 * Set the message return associated with this message.
162 *
163 * @param message the message return to associate
164 */
165 protected void setMessageReturn(AsyncMessageReturn message) {
eb63f5ff 166 fMessageReturn = message;
73005152
BH
167 }
168
169 /**
170 * Set the event occurrence attached to this message for its end lifeline
171 *
172 * @param occurrence the event occurrence to set
173 */
174 public void setEndOccurrence(int occurrence) {
eb63f5ff 175 fEndEventOccurrence = occurrence;
df0b8ff4 176 if (getStartLifeline() == null) {
eb63f5ff 177 fStartEventOccurrence = occurrence;
df0b8ff4 178 }
73005152
BH
179 informFrame(getEndLifeline(), occurrence);
180 }
181
df0b8ff4
BH
182 /**
183 * Informs the given lifeline about the maximum occurrence if applicable.
184 *
185 * @param lifeLine
186 * @param occurrence
187 */
73005152 188 protected void informFrame(Lifeline lifeLine, int occurrence) {
df0b8ff4
BH
189 if ((lifeLine != null) && (lifeLine.getFrame() != null) && (lifeLine.getFrame().getMaxEventOccurrence() < occurrence)) {
190 lifeLine.getFrame().setMaxEventOccurrence(occurrence);
191 }
73005152
BH
192 }
193
194 /**
195 * Set the event occurrence attached to this message for its start lifeline
196 *
197 * @param occurrence the event occurrence to set
198 */
199 public void setStartOccurrence(int occurrence) {
eb63f5ff 200 fStartEventOccurrence = occurrence;
df0b8ff4 201 if (getEndLifeline() == null) {
eb63f5ff 202 fEndEventOccurrence = fStartEventOccurrence;
df0b8ff4 203 }
73005152
BH
204 informFrame(getStartLifeline(), occurrence);
205 }
206
207 /**
208 * Set the lifeLine which has sent the message.<br>
209 * A new EventOccurence will be create on this lifeLine.<br>
210 *
211 * @param lifeline the message sender
73005152
BH
212 */
213 public void autoSetStartLifeline(Lifeline lifeline) {
214 lifeline.getNewEventOccurrence();
215 setStartLifeline(lifeline);
216 }
217
218 /**
219 * Set the lifeLine which has received the message.<br>
220 * A new EventOccurence will be create on this lifeLine.<br>
221 *
222 * @param lifeline the message receiver
73005152
BH
223 */
224 public void autoSetEndLifeline(Lifeline lifeline) {
225 lifeline.getNewEventOccurrence();
226 setEndLifeline(lifeline);
227 }
228
df0b8ff4
BH
229 /*
230 * (non-Javadoc)
231 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#setStartLifeline(org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline)
73005152
BH
232 */
233 @Override
234 public void setStartLifeline(Lifeline lifeline) {
235 super.setStartLifeline(lifeline);
236 setStartOccurrence(getStartLifeline().getEventOccurrence());
df0b8ff4 237 if (getEndLifeline() == null) {
eb63f5ff 238 fEndEventOccurrence = fStartEventOccurrence;
df0b8ff4 239 }
73005152
BH
240 }
241
df0b8ff4
BH
242 /*
243 * (non-Javadoc)
244 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#setEndLifeline(org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline)
73005152
BH
245 */
246 @Override
247 public void setEndLifeline(Lifeline lifeline) {
248 super.setEndLifeline(lifeline);
249 setEventOccurrence(getEndLifeline().getEventOccurrence());
250 }
251
252 /**
253 * Returns true if the point C is on the segment defined with the point A and B
254 *
255 * @param xA point A x coordinate
256 * @param yA point A y coordinate
257 * @param xB point B x coordinate
258 * @param yB point B y coordinate
259 * @param xC point C x coordinate
260 * @param yC point C y coordinate
261 * @return Return true if the point C is on the segment defined with the point A and B, else otherwise
262 */
263 protected boolean isNearSegment(int xA, int yA, int xB, int yB, int xC, int yC) {
df0b8ff4 264 if ((xA > xB) && (xC > xA)) {
73005152 265 return false;
df0b8ff4
BH
266 }
267 if ((xA < xB) && (xC > xB)) {
73005152 268 return false;
df0b8ff4
BH
269 }
270 if ((xA < xB) && (xC < xA)) {
73005152 271 return false;
df0b8ff4
BH
272 }
273 if ((xA > xB) && (xC < xB)) {
73005152 274 return false;
df0b8ff4 275 }
73005152
BH
276 double distAB = Math.sqrt((xB - xA) * (xB - xA) + (yB - yA) * (yB - yA));
277 double scalar = ((xB - xA) * (xC - xA) + (yB - yA) * (yC - yA)) / distAB;
278 double distAC = Math.sqrt((xC - xA) * (xC - xA) + (yC - yA) * (yC - yA));
279 double distToSegment = Math.sqrt(Math.abs(distAC * distAC - scalar * scalar));
df0b8ff4 280 if (distToSegment <= Metrics.MESSAGE_SELECTION_TOLERANCE) {
73005152 281 return true;
df0b8ff4 282 }
73005152
BH
283 return false;
284 }
285
df0b8ff4
BH
286 /*
287 * (non-Javadoc)
288 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#contains(int, int)
289 */
73005152
BH
290 @Override
291 public boolean contains(int x, int y) {
292 // Is it a self message?
eb63f5ff 293 if (fStartLifeline == fEndLifeline) {
73005152
BH
294 return super.contains(x, y);
295 }
296 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x, y))
297 return true;
298 int messageMaxWidth = Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH;
299 int nameWidth = getName().length() * Metrics.getAverageCharWidth();
300 if (getName().length() * Metrics.getAverageCharWidth() > messageMaxWidth) {
df0b8ff4 301 if (Frame.contains(getX(), getY() - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), messageMaxWidth, Metrics.getMessageFontHeigth(), x, y)) {
73005152 302 return true;
df0b8ff4 303 }
73005152 304 } else {
df0b8ff4 305 if (Frame.contains(getX() + (messageMaxWidth - nameWidth) / 2, getY() + getHeight() / 2 - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), nameWidth, Metrics.getMessageFontHeigth(), x, y)) {
73005152 306 return true;
df0b8ff4 307 }
73005152
BH
308 }
309 return false;
310 }
311
df0b8ff4
BH
312 /**
313 * Draws the asynchronous message using giving graphical context.
314 *
315 * @param context A graphical context to draw in.
316 */
73005152 317 protected void drawAsyncMessage(IGC context) {
eb63f5ff 318 if (fStartLifeline != null && fEndLifeline != null && fStartLifeline == fEndLifeline && (fStartEventOccurrence != fEndEventOccurrence)) {
73005152
BH
319 int x = getX();
320 int y = getY();
321 int height = getHeight();
322 int tempx = 0;
eb63f5ff
BH
323 boolean startInActivation = isMessageStartInActivation(fStartEventOccurrence);
324 boolean endInActivation = isMessageEndInActivation(fEndEventOccurrence);
73005152 325
df0b8ff4 326 if (endInActivation && !startInActivation) {
73005152 327 tempx = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
df0b8ff4
BH
328 }
329 if (startInActivation && !endInActivation) {
73005152 330 tempx = -Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
df0b8ff4 331 }
73005152
BH
332
333 int tempy = Metrics.INTERNAL_MESSAGE_WIDTH / 2;
df0b8ff4 334 if (getHeight() <= Metrics.INTERNAL_MESSAGE_WIDTH) {
73005152 335 tempy = getHeight() / 2;
df0b8ff4 336 }
73005152
BH
337
338 context.drawLine(x, y, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y);
339 context.drawLine(x + Metrics.INTERNAL_MESSAGE_WIDTH, y + tempy, x + Metrics.INTERNAL_MESSAGE_WIDTH, y + height - tempy);
340 context.drawLine(x + tempx, y + height, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y + height);
341
eb63f5ff
BH
342 Double xt = Double.valueOf(Math.cos(0.75) * 7);
343 Double yt = Double.valueOf(Math.sin(0.75) * 7);
73005152
BH
344
345 context.drawLine(x + xt.intValue() + tempx, y + height + yt.intValue(), x + tempx, y + height);
346 context.drawArc(x, y, Metrics.INTERNAL_MESSAGE_WIDTH, 2 * tempy, 0, 90);
347 context.drawArc(x, y + height, Metrics.INTERNAL_MESSAGE_WIDTH, -2 * tempy, 0, -90);
348 context.drawLine(x + xt.intValue() + tempx, y + height - yt.intValue(), x + tempx, y + height);
349
350 context.drawTextTruncated(getName(), x + Metrics.INTERNAL_MESSAGE_WIDTH + Metrics.INTERNAL_MESSAGE_V_MARGIN, y, Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH + -Metrics.INTERNAL_MESSAGE_WIDTH,
351 +Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), !isSelected());
df0b8ff4 352 } else {
73005152 353 super.draw(context);
df0b8ff4 354 }
73005152
BH
355 }
356
df0b8ff4
BH
357 /*
358 * (non-Javadoc)
359 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#draw(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC)
73005152
BH
360 */
361 @Override
362 public void draw(IGC context) {
df0b8ff4 363 if (!isVisible()) {
73005152 364 return;
df0b8ff4 365 }
73005152 366 // Draw it selected?
eb63f5ff 367 if (isSelected() && (fStartLifeline != null && fEndLifeline != null && fStartLifeline == fEndLifeline && (fStartEventOccurrence != fEndEventOccurrence))) {
73005152
BH
368 /*
369 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
370 * colors This create the highlight effect
371 */
372 context.setForeground(Frame.getUserPref().getBackGroundColorSelection());
373 context.setLineWidth(Metrics.SELECTION_LINE_WIDTH);
374 drawAsyncMessage(context);
375 context.setBackground(Frame.getUserPref().getBackGroundColorSelection());
376 context.setForeground(Frame.getUserPref().getForeGroundColorSelection());
377 // Second drawing is done after the else
378 } else {
eb63f5ff
BH
379 context.setBackground(Frame.getUserPref().getBackGroundColor(fPrefId));
380 context.setForeground(Frame.getUserPref().getForeGroundColor(fPrefId));
73005152
BH
381 }
382 if (hasFocus()) {
383 context.setDrawTextWithFocusStyle(true);
384 }
385 context.setLineWidth(Metrics.NORMAL_LINE_WIDTH);
386 drawAsyncMessage(context);
387 if (hasFocus()) {
388 context.setDrawTextWithFocusStyle(false);
389 }
390 }
391
392 /**
393 * Set the time when the message end
394 *
395 * @param time the time when the message end
396 */
4df4581d 397 public void setEndTime(ITmfTimestamp time) {
eb63f5ff
BH
398 fEndTime = time.clone();
399 fHasTime = true;
df0b8ff4 400 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
73005152 401 getStartLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 402 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
73005152 403 getEndLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 404 }
73005152
BH
405 }
406
407 /**
408 * Set the time when the message start
409 *
410 * @param time the time when the message start
411 */
4df4581d 412 public void setStartTime(ITmfTimestamp time) {
eb63f5ff
BH
413 fStartTime = time.clone();
414 fHasTime = true;
df0b8ff4 415 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
73005152 416 getStartLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 417 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
73005152 418 getEndLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 419 }
73005152
BH
420 }
421
df0b8ff4
BH
422 /*
423 * (non-Javadoc)
424 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#getEndTime()
73005152
BH
425 */
426 @Override
4df4581d 427 public ITmfTimestamp getEndTime() {
eb63f5ff 428 return fEndTime;
73005152
BH
429 }
430
df0b8ff4
BH
431 /*
432 * (non-Javadoc)
433 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#getStartTime()
73005152
BH
434 */
435 @Override
4df4581d 436 public ITmfTimestamp getStartTime() {
eb63f5ff 437 return fStartTime;
73005152
BH
438 }
439
df0b8ff4
BH
440 /*
441 * (non-Javadoc)
442 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ITimeRange#hasTimeInfo()
443 */
73005152
BH
444 @Override
445 public boolean hasTimeInfo() {
eb63f5ff 446 return fHasTime;
73005152
BH
447 }
448
df0b8ff4
BH
449 /*
450 * (non-Javadoc)
451 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#isVisible(int, int, int, int)
452 */
73005152
BH
453 @Override
454 public boolean isVisible(int x, int y, int width, int height) {
455 int toDrawY = getY();
456 int toDrawHeight = getHeight();
df0b8ff4 457 if ((toDrawY > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) && (toDrawY + toDrawHeight > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth())) {
73005152 458 return false;
df0b8ff4
BH
459 }
460 if (toDrawY < y && (toDrawY + toDrawHeight < y)) {
73005152 461 return false;
df0b8ff4 462 }
73005152
BH
463 return super.isVisible(x, y, width, height);
464 }
465
df0b8ff4
BH
466 /*
467 * (non-Javadoc)
468 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getComparator()
469 */
73005152
BH
470 @Override
471 public Comparator<GraphNode> getComparator() {
472 return new SortAsyncMessageComparator();
473 }
474
df0b8ff4
BH
475 /*
476 * (non-Javadoc)
477 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getArrayId()
478 */
73005152
BH
479 @Override
480 public String getArrayId() {
481 return ASYNC_MESS_TAG;
482 }
483
df0b8ff4
BH
484 /*
485 * (non-Javadoc)
486 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getBackComparator()
487 */
73005152
BH
488 @Override
489 public Comparator<GraphNode> getBackComparator() {
490 return new SortAsyncForBackward();
491 }
492
df0b8ff4
BH
493 /*
494 * (non-Javadoc)
495 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#positiveDistanceToPoint(int, int)
496 */
73005152
BH
497 @Override
498 public boolean positiveDistanceToPoint(int x, int y) {
499 int mY = getY();
500 int mH = getHeight();
df0b8ff4 501 if ((mY > y) || (mY + mH > y)) {
73005152 502 return true;
df0b8ff4 503 }
73005152
BH
504 return false;
505 }
506}
This page took 0.050846 seconds and 5 git commands to generate.