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