Fix static analysis warnings for UML2SD
[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 import java.util.Map;
23
24 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiTracer;
25 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC;
26 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.ISDPreferences;
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 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39 /**
40 * The start event occurrence.
41 */
42 protected int fStartEventOccurrence = 0;
43 /**
44 * The event event occurrence.
45 */
46 protected int fEndEventOccurrence = 0;
47 /**
48 * Preference ColorId to use to draw font
49 */
50 public String fPrefId = ISDPreferences.PREF_SYNC_MESS;
51 /**
52 * The selection state of the graph node.
53 */
54 protected boolean fSelected = false;
55 /**
56 * The focus state of the graph node.
57 */
58 protected boolean fFocused = false;
59 /**
60 * Flag to indicate whether node has children or not.
61 */
62 protected boolean fHasChilden = false;
63 /**
64 * The graph node name used to label the graph node in the View.
65 */
66 protected String fName = ""; //$NON-NLS-1$
67 /**
68 * A map from node name to graph node.
69 */
70 protected Map<String, List<GraphNode>> fNodes;
71 /**
72 * A map from node name to graph node for forward sorting
73 */
74 protected Map<String, List<GraphNode>> fForwardNodes;
75 /**
76 * A map from node name to graph node for backwards sorting.
77 */
78 protected Map<String, List<GraphNode>> fBackwardNodes;
79 /**
80 * A map from node name to index.
81 */
82 protected Map<String, Integer> fIndexes;
83 /**
84 * A map from node name to index for forwards sorting.
85 */
86 protected Map<String, Boolean> fForwardSort;
87 /**
88 * A map from node name to index for forwards sorting.
89 */
90 protected Map<String, Boolean> fBackwardSort;
91
92 // ------------------------------------------------------------------------
93 // Methods
94 // ------------------------------------------------------------------------
95
96 /**
97 * Reset the internal index of the first visible GraphNode for each ordered GraphNode lists
98 */
99 public void resetIndex() {
100 if (!fHasChilden) {
101 return;
102 }
103
104 Iterator<String> it = fIndexes.keySet().iterator();
105 while (it.hasNext()) {
106 String nodeType = it.next();
107 fIndexes.put(nodeType, Integer.valueOf(0));
108 }
109 }
110
111 /**
112 * Add a GraphNode into the receiver
113 *
114 * @param nodeToAdd the node to add
115 */
116 public void addNode(GraphNode nodeToAdd) {
117 if (!fHasChilden) {
118 fNodes = new HashMap<String, List<GraphNode>>(2);
119 fForwardNodes = new HashMap<String, List<GraphNode>>(2);
120 fBackwardNodes = new HashMap<String, List<GraphNode>>(2);
121 fIndexes = new HashMap<String, Integer>(2);
122 fBackwardSort = new HashMap<String, Boolean>(2);
123 fForwardSort = new HashMap<String, Boolean>(2);
124 fHasChilden = true;
125 }
126
127 // Nothing to add
128 if (nodeToAdd == null) {
129 return;
130 }
131
132 if (fNodes.get(nodeToAdd.getArrayId()) == null) {
133 fNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
134 fIndexes.put(nodeToAdd.getArrayId(), Integer.valueOf(0));
135 fForwardNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
136 fForwardSort.put(nodeToAdd.getArrayId(), Boolean.valueOf(false));
137 if (nodeToAdd.getBackComparator() != null) {
138 fBackwardNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1));
139 fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.valueOf(false));
140 }
141 }
142
143 List<GraphNode> fNodeList = (List<GraphNode>) fForwardNodes.get(nodeToAdd.getArrayId());
144 List<GraphNode> bNodeList = null;
145 if (fBackwardNodes != null) {
146 bNodeList = (List<GraphNode>) fBackwardNodes.get(nodeToAdd.getArrayId());
147 }
148 if (fNodeList != null && fNodeList.size() > 0) {
149 // check if the nodes are added y ordered
150 // if not, tag the list to sort it later (during draw)
151 GraphNode node = (GraphNode) fNodeList.get(fNodeList.size() - 1);
152 Comparator<GraphNode> fcomp = nodeToAdd.getComparator();
153 Comparator<GraphNode> bcomp = nodeToAdd.getBackComparator();
154 if ((fcomp != null) && (fcomp.compare(node, nodeToAdd) > 0)) {
155 fForwardSort.put(nodeToAdd.getArrayId(), Boolean.valueOf(true));
156 }
157 if ((bcomp != null) && (bcomp.compare(node, nodeToAdd) > 0)) {
158 fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.valueOf(true));
159 }
160 }
161
162 if (fNodeList == null) {
163 fNodeList = new ArrayList<GraphNode>();
164 }
165
166 fNodeList.add(nodeToAdd);
167 fNodes.put(nodeToAdd.getArrayId(), fNodeList);
168 fForwardNodes.put(nodeToAdd.getArrayId(), fNodeList);
169 if (nodeToAdd.getBackComparator() != null) {
170 bNodeList.add(nodeToAdd);
171 fBackwardNodes.put(nodeToAdd.getArrayId(), bNodeList);
172 }
173 }
174
175 /**
176 * Set the graph node name.<br>
177 * It is the name display in the view to label the graph node.
178 *
179 * @param nodeName the name to set
180 */
181 public void setName(String nodeName) {
182 fName = nodeName;
183 }
184
185 /**
186 * Returns the graph node name.<br>
187 * It is the name display in the view to label the graph node.
188 *
189 * @return the graph node name
190 */
191 public String getName() {
192 return fName;
193 }
194
195 /**
196 * Tags the the graph node has selected.<br>
197 * WARNING: This method is only used to draw the graph node using the system selection colors. <br>
198 * To use the complete SDViewer selection mechanism (selection management, notification, etc..) see SDWidget class
199 *
200 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#addSelection(GraphNode)
201 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#removeSelection(GraphNode)
202 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#clearSelection()
203 * @param selection - true to set selected, false to set unselected
204 */
205 public void setSelected(boolean selection) {
206 fSelected = selection;
207 }
208
209 /**
210 * Tags the the graph node as focused.<br>
211 * WARNING: This method is only used to draw the graph node using the system focus style. <br>
212 * To use the complete SDViewer focus mechanism see SDWidget class
213 *
214 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#addSelection(GraphNode)
215 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#removeSelection(GraphNode)
216 * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDWidget#clearSelection()
217 * @param focus - true to set focued, false otherwise
218 */
219 public void setFocused(boolean focus) {
220 fFocused = focus;
221 }
222
223 /**
224 * Returns true if the graph node is selected, false otherwise.<br>
225 * The returned value is used to highlight the graph node in the View.
226 *
227 * @return true if selected, false otherwise
228 */
229 public boolean isSelected() {
230 return fSelected;
231 }
232
233 /**
234 * Returns true if the graph node is focused, false otherwise.<br>
235 * The returned value is used to highlight the graph node in the View.
236 *
237 * @return true if focued, false otherwise
238 */
239 public boolean hasFocus() {
240 return fFocused;
241 }
242
243 /**
244 * Returns true if the graph node contains the point given in parameter, return false otherwise.
245 *
246 * @param x the x coordinate of the point to test containment <br>
247 * y the y coordinate of the point to test containment
248 * @return true if contained, false otherwise
249 */
250 abstract public boolean contains(int x, int y);
251
252 /**
253 * Returns the x coordinate of the graph node
254 *
255 * @return the x coordinate
256 */
257 abstract public int getX();
258
259 /**
260 * Returns the y coordinate of the graph node
261 *
262 * @return the y coordinate
263 */
264 abstract public int getY();
265
266 /**
267 * Returns the graph node height
268 *
269 * @return the graph node height
270 */
271 abstract public int getHeight();
272
273 /**
274 * Returns the graph node width
275 *
276 * @return the graph node width
277 */
278 abstract public int getWidth();
279
280 /**
281 * Draws the graph node in the given context
282 *
283 * @param context the graphical context to draw in
284 */
285 abstract protected void draw(IGC context);
286
287 /**
288 * Returns the GraphNode visibility for the given visible area. Wrong visibility calculation, may strongly impact
289 * drawing performance
290 *
291 * @param x
292 * @param y
293 * @param width
294 * @param height
295 * @return true if visible false otherwise
296 */
297 public boolean isVisible(int x, int y, int width, int height) {
298 return true;
299 }
300
301 /**
302 * Return a comparator to sort the GraphNode of the same type This comparator is used to order the GraphNode array
303 * of the given node type. (see getArrayId).
304 *
305 * @return the comparator
306 */
307 public Comparator<GraphNode> getComparator() {
308 return null;
309 }
310
311 /**
312 * If needed, return a different comparator to backward scan the GraphNode array
313 *
314 * @return the backward comparator or null if not needed
315 */
316 public Comparator<GraphNode> getBackComparator() {
317 return null;
318 }
319
320 /**
321 * Compare two graphNodes
322 *
323 * @param node the node to compare to
324 * @return true if equal false otherwise
325 */
326 public boolean isSameAs(GraphNode node) {
327 return false;
328 }
329
330 /**
331 * Return the node type for all class instances. This id is used to store the same nodes kind in the same ordered
332 * array.
333 *
334 * @return the node type identifier
335 */
336 abstract public String getArrayId();
337
338 /**
339 * Return true if the distance from the GraphNode to the given point is positive
340 *
341 * @param x the point x coordinate
342 * @param y the point y coordinate
343 * @return true if positive false otherwise
344 */
345 public boolean positiveDistanceToPoint(int x, int y) {
346 return false;
347 }
348
349 /**
350 * Returns the graph node which contains the point given in parameter WARNING: Only graph nodes in the current
351 * visible area can be returned
352 *
353 * @param x the x coordinate of the point to test
354 * @param y the y coordinate of the point to test
355 * @return the graph node containing the point given in parameter, null otherwise
356 */
357 public GraphNode getNodeAt(int x, int y) {
358 GraphNode toReturn = null;
359
360 if (!fHasChilden) {
361 return null;
362 }
363
364 Iterator<String> it = fNodes.keySet().iterator();
365 GraphNode node = null;
366 while (it.hasNext()) {
367 Object nodeType = it.next();
368 List<GraphNode> list = (List<GraphNode>) fNodes.get(nodeType);
369 int index = ((Integer) fIndexes.get(nodeType)).intValue();
370 node = getNodeFromListAt(x, y, list, index);
371 if (toReturn == null) {
372 toReturn = node;
373 }
374 if (node != null) {
375 GraphNode internalNode = node.getNodeAt(x, y);
376 if (internalNode != null) {
377 return internalNode;
378 } else if (Math.abs(node.getWidth()) < Math.abs(toReturn.getWidth()) || Math.abs(node.getHeight()) < Math.abs(toReturn.getHeight())) {
379 toReturn = node;
380 }
381 }
382 }
383 return toReturn;
384 }
385
386 /**
387 * Gets node list from node A to node B
388
389 * @param from A from node
390 * @param to A to node
391 * @return the list of nodes
392 */
393 public List<GraphNode> getNodeList(GraphNode from, GraphNode to) {
394 List<GraphNode> result = new ArrayList<GraphNode>();
395
396 if (from != null) {
397 result.add(from);
398 } else if (to != null) {
399 result.add(to);
400 }
401
402 if ((from == null) || (to == null)) {
403 return result;
404 }
405
406 if (from == to) {
407 return result;
408 }
409
410 int startX = Math.min(from.getX(), Math.min(to.getX(), Math.min(from.getX() + from.getWidth(), to.getX() + to.getWidth())));
411 int endX = Math.max(from.getX(), Math.max(to.getX(), Math.max(from.getX() + from.getWidth(), to.getX() + to.getWidth())));
412 int startY = Math.min(from.getY(), Math.min(to.getY(), Math.min(from.getY() + from.getHeight(), to.getY() + to.getHeight())));
413 int endY = Math.max(from.getY(), Math.max(to.getY(), Math.max(from.getY() + from.getHeight(), to.getY() + to.getHeight())));
414
415 if (!fHasChilden) {
416 return result;
417 }
418
419 Iterator<String> it = fNodes.keySet().iterator();
420 while (it.hasNext()) {
421 Object nodeType = it.next();
422 List<GraphNode> nodesList = (List<GraphNode>) fNodes.get(nodeType);
423 if (nodesList == null || nodesList.isEmpty()) {
424 return null;
425 }
426 for (int i = 0; i < nodesList.size(); i++) {
427 GraphNode node = (GraphNode) nodesList.get(i);
428 int nw = node.getWidth();
429 int nh = node.getHeight();
430 int nx = node.getX();
431 int ny = node.getY();
432 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)) {
433 result.add(node);
434 }
435 result.addAll(node.getNodeList(from, to));
436 }
437 }
438
439 if (!result.contains(to)) {
440 result.add(to);
441 }
442 return result;
443 }
444
445 /**
446 * Returns the graph node which contains the point given in parameter for the given graph node list and starting the
447 * iteration at the given index<br>
448 * WARNING: Only graph nodes with smaller coordinates than the current visible area can be returned.<br>
449 *
450 * @param x the x coordinate of the point to test
451 * @param y the y coordinate of the point to test
452 * @param list the list to search in
453 * @param fromIndex list browsing starting point
454 * @return the graph node containing the point given in parameter, null otherwise
455 */
456 protected GraphNode getNodeFromListAt(int x, int y, List<GraphNode> list, int fromIndex) {
457 if (list == null) {
458 return null;
459 }
460 for (int i = fromIndex; i < list.size(); i++) {
461 GraphNode node = (GraphNode) list.get(i);
462 if (node.contains(x, y)) {
463 return node;
464 }
465 }
466 return null;
467 }
468
469 /**
470 * Returns the start event occurrence attached to this graphNode.
471 *
472 * @return the start event occurrence attached to the graphNode
473 */
474 public int getStartOccurrence() {
475 return fStartEventOccurrence;
476 }
477
478 /**
479 * Returns the end event occurrence attached to this graphNode
480 *
481 * @return the start event occurrence attached to the graphNode
482 */
483 public int getEndOccurrence() {
484 return fEndEventOccurrence;
485 }
486
487 /**
488 * Computes the index of the first visible GraphNode for each ordered graph node lists depending on the visible area
489 * given in parameter
490 *
491 * @param x visible area top left corner x coordinate
492 * @param y visible area top left corner y coordinate
493 * @param width visible area width
494 * @param height visible area height
495 */
496 public void updateIndex(int x, int y, int width, int height) {
497 if (!fHasChilden) {
498 return;
499 }
500 if(TmfUiTracer.isIndexTraced()) {
501 TmfUiTracer.traceIndex("*****************************\n"); //$NON-NLS-1$
502 TmfUiTracer.traceIndex("Visible area position in virtual screen (x,y)= " + x + " " + y + "\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
503 }
504
505 Iterator<String> it = fNodes.keySet().iterator();
506 while (it.hasNext()) {
507 String nodeType = it.next();
508 int direction = 1;
509 int drawIndex = ((Integer) fIndexes.get(nodeType)).intValue();
510 /*
511 * if (x==0) { drawIndex = 0; indexes.put(nodeType,new Integer(drawIndex)); }
512 */
513 if ((fNodes.get(nodeType) != null) && (((List<GraphNode>) fNodes.get(nodeType)).size() > 1)) {
514 if (((GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(drawIndex)).positiveDistanceToPoint(x, y)) {
515 direction = -1;
516 }
517
518 if (drawIndex == 0) {
519 direction = 1;
520 }
521
522 if ((direction == -1) && (fBackwardNodes.get(nodeType) != null)) {
523 GraphNode currentNode = (GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(drawIndex);
524 drawIndex = Arrays.binarySearch(((List<GraphNode>) fBackwardNodes.get(nodeType)).toArray(new GraphNode[0]), ((List<GraphNode>) fNodes.get(nodeType)).get(drawIndex), currentNode.getBackComparator());
525 fNodes.put(nodeType, (List<GraphNode>) fBackwardNodes.get(nodeType));
526 if (drawIndex < 0) {
527 drawIndex = 0;
528 direction = 1;
529 } else {
530 fNodes.put(nodeType, (List<GraphNode>) fBackwardNodes.get(nodeType));
531 }
532 }
533 GraphNode prev = null;
534
535 for (int i = drawIndex; i < ((List<GraphNode>) fNodes.get(nodeType)).size() && i >= 0; i = i + direction) {
536 drawIndex = i;
537 fIndexes.put(nodeType, Integer.valueOf(i));
538
539 GraphNode currentNode = (GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(i);
540
541 if (prev == null) {
542 prev = currentNode;
543 }
544
545 Comparator<GraphNode> comp = currentNode.getComparator();
546 Map<String, Boolean> sort = fForwardSort;
547
548 if ((direction == -1) && (currentNode.getBackComparator() != null)) {
549 comp = currentNode.getBackComparator();
550 sort = fBackwardSort;
551 }
552
553 if (i < ((List<GraphNode>) fNodes.get(nodeType)).size() - 1) {
554 GraphNode next = (GraphNode) ((List<GraphNode>) fNodes.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>) fNodes.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 fNodes.put(nodeType, fForwardNodes.get(nodeType));
583 if ((fBackwardNodes.get(nodeType) != null) && (direction == -1)) {
584 // nodes.put(nodeType,fnodes.get(nodeType));
585 int index = ((Integer) fIndexes.get(nodeType)).intValue();
586 List<GraphNode> list = (List<GraphNode>) fNodes.get(nodeType);
587 List<GraphNode> backList = (List<GraphNode>) fBackwardNodes.get(nodeType);
588 GraphNode currentNode = (GraphNode) (backList.get(index));
589 if (index > 0) {
590 index = Arrays.binarySearch(list.toArray(new GraphNode[list.size()]), backList.get(index), currentNode.getComparator());
591 if (index < 0) {
592 index = 0;
593 }
594 fIndexes.put(nodeType, Integer.valueOf(index));
595 }
596 }
597
598 for (int i = drawIndex; i < ((List<GraphNode>) fNodes.get(nodeType)).size() && i >= 0; i++) {
599 GraphNode toDraw = (GraphNode) ((List<GraphNode>) fNodes.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 (!fHasChilden) {
628 return;
629 }
630 // If the nodes have not been added ordered, the array is ordered
631 Iterator<String> it = fForwardSort.keySet().iterator();
632 while (it.hasNext()) {
633 String nodeType = it.next();
634 boolean sort = ((Boolean) fForwardSort.get(nodeType)).booleanValue();
635 if (sort) {
636 GraphNode[] temp = ((List<GraphNode>) fForwardNodes.get(nodeType)).toArray(new GraphNode[((List<GraphNode>)fForwardNodes.get(nodeType)).size()]);
637 GraphNode node = (GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(0);
638 Arrays.sort(temp, node.getComparator());
639 fForwardSort.put(nodeType, Boolean.valueOf(false));
640 fNodes.put(nodeType, Arrays.asList(temp));
641 fForwardNodes.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 = fBackwardSort.keySet().iterator();
649 while (it2.hasNext()) {
650 String nodeType = it2.next();
651 boolean sort = ((Boolean) fBackwardSort.get(nodeType)).booleanValue();
652 if (sort) {
653 GraphNode[] temp = ((List<GraphNode>) fBackwardNodes.get(nodeType)).toArray(new GraphNode[((List<GraphNode>) fBackwardNodes.get(nodeType)).size()]);
654 GraphNode node = (GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(0);
655 Arrays.sort(temp, node.getBackComparator());
656 fBackwardSort.put(nodeType, Boolean.valueOf(false));
657 fBackwardNodes.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 = fForwardSort.keySet().iterator();
675 while (it3.hasNext()) {
676 count = 0;
677 Object nodeType = it3.next();
678 GraphNode node = (GraphNode) ((List<GraphNode>) fNodes.get(nodeType)).get(0);
679 context.setFont(Frame.getUserPref().getFont(node.fPrefId));
680 int index = ((Integer) fIndexes.get(nodeType)).intValue();
681 count = drawNodes(context, (List<GraphNode>) fNodes.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 (!fHasChilden) {
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.fPrefId));
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 fForwardSort.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.047222 seconds and 6 git commands to generate.