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