1 /**********************************************************************
2 * Copyright (c) 2005, 2014 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
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
;
15 import java
.util
.Comparator
;
17 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
19 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.drawings
.IGC
;
20 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.preferences
.ISDPreferences
;
21 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.preferences
.SDViewPref
;
22 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.util
.SortAsyncForBackward
;
23 import org
.eclipse
.tracecompass
.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
53 public class AsyncMessage
extends BaseMessage
implements ITimeRange
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 * The grahNode ID constant
61 public static final String ASYNC_MESS_TAG
= "AsyncMessage"; //$NON-NLS-1$
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 * Flag whether message has time information or not.
69 private boolean fHasTime
= false;
71 * The time when the message begin
73 private ITmfTimestamp fEndTime
= new TmfTimestamp();
75 * The time when the message end
77 private ITmfTimestamp fStartTime
= new TmfTimestamp();
79 * The associated message.
81 protected AsyncMessageReturn fMessageReturn
= null;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
89 public AsyncMessage() {
90 setColorPrefId(ISDPreferences
.PREF_ASYNC_MESS
);
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
99 int x
= super.getX(true);
100 int activationWidth
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
101 if ((getStartLifeline() != null) && (getEndLifeline() != null) && (getStartLifeline().getX() > getEndLifeline().getX())) {
102 activationWidth
= -activationWidth
;
105 if (isMessageStartInActivation(getStartOccurrence())) {
106 x
= x
+ activationWidth
;
113 if ((getStartLifeline() != null) && (getEndLifeline() != null)) {
114 return getEndLifeline().getY() + getEndLifeline().getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * getStartOccurrence();
120 public int getWidth() {
121 int width
= super.getWidth(true);
122 int activationWidth
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
123 if ((getStartLifeline() != null) && (getEndLifeline() != null) && (getStartLifeline().getX() > getEndLifeline().getX())) {
124 activationWidth
= -activationWidth
;
127 if (isMessageStartInActivation(getStartOccurrence())) {
128 width
= width
- activationWidth
;
131 if (isMessageEndInActivation(getEndOccurrence())) {
132 width
= width
- activationWidth
;
139 public int getHeight() {
140 if ((getStartLifeline() != null) && (getEndLifeline() != null)) {
141 return (getEndLifeline().getY() + getEndLifeline().getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * getEndOccurrence()) - getY();
143 return super.getHeight();
147 * Set the message return associated with this message.
149 * @param message the message return to associate
151 protected void setMessageReturn(AsyncMessageReturn message
) {
152 fMessageReturn
= message
;
156 * Set the event occurrence attached to this message for its end lifeline
158 * @param occurrence the event occurrence to set
161 public void setEndOccurrence(int occurrence
) {
162 super.setEndOccurrence(occurrence
);
163 if (getStartLifeline() == null) {
164 setStartOccurrence(occurrence
);
166 informFrame(getEndLifeline(), occurrence
);
170 * Informs the given lifeline about the maximum occurrence if applicable.
177 protected void informFrame(Lifeline lifeLine
, int occurrence
) {
178 if ((lifeLine
!= null) && (lifeLine
.getFrame() != null) && (lifeLine
.getFrame().getMaxEventOccurrence() < occurrence
)) {
179 lifeLine
.getFrame().setMaxEventOccurrence(occurrence
);
184 * Set the event occurrence attached to this message for its start lifeline
186 * @param occurrence the event occurrence to set
189 public void setStartOccurrence(int occurrence
) {
190 super.setStartOccurrence(occurrence
);
191 if (getEndLifeline() == null) {
192 setEndOccurrence(getStartOccurrence());
194 informFrame(getStartLifeline(), occurrence
);
198 * Set the lifeLine which has sent the message.<br>
199 * A new EventOccurence will be create on this lifeLine.<br>
201 * @param lifeline the message sender
203 public void autoSetStartLifeline(Lifeline lifeline
) {
204 lifeline
.getNewEventOccurrence();
205 setStartLifeline(lifeline
);
209 * Set the lifeLine which has received the message.<br>
210 * A new EventOccurence will be create on this lifeLine.<br>
212 * @param lifeline the message receiver
214 public void autoSetEndLifeline(Lifeline lifeline
) {
215 lifeline
.getNewEventOccurrence();
216 setEndLifeline(lifeline
);
220 public void setStartLifeline(Lifeline lifeline
) {
221 super.setStartLifeline(lifeline
);
222 setStartOccurrence(getStartLifeline().getEventOccurrence());
223 if (getEndLifeline() == null) {
224 setEndOccurrence(getStartOccurrence());
229 public void setEndLifeline(Lifeline lifeline
) {
230 super.setEndLifeline(lifeline
);
231 setEventOccurrence(getEndLifeline().getEventOccurrence());
235 * Returns true if the point C is on the segment defined with the point A and B
237 * @param xA point A x coordinate
238 * @param yA point A y coordinate
239 * @param xB point B x coordinate
240 * @param yB point B y coordinate
241 * @param xC point C x coordinate
242 * @param yC point C y coordinate
243 * @return Return true if the point C is on the segment defined with the point A and B, else otherwise
245 protected boolean isNearSegment(int xA
, int yA
, int xB
, int yB
, int xC
, int yC
) {
246 if ((xA
> xB
) && (xC
> xA
)) {
249 if ((xA
< xB
) && (xC
> xB
)) {
252 if ((xA
< xB
) && (xC
< xA
)) {
255 if ((xA
> xB
) && (xC
< xB
)) {
258 double distAB
= Math
.sqrt((xB
- xA
) * (xB
- xA
) + (yB
- yA
) * (yB
- yA
));
259 double scalar
= ((xB
- xA
) * (xC
- xA
) + (yB
- yA
) * (yC
- yA
)) / distAB
;
260 double distAC
= Math
.sqrt((xC
- xA
) * (xC
- xA
) + (yC
- yA
) * (yC
- yA
));
261 double distToSegment
= Math
.sqrt(Math
.abs(distAC
* distAC
- scalar
* scalar
));
262 if (distToSegment
<= Metrics
.MESSAGE_SELECTION_TOLERANCE
) {
269 public boolean contains(int x
, int y
) {
270 // Is it a self message?
271 if (getStartLifeline() == getEndLifeline()) {
272 return super.contains(x
, y
);
274 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x
, y
)) {
277 int messageMaxWidth
= Metrics
.swimmingLaneWidth() - Metrics
.EXECUTION_OCCURRENCE_WIDTH
;
278 int nameWidth
= getName().length() * Metrics
.getAverageCharWidth();
279 if (getName().length() * Metrics
.getAverageCharWidth() > messageMaxWidth
) {
280 if (GraphNode
.contains(getX(), getY() - Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), messageMaxWidth
, Metrics
.getMessageFontHeigth(), x
, y
)) {
284 if (GraphNode
.contains(getX() + (messageMaxWidth
- nameWidth
) / 2, getY() + getHeight() / 2 - Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), nameWidth
, Metrics
.getMessageFontHeigth(), x
, y
)) {
292 * Draws the asynchronous message using giving graphical context.
294 * @param context A graphical context to draw in.
296 protected void drawAsyncMessage(IGC context
) {
297 if (getStartLifeline() != null && getEndLifeline() != null && getStartLifeline() == getEndLifeline() && (getStartOccurrence() != getEndOccurrence())) {
300 int height
= getHeight();
302 boolean startInActivation
= isMessageStartInActivation(getStartOccurrence());
303 boolean endInActivation
= isMessageEndInActivation(getEndOccurrence());
305 if (endInActivation
&& !startInActivation
) {
306 tempx
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
308 if (startInActivation
&& !endInActivation
) {
309 tempx
= -Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
312 int tempy
= Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2;
313 if (getHeight() <= Metrics
.INTERNAL_MESSAGE_WIDTH
) {
314 tempy
= getHeight() / 2;
317 context
.drawLine(x
, y
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2, y
);
318 context
.drawLine(x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
, y
+ tempy
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
, y
+ height
- tempy
);
319 context
.drawLine(x
+ tempx
, y
+ height
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2, y
+ height
);
321 Double xt
= Double
.valueOf(Math
.cos(0.75) * 7);
322 Double yt
= Double
.valueOf(Math
.sin(0.75) * 7);
324 context
.drawLine(x
+ xt
.intValue() + tempx
, y
+ height
+ yt
.intValue(), x
+ tempx
, y
+ height
);
325 context
.drawArc(x
, y
, Metrics
.INTERNAL_MESSAGE_WIDTH
, 2 * tempy
, 0, 90);
326 context
.drawArc(x
, y
+ height
, Metrics
.INTERNAL_MESSAGE_WIDTH
, -2 * tempy
, 0, -90);
327 context
.drawLine(x
+ xt
.intValue() + tempx
, y
+ height
- yt
.intValue(), x
+ tempx
, y
+ height
);
329 context
.drawTextTruncated(getName(), x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
+ Metrics
.INTERNAL_MESSAGE_V_MARGIN
, y
, Metrics
.swimmingLaneWidth() - Metrics
.EXECUTION_OCCURRENCE_WIDTH
+ -Metrics
.INTERNAL_MESSAGE_WIDTH
,
330 +Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), !isSelected());
337 public void draw(IGC context
) {
342 ISDPreferences pref
= SDViewPref
.getInstance();
345 if (isSelected() && (getStartLifeline() != null && getEndLifeline() != null && getStartLifeline() == getEndLifeline() && (getStartOccurrence() != getEndOccurrence()))) {
347 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
348 * colors This create the highlight effect
350 context
.setForeground(pref
.getBackGroundColorSelection());
351 context
.setLineWidth(Metrics
.SELECTION_LINE_WIDTH
);
352 drawAsyncMessage(context
);
353 context
.setBackground(pref
.getBackGroundColorSelection());
354 context
.setForeground(pref
.getForeGroundColorSelection());
355 // Second drawing is done after the else
357 context
.setBackground(pref
.getBackGroundColor(getColorPrefId()));
358 context
.setForeground(pref
.getForeGroundColor(getColorPrefId()));
361 context
.setDrawTextWithFocusStyle(true);
363 context
.setLineWidth(Metrics
.NORMAL_LINE_WIDTH
);
364 drawAsyncMessage(context
);
366 context
.setDrawTextWithFocusStyle(false);
371 * Set the time when the message end
373 * @param time the time when the message end
375 public void setEndTime(ITmfTimestamp time
) {
378 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
379 getStartLifeline().getFrame().setHasTimeInfo(true);
380 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
381 getEndLifeline().getFrame().setHasTimeInfo(true);
386 * Set the time when the message start
388 * @param time the time when the message start
390 public void setStartTime(ITmfTimestamp time
) {
393 if (getStartLifeline() != null && getStartLifeline().getFrame() != null) {
394 getStartLifeline().getFrame().setHasTimeInfo(true);
395 } else if (getEndLifeline() != null && getEndLifeline().getFrame() != null) {
396 getEndLifeline().getFrame().setHasTimeInfo(true);
401 public ITmfTimestamp
getEndTime() {
406 public ITmfTimestamp
getStartTime() {
411 public boolean hasTimeInfo() {
416 * @return message return instance or null
418 public AsyncMessageReturn
getMessageReturn() {
419 return fMessageReturn
;
423 public boolean isVisible(int x
, int y
, int width
, int height
) {
424 int toDrawY
= getY();
425 int toDrawHeight
= getHeight();
426 if ((toDrawY
> y
+ height
+ Metrics
.MESSAGES_NAME_SPACING
+ Metrics
.getMessageFontHeigth()) && (toDrawY
+ toDrawHeight
> y
+ height
+ Metrics
.MESSAGES_NAME_SPACING
+ Metrics
.getMessageFontHeigth())) {
429 if (toDrawY
< y
&& (toDrawY
+ toDrawHeight
< y
)) {
432 return super.isVisible(x
, y
, width
, height
);
436 public Comparator
<GraphNode
> getComparator() {
437 return new SortAsyncMessageComparator();
441 public String
getArrayId() {
442 return ASYNC_MESS_TAG
;
446 public Comparator
<GraphNode
> getBackComparator() {
447 return new SortAsyncForBackward();
451 public boolean positiveDistanceToPoint(int x
, int y
) {
453 int mH
= getHeight();
454 if ((mY
> y
) || (mY
+ mH
> y
)) {