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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.core
;
15 import java
.util
.Comparator
;
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
;
26 * A AsyncMessage is a asynchronous message which appear at two different event occurrences on each lifeline ends (sender
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("Message label");
45 * // add the message to the frame
46 * frame.addMessage(message);
49 * @see Lifeline Lifeline for more event occurence details
54 public class AsyncMessage
extends BaseMessage
implements ITimeRange
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 * The grahNode ID constant
62 public static final String ASYNC_MESS_TAG
= "AsyncMessage"; //$NON-NLS-1$
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
68 * Flag whether message has time information or not.
70 protected boolean fHasTime
= false;
72 * The time when the message begin
74 protected ITmfTimestamp fEndTime
= new TmfTimestamp();
76 * The time when the message end
78 protected ITmfTimestamp fStartTime
= new TmfTimestamp();
80 * The associated message.
82 protected AsyncMessageReturn fMessageReturn
= null;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
90 public AsyncMessage() {
91 fPrefId
= ISDPreferences
.PREF_ASYNC_MESS
;
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#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
;
109 if (isMessageStartInActivation(fStartEventOccurrence
)) {
110 x
= x
+ activationWidth
;
117 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getY()
121 if ((fStartLifeline
!= null) && (fEndLifeline
!= null)) {
122 return fEndLifeline
.getY() + fEndLifeline
.getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * fStartEventOccurrence
;
129 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getWidth()
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
;
139 if (isMessageStartInActivation(fStartEventOccurrence
)) {
140 width
= width
- activationWidth
;
143 if (isMessageEndInActivation(fEndEventOccurrence
)) {
144 width
= width
- activationWidth
;
152 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#getHeight()
155 public int getHeight() {
156 if ((fStartLifeline
!= null) && (fEndLifeline
!= null)) {
157 return (fEndLifeline
.getY() + fEndLifeline
.getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * fEndEventOccurrence
) - getY();
159 return super.getHeight();
163 * Set the message return associated with this message.
165 * @param message the message return to associate
167 protected void setMessageReturn(AsyncMessageReturn message
) {
168 fMessageReturn
= message
;
172 * Set the event occurrence attached to this message for its end lifeline
174 * @param occurrence the event occurrence to set
176 public void setEndOccurrence(int occurrence
) {
177 fEndEventOccurrence
= occurrence
;
178 if (getStartLifeline() == null) {
179 fStartEventOccurrence
= occurrence
;
181 informFrame(getEndLifeline(), occurrence
);
185 * Informs the given lifeline about the maximum occurrence if applicable.
192 protected void informFrame(Lifeline lifeLine
, int occurrence
) {
193 if ((lifeLine
!= null) && (lifeLine
.getFrame() != null) && (lifeLine
.getFrame().getMaxEventOccurrence() < occurrence
)) {
194 lifeLine
.getFrame().setMaxEventOccurrence(occurrence
);
199 * Set the event occurrence attached to this message for its start lifeline
201 * @param occurrence the event occurrence to set
203 public void setStartOccurrence(int occurrence
) {
204 fStartEventOccurrence
= occurrence
;
205 if (getEndLifeline() == null) {
206 fEndEventOccurrence
= fStartEventOccurrence
;
208 informFrame(getStartLifeline(), occurrence
);
212 * Set the lifeLine which has sent the message.<br>
213 * A new EventOccurence will be create on this lifeLine.<br>
215 * @param lifeline the message sender
217 public void autoSetStartLifeline(Lifeline lifeline
) {
218 lifeline
.getNewEventOccurrence();
219 setStartLifeline(lifeline
);
223 * Set the lifeLine which has received the message.<br>
224 * A new EventOccurence will be create on this lifeLine.<br>
226 * @param lifeline the message receiver
228 public void autoSetEndLifeline(Lifeline lifeline
) {
229 lifeline
.getNewEventOccurrence();
230 setEndLifeline(lifeline
);
235 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#setStartLifeline(org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline)
238 public void setStartLifeline(Lifeline lifeline
) {
239 super.setStartLifeline(lifeline
);
240 setStartOccurrence(getStartLifeline().getEventOccurrence());
241 if (getEndLifeline() == null) {
242 fEndEventOccurrence
= fStartEventOccurrence
;
248 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#setEndLifeline(org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline)
251 public void setEndLifeline(Lifeline lifeline
) {
252 super.setEndLifeline(lifeline
);
253 setEventOccurrence(getEndLifeline().getEventOccurrence());
257 * Returns true if the point C is on the segment defined with the point A and B
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
267 protected boolean isNearSegment(int xA
, int yA
, int xB
, int yB
, int xC
, int yC
) {
268 if ((xA
> xB
) && (xC
> xA
)) {
271 if ((xA
< xB
) && (xC
> xB
)) {
274 if ((xA
< xB
) && (xC
< xA
)) {
277 if ((xA
> xB
) && (xC
< xB
)) {
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
) {
292 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#contains(int, int)
295 public boolean contains(int x
, int y
) {
296 // Is it a self message?
297 if (fStartLifeline
== fEndLifeline
) {
298 return super.contains(x
, y
);
300 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x
, y
)) {
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
)) {
310 if (GraphNode
.contains(getX() + (messageMaxWidth
- nameWidth
) / 2, getY() + getHeight() / 2 - Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), nameWidth
, Metrics
.getMessageFontHeigth(), x
, y
)) {
318 * Draws the asynchronous message using giving graphical context.
320 * @param context A graphical context to draw in.
322 protected void drawAsyncMessage(IGC context
) {
323 if (fStartLifeline
!= null && fEndLifeline
!= null && fStartLifeline
== fEndLifeline
&& (fStartEventOccurrence
!= fEndEventOccurrence
)) {
326 int height
= getHeight();
328 boolean startInActivation
= isMessageStartInActivation(fStartEventOccurrence
);
329 boolean endInActivation
= isMessageEndInActivation(fEndEventOccurrence
);
331 if (endInActivation
&& !startInActivation
) {
332 tempx
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
334 if (startInActivation
&& !endInActivation
) {
335 tempx
= -Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
338 int tempy
= Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2;
339 if (getHeight() <= Metrics
.INTERNAL_MESSAGE_WIDTH
) {
340 tempy
= getHeight() / 2;
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
);
347 Double xt
= Double
.valueOf(Math
.cos(0.75) * 7);
348 Double yt
= Double
.valueOf(Math
.sin(0.75) * 7);
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
);
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());
364 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#draw(org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC)
367 public void draw(IGC context
) {
372 ISDPreferences pref
= SDViewPref
.getInstance();
375 if (isSelected() && (fStartLifeline
!= null && fEndLifeline
!= null && fStartLifeline
== fEndLifeline
&& (fStartEventOccurrence
!= fEndEventOccurrence
))) {
377 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
378 * colors This create the highlight effect
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
387 context
.setBackground(pref
.getBackGroundColor(fPrefId
));
388 context
.setForeground(pref
.getForeGroundColor(fPrefId
));
391 context
.setDrawTextWithFocusStyle(true);
393 context
.setLineWidth(Metrics
.NORMAL_LINE_WIDTH
);
394 drawAsyncMessage(context
);
396 context
.setDrawTextWithFocusStyle(false);
401 * Set the time when the message end
403 * @param time the time when the message end
406 public void setEndTime(ITmfTimestamp time
) {
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);
417 * Set the time when the message start
419 * @param time the time when the message start
422 public void setStartTime(ITmfTimestamp time
) {
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);
436 public ITmfTimestamp
getEndTime() {
444 public ITmfTimestamp
getStartTime() {
449 public boolean hasTimeInfo() {
455 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BaseMessage#isVisible(int, int, int, int)
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())) {
464 if (toDrawY
< y
&& (toDrawY
+ toDrawHeight
< y
)) {
467 return super.isVisible(x
, y
, width
, height
);
472 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getComparator()
475 public Comparator
<GraphNode
> getComparator() {
476 return new SortAsyncMessageComparator();
481 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getArrayId()
484 public String
getArrayId() {
485 return ASYNC_MESS_TAG
;
490 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#getBackComparator()
493 public Comparator
<GraphNode
> getBackComparator() {
494 return new SortAsyncForBackward();
499 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#positiveDistanceToPoint(int, int)
502 public boolean positiveDistanceToPoint(int x
, int y
) {
504 int mH
= getHeight();
505 if ((mY
> y
) || (mY
+ mH
> y
)) {