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