1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
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 * $Id: AsyncMessage.java,v 1.3 2008/01/24 02:28:49 apnan Exp $
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.core
;
15 import java
.util
.Comparator
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
18 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.drawings
.IGC
;
19 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.drawings
.ISDPreferences
;
20 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SortAsyncForBackward
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SortAsyncMessageComparator
;
24 * A AsyncMessage is a asynchronous message which appear at two different event occurrences on each lifeline ends (sender
35 * AsyncMessage message = new AsyncMessage();
36 * // Create a new event occurrence on each lifeline
37 * lifeline1.getNewOccurrenceIndex();
38 * lifeline2.getNewOccurrenceIndex();
39 * // Set the message sender and receiver
40 * message.setStartLifeline(lifeLine1);
41 * message.setEndLifline(lifeline2);
42 * message.setName("Message label");
43 * // add the message to the frame
44 * frame.addMessage(message);
47 * @see Lifeline Lifeline for more event occurence details
51 public class AsyncMessage
extends BaseMessage
implements ITimeRange
{
53 protected boolean hasTime
= false;
55 * The time when the message begin
57 protected TmfTimestamp endTime
= new TmfTimestamp();
60 * The time when the message end
62 protected TmfTimestamp startTime
= new TmfTimestamp();
65 * The associated message.
67 protected AsyncMessageReturn messageReturn
= null;
69 public static final String ASYNC_MESS_TAG
= "AsyncMessage"; //$NON-NLS-1$
71 public AsyncMessage() {
72 prefId
= ISDPreferences
.PREF_ASYNC_MESS
;
77 int x
= super.getX(true);
78 int activationWidth
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
79 if ((startLifeline
!= null) && (endLifeline
!= null) && (startLifeline
.getX() > endLifeline
.getX())) {
80 activationWidth
= -activationWidth
;
83 if (isMessageStartInActivation(startEventOccurrence
)) {
84 x
= x
+ activationWidth
;
91 if ((startLifeline
!= null) && (endLifeline
!= null)) {
92 return endLifeline
.getY() + endLifeline
.getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * startEventOccurrence
;
98 public int getWidth() {
99 int width
= super.getWidth(true);
100 int activationWidth
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
101 if ((startLifeline
!= null) && (endLifeline
!= null) && (startLifeline
.getX() > endLifeline
.getX())) {
102 activationWidth
= -activationWidth
;
105 if (isMessageStartInActivation(startEventOccurrence
))
106 width
= width
- activationWidth
;
108 if (isMessageEndInActivation(endEventOccurrence
))
109 width
= width
- activationWidth
;
115 public int getHeight() {
116 if ((startLifeline
!= null) && (endLifeline
!= null)) {
117 return (endLifeline
.getY() + endLifeline
.getHeight() + (Metrics
.getMessageFontHeigth() + Metrics
.getMessagesSpacing()) * endEventOccurrence
) - getY();
119 return super.getHeight();
123 * Set the message return associated with this message.
125 * @param message the message return to associate
127 protected void setMessageReturn(AsyncMessageReturn message
) {
128 messageReturn
= message
;
132 * Set the event occurrence attached to this message for its end lifeline
134 * @param occurrence the event occurrence to set
136 public void setEndOccurrence(int occurrence
) {
137 endEventOccurrence
= occurrence
;
138 if (getStartLifeline() == null)
139 startEventOccurrence
= occurrence
;
140 informFrame(getEndLifeline(), occurrence
);
143 protected void informFrame(Lifeline lifeLine
, int occurrence
) {
144 if ((lifeLine
!= null) && (lifeLine
.getFrame() != null))
145 if (lifeLine
.getFrame().getMaxEventOccurrence() < occurrence
)
146 lifeLine
.getFrame().setMaxEventOccurrence(occurrence
);
150 * Set the event occurrence attached to this message for its start lifeline
152 * @param occurrence the event occurrence to set
154 public void setStartOccurrence(int occurrence
) {
155 startEventOccurrence
= occurrence
;
156 if (getEndLifeline() == null)
157 endEventOccurrence
= startEventOccurrence
;
158 informFrame(getStartLifeline(), occurrence
);
162 * Set the lifeLine which has sent the message.<br>
163 * A new EventOccurence will be create on this lifeLine.<br>
165 * @param lifeline the message sender
166 * @param autoCreateEvent if true, create an eventOccurence lifeline given in parameter
168 public void autoSetStartLifeline(Lifeline lifeline
) {
169 lifeline
.getNewEventOccurrence();
170 setStartLifeline(lifeline
);
174 * Set the lifeLine which has received the message.<br>
175 * A new EventOccurence will be create on this lifeLine.<br>
177 * @param lifeline the message receiver
178 * @param autoCreateEvent if true, create an eventOccurence lifeline given in parameter
180 public void autoSetEndLifeline(Lifeline lifeline
) {
181 lifeline
.getNewEventOccurrence();
182 setEndLifeline(lifeline
);
186 * Set the lifeLine which has sent the message.<br>
188 * @param lifeline the message sender
191 public void setStartLifeline(Lifeline lifeline
) {
192 super.setStartLifeline(lifeline
);
193 setStartOccurrence(getStartLifeline().getEventOccurrence());
194 if (getEndLifeline() == null)
195 endEventOccurrence
= startEventOccurrence
;
199 * Set the lifeLine which has received the message.<br>
201 * @param lifeline the message receiver
204 public void setEndLifeline(Lifeline lifeline
) {
205 super.setEndLifeline(lifeline
);
206 setEventOccurrence(getEndLifeline().getEventOccurrence());
210 * Returns true if the point C is on the segment defined with the point A and B
212 * @param xA point A x coordinate
213 * @param yA point A y coordinate
214 * @param xB point B x coordinate
215 * @param yB point B y coordinate
216 * @param xC point C x coordinate
217 * @param yC point C y coordinate
218 * @return Return true if the point C is on the segment defined with the point A and B, else otherwise
220 protected boolean isNearSegment(int xA
, int yA
, int xB
, int yB
, int xC
, int yC
) {
221 if ((xA
> xB
) && (xC
> xA
))
223 if ((xA
< xB
) && (xC
> xB
))
225 if ((xA
< xB
) && (xC
< xA
))
227 if ((xA
> xB
) && (xC
< xB
))
229 double distAB
= Math
.sqrt((xB
- xA
) * (xB
- xA
) + (yB
- yA
) * (yB
- yA
));
230 double scalar
= ((xB
- xA
) * (xC
- xA
) + (yB
- yA
) * (yC
- yA
)) / distAB
;
231 double distAC
= Math
.sqrt((xC
- xA
) * (xC
- xA
) + (yC
- yA
) * (yC
- yA
));
232 double distToSegment
= Math
.sqrt(Math
.abs(distAC
* distAC
- scalar
* scalar
));
233 if (distToSegment
<= Metrics
.MESSAGE_SELECTION_TOLERANCE
)
239 public boolean contains(int x
, int y
) {
240 // Is it a self message?
241 if (startLifeline
== endLifeline
) {
242 return super.contains(x
, y
);
244 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x
, y
))
246 int messageMaxWidth
= Metrics
.swimmingLaneWidth() - Metrics
.EXECUTION_OCCURRENCE_WIDTH
;
247 int nameWidth
= getName().length() * Metrics
.getAverageCharWidth();
248 if (getName().length() * Metrics
.getAverageCharWidth() > messageMaxWidth
) {
249 if (Frame
.contains(getX(), getY() - Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), messageMaxWidth
, Metrics
.getMessageFontHeigth(), x
, y
))
252 if (Frame
.contains(getX() + (messageMaxWidth
- nameWidth
) / 2, getY() + getHeight() / 2 - Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), nameWidth
, Metrics
.getMessageFontHeigth(), x
, y
))
258 protected void drawAsyncMessage(IGC context
) {
259 if (startLifeline
!= null && endLifeline
!= null && startLifeline
== endLifeline
&& (startEventOccurrence
!= endEventOccurrence
)) {
262 int height
= getHeight();
264 boolean startInActivation
= isMessageStartInActivation(startEventOccurrence
);
265 boolean endInActivation
= isMessageEndInActivation(endEventOccurrence
);
267 if (endInActivation
&& !startInActivation
)
268 tempx
= Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
269 if (startInActivation
&& !endInActivation
)
270 tempx
= -Metrics
.EXECUTION_OCCURRENCE_WIDTH
/ 2;
272 int tempy
= Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2;
273 if (getHeight() <= Metrics
.INTERNAL_MESSAGE_WIDTH
)
274 tempy
= getHeight() / 2;
276 context
.drawLine(x
, y
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2, y
);
277 context
.drawLine(x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
, y
+ tempy
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
, y
+ height
- tempy
);
278 context
.drawLine(x
+ tempx
, y
+ height
, x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
/ 2, y
+ height
);
280 Double xt
= new Double(Math
.cos(0.75) * 7);
281 Double yt
= new Double(Math
.sin(0.75) * 7);
283 context
.drawLine(x
+ xt
.intValue() + tempx
, y
+ height
+ yt
.intValue(), x
+ tempx
, y
+ height
);
284 context
.drawArc(x
, y
, Metrics
.INTERNAL_MESSAGE_WIDTH
, 2 * tempy
, 0, 90);
285 context
.drawArc(x
, y
+ height
, Metrics
.INTERNAL_MESSAGE_WIDTH
, -2 * tempy
, 0, -90);
286 context
.drawLine(x
+ xt
.intValue() + tempx
, y
+ height
- yt
.intValue(), x
+ tempx
, y
+ height
);
288 context
.drawTextTruncated(getName(), x
+ Metrics
.INTERNAL_MESSAGE_WIDTH
+ Metrics
.INTERNAL_MESSAGE_V_MARGIN
, y
, Metrics
.swimmingLaneWidth() - Metrics
.EXECUTION_OCCURRENCE_WIDTH
+ -Metrics
.INTERNAL_MESSAGE_WIDTH
,
289 +Metrics
.MESSAGES_NAME_SPACING
- Metrics
.getMessageFontHeigth(), !isSelected());
295 * Draws the asynchronous message in the given GC
298 public void draw(IGC context
) {
302 if (isSelected() && (startLifeline
!= null && endLifeline
!= null && startLifeline
== endLifeline
&& (startEventOccurrence
!= endEventOccurrence
))) {
304 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
305 * colors This create the highlight effect
307 context
.setForeground(Frame
.getUserPref().getBackGroundColorSelection());
308 context
.setLineWidth(Metrics
.SELECTION_LINE_WIDTH
);
309 drawAsyncMessage(context
);
310 context
.setBackground(Frame
.getUserPref().getBackGroundColorSelection());
311 context
.setForeground(Frame
.getUserPref().getForeGroundColorSelection());
312 // Second drawing is done after the else
314 context
.setBackground(Frame
.getUserPref().getBackGroundColor(prefId
));
315 context
.setForeground(Frame
.getUserPref().getForeGroundColor(prefId
));
318 context
.setDrawTextWithFocusStyle(true);
320 context
.setLineWidth(Metrics
.NORMAL_LINE_WIDTH
);
321 drawAsyncMessage(context
);
323 context
.setDrawTextWithFocusStyle(false);
328 * Set the time when the message end
330 * @param time the time when the message end
332 public void setEndTime(TmfTimestamp time
) {
333 endTime
= time
.clone();
335 if (getStartLifeline() != null && getStartLifeline().getFrame() != null)
336 getStartLifeline().getFrame().setHasTimeInfo(true);
337 else if (getEndLifeline() != null && getEndLifeline().getFrame() != null)
338 getEndLifeline().getFrame().setHasTimeInfo(true);
342 * Set the time when the message start
344 * @param time the time when the message start
346 public void setStartTime(TmfTimestamp time
) {
347 startTime
= time
.clone();
349 if (getStartLifeline() != null && getStartLifeline().getFrame() != null)
350 getStartLifeline().getFrame().setHasTimeInfo(true);
351 else if (getEndLifeline() != null && getEndLifeline().getFrame() != null)
352 getEndLifeline().getFrame().setHasTimeInfo(true);
356 * Returns the time when the message begin
361 public TmfTimestamp
getEndTime() {
366 * Returns the time when the message end
371 public TmfTimestamp
getStartTime() {
376 public boolean hasTimeInfo() {
381 public boolean isVisible(int x
, int y
, int width
, int height
) {
382 int toDrawY
= getY();
383 int toDrawHeight
= getHeight();
384 if ((toDrawY
> y
+ height
+ Metrics
.MESSAGES_NAME_SPACING
+ Metrics
.getMessageFontHeigth()) && (toDrawY
+ toDrawHeight
> y
+ height
+ Metrics
.MESSAGES_NAME_SPACING
+ Metrics
.getMessageFontHeigth()))
386 if (toDrawY
< y
&& (toDrawY
+ toDrawHeight
< y
))
388 return super.isVisible(x
, y
, width
, height
);
392 public Comparator
<GraphNode
> getComparator() {
393 return new SortAsyncMessageComparator();
397 public String
getArrayId() {
398 return ASYNC_MESS_TAG
;
402 public Comparator
<GraphNode
> getBackComparator() {
403 return new SortAsyncForBackward();
407 public boolean positiveDistanceToPoint(int x
, int y
) {
409 int mH
= getHeight();
410 if ((mY
> y
) || (mY
+ mH
> y
))