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