d8aa8f7b73196265b208241a78f78c8ccaa73d94
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / uml2sd / core / GraphNode.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.Arrays;
17 import java.util.Comparator;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.tracecompass.internal.tmf.ui.TmfUiTracer;
24 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.drawings.IGC;
25 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.ISDPreferences;
26 import org.eclipse.tracecompass.tmf.ui.views.uml2sd.preferences.SDViewPref;
27
28 /**
29 * The base class used for all UML2 graph nodes displayed in the Sequence Diagram SDWidget.
30 *
31 * @author sveyrier
32 * @version 1.0
33 */
34 public abstract class GraphNode {
35
36 private static final String UI_DELIMITER = "*****************************\n"; //$NON-NLS-1$
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40 /**
41 * The start event occurrence.
42 */
43 private int fStartEventOccurrence = 0;
44 /**
45 * The event event occurrence.
46 */
47 private int fEndEventOccurrence = 0;
48 /**
49 * Preference ColorId to use to draw font
50 */
51 private String fPrefId = ISDPreferences.PREF_SYNC_MESS;
52 /**
53 * The selection state of the graph node.
54 */
55 private boolean fSelected = false;
56 /**
57 * The focus state of the graph node.
58 */
59 private boolean fFocused = false;
60 /**
61 * Flag to indicate whether node has children or not.
62 */
63 private boolean fHasChilden = false;
64 /**
65 * The graph node name used to label the graph node in the View.
66 */
67 private String fName = ""; //$NON-NLS-1$
68 /**
69 * A map from node name to graph node.
70 */
71 private Map<String, List<GraphNode>> fNodes;
72 /**
73 * A map from node name to graph node for forward sorting
74 */
75 private Map<String, List<GraphNode>> fForwardNodes;
76 /**
77 * A map from node name to graph node for backwards sorting.
78 */
79 private Map<String, List<GraphNode>> fBackwardNodes;
80 /**
81 * A map from node name to index.
82 */
83 private Map<String, Integer> fIndexes;
84 /**
85 * A map from node name to flag for forwards sorting.
86 */
87 private Map<String, Boolean> fForwardSort;
88 /**
89 * A map from node name to flag for backwards sorting.
90 */
91 private Map<String, Boolean> fBackwardSort;
92
93 // ------------------------------------------------------------------------
94 // Methods
95 // ------------------------------------------------------------------------
96
97 /**
98 * Reset the internal index of the first visible GraphNode for each ordered GraphNode lists
99 */
100 public void resetIndex() {
101 if (!fHasChilden) {
102 return;
103 }
104
105 Iterator<String> it = fIndexes.keySet().iterator();
106 while (it.hasNext()) {
107 String nodeType = it.next();
108 fIndexes.put(nodeType, Integer.valueOf(0));
109 }
110 }
111
112 /**
113 * Add a GraphNode into the receiver
114 *
115 * @param nodeToAdd the node to add
116 */
117 public void addNode(GraphNode nodeToAdd) {
118 if (!fHasChilden) {
119 fNodes = new HashMap<>(2);
120 fForwardNodes = new HashMap<>(2);
121 fBackwardNodes = new HashMap<>(2);
122 fIndexes = new HashMap<>(2);
123 fBackwardSort = new HashMap<>(2);
124 fForwardSort = new HashMap<>(2);
125 fHasChilden = true;
126 }
127
128 // Nothing to add
129 if (nodeToAdd == null) {
130 return;
131 }
132
133 if (fNodes.get(nodeToAdd.getArrayId()) == null) {
134 fNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
135 fIndexes.put(nodeToAdd.getArrayId(), Integer.valueOf(0));
136 fForwardNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
137 fForwardSort.put(nodeToAdd.getArrayId(), Boolean.FALSE);
138 if (nodeToAdd.getBackComparator() != null) {
139 fBackwardNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
140 fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.FALSE);
141 }
142 }
143
144 List<GraphNode> fNodeList = fForwardNodes.get(nodeToAdd.getArrayId());
145 List<GraphNode> bNodeList = null;
146 if (fBackwardNodes != null) {
147 bNodeList = fBackwardNodes.get(nodeToAdd.getArrayId());
148 }
149 if (fNodeList != null && fNodeList.size() > 0) {
150 // check if the nodes are added y ordered
151 // if not, tag the list to sort it later (during draw)
152 GraphNode node = fNodeList.get(fNodeList.size() - 1);
153 Comparator<GraphNode> fcomp = nodeToAdd.getComparator();
154 Comparator<GraphNode> bcomp = nodeToAdd.getBackComparator();
155 if ((fcomp != null) && (fcomp.compare(node, nodeToAdd) > 0)) {
156 fForwardSort.put(nodeToAdd.getArrayId(), Boolean.TRUE);
157 }
158 if ((bcomp != null) && (bcomp.compare(node, nodeToAdd) > 0)) {
159 fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.TRUE);
160 }
161 }
162
163 if (fNodeList == null) {
164 fNodeList = new ArrayList<>();
165 }
166
167 fNodeList.add(nodeToAdd);
168 fNodes.put(nodeToAdd.getArrayId(), fNodeList);
169 fForwardNodes.put(nodeToAdd.getArrayId(), fNodeList);
170 if ((bNodeList != null) && (nodeToAdd.getBackComparator() != null)) {
171 bNodeList.add(nodeToAdd);
172 fBackwardNodes.put(nodeToAdd.getArrayId(), bNodeList);
173 }
174 }
175
176 /**
177 * Set the graph node name.<br>
178 * It is the name display in the view to label the graph node.
179 *
180 * @param nodeName the name to set
181 */
182 public void setName(String nodeName) {
183 fName = nodeName;
184 }
185
186 /**
187 * Returns the graph node name.<br>
188 * It is the name display in the view to label the graph node.
189 *
190 * @return the graph node name
191 */
192 public String getName() {
193 return fName;
194 }
195
196 /**
197 * Tags the the graph node has selected.<br>
198 * WARNING: This method is only used to draw the graph node using the system selection colors. <br>
199 * To use the complete SDViewer selection mechanism (selection management, notification, etc..) see SDWidget class
200 *
201 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#addSelection(GraphNode)
202 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#removeSelection(GraphNode)
203 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#clearSelection()
204 * @param selection - true to set selected, false to set unselected
205 */
206 public void setSelected(boolean selection) {
207 fSelected = selection;
208 }
209
210 /**
211 * Tags the the graph node as focused.<br>
212 * WARNING: This method is only used to draw the graph node using the system focus style. <br>
213 * To use the complete SDViewer focus mechanism see SDWidget class
214 *
215 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#addSelection(GraphNode)
216 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#removeSelection(GraphNode)
217 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.SDWidget#clearSelection()
218 * @param focus - true to set focued, false otherwise
219 */
220 public void setFocused(boolean focus) {
221 fFocused = focus;
222 }
223
224 /**
225 * Returns true if the graph node is selected, false otherwise.<br>
226 * The returned value is used to highlight the graph node in the View.
227 *
228 * @return true if selected, false otherwise
229 */
230 public boolean isSelected() {
231 return fSelected;
232 }
233
234 /**
235 * Returns true if the graph node is focused, false otherwise.<br>
236 * The returned value is used to highlight the graph node in the View.
237 *
238 * @return true if focused, false otherwise
239 */
240 public boolean hasFocus() {
241 return fFocused;
242 }
243
244 /**
245 * Returns true if the graph node contains the point given in parameter,
246 * return false otherwise.
247 *
248 * @param x
249 * the x coordinate of the point to test containment
250 * @param y
251 * the y coordinate of the point to test containment
252 * @return true if contained, false otherwise
253 */
254 public abstract boolean contains(int x, int y);
255
256 /**
257 * Returns the x coordinate of the graph node
258 *
259 * @return the x coordinate
260 */
261 public abstract int getX();
262
263 /**
264 * Returns the y coordinate of the graph node
265 *
266 * @return the y coordinate
267 */
268 public abstract int getY();
269
270 /**
271 * Returns the graph node height
272 *
273 * @return the graph node height
274 */
275 public abstract int getHeight();
276
277 /**
278 * Returns the graph node width
279 *
280 * @return the graph node width
281 */
282 public abstract int getWidth();
283
284 /**
285 * Draws the graph node in the given context
286 *
287 * @param context the graphical context to draw in
288 */
289 protected abstract void draw(IGC context);
290
291 /**
292 * Returns the GraphNode visibility for the given visible area. Wrong
293 * visibility calculation, may strongly impact drawing performance
294 *
295 * @param x
296 * The X coordinate
297 * @param y
298 * The Y coordinate
299 * @param width
300 * The width of the area
301 * @param height
302 * The height of the area
303 * @return true if visible, false otherwise
304 */
305 public boolean isVisible(int x, int y, int width, int height) {
306 return true;
307 }
308
309 /**
310 * Return a comparator to sort the GraphNode of the same type This
311 * comparator is used to order the GraphNode array of the given node type.
312 * (see getArrayId).
313 *
314 * @return the comparator
315 */
316 public Comparator<GraphNode> getComparator() {
317 return null;
318 }
319
320 /**
321 * If needed, return a different comparator to backward scan the GraphNode
322 * array
323 *
324 * @return the backward comparator or null if not needed
325 */
326 public Comparator<GraphNode> getBackComparator() {
327 return null;
328 }
329
330 /**
331 * Compare two graphNodes
332 *
333 * @param node
334 * the node to compare to
335 * @return true if equal false otherwise
336 */
337 public boolean isSameAs(GraphNode node) {
338 return false;
339 }
340
341 /**
342 * Return the node type for all class instances. This id is used to store the same nodes kind in the same ordered
343 * array.
344 *
345 * @return the node type identifier
346 */
347 public abstract String getArrayId();
348
349 /**
350 * Return true if the distance from the GraphNode to the given point is positive
351 *
352 * @param x the point x coordinate
353 * @param y the point y coordinate
354 * @return true if positive false otherwise
355 */
356 public boolean positiveDistanceToPoint(int x, int y) {
357 return false;
358 }
359
360 /**
361 * Returns the graph node which contains the point given in parameter WARNING: Only graph nodes in the current
362 * visible area can be returned
363 *
364 * @param x the x coordinate of the point to test
365 * @param y the y coordinate of the point to test
366 * @return the graph node containing the point given in parameter, null otherwise
367 */
368 public GraphNode getNodeAt(int x, int y) {
369 GraphNode toReturn = null;
370
371 if (!fHasChilden) {
372 return null;
373 }
374
375 GraphNode node = null;
376 for (Map.Entry<String, List<GraphNode>> entry : fNodes.entrySet()) {
377 List<GraphNode> list = entry.getValue();
378 int index = fIndexes.get(entry.getKey()).intValue();
379 node = getNodeFromListAt(x, y, list, index);
380 if (toReturn == null) {
381 toReturn = node;
382 }
383 if (node != null) {
384 GraphNode internalNode = node.getNodeAt(x, y);
385 if (internalNode != null) {
386 return internalNode;
387 } else if (Math.abs(node.getWidth()) < Math.abs(toReturn.getWidth()) || Math.abs(node.getHeight()) < Math.abs(toReturn.getHeight())) {
388 toReturn = node;
389 }
390 }
391 }
392 return toReturn;
393 }
394
395 /**
396 * Gets node list from node A to node B
397
398 * @param from A from node
399 * @param to A to node
400 * @return the list of nodes
401 */
402 public List<GraphNode> getNodeList(GraphNode from, GraphNode to) {
403 List<GraphNode> result = new ArrayList<>();
404
405 if (from != null) {
406 result.add(from);
407 } else if (to != null) {
408 result.add(to);
409 }
410
411 if ((from == null) || (to == null)) {
412 return result;
413 }
414
415 if (from == to) {
416 return result;
417 }
418
419 int startX = Math.min(from.getX(), Math.min(to.getX(), Math.min(from.getX() + from.getWidth(), to.getX() + to.getWidth())));
420 int endX = Math.max(from.getX(), Math.max(to.getX(), Math.max(from.getX() + from.getWidth(), to.getX() + to.getWidth())));
421 int startY = Math.min(from.getY(), Math.min(to.getY(), Math.min(from.getY() + from.getHeight(), to.getY() + to.getHeight())));
422 int endY = Math.max(from.getY(), Math.max(to.getY(), Math.max(from.getY() + from.getHeight(), to.getY() + to.getHeight())));
423
424 if (!fHasChilden) {
425 return result;
426 }
427
428 for (Map.Entry<String, List<GraphNode>> entry : fNodes.entrySet()) {
429 List<GraphNode> nodesList = entry.getValue();
430 if (nodesList == null || nodesList.isEmpty()) {
431 return null;
432 }
433 for (int i = 0; i < nodesList.size(); i++) {
434 GraphNode node = nodesList.get(i);
435 int nw = node.getWidth();
436 int nh = node.getHeight();
437 int nx = node.getX();
438 int ny = node.getY();
439 if (contains(startX, startY, endX - startX, endY - startY, nx + 1, ny + 1) && contains(startX, startY, endX - startX, endY - startY, nx + nw - 2, ny + nh - 2)) {
440 result.add(node);
441 }
442 result.addAll(node.getNodeList(from, to));
443 }
444 }
445
446 if (!result.contains(to)) {
447 result.add(to);
448 }
449 return result;
450 }
451
452 /**
453 * Returns the graph node which contains the point given in parameter for the given graph node list and starting the
454 * iteration at the given index<br>
455 * WARNING: Only graph nodes with smaller coordinates than the current visible area can be returned.<br>
456 *
457 * @param x the x coordinate of the point to test
458 * @param y the y coordinate of the point to test
459 * @param list the list to search in
460 * @param fromIndex list browsing starting point
461 * @return the graph node containing the point given in parameter, null otherwise
462 */
463 protected GraphNode getNodeFromListAt(int x, int y, List<GraphNode> list, int fromIndex) {
464 if (list == null) {
465 return null;
466 }
467 for (int i = fromIndex; i < list.size(); i++) {
468 GraphNode node = list.get(i);
469 if (node.contains(x, y)) {
470 return node;
471 }
472 }
473 return null;
474 }
475
476 /**
477 * Returns the start event occurrence attached to this graphNode.
478 *
479 * @return the start event occurrence attached to the graphNode
480 */
481 public int getStartOccurrence() {
482 return fStartEventOccurrence;
483 }
484
485 /**
486 * Returns the end event occurrence attached to this graphNode
487 *
488 * @return the start event occurrence attached to the graphNode
489 */
490 public int getEndOccurrence() {
491 return fEndEventOccurrence;
492 }
493
494 /**
495 * Computes the index of the first visible GraphNode for each ordered graph node lists depending on the visible area
496 * given in parameter
497 *
498 * @param x visible area top left corner x coordinate
499 * @param y visible area top left corner y coordinate
500 * @param width visible area width
501 * @param height visible area height
502 */
503 public void updateIndex(int x, int y, int width, int height) {
504 if (!fHasChilden) {
505 return;
506 }
507 if(TmfUiTracer.isIndexTraced()) {
508 TmfUiTracer.traceIndex(UI_DELIMITER);
509 TmfUiTracer.traceIndex("Visible area position in virtual screen (x,y)= " + x + " " + y + "\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
510 }
511
512 for (Map.Entry<String, List<GraphNode>> entry : fNodes.entrySet()) {
513 String nodeType = entry.getKey();
514 int direction = 1;
515 int drawIndex = fIndexes.get(nodeType).intValue();
516 /*
517 * if (x==0) { drawIndex = 0; indexes.put(nodeType,new Integer(drawIndex)); }
518 */
519 if ((entry.getValue() != null) && (entry.getValue().size() > 1)) {
520 if (entry.getValue().get(drawIndex).positiveDistanceToPoint(x, y)) {
521 direction = -1;
522 }
523
524 if (drawIndex == 0) {
525 direction = 1;
526 }
527
528 if ((direction == -1) && (fBackwardNodes.get(nodeType) != null)) {
529 GraphNode currentNode = entry.getValue().get(drawIndex);
530 drawIndex = Arrays.binarySearch(fBackwardNodes.get(nodeType).toArray(new GraphNode[fBackwardNodes.get(nodeType).size()]),
531 entry.getValue().get(drawIndex), currentNode.getBackComparator());
532 entry.setValue(fBackwardNodes.get(nodeType));
533 if (drawIndex < 0) {
534 drawIndex = 0;
535 direction = 1;
536 } else {
537 entry.setValue(fBackwardNodes.get(nodeType));
538 }
539 }
540 GraphNode prev = null;
541
542 for (int i = drawIndex; i < entry.getValue().size() && i >= 0; i = i + direction) {
543 drawIndex = i;
544 fIndexes.put(nodeType, Integer.valueOf(i));
545
546 GraphNode currentNode = entry.getValue().get(i);
547
548 if (prev == null) {
549 prev = currentNode;
550 }
551
552 Comparator<GraphNode> comp = currentNode.getComparator();
553 Map<String, Boolean> sort = fForwardSort;
554
555 if ((direction == -1) && (currentNode.getBackComparator() != null)) {
556 comp = currentNode.getBackComparator();
557 sort = fBackwardSort;
558 }
559
560 if (i < entry.getValue().size() - 1) {
561 GraphNode next = entry.getValue().get(i + 1);
562
563 if ((comp != null) && (comp.compare(currentNode, next) > 0)) {
564 sort.put(nodeType, Boolean.TRUE);
565 }
566 }
567 if (direction == 1) {
568 if (entry.getValue().get(i).positiveDistanceToPoint(x, y)) {
569 break;
570 }
571 } else {
572 if (currentNode.getBackComparator() == null) {
573 if // (currentNode.isVisible(x,y,width,height)
574 (!currentNode.positiveDistanceToPoint(x, y)) {
575 break;
576 }
577 } else {
578 if (currentNode.isVisible(x, y, width, height) && !currentNode.positiveDistanceToPoint(x, y)) {
579 if ((comp != null) && (comp.compare(currentNode, prev) <= 0)) {
580 break;
581 }
582 } else if ((comp != null) && (comp.compare(currentNode, prev) <= 0)) {
583 prev = currentNode;
584 }
585 }
586 }
587 }
588
589 entry.setValue(fForwardNodes.get(nodeType));
590 if ((fBackwardNodes.get(nodeType) != null) && (direction == -1)) {
591 // nodes.put(nodeType,fnodes.get(nodeType));
592 int index = fIndexes.get(nodeType).intValue();
593 List<GraphNode> list = entry.getValue();
594 List<GraphNode> backList = fBackwardNodes.get(nodeType);
595 GraphNode currentNode = (backList.get(index));
596 if (index > 0) {
597 index = Arrays.binarySearch(list.toArray(new GraphNode[list.size()]), backList.get(index), currentNode.getComparator());
598 if (index < 0) {
599 index = 0;
600 }
601 fIndexes.put(nodeType, Integer.valueOf(index));
602 }
603 }
604
605 for (int i = drawIndex; i < entry.getValue().size() && i >= 0; i++) {
606 GraphNode toDraw = entry.getValue().get(i);
607 toDraw.updateIndex(x, y, width, height);
608 if (!toDraw.isVisible(x, y, width, height)) {
609 break;
610 }
611 }
612 }
613 if (TmfUiTracer.isIndexTraced()) {
614 TmfUiTracer.traceIndex("First drawn " + nodeType + " index = " + drawIndex + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
615 TmfUiTracer.traceIndex(nodeType + " found in " + 0 + " iterations\n"); //$NON-NLS-1$ //$NON-NLS-2$
616 }
617 }
618
619 if (TmfUiTracer.isIndexTraced()) {
620 TmfUiTracer.traceIndex(UI_DELIMITER);
621 }
622 }
623
624 /**
625 * Draws the children nodes on the given context.<br>
626 * This method start width GraphNodes ordering if needed.<br>
627 * After, depending on the visible area, only visible GraphNodes are drawn.<br>
628 *
629 * @param context the context to draw to
630 * @see org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.GraphNode#draw(IGC)
631 */
632 protected void drawChildenNodes(IGC context) {
633
634 if (!fHasChilden) {
635 return;
636 }
637 // If the nodes have not been added ordered, the array is ordered
638 for (Map.Entry<String, Boolean> entry : fForwardSort.entrySet()) {
639 String nodeType = entry.getKey();
640 boolean sort = entry.getValue().booleanValue();
641 if (sort) {
642 GraphNode[] temp = fForwardNodes.get(nodeType).toArray(new GraphNode[fForwardNodes.get(nodeType).size()]);
643 GraphNode node = fNodes.get(nodeType).get(0);
644 Arrays.sort(temp, node.getComparator());
645 entry.setValue(Boolean.FALSE);
646 fNodes.put(nodeType, Arrays.asList(temp));
647 fForwardNodes.put(nodeType, Arrays.asList(temp));
648 if (TmfUiTracer.isSortingTraced()) {
649 TmfUiTracer.traceSorting(nodeType + " array sorted\n"); //$NON-NLS-1$
650 }
651 }
652 }
653
654 for (Map.Entry<String, Boolean> entry : fBackwardSort.entrySet()) {
655 String nodeType = entry.getKey();
656 boolean sort = entry.getValue().booleanValue();
657 if (sort) {
658 GraphNode[] temp = fBackwardNodes.get(nodeType).toArray(new GraphNode[fBackwardNodes.get(nodeType).size()]);
659 GraphNode node = fNodes.get(nodeType).get(0);
660 Arrays.sort(temp, node.getBackComparator());
661 entry.setValue(Boolean.FALSE);
662 fBackwardNodes.put(nodeType, Arrays.asList(temp));
663 if (TmfUiTracer.isSortingTraced()) {
664 TmfUiTracer.traceSorting(nodeType + " back array sorted\n"); //$NON-NLS-1$
665 }
666 }
667 }
668
669 if (TmfUiTracer.isDisplayTraced()) {
670 TmfUiTracer.traceDisplay(UI_DELIMITER);
671 }
672
673 int arrayStep = 1;
674 if ((Metrics.getMessageFontHeigth() + Metrics.MESSAGES_NAME_SPACING * 2) * context.getZoom() < Metrics.MESSAGE_SIGNIFICANT_VSPACING) {
675 arrayStep = Math.round(Metrics.MESSAGE_SIGNIFICANT_VSPACING / ((Metrics.getMessageFontHeigth() + Metrics.MESSAGES_NAME_SPACING * 2) * context.getZoom()));
676 }
677
678 int count = 0;
679 Iterator<String> it3 = fForwardSort.keySet().iterator();
680 while (it3.hasNext()) {
681 count = 0;
682 Object nodeType = it3.next();
683 GraphNode node = fNodes.get(nodeType).get(0);
684 context.setFont(SDViewPref.getInstance().getFont(node.fPrefId));
685 int index = fIndexes.get(nodeType).intValue();
686 count = drawNodes(context, fNodes.get(nodeType), index, arrayStep);
687 if (TmfUiTracer.isDisplayTraced()) {
688 TmfUiTracer.traceDisplay(count + " " + nodeType + " drawn, starting from index " + index + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
689 }
690 }
691 if (TmfUiTracer.isDisplayTraced()) {
692 TmfUiTracer.traceDisplay(UI_DELIMITER);
693 }
694
695 }
696
697 /**
698 * Draw the GraphNode stored in the given list, starting at index startIndex with the given step
699 *
700 * @param context the context to draw to
701 * @param list the GraphNodes list
702 * @param startIndex the start index
703 * @param step the step to browse the list
704 * @return the number of GraphNodes drawn
705 */
706 protected int drawNodes(IGC context, List<GraphNode> list, int startIndex, int step) {
707 if (!fHasChilden) {
708 return 0;
709 }
710
711 GraphNode last = null;
712 int nodesCount = 0;
713 if (list.isEmpty()) {
714 return 0;
715 }
716
717 GraphNode node = list.get(0);
718 context.setFont(SDViewPref.getInstance().getFont(node.fPrefId));
719 Comparator<GraphNode> comparator = node.getComparator();
720 for (int i = startIndex; i < list.size(); i = i + step) {
721 GraphNode toDraw = list.get(i);
722 if (i < list.size() - 1) {
723 GraphNode next = list.get(i + 1);
724 if ((comparator != null) && (comparator.compare(toDraw, next) > 0)) {
725 fForwardSort.put(next.getArrayId(), Boolean.TRUE);
726 }
727 }
728 int cx = context.getContentsX();
729 int cy = context.getContentsY();
730 int cw = context.getVisibleWidth();
731 int ch = context.getVisibleHeight();
732 // The arrays should be ordered, no needs to continue for this one
733 if (!toDraw.isVisible(cx, cy, cw, ch) && toDraw.positiveDistanceToPoint(cx + cw, cy + ch)) {
734 break;
735 }
736 // ***Common*** nodes visibility
737 if ((!toDraw.isSameAs(last) || toDraw.isSelected()) && (toDraw.isVisible(context.getContentsX(), context.getContentsY(), context.getVisibleWidth(), context.getVisibleHeight()))) {
738 nodesCount++;
739
740 toDraw.draw(context);
741 if (hasFocus()) {
742 toDraw.drawFocus(context);
743 }
744 }
745 last = toDraw;
746 }
747 return nodesCount;
748 }
749
750 /**
751 * Draws the focus within the graphical context.
752 *
753 * @param context
754 * The context
755 */
756 public void drawFocus(IGC context) {
757 context.drawFocus(getX(), getY(), getWidth(), getHeight());
758 }
759
760 /**
761 * Determine if the given point (px,py) is contained in the rectangle (x,y,width,height)
762 *
763 * @param x the rectangle x coordinate
764 * @param y the rectangle y coordinate
765 * @param width the rectangle width
766 * @param height the rectangle height
767 * @param px the x coordinate of the point to test
768 * @param py the y coordinate of the point to test
769 * @return true if contained false otherwise
770 */
771 public static boolean contains(int x, int y, int width, int height, int px, int py) {
772 int locX = x;
773 int locY = y;
774 int locWidth = width;
775 int locHeight = height;
776
777 if (width < 0) {
778 locX = locX + width;
779 locWidth = -locWidth;
780 }
781
782 if (height < 0) {
783 locY = locY + height;
784 locHeight = -locHeight;
785 }
786 return (px >= locX) && (py >= locY) && ((px - locX) <= locWidth) && ((py - locY) <= locHeight);
787 }
788
789 /**
790 * Sets the start event occurrence attached to this graphNode.
791 *
792 * @param occurence
793 * the start event occurrence attached to the graphNode
794 */
795 protected void setStartOccurrence(int occurence) {
796 fStartEventOccurrence = occurence;
797 }
798
799 /**
800 * Sets the end event occurrence attached to this graphNode
801 *
802 * @param occurence
803 * the start event occurrence attached to the graphNode
804 */
805 protected void setEndOccurrence(int occurence) {
806 fEndEventOccurrence = occurence;
807 }
808
809 /**
810 * Sets the color preference id
811 * @param id
812 * The color preference id
813 */
814 protected void setColorPrefId(String id) {
815 fPrefId = id;
816 }
817
818 /**
819 * Gets the color preference id
820 * @return the color preference id
821 */
822 protected String getColorPrefId() {
823 return fPrefId;
824 }
825
826 /**
827 * @return if node has children or not
828 */
829 protected boolean hasChildren() {
830 return fHasChilden;
831 }
832
833 /**
834 * Sets the flag indicating where the node has children or not.
835 * @param hasChildren
836 * if node has children or not
837 */
838 protected void hasChildren(boolean hasChildren) {
839 fHasChilden = hasChildren;
840 }
841 /**
842 * Returns a map from node name to graph node.
843 *
844 * @return map with children graph bodes
845 */
846 protected Map<String, List<GraphNode>> getNodeMap() {
847 return fNodes;
848 }
849 /**
850 * Returns a map from node name to graph node for forward sorting
851 *
852 * @return forward sorting map
853 */
854 protected Map<String, List<GraphNode>> getForwardNodes() {
855 return fForwardNodes;
856 }
857 /**
858 * Returns a map from node name to graph node for backwards sorting.
859 *
860 * @return backwards sorting map
861 */
862 protected Map<String, List<GraphNode>> getBackwardNodes() {
863 return fBackwardNodes;
864 }
865 /**
866 * Returns a map from node name to index.
867 *
868 * @return map with node name to index
869 */
870 protected Map<String, Integer> getIndexes() {
871 return fIndexes;
872 }
873
874 /**
875 * Returns a map from node name to sort flag for forwards sorting.
876 * @return a map from node name to sort flag
877 */
878 protected Map<String, Boolean> getForwardSortMap() {
879 return fForwardSort;
880 }
881 /**
882 * Returns a map from node name to flag for backwards sorting.
883 * @return map from node name to flag for backwards sorting.
884 */
885 protected Map<String, Boolean> getBackwardSortMap() {
886 return fBackwardSort;
887 }
888 }
This page took 0.050101 seconds and 4 git commands to generate.