analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / core / AsyncMessage.java
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
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.views.uml2sd.core;
14
15 import java.util.Comparator;
16
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;
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 */
53 public class AsyncMessage extends BaseMessage implements ITimeRange {
54
55 // ------------------------------------------------------------------------
56 // Constants
57 // ------------------------------------------------------------------------
58 /**
59 * The grahNode ID constant
60 */
61 public static final String ASYNC_MESS_TAG = "AsyncMessage"; //$NON-NLS-1$
62
63 // ------------------------------------------------------------------------
64 // Attributes
65 // ------------------------------------------------------------------------
66 /**
67 * Flag whether message has time information or not.
68 */
69 private boolean fHasTime = false;
70 /**
71 * The time when the message begin
72 */
73 private ITmfTimestamp fEndTime = new TmfTimestamp();
74 /**
75 * The time when the message end
76 */
77 private ITmfTimestamp fStartTime = new TmfTimestamp();
78 /**
79 * The associated message.
80 */
81 protected AsyncMessageReturn fMessageReturn = null;
82
83 // ------------------------------------------------------------------------
84 // Constructors
85 // ------------------------------------------------------------------------
86 /**
87 * Default constructor
88 */
89 public AsyncMessage() {
90 setColorPrefId(ISDPreferences.PREF_ASYNC_MESS);
91 }
92
93 // ------------------------------------------------------------------------
94 // Methods
95 // ------------------------------------------------------------------------
96
97 @Override
98 public int getX() {
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;
103 }
104
105 if (isMessageStartInActivation(getStartOccurrence())) {
106 x = x + activationWidth;
107 }
108 return x;
109 }
110
111 @Override
112 public int getY() {
113 if ((getStartLifeline() != null) && (getEndLifeline() != null)) {
114 return getEndLifeline().getY() + getEndLifeline().getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * getStartOccurrence();
115 }
116 return super.getY();
117 }
118
119 @Override
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;
125 }
126
127 if (isMessageStartInActivation(getStartOccurrence())) {
128 width = width - activationWidth;
129 }
130
131 if (isMessageEndInActivation(getEndOccurrence())) {
132 width = width - activationWidth;
133 }
134
135 return width;
136 }
137
138 @Override
139 public int getHeight() {
140 if ((getStartLifeline() != null) && (getEndLifeline() != null)) {
141 return (getEndLifeline().getY() + getEndLifeline().getHeight() + (Metrics.getMessageFontHeigth() + Metrics.getMessagesSpacing()) * getEndOccurrence()) - getY();
142 }
143 return super.getHeight();
144 }
145
146 /**
147 * Set the message return associated with this message.
148 *
149 * @param message the message return to associate
150 */
151 protected void setMessageReturn(AsyncMessageReturn message) {
152 fMessageReturn = message;
153 }
154
155 /**
156 * Set the event occurrence attached to this message for its end lifeline
157 *
158 * @param occurrence the event occurrence to set
159 */
160 @Override
161 public void setEndOccurrence(int occurrence) {
162 super.setEndOccurrence(occurrence);
163 if (getStartLifeline() == null) {
164 setStartOccurrence(occurrence);
165 }
166 informFrame(getEndLifeline(), occurrence);
167 }
168
169 /**
170 * Informs the given lifeline about the maximum occurrence if applicable.
171 *
172 * @param lifeLine
173 * Concerned lifeline
174 * @param occurrence
175 * Occurrence number
176 */
177 protected void informFrame(Lifeline lifeLine, int occurrence) {
178 if ((lifeLine != null) && (lifeLine.getFrame() != null) && (lifeLine.getFrame().getMaxEventOccurrence() < occurrence)) {
179 lifeLine.getFrame().setMaxEventOccurrence(occurrence);
180 }
181 }
182
183 /**
184 * Set the event occurrence attached to this message for its start lifeline
185 *
186 * @param occurrence the event occurrence to set
187 */
188 @Override
189 public void setStartOccurrence(int occurrence) {
190 super.setStartOccurrence(occurrence);
191 if (getEndLifeline() == null) {
192 setEndOccurrence(getStartOccurrence());
193 }
194 informFrame(getStartLifeline(), occurrence);
195 }
196
197 /**
198 * Set the lifeLine which has sent the message.<br>
199 * A new EventOccurence will be create on this lifeLine.<br>
200 *
201 * @param lifeline the message sender
202 */
203 public void autoSetStartLifeline(Lifeline lifeline) {
204 lifeline.getNewEventOccurrence();
205 setStartLifeline(lifeline);
206 }
207
208 /**
209 * Set the lifeLine which has received the message.<br>
210 * A new EventOccurence will be create on this lifeLine.<br>
211 *
212 * @param lifeline the message receiver
213 */
214 public void autoSetEndLifeline(Lifeline lifeline) {
215 lifeline.getNewEventOccurrence();
216 setEndLifeline(lifeline);
217 }
218
219 @Override
220 public void setStartLifeline(Lifeline lifeline) {
221 super.setStartLifeline(lifeline);
222 setStartOccurrence(getStartLifeline().getEventOccurrence());
223 if (getEndLifeline() == null) {
224 setEndOccurrence(getStartOccurrence());
225 }
226 }
227
228 @Override
229 public void setEndLifeline(Lifeline lifeline) {
230 super.setEndLifeline(lifeline);
231 setEventOccurrence(getEndLifeline().getEventOccurrence());
232 }
233
234 /**
235 * Returns true if the point C is on the segment defined with the point A and B
236 *
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
244 */
245 protected boolean isNearSegment(int xA, int yA, int xB, int yB, int xC, int yC) {
246 if ((xA > xB) && (xC > xA)) {
247 return false;
248 }
249 if ((xA < xB) && (xC > xB)) {
250 return false;
251 }
252 if ((xA < xB) && (xC < xA)) {
253 return false;
254 }
255 if ((xA > xB) && (xC < xB)) {
256 return false;
257 }
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) {
263 return true;
264 }
265 return false;
266 }
267
268 @Override
269 public boolean contains(int x, int y) {
270 // Is it a self message?
271 if (getStartLifeline() == getEndLifeline()) {
272 return super.contains(x, y);
273 }
274 if (isNearSegment(getX(), getY(), getX() + getWidth(), getY() + getHeight(), x, y)) {
275 return true;
276 }
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)) {
281 return true;
282 }
283 } else {
284 if (GraphNode.contains(getX() + (messageMaxWidth - nameWidth) / 2, getY() + getHeight() / 2 - Metrics.MESSAGES_NAME_SPACING - Metrics.getMessageFontHeigth(), nameWidth, Metrics.getMessageFontHeigth(), x, y)) {
285 return true;
286 }
287 }
288 return false;
289 }
290
291 /**
292 * Draws the asynchronous message using giving graphical context.
293 *
294 * @param context A graphical context to draw in.
295 */
296 protected void drawAsyncMessage(IGC context) {
297 if (getStartLifeline() != null && getEndLifeline() != null && getStartLifeline() == getEndLifeline() && (getStartOccurrence() != getEndOccurrence())) {
298 int x = getX();
299 int y = getY();
300 int height = getHeight();
301 int tempx = 0;
302 boolean startInActivation = isMessageStartInActivation(getStartOccurrence());
303 boolean endInActivation = isMessageEndInActivation(getEndOccurrence());
304
305 if (endInActivation && !startInActivation) {
306 tempx = Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
307 }
308 if (startInActivation && !endInActivation) {
309 tempx = -Metrics.EXECUTION_OCCURRENCE_WIDTH / 2;
310 }
311
312 int tempy = Metrics.INTERNAL_MESSAGE_WIDTH / 2;
313 if (getHeight() <= Metrics.INTERNAL_MESSAGE_WIDTH) {
314 tempy = getHeight() / 2;
315 }
316
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);
320
321 Double xt = Double.valueOf(Math.cos(0.75) * 7);
322 Double yt = Double.valueOf(Math.sin(0.75) * 7);
323
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);
328
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());
331 } else {
332 super.draw(context);
333 }
334 }
335
336 @Override
337 public void draw(IGC context) {
338 if (!isVisible()) {
339 return;
340 }
341
342 ISDPreferences pref = SDViewPref.getInstance();
343
344 // Draw it selected?
345 if (isSelected() && (getStartLifeline() != null && getEndLifeline() != null && getStartLifeline() == getEndLifeline() && (getStartOccurrence() != getEndOccurrence()))) {
346 /*
347 * Draw it twice First time, bigger inverting selection colors Second time, regular drawing using selection
348 * colors This create the highlight effect
349 */
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
356 } else {
357 context.setBackground(pref.getBackGroundColor(getColorPrefId()));
358 context.setForeground(pref.getForeGroundColor(getColorPrefId()));
359 }
360 if (hasFocus()) {
361 context.setDrawTextWithFocusStyle(true);
362 }
363 context.setLineWidth(Metrics.NORMAL_LINE_WIDTH);
364 drawAsyncMessage(context);
365 if (hasFocus()) {
366 context.setDrawTextWithFocusStyle(false);
367 }
368 }
369
370 /**
371 * Set the time when the message end
372 *
373 * @param time the time when the message end
374 */
375 public void setEndTime(ITmfTimestamp time) {
376 fEndTime = time;
377 fHasTime = true;
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);
382 }
383 }
384
385 /**
386 * Set the time when the message start
387 *
388 * @param time the time when the message start
389 */
390 public void setStartTime(ITmfTimestamp time) {
391 fStartTime = time;
392 fHasTime = true;
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);
397 }
398 }
399
400 @Override
401 public ITmfTimestamp getEndTime() {
402 return fEndTime;
403 }
404
405 @Override
406 public ITmfTimestamp getStartTime() {
407 return fStartTime;
408 }
409
410 @Override
411 public boolean hasTimeInfo() {
412 return fHasTime;
413 }
414
415 /**
416 * @return message return instance or null
417 */
418 public AsyncMessageReturn getMessageReturn() {
419 return fMessageReturn;
420 }
421
422 @Override
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())) {
427 return false;
428 }
429 if (toDrawY < y && (toDrawY + toDrawHeight < y)) {
430 return false;
431 }
432 return super.isVisible(x, y, width, height);
433 }
434
435 @Override
436 public Comparator<GraphNode> getComparator() {
437 return new SortAsyncMessageComparator();
438 }
439
440 @Override
441 public String getArrayId() {
442 return ASYNC_MESS_TAG;
443 }
444
445 @Override
446 public Comparator<GraphNode> getBackComparator() {
447 return new SortAsyncForBackward();
448 }
449
450 @Override
451 public boolean positiveDistanceToPoint(int x, int y) {
452 int mY = getY();
453 int mH = getHeight();
454 if ((mY > y) || (mY + mH > y)) {
455 return true;
456 }
457 return false;
458 }
459 }
This page took 0.041434 seconds and 5 git commands to generate.