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