Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / core / AsyncMessage.java
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 $
8 *
9 * Contributors:
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
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.event.ITmfTimestamp;
18 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
19 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
20 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.ISDPreferences;
21 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncForBackward;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SortAsyncMessageComparator;
23
24 /**
25 * A AsyncMessage is a asynchronous message which appear at two different event occurrences on each lifeline ends (sender
26 * and receiver).<br>
27 * <br>
28 * <br>
29 * Usage example:
30 *
31 * <pre>
32 * Frame frame;
33 * Lifeline lifeLine1;
34 * Lifeline lifeLine2;
35 *
36 * AsyncMessage message = new AsyncMessage();
37 * // Create a new event occurrence on each lifeline
38 * lifeline1.getNewOccurrenceIndex();
39 * lifeline2.getNewOccurrenceIndex();
40 * // Set the message sender and receiver
41 * message.setStartLifeline(lifeLine1);
42 * message.setEndLifline(lifeline2);
43 * message.setName(&quot;Message label&quot;);
44 * // add the message to the frame
45 * frame.addMessage(message);
46 * </pre>
47 *
48 * @see Lifeline Lifeline for more event occurence details
49 * @author sveyrier
50 *
51 */
52 public class AsyncMessage extends BaseMessage implements ITimeRange {
53
54 protected boolean hasTime = false;
55 /**
56 * The time when the message begin
57 */
58 protected ITmfTimestamp endTime = new TmfTimestamp();
59
60 /**
61 * The time when the message end
62 */
63 protected ITmfTimestamp startTime = new TmfTimestamp();
64
65 /**
66 * The associated message.
67 */
68 protected AsyncMessageReturn messageReturn = null;
69
70 public static final String ASYNC_MESS_TAG = "AsyncMessage"; //$NON-NLS-1$
71
72 public AsyncMessage() {
73 prefId = ISDPreferences.PREF_ASYNC_MESS;
74 }
75
76 @Override
77 public int getX() {
78 int x = super.getX(true);
79 int activationWidth = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
80 if ((startLifeline != null) && (endLifeline != null) && (startLifeline.getX() > endLifeline.getX())) {
81 activationWidth = -activationWidth;
82 }
83
84 if (isMessageStartInActivation(startEventOccurrence)) {
85 x = x + activationWidth;
86 }
87 return x;
88 }
89
90 @Override
91 public int getY() {
92 if ((startLifeline != null) && (endLifeline != null)) {
93 return endLifeline.getY() + endLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * startEventOccurrence;
94 }
95 return super.getY();
96 }
97
98 @Override
99 public int getWidth() {
100 int width = super.getWidth(true);
101 int activationWidth = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
102 if ((startLifeline != null) && (endLifeline != null) && (startLifeline.getX() > endLifeline.getX())) {
103 activationWidth = -activationWidth;
104 }
105
106 if (isMessageStartInActivation(startEventOccurrence))
107 width = width - activationWidth;
108
109 if (isMessageEndInActivation(endEventOccurrence))
110 width = width - activationWidth;
111
112 return width;
113 }
114
115 @Override
116 public int getHeight() {
117 if ((startLifeline != null) && (endLifeline != null)) {
118 return (endLifeline.getY() + endLifeline.getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * endEventOccurrence) - getY();
119 }
120 return super.getHeight();
121 }
122
123 /**
124 * Set the message return associated with this message.
125 *
126 * @param message the message return to associate
127 */
128 protected void setMessageReturn(AsyncMessageReturn message) {
129 messageReturn = message;
130 }
131
132 /**
133 * Set the event occurrence attached to this message for its end lifeline
134 *
135 * @param occurrence the event occurrence to set
136 */
137 public void setEndOccurrence(int occurrence) {
138 endEventOccurrence = occurrence;
139 if (getStartLifeline() == null)
140 startEventOccurrence = occurrence;
141 informFrame(getEndLifeline(), occurrence);
142 }
143
144 protected void informFrame(Lifeline lifeLine, int occurrence) {
145 if ((lifeLine != null) && (lifeLine.getFrame() != null))
146 if (lifeLine.getFrame().getMaxEventOccurrence() < occurrence)
147 lifeLine.getFrame().setMaxEventOccurrence(occurrence);
148 }
149
150 /**
151 * Set the event occurrence attached to this message for its start lifeline
152 *
153 * @param occurrence the event occurrence to set
154 */
155 public void setStartOccurrence(int occurrence) {
156 startEventOccurrence = occurrence;
157 if (getEndLifeline() == null)
158 endEventOccurrence = startEventOccurrence;
159 informFrame(getStartLifeline(), occurrence);
160 }
161
162 /**
163 * Set the lifeLine which has sent the message.<br>
164 * A new EventOccurence will be create on this lifeLine.<br>
165 *
166 * @param lifeline the message sender
167 */
168 public void autoSetStartLifeline(Lifeline lifeline) {
169 lifeline.getNewEventOccurrence();
170 setStartLifeline(lifeline);
171 }
172
173 /**
174 * Set the lifeLine which has received the message.<br>
175 * A new EventOccurence will be create on this lifeLine.<br>
176 *
177 * @param lifeline the message receiver
178 */
179 public void autoSetEndLifeline(Lifeline lifeline) {
180 lifeline.getNewEventOccurrence();
181 setEndLifeline(lifeline);
182 }
183
184 /**
185 * Set the lifeLine which has sent the message.<br>
186 *
187 * @param lifeline the message sender
188 */
189 @Override
190 public void setStartLifeline(Lifeline lifeline) {
191 super.setStartLifeline(lifeline);
192 setStartOccurrence(getStartLifeline().getEventOccurrence());
193 if (getEndLifeline() == null)
194 endEventOccurrence = startEventOccurrence;
195 }
196
197 /**
198 * Set the lifeLine which has received the message.<br>
199 *
200 * @param lifeline the message receiver
201 */
202 @Override
203 public void setEndLifeline(Lifeline lifeline) {
204 super.setEndLifeline(lifeline);
205 setEventOccurrence(getEndLifeline().getEventOccurrence());
206 }
207
208 /**
209 * Returns true if the point C is on the segment defined with the point A and B
210 *
211 * @param xA point A x coordinate
212 * @param yA point A y coordinate
213 * @param xB point B x coordinate
214 * @param yB point B y coordinate
215 * @param xC point C x coordinate
216 * @param yC point C y coordinate
217 * @return Return true if the point C is on the segment defined with the point A and B, else otherwise
218 */
219 protected boolean isNearSegment(int xA, int yA, int xB, int yB, int xC, int yC) {
220 if ((xA > xB) && (xC > xA))
221 return false;
222 if ((xA < xB) && (xC > xB))
223 return false;
224 if ((xA < xB) && (xC < xA))
225 return false;
226 if ((xA > xB) && (xC < xB))
227 return false;
228 double distAB = Math.sqrt((xB - xA) * (xB - xA) + (yB - yA) * (yB - yA));
229 double scalar = ((xB - xA) * (xC - xA) + (yB - yA) * (yC - yA)) / distAB;
230 double distAC = Math.sqrt((xC - xA) * (xC - xA) + (yC - yA) * (yC - yA));
231 double distToSegment = Math.sqrt(Math.abs(distAC * distAC - scalar * scalar));
232 if (distToSegment <= Metrics.MESSAGE_SELECTION_TOLERANCE)
233 return true;
234 return false;
235 }
236
237 @Override
238 public boolean contains(int x, int y) {
239 // Is it a self message?
240 if (startLifeline == endLifeline) {
241 return super.contains(x, y);
242 }
243 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x, y))
244 return true;
245 int messageMaxWidth = Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH;
246 int nameWidth = getName().length() * Metrics.getAverageCharWidth();
247 if (getName().length() * Metrics.getAverageCharWidth() > messageMaxWidth) {
248 if (Frame.contains(getX(), getY() - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), messageMaxWidth, Metrics.getMessageFontHeigth(), x, y))
249 return true;
250 } else {
251 if (Frame.contains(getX() + (messageMaxWidth - nameWidth) / 2, getY() + getHeight() / 2 - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), nameWidth, Metrics.getMessageFontHeigth(), x, y))
252 return true;
253 }
254 return false;
255 }
256
257 protected void drawAsyncMessage(IGC context) {
258 if (startLifeline != null && endLifeline != null && startLifeline == endLifeline && (startEventOccurrence != endEventOccurrence)) {
259 int x = getX();
260 int y = getY();
261 int height = getHeight();
262 int tempx = 0;
263 boolean startInActivation = isMessageStartInActivation(startEventOccurrence);
264 boolean endInActivation = isMessageEndInActivation(endEventOccurrence);
265
266 if (endInActivation && !startInActivation)
267 tempx = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
268 if (startInActivation && !endInActivation)
269 tempx = -Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
270
271 int tempy = Metrics.INTERNAL_MESSAGE_WIDTH / 2;
272 if (getHeight() <= Metrics.INTERNAL_MESSAGE_WIDTH)
273 tempy = getHeight() / 2;
274
275 context.drawLine(x, y, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y);
276 context.drawLine(x + Metrics.INTERNAL_MESSAGE_WIDTH, y + tempy, x + Metrics.INTERNAL_MESSAGE_WIDTH, y + height - tempy);
277 context.drawLine(x + tempx, y + height, x + Metrics.INTERNAL_MESSAGE_WIDTH / 2, y + height);
278
279 Double xt = new Double(Math.cos(0.75) * 7);
280 Double yt = new Double(Math.sin(0.75) * 7);
281
282 context.drawLine(x + xt.intValue() + tempx, y + height + yt.intValue(), x + tempx, y + height);
283 context.drawArc(x, y, Metrics.INTERNAL_MESSAGE_WIDTH, 2 * tempy, 0, 90);
284 context.drawArc(x, y + height, Metrics.INTERNAL_MESSAGE_WIDTH, -2 * tempy, 0, -90);
285 context.drawLine(x + xt.intValue() + tempx, y + height - yt.intValue(), x + tempx, y + height);
286
287 context.drawTextTruncated(getName(), x + Metrics.INTERNAL_MESSAGE_WIDTH + Metrics.INTERNAL_MESSAGE_V_MARGIN, y, Metrics.swimmingLaneWidth() - Metrics.EXECUTION_OCCURRENCE_WIDTH + -Metrics.INTERNAL_MESSAGE_WIDTH,
288 +Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), !isSelected());
289 } else
290 super.draw(context);
291 }
292
293 /**
294 * Draws the asynchronous message in the given GC
295 */
296 @Override
297 public void draw(IGC context) {
298 if (!isVisible())
299 return;
300 // Draw it selected?
301 if (isSelected() && (startLifeline != null && endLifeline != null && startLifeline == endLifeline && (startEventOccurrence != endEventOccurrence))) {
302 /*
303 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
304 * colors This create the highlight effect
305 */
306 context.setForeground(Frame.getUserPref().getBackGroundColorSelection());
307 context.setLineWidth(Metrics.SELECTION_LINE_WIDTH);
308 drawAsyncMessage(context);
309 context.setBackground(Frame.getUserPref().getBackGroundColorSelection());
310 context.setForeground(Frame.getUserPref().getForeGroundColorSelection());
311 // Second drawing is done after the else
312 } else {
313 context.setBackground(Frame.getUserPref().getBackGroundColor(prefId));
314 context.setForeground(Frame.getUserPref().getForeGroundColor(prefId));
315 }
316 if (hasFocus()) {
317 context.setDrawTextWithFocusStyle(true);
318 }
319 context.setLineWidth(Metrics.NORMAL_LINE_WIDTH);
320 drawAsyncMessage(context);
321 if (hasFocus()) {
322 context.setDrawTextWithFocusStyle(false);
323 }
324 }
325
326 /**
327 * Set the time when the message end
328 *
329 * @param time the time when the message end
330 */
331 public void setEndTime(ITmfTimestamp time) {
332 endTime = time.clone();
333 hasTime = true;
334 if (getStartLifeline() != null && getStartLifeline().getFrame() != null)
335 getStartLifeline().getFrame().setHasTimeInfo(true);
336 else if (getEndLifeline() != null && getEndLifeline().getFrame() != null)
337 getEndLifeline().getFrame().setHasTimeInfo(true);
338 }
339
340 /**
341 * Set the time when the message start
342 *
343 * @param time the time when the message start
344 */
345 public void setStartTime(ITmfTimestamp time) {
346 startTime = time.clone();
347 hasTime = true;
348 if (getStartLifeline() != null && getStartLifeline().getFrame() != null)
349 getStartLifeline().getFrame().setHasTimeInfo(true);
350 else if (getEndLifeline() != null && getEndLifeline().getFrame() != null)
351 getEndLifeline().getFrame().setHasTimeInfo(true);
352 }
353
354 /**
355 * Returns the time when the message begin
356 *
357 * @return the time
358 */
359 @Override
360 public ITmfTimestamp getEndTime() {
361 return endTime;
362 }
363
364 /**
365 * Returns the time when the message end
366 *
367 * @return the time
368 */
369 @Override
370 public ITmfTimestamp getStartTime() {
371 return startTime;
372 }
373
374 @Override
375 public boolean hasTimeInfo() {
376 return hasTime;
377 }
378
379 @Override
380 public boolean isVisible(int x, int y, int width, int height) {
381 int toDrawY = getY();
382 int toDrawHeight = getHeight();
383 if ((toDrawY > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()) && (toDrawY + toDrawHeight > y + height + Metrics.MESSAGES_NAME_SPACING + Metrics.getMessageFontHeigth()))
384 return false;
385 if (toDrawY < y && (toDrawY + toDrawHeight < y))
386 return false;
387 return super.isVisible(x, y, width, height);
388 }
389
390 @Override
391 public Comparator<GraphNode> getComparator() {
392 return new SortAsyncMessageComparator();
393 }
394
395 @Override
396 public String getArrayId() {
397 return ASYNC_MESS_TAG;
398 }
399
400 @Override
401 public Comparator<GraphNode> getBackComparator() {
402 return new SortAsyncForBackward();
403 }
404
405 @Override
406 public boolean positiveDistanceToPoint(int x, int y) {
407 int mY = getY();
408 int mH = getHeight();
409 if ((mY > y) || (mY + mH > y))
410 return true;
411 return false;
412 }
413 }
This page took 0.051747 seconds and 6 git commands to generate.