tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / core / AsyncMessage.java
CommitLineData
73005152 1/**********************************************************************
c8422608 2 * Copyright (c) 2005, 2013 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
73005152
BH
13package org.eclipse.linuxtools.tmf.ui.views.uml2sd.core;
14
15import java.util.Comparator;
16
3bd46eef
AM
17import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
18import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
73005152 19import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
df0b8ff4 20import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
3145ec83 21import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref;
73005152
BH
22import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncForBackward;
23import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncMessageComparator;
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 */
eb63f5ff 70 protected boolean fHasTime = false;
73005152
BH
71 /**
72 * The time when the message begin
73 */
eb63f5ff 74 protected ITmfTimestamp fEndTime = new TmfTimestamp();
73005152
BH
75 /**
76 * The time when the message end
77 */
eb63f5ff 78 protected 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() {
eb63f5ff 91 fPrefId = 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;
eb63f5ff 102 if ((fStartLifeline != null) && (fEndLifeline != null) && (fStartLifeline.getX() > fEndLifeline.getX())) {
73005152
BH
103 activationWidth = -activationWidth;
104 }
105
eb63f5ff 106 if (isMessageStartInActivation(fStartEventOccurrence)) {
73005152
BH
107 x = x + activationWidth;
108 }
109 return x;
110 }
013a5f1c 111
73005152
BH
112 @Override
113 public int getY() {
eb63f5ff
BH
114 if ((fStartLifeline != null) && (fEndLifeline != null)) {
115 return fEndLifeline.getY() + fEndLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * fStartEventOccurrence;
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;
eb63f5ff 124 if ((fStartLifeline != null) && (fEndLifeline != null) && (fStartLifeline.getX() > fEndLifeline.getX())) {
73005152
BH
125 activationWidth = -activationWidth;
126 }
127
eb63f5ff 128 if (isMessageStartInActivation(fStartEventOccurrence)) {
73005152 129 width = width - activationWidth;
df0b8ff4 130 }
73005152 131
eb63f5ff 132 if (isMessageEndInActivation(fEndEventOccurrence)) {
73005152 133 width = width - activationWidth;
df0b8ff4 134 }
73005152
BH
135
136 return width;
137 }
138
139 @Override
140 public int getHeight() {
eb63f5ff
BH
141 if ((fStartLifeline != null) && (fEndLifeline != null)) {
142 return (fEndLifeline.getY() + fEndLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * fEndEventOccurrence) - 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 */
161 public void setEndOccurrence(int occurrence) {
eb63f5ff 162 fEndEventOccurrence = occurrence;
df0b8ff4 163 if (getStartLifeline() == null) {
eb63f5ff 164 fStartEventOccurrence = occurrence;
df0b8ff4 165 }
73005152
BH
166 informFrame(getEndLifeline(), occurrence);
167 }
168
df0b8ff4
BH
169 /**
170 * Informs the given lifeline about the maximum occurrence if applicable.
013a5f1c 171 *
df0b8ff4 172 * @param lifeLine
a0a88f65 173 * Concerned lifeline
df0b8ff4 174 * @param occurrence
a0a88f65 175 * Occurrence number
df0b8ff4 176 */
73005152 177 protected void informFrame(Lifeline lifeLine, int occurrence) {
df0b8ff4
BH
178 if ((lifeLine != null) && (lifeLine.getFrame() != null) && (lifeLine.getFrame().getMaxEventOccurrence() < occurrence)) {
179 lifeLine.getFrame().setMaxEventOccurrence(occurrence);
180 }
73005152
BH
181 }
182
183 /**
184 * Set the event occurrence attached to this message for its start lifeline
013a5f1c 185 *
73005152
BH
186 * @param occurrence the event occurrence to set
187 */
188 public void setStartOccurrence(int occurrence) {
eb63f5ff 189 fStartEventOccurrence = occurrence;
df0b8ff4 190 if (getEndLifeline() == null) {
eb63f5ff 191 fEndEventOccurrence = fStartEventOccurrence;
df0b8ff4 192 }
73005152
BH
193 informFrame(getStartLifeline(), occurrence);
194 }
195
196 /**
197 * Set the lifeLine which has sent the message.<br>
198 * A new EventOccurence will be create on this lifeLine.<br>
013a5f1c 199 *
73005152 200 * @param lifeline the message sender
73005152
BH
201 */
202 public void autoSetStartLifeline(Lifeline lifeline) {
203 lifeline.getNewEventOccurrence();
204 setStartLifeline(lifeline);
205 }
206
207 /**
208 * Set the lifeLine which has received the message.<br>
209 * A new EventOccurence will be create on this lifeLine.<br>
013a5f1c 210 *
73005152 211 * @param lifeline the message receiver
73005152
BH
212 */
213 public void autoSetEndLifeline(Lifeline lifeline) {
214 lifeline.getNewEventOccurrence();
215 setEndLifeline(lifeline);
216 }
217
73005152
BH
218 @Override
219 public void setStartLifeline(Lifeline lifeline) {
220 super.setStartLifeline(lifeline);
221 setStartOccurrence(getStartLifeline().getEventOccurrence());
df0b8ff4 222 if (getEndLifeline() == null) {
eb63f5ff 223 fEndEventOccurrence = fStartEventOccurrence;
df0b8ff4 224 }
73005152
BH
225 }
226
73005152
BH
227 @Override
228 public void setEndLifeline(Lifeline lifeline) {
229 super.setEndLifeline(lifeline);
230 setEventOccurrence(getEndLifeline().getEventOccurrence());
231 }
232
233 /**
234 * Returns true if the point C is on the segment defined with the point A and B
013a5f1c 235 *
73005152
BH
236 * @param xA point A x coordinate
237 * @param yA point A y coordinate
238 * @param xB point B x coordinate
239 * @param yB point B y coordinate
240 * @param xC point C x coordinate
241 * @param yC point C y coordinate
242 * @return Return true if the point C is on the segment defined with the point A and B, else otherwise
243 */
244 protected boolean isNearSegment(int xA, int yA, int xB, int yB, int xC, int yC) {
df0b8ff4 245 if ((xA > xB) && (xC > xA)) {
73005152 246 return false;
df0b8ff4
BH
247 }
248 if ((xA < xB) && (xC > xB)) {
73005152 249 return false;
df0b8ff4
BH
250 }
251 if ((xA < xB) && (xC < xA)) {
73005152 252 return false;
df0b8ff4
BH
253 }
254 if ((xA > xB) && (xC < xB)) {
73005152 255 return false;
df0b8ff4 256 }
73005152
BH
257 double distAB = Math.sqrt((xB - xA) * (xB - xA) + (yB - yA) * (yB - yA));
258 double scalar = ((xB - xA) * (xC - xA) + (yB - yA) * (yC - yA)) / distAB;
259 double distAC = Math.sqrt((xC - xA) * (xC - xA) + (yC - yA) * (yC - yA));
260 double distToSegment = Math.sqrt(Math.abs(distAC * distAC - scalar * scalar));
df0b8ff4 261 if (distToSegment <= Metrics.MESSAGE_SELECTION_TOLERANCE) {
73005152 262 return true;
df0b8ff4 263 }
73005152
BH
264 return false;
265 }
266
267 @Override
268 public boolean contains(int x, int y) {
269 // Is it a self message?
eb63f5ff 270 if (fStartLifeline == fEndLifeline) {
73005152
BH
271 return super.contains(x, y);
272 }
013a5f1c 273 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x, y)) {
73005152 274 return true;
013a5f1c 275 }
73005152
BH
276 int messageMaxWidth = Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH;
277 int nameWidth = getName().length() * Metrics.getAverageCharWidth();
278 if (getName().length() * Metrics.getAverageCharWidth() > messageMaxWidth) {
abbdd66a 279 if (GraphNode.contains(getX(), getY() - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), messageMaxWidth, Metrics.getMessageFontHeigth(), x, y)) {
73005152 280 return true;
df0b8ff4 281 }
73005152 282 } else {
abbdd66a 283 if (GraphNode.contains(getX() + (messageMaxWidth - nameWidth) / 2, getY() + getHeight() / 2 - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), nameWidth, Metrics.getMessageFontHeigth(), x, y)) {
73005152 284 return true;
df0b8ff4 285 }
73005152
BH
286 }
287 return false;
288 }
289
df0b8ff4
BH
290 /**
291 * Draws the asynchronous message using giving graphical context.
013a5f1c 292 *
df0b8ff4
BH
293 * @param context A graphical context to draw in.
294 */
73005152 295 protected void drawAsyncMessage(IGC context) {
eb63f5ff 296 if (fStartLifeline != null && fEndLifeline != null && fStartLifeline == fEndLifeline && (fStartEventOccurrence != fEndEventOccurrence)) {
73005152
BH
297 int x = getX();
298 int y = getY();
299 int height = getHeight();
300 int tempx = 0;
eb63f5ff
BH
301 boolean startInActivation = isMessageStartInActivation(fStartEventOccurrence);
302 boolean endInActivation = isMessageEndInActivation(fEndEventOccurrence);
73005152 303
df0b8ff4 304 if (endInActivation && !startInActivation) {
73005152 305 tempx = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
df0b8ff4
BH
306 }
307 if (startInActivation && !endInActivation) {
73005152 308 tempx = -Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
df0b8ff4 309 }
73005152
BH
310
311 int tempy = Metrics.INTERNAL_MESSAGE_WIDTH / 2;
df0b8ff4 312 if (getHeight() <= Metrics.INTERNAL_MESSAGE_WIDTH) {
73005152 313 tempy = getHeight() / 2;
df0b8ff4 314 }
73005152
BH
315
316 context.drawLine(x, y, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y);
317 context.drawLine(x + Metrics.INTERNAL_MESSAGE_WIDTH, y + tempy, x + Metrics.INTERNAL_MESSAGE_WIDTH, y + height - tempy);
318 context.drawLine(x + tempx, y + height, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y + height);
319
eb63f5ff
BH
320 Double xt = Double.valueOf(Math.cos(0.75) * 7);
321 Double yt = Double.valueOf(Math.sin(0.75) * 7);
73005152
BH
322
323 context.drawLine(x + xt.intValue() + tempx, y + height + yt.intValue(), x + tempx, y + height);
324 context.drawArc(x, y, Metrics.INTERNAL_MESSAGE_WIDTH, 2 * tempy, 0, 90);
325 context.drawArc(x, y + height, Metrics.INTERNAL_MESSAGE_WIDTH, -2 * tempy, 0, -90);
326 context.drawLine(x + xt.intValue() + tempx, y + height - yt.intValue(), x + tempx, y + height);
327
328 context.drawTextTruncated(getName(), x + Metrics.INTERNAL_MESSAGE_WIDTH + Metrics.INTERNAL_MESSAGE_V_MARGIN, y, Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH + -Metrics.INTERNAL_MESSAGE_WIDTH,
329 +Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), !isSelected());
df0b8ff4 330 } else {
73005152 331 super.draw(context);
df0b8ff4 332 }
73005152
BH
333 }
334
73005152
BH
335 @Override
336 public void draw(IGC context) {
df0b8ff4 337 if (!isVisible()) {
73005152 338 return;
df0b8ff4 339 }
3145ec83
BH
340
341 ISDPreferences pref = SDViewPref.getInstance();
342
73005152 343 // Draw it selected?
eb63f5ff 344 if (isSelected() && (fStartLifeline != null && fEndLifeline != null && fStartLifeline == fEndLifeline && (fStartEventOccurrence != fEndEventOccurrence))) {
73005152
BH
345 /*
346 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
347 * colors This create the highlight effect
348 */
3145ec83 349 context.setForeground(pref.getBackGroundColorSelection());
73005152
BH
350 context.setLineWidth(Metrics.SELECTION_LINE_WIDTH);
351 drawAsyncMessage(context);
3145ec83
BH
352 context.setBackground(pref.getBackGroundColorSelection());
353 context.setForeground(pref.getForeGroundColorSelection());
73005152
BH
354 // Second drawing is done after the else
355 } else {
3145ec83
BH
356 context.setBackground(pref.getBackGroundColor(fPrefId));
357 context.setForeground(pref.getForeGroundColor(fPrefId));
73005152
BH
358 }
359 if (hasFocus()) {
360 context.setDrawTextWithFocusStyle(true);
361 }
362 context.setLineWidth(Metrics.NORMAL_LINE_WIDTH);
363 drawAsyncMessage(context);
364 if (hasFocus()) {
365 context.setDrawTextWithFocusStyle(false);
366 }
367 }
368
369 /**
370 * Set the time when the message end
013a5f1c 371 *
73005152 372 * @param time the time when the message end
3bd46eef 373 * @since 2.0
73005152 374 */
4df4581d 375 public void setEndTime(ITmfTimestamp time) {
4593bd5b 376 fEndTime = time;
eb63f5ff 377 fHasTime = true;
df0b8ff4 378 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
73005152 379 getStartLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 380 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
73005152 381 getEndLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 382 }
73005152
BH
383 }
384
385 /**
386 * Set the time when the message start
013a5f1c 387 *
73005152 388 * @param time the time when the message start
3bd46eef 389 * @since 2.0
73005152 390 */
4df4581d 391 public void setStartTime(ITmfTimestamp time) {
4593bd5b 392 fStartTime = time;
eb63f5ff 393 fHasTime = true;
df0b8ff4 394 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
73005152 395 getStartLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 396 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
73005152 397 getEndLifeline().getFrame().setHasTimeInfo(true);
df0b8ff4 398 }
73005152
BH
399 }
400
3bd46eef
AM
401 /**
402 * @since 2.0
73005152
BH
403 */
404 @Override
4df4581d 405 public ITmfTimestamp getEndTime() {
eb63f5ff 406 return fEndTime;
73005152
BH
407 }
408
3bd46eef
AM
409 /**
410 * @since 2.0
73005152
BH
411 */
412 @Override
4df4581d 413 public ITmfTimestamp getStartTime() {
eb63f5ff 414 return fStartTime;
73005152
BH
415 }
416
417 @Override
418 public boolean hasTimeInfo() {
eb63f5ff 419 return fHasTime;
73005152
BH
420 }
421
422 @Override
423 public boolean isVisible(int x, int y, int width, int height) {
424 int toDrawY = getY();
425 int toDrawHeight = getHeight();
df0b8ff4 426 if ((toDrawY > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) && (toDrawY + toDrawHeight > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth())) {
73005152 427 return false;
df0b8ff4
BH
428 }
429 if (toDrawY < y && (toDrawY + toDrawHeight < y)) {
73005152 430 return false;
df0b8ff4 431 }
73005152
BH
432 return super.isVisible(x, y, width, height);
433 }
434
435 @Override
436 public Comparator<GraphNode> getComparator() {
437 return new SortAsyncMessageComparator();
438 }
439
440 @Override
441 public String getArrayId() {
442 return ASYNC_MESS_TAG;
443 }
444
445 @Override
446 public Comparator<GraphNode> getBackComparator() {
447 return new SortAsyncForBackward();
448 }
449
450 @Override
451 public boolean positiveDistanceToPoint(int x, int y) {
452 int mY = getY();
453 int mH = getHeight();
df0b8ff4 454 if ((mY > y) || (mY + mH > y)) {
73005152 455 return true;
df0b8ff4 456 }
73005152
BH
457 return false;
458 }
459}
This page took 0.058796 seconds and 5 git commands to generate.