2f7272dea600f6eee7f3075c6c06b32a2bfbe5af
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / core / BasicFrame.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.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
20 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
21 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.drawings.IGC;
22 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.ISDPreferences;
23 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.SDViewPref;
24
25 /**
26 * The Frame class is the base sequence diagram graph nodes container.<br>
27 * For instance, only one frame can be drawn in the View.<br>
28 * Lifelines, Messages and Stop which are supposed to represent a Sequence diagram are drawn in a Frame.<br>
29 * Only the graph node added to their representing list will be drawn.
30 *
31 * The lifelines are appended along the X axsis when added in a frame.<br>
32 * The syncMessages are ordered along the Y axsis depending on the event occurrence they are attached to.<br>
33 *
34 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.Lifeline Lifeline for more event occurence details
35 * @author sveyrier
36 * @version 1.0
37 */
38 public class BasicFrame extends GraphNode {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 /**
45 * Contains the max elapsed time between two consecutive messages in the whole frame
46 */
47 private ITmfTimestamp fMaxTime = new TmfTimestamp(0);
48 /**
49 * Contains the min elapsed time between two consecutive messages in the whole frame
50 */
51 private ITmfTimestamp fMinTime = new TmfTimestamp(0);
52 /**
53 * Indicate if the min and max elapsed time between two consecutive messages in the whole frame need to be computed
54 */
55 private boolean fComputeMinMax = true;
56 /**
57 * Store the preference set by the user regarding the external time. This flag is used determine if the min and max
58 * need to be recomputed in case this preference is changed.
59 */
60 private boolean fLastExternalTimePref = SDViewPref.getInstance().excludeExternalTime();
61 /**
62 * The greater event occurrence created on graph nodes drawn in this Frame This directly impact the Frame height
63 */
64 private int fVerticalIndex = 0;
65 /**
66 * The index along the x axis where the next lifeline will is drawn This directly impact the Frame width
67 */
68 private int fHorizontalIndex = 0;
69 /**
70 * The time information flag.
71 */
72 private boolean fHasTimeInfo = false;
73 /**
74 * The current Frame visible area - x coordinates
75 */
76 private int fVisibleAreaX;
77 /**
78 * The current Frame visible area - y coordinates
79 */
80 private int fVisibleAreaY;
81 /**
82 * The current Frame visible area - width
83 */
84 private int fVisibleAreaWidth;
85 /**
86 * The current Frame visible area - height
87 */
88 private int fVisibleAreaHeight;
89 /**
90 * The event occurrence spacing (-1 for none)
91 */
92 private int fForceEventOccurrenceSpacing = -1;
93 /**
94 * Flag to indicate customized minumum and maximum.
95 */
96 private boolean fCustomMinMax = false;
97 /**
98 * The minimum time between messages of the sequence diagram frame.
99 */
100 private ITmfTimestamp fMinSDTime = new TmfTimestamp();
101 /**
102 * The maximum time between messages of the sequence diagram frame.
103 */
104 private ITmfTimestamp fMaxSDTime = new TmfTimestamp();
105 /**
106 * Flag to indicate that initial minimum has to be computed.
107 */
108 private boolean fInitSDMin = true;
109
110 // ------------------------------------------------------------------------
111 // Constructors
112 // ------------------------------------------------------------------------
113
114 /**
115 * Creates an empty frame.
116 */
117 public BasicFrame() {
118 Metrics.setForcedEventSpacing(fForceEventOccurrenceSpacing);
119 }
120
121 // ------------------------------------------------------------------------
122 // Methods
123 // ------------------------------------------------------------------------
124
125 /**
126 *
127 * Returns the greater event occurence known by the Frame
128 *
129 * @return the greater event occurrence
130 */
131 protected int getMaxEventOccurrence() {
132 return fVerticalIndex;
133 }
134
135 /**
136 * Set the greater event occurrence created in GraphNodes included in the frame
137 *
138 * @param eventOccurrence the new greater event occurrence
139 */
140 protected void setMaxEventOccurrence(int eventOccurrence) {
141 fVerticalIndex = eventOccurrence;
142 }
143
144 /**
145 * This method increase the lifeline place holder The return value is usually assign to a lifeline. This can be used
146 * to set the lifelines drawing order. Also, calling this method two times and assigning only the last given index
147 * to a lifeline will increase this lifeline draw spacing (2 times the default spacing) from the last added
148 * lifeline.
149 *
150 * @return a new lifeline index
151 */
152 protected int getNewHorizontalIndex() {
153 return ++fHorizontalIndex;
154 }
155
156 /**
157 * Returns the current horizontal index
158 *
159 * @return the current horizontal index
160 * @see Frame#getNewHorizontalIndex() for horizontal index description
161 */
162 protected int getHorizontalIndex() {
163 return fHorizontalIndex;
164 }
165
166 @Override
167 public void addNode(GraphNode nodeToAdd) {
168 setComputeMinMax(true);
169 super.addNode(nodeToAdd);
170 }
171
172 @Override
173 public int getX() {
174 return Metrics.FRAME_H_MARGIN;
175 }
176
177 @Override
178 public int getY() {
179 return Metrics.FRAME_V_MARGIN;
180 }
181
182 @Override
183 public int getWidth() {
184 if (fHorizontalIndex == 0) {
185 return 3 * Metrics.swimmingLaneWidth() + Metrics.LIFELINE_H_MAGIN * 2 - Metrics.FRAME_H_MARGIN - Metrics.LIFELINE_SPACING / 2;
186 }
187 return fHorizontalIndex * Metrics.swimmingLaneWidth() + Metrics.LIFELINE_H_MAGIN * 2 + 1 - Metrics.LIFELINE_SPACING;
188 }
189
190 @Override
191 public int getHeight() {
192 // The Frame height depends on the maximum number of messages added to a lifeline
193 if (fVerticalIndex == 0) {
194 return 5 * (Metrics.getMessagesSpacing() + Metrics.getMessageFontHeigth()) + Metrics.LIFELINE_NAME_H_MARGIN + Metrics.FRAME_NAME_H_MARGIN + Metrics.getFrameFontHeigth() + Metrics.LIFELINE_VT_MAGIN + Metrics.LIFELINE_VB_MAGIN
195 + Metrics.LIFELINE_NAME_H_MARGIN + Metrics.FRAME_NAME_H_MARGIN + Metrics.getLifelineFontHeigth() * 2;
196 }
197 if (fForceEventOccurrenceSpacing >= 0) {
198 Metrics.setForcedEventSpacing(fForceEventOccurrenceSpacing);
199 }
200 return fVerticalIndex * (Metrics.getMessagesSpacing() + Metrics.getMessageFontHeigth()) + Metrics.LIFELINE_NAME_H_MARGIN + Metrics.FRAME_NAME_H_MARGIN + Metrics.getFrameFontHeigth() + Metrics.LIFELINE_VT_MAGIN + Metrics.LIFELINE_VB_MAGIN
201 + Metrics.LIFELINE_NAME_H_MARGIN + Metrics.FRAME_NAME_H_MARGIN + Metrics.getLifelineFontHeigth() * 2;
202 }
203
204 /**
205 * @return true if mininum and maximum time needs to be calculated else false
206 */
207 protected boolean isComputeMinMax() {
208 return fComputeMinMax;
209 }
210
211 /**
212 * @return true if mininum and maximum time needs to be calculated else false
213 */
214 protected boolean isCustomMinMax() {
215 return fCustomMinMax;
216 }
217
218 /**
219 * gets the initialization flag for SD minimum.
220 *
221 * @return the initialization flag for SD minimum
222 */
223 protected boolean getInitSDMin() {
224 return fInitSDMin;
225 }
226
227 /**
228 * Returns the graph node which contains the point given in parameter for the given graph node list and starting the
229 * iteration at the given index<br>
230 * WARNING: Only graph nodes with smaller coordinates than the current visible area can be returned.<br>
231 *
232 * @param x the x coordinate of the point to test
233 * @param y the y coordinate of the point to test
234 * @param list the list to search in
235 * @param fromIndex list browsing starting point
236 * @return the graph node containing the point given in parameter, null otherwise
237 *
238 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.GraphNode#getNodeFromListAt(int, int, java.util.List, int)
239 */
240 @Override
241 protected GraphNode getNodeFromListAt(int x, int y, List<GraphNode> list, int fromIndex) {
242 if (list == null) {
243 return null;
244 }
245 for (int i = fromIndex; i < list.size(); i++) {
246 GraphNode node = list.get(i);
247 // only lifeline list is x ordered
248 // Stop browsing the list if the node is outside the visible area
249 // all others nodes will be not visible
250 if ((node instanceof Lifeline) && (node.getX() > fVisibleAreaX + fVisibleAreaWidth)) {
251 break;
252 }
253 if (node.getHeight() < 0) {
254 if (node.getY() + node.getHeight() > fVisibleAreaY + fVisibleAreaHeight) {
255 break;
256 }
257 } else {
258 if (node.getY() > fVisibleAreaY + fVisibleAreaHeight) {
259 break;
260 }
261 }
262 if (node.contains(x, y)) {
263 return node;
264 }
265 }
266 return null;
267 }
268
269 /**
270 * Draw the Frame rectangle
271 *
272 * @param context the context to draw to
273 */
274 protected void drawFrame(IGC context) {
275
276 ISDPreferences pref = SDViewPref.getInstance();
277
278 context.setBackground(pref.getBackGroundColor(ISDPreferences.PREF_FRAME));
279 context.setForeground(pref.getForeGroundColor(ISDPreferences.PREF_FRAME));
280
281 int x = getX();
282 int y = getY();
283 int w = getWidth();
284 int h = getHeight();
285
286 // Draw the frame main rectangle
287 context.fillRectangle(x, y, w, h);
288 context.drawRectangle(x, y, w, h);
289
290 context.setBackground(pref.getBackGroundColor(ISDPreferences.PREF_FRAME_NAME));
291 context.setForeground(pref.getForeGroundColor(ISDPreferences.PREF_FRAME_NAME));
292 context.setFont(pref.getFont(ISDPreferences.PREF_FRAME_NAME));
293
294 int nameWidth = context.textExtent(getName()) + 2 * Metrics.FRAME_NAME_V_MARGIN;
295 int nameHeight = Metrics.getFrameFontHeigth() + +Metrics.FRAME_NAME_H_MARGIN * 2;
296
297 // Draw the frame name area
298 if (nameWidth > w) {
299 nameWidth = w;
300 }
301
302 int[] points = { x, y, x + nameWidth, y, x + nameWidth, y - 11 + nameHeight, x - 11 + nameWidth, y + nameHeight, x, y + nameHeight, x, y + nameHeight };
303 context.fillPolygon(points);
304 context.drawPolygon(points);
305 context.drawLine(x, y, x, y + nameHeight);
306
307 context.setForeground(pref.getFontColor(ISDPreferences.PREF_FRAME_NAME));
308 context.drawTextTruncatedCentred(getName(), x, y, nameWidth - 11, nameHeight, false);
309
310 context.setBackground(pref.getBackGroundColor(ISDPreferences.PREF_FRAME));
311 context.setForeground(pref.getForeGroundColor(ISDPreferences.PREF_FRAME));
312 }
313
314 @Override
315 public void draw(IGC context) {
316 draw(context, true);
317 }
318
319 /**
320 * Draws the Frame on the given context.<br>
321 * This method start width GraphNodes ordering if needed.<br>
322 * After, depending on the visible area, only visible GraphNodes are drawn.<br>
323 *
324 * @param context the context to draw to
325 * @param drawFrame indicate if the frame rectangle need to be redrawn
326 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.GraphNode#draw(IGC)
327 */
328 protected void draw(IGC context, boolean drawFrame) {
329 fVisibleAreaHeight = context.getVisibleHeight();
330 fVisibleAreaWidth = context.getVisibleWidth();
331 fVisibleAreaX = context.getContentsX();
332 fVisibleAreaY = context.getContentsY();
333
334 if (fForceEventOccurrenceSpacing >= 0) {
335 Metrics.setForcedEventSpacing(fForceEventOccurrenceSpacing);
336 } else {
337 Metrics.setForcedEventSpacing(-1);
338 }
339
340 super.drawChildenNodes(context);
341 }
342
343 /**
344 * Sets the event occurrence spacing (-1 for none)
345 *
346 * @param space A spacing to set.
347 */
348 public void forceEventOccurrenceSpacing(int space) {
349 fForceEventOccurrenceSpacing = space;
350 }
351
352 /**
353 * Return the X coordinates of the frame visible area
354 *
355 * @return the X coordinates of the frame visible area
356 */
357 public int getVisibleAreaX() {
358 return fVisibleAreaX;
359 }
360
361 /**
362 * Return the frame visible area width
363 *
364 * @return the frame visible area width
365 */
366 public int getVisibleAreaWidth() {
367 return fVisibleAreaWidth;
368 }
369
370 /**
371 * Return the frame visible area height
372 *
373 * @return the frame visible area height
374 */
375 public int getVisibleAreaHeight() {
376 return fVisibleAreaHeight;
377 }
378
379 /**
380 * Return the X coordinates of the frame visible area
381 *
382 * @return the X coordinates of the frame visible area
383 */
384 public int getVisibleAreaY() {
385 return fVisibleAreaY;
386 }
387
388 /**
389 * Return the minimum time stored in the frame taking all GraphNodes into account
390 *
391 * @return the minimum GraphNode time
392 */
393 public ITmfTimestamp getMinTime() {
394 if (fLastExternalTimePref != SDViewPref.getInstance().excludeExternalTime()) {
395 fLastExternalTimePref = SDViewPref.getInstance().excludeExternalTime();
396 setComputeMinMax(true);
397 }
398 if ((fComputeMinMax) && (!fCustomMinMax)) {
399 computeMinMax();
400 setComputeMinMax(false);
401 }
402 return fMinTime;
403 }
404
405 /**
406 * Set the minimum timestamp of the frame.
407 *
408 * @param min
409 * The minimum timestamp
410 */
411 public void setMin(ITmfTimestamp min) {
412 fMinTime = min;
413 fCustomMinMax = true;
414 }
415
416 /**
417 * Set the maximum timestamp of the frame.
418 *
419 * @param max
420 * The maximum timestamp
421 */
422 public void setMax(ITmfTimestamp max) {
423 fMaxTime = max;
424 fCustomMinMax = true;
425 }
426
427 /**
428 * Reset min/max timestamp values to the default ones.
429 */
430 public void resetCustomMinMax() {
431 fCustomMinMax = false;
432 setComputeMinMax(true);
433 }
434
435 /**
436 * Return the maximum time stored in the frame taking all GraphNodes into account
437 *
438 * @return the maximum GraphNode time
439 */
440 public ITmfTimestamp getMaxTime() {
441 if (fLastExternalTimePref != SDViewPref.getInstance().excludeExternalTime()) {
442 fLastExternalTimePref = SDViewPref.getInstance().excludeExternalTime();
443 setComputeMinMax(true);
444 }
445 if (fComputeMinMax) {
446 computeMinMax();
447 setComputeMinMax(false);
448 }
449 return fMaxTime;
450 }
451
452 /**
453 * Computes the minimum and maximum time between consecutive messages within the frame.
454 */
455 protected void computeMaxMinTime() {
456 if (!fInitSDMin) {
457 return;
458 }
459
460 List<SDTimeEvent> timeArray = buildTimeArray();
461
462 if ((timeArray == null) || timeArray.isEmpty()) {
463 return;
464 }
465 for (int i = 0; i < timeArray.size(); i++) {
466 SDTimeEvent m = timeArray.get(i);
467
468 if (m.getTime().compareTo(fMaxSDTime) > 0) {
469 fMaxSDTime = m.getTime();
470 }
471
472 if ((m.getTime().compareTo(fMinSDTime) < 0) || fInitSDMin) {
473 fMinSDTime = m.getTime();
474 fInitSDMin = false;
475 }
476 }
477 }
478
479 /**
480 * Returns the minimum time between consecutive messages.
481 *
482 * @return the minimum time between consecutive messages
483 */
484 public ITmfTimestamp getSDMinTime() {
485 computeMaxMinTime();
486 return fMinSDTime;
487 }
488
489 /**
490 * Returns the maximum time between consecutive messages.
491 *
492 * @return the maximum time between consecutive messages
493 */
494 public ITmfTimestamp getSDMaxTime() {
495 computeMaxMinTime();
496 return fMaxSDTime;
497 }
498
499 /**
500 * Browse all the GraphNode to compute the min and max times store in the Frame
501 */
502 protected void computeMinMax() {
503 List<SDTimeEvent> timeArray = buildTimeArray();
504
505 if ((timeArray == null) || timeArray.isEmpty()) {
506 return;
507 }
508 for (int i = 0; i < timeArray.size() - 1; i++) {
509 SDTimeEvent m1 = timeArray.get(i);
510 SDTimeEvent m2 = timeArray.get(i + 1);
511
512 updateMinMax(m1, m2);
513 }
514 }
515
516 /**
517 * Updates the minimum and maximum time between consecutive message within the frame based on the given values.
518 *
519 * @param m1 A first SD time event.
520 * @param m2 A second SD time event.
521 */
522 protected void updateMinMax(SDTimeEvent m1, SDTimeEvent m2) {
523 ITmfTimestamp delta = m2.getTime().getDelta(m1.getTime());
524 if (fComputeMinMax) {
525 fMinTime = delta;
526 if (fMinTime.compareTo(TmfTimestamp.ZERO) < 0) {
527 fMinTime = new TmfTimestamp(0, m1.getTime().getScale());
528 }
529 fMaxTime = fMinTime;
530 setComputeMinMax(false);
531 }
532
533 if ((delta.compareTo(fMinTime) < 0) && (delta.compareTo(TmfTimestamp.ZERO) > 0)) {
534 fMinTime = delta;
535 }
536
537 if ((delta.compareTo(fMaxTime) > 0) && (delta.compareTo(TmfTimestamp.ZERO) > 0)) {
538 fMaxTime = delta;
539 }
540 }
541
542 /**
543 * Builds the time array based on the list of graph nodes.
544 *
545 * @return the time array else empty list.
546 */
547 protected List<SDTimeEvent> buildTimeArray() {
548 if (!hasChildren()) {
549 return new ArrayList<>();
550 }
551
552 Iterator<String> it = getForwardSortMap().keySet().iterator();
553 List<SDTimeEvent> timeArray = new ArrayList<>();
554 while (it.hasNext()) {
555 String nodeType = it.next();
556 List<GraphNode> list = getNodeMap().get(nodeType);
557 for (int i = 0; i < list.size(); i++) {
558 Object timedNode = list.get(i);
559 if ((timedNode instanceof ITimeRange) && ((ITimeRange) timedNode).hasTimeInfo()) {
560 int event = list.get(i).getStartOccurrence();
561 ITmfTimestamp time = ((ITimeRange) list.get(i)).getStartTime();
562 SDTimeEvent f = new SDTimeEvent(time, event, (ITimeRange) list.get(i));
563 timeArray.add(f);
564 if (event != list.get(i).getEndOccurrence()) {
565 event = (list.get(i)).getEndOccurrence();
566 time = ((ITimeRange) list.get(i)).getEndTime();
567 f = new SDTimeEvent(time, event, (ITimeRange) list.get(i));
568 timeArray.add(f);
569 }
570 }
571 }
572 }
573 return timeArray;
574 }
575
576 @Override
577 public String getArrayId() {
578 return null;
579 }
580
581 @Override
582 public boolean contains(int x, int y) {
583 return false;
584 }
585
586 /**
587 * @return true if frame has time info else false
588 */
589 public boolean hasTimeInfo() {
590 return fHasTimeInfo;
591 }
592
593 /**
594 * Sets the flag whether the frame has time info or not
595 *
596 * @param hasTimeInfo
597 * true if frame has time info else false
598 */
599 public void setHasTimeInfo(boolean hasTimeInfo) {
600 fHasTimeInfo = hasTimeInfo;
601 }
602
603 /**
604 * Sets the flag for minimum and maximum computation.
605 *
606 * @param computeMinMax
607 * true if mininum and maximum time needs to be calculated else false
608 */
609 public void setComputeMinMax(boolean computeMinMax) {
610 fComputeMinMax = computeMinMax;
611 }
612
613 /**
614 * Sets the initialization flag for SD minimum.
615 *
616 * @param initSDMin
617 * the flag to set
618 */
619 public void setInitSDMin(boolean initSDMin) {
620 fInitSDMin = initSDMin;
621 }
622 }
This page took 0.056839 seconds and 4 git commands to generate.