| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2005, 2012 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.linuxtools.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.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 | import org.eclipse.linuxtools.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 | // ------------------------------------------------------------------------ |
| 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.FALSE); |
| 137 | if (nodeToAdd.getBackComparator() != null) { |
| 138 | fBackwardNodes.put(nodeToAdd.getArrayId(), new ArrayList<GraphNode>(1)); |
| 139 | fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.FALSE); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | List<GraphNode> fNodeList = fForwardNodes.get(nodeToAdd.getArrayId()); |
| 144 | List<GraphNode> bNodeList = null; |
| 145 | if (fBackwardNodes != null) { |
| 146 | bNodeList = 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 = 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.TRUE); |
| 156 | } |
| 157 | if ((bcomp != null) && (bcomp.compare(node, nodeToAdd) > 0)) { |
| 158 | fBackwardSort.put(nodeToAdd.getArrayId(), Boolean.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 ((bNodeList != null) && (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 focused, 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, |
| 245 | * return false otherwise. |
| 246 | * |
| 247 | * @param x |
| 248 | * the x coordinate of the point to test containment |
| 249 | * @param y |
| 250 | * the y coordinate of the point to test containment |
| 251 | * @return true if contained, false otherwise |
| 252 | */ |
| 253 | abstract public boolean contains(int x, int y); |
| 254 | |
| 255 | /** |
| 256 | * Returns the x coordinate of the graph node |
| 257 | * |
| 258 | * @return the x coordinate |
| 259 | */ |
| 260 | abstract public int getX(); |
| 261 | |
| 262 | /** |
| 263 | * Returns the y coordinate of the graph node |
| 264 | * |
| 265 | * @return the y coordinate |
| 266 | */ |
| 267 | abstract public int getY(); |
| 268 | |
| 269 | /** |
| 270 | * Returns the graph node height |
| 271 | * |
| 272 | * @return the graph node height |
| 273 | */ |
| 274 | abstract public int getHeight(); |
| 275 | |
| 276 | /** |
| 277 | * Returns the graph node width |
| 278 | * |
| 279 | * @return the graph node width |
| 280 | */ |
| 281 | abstract public int getWidth(); |
| 282 | |
| 283 | /** |
| 284 | * Draws the graph node in the given context |
| 285 | * |
| 286 | * @param context the graphical context to draw in |
| 287 | */ |
| 288 | abstract protected void draw(IGC context); |
| 289 | |
| 290 | /** |
| 291 | * Returns the GraphNode visibility for the given visible area. Wrong |
| 292 | * visibility calculation, may strongly impact drawing performance |
| 293 | * |
| 294 | * @param x |
| 295 | * The X coordinate |
| 296 | * @param y |
| 297 | * The Y coordinate |
| 298 | * @param width |
| 299 | * The width of the area |
| 300 | * @param height |
| 301 | * The height of the area |
| 302 | * @return true if visible, false otherwise |
| 303 | */ |
| 304 | public boolean isVisible(int x, int y, int width, int height) { |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Return a comparator to sort the GraphNode of the same type This |
| 310 | * comparator is used to order the GraphNode array of the given node type. |
| 311 | * (see getArrayId). |
| 312 | * |
| 313 | * @return the comparator |
| 314 | */ |
| 315 | public Comparator<GraphNode> getComparator() { |
| 316 | return null; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * If needed, return a different comparator to backward scan the GraphNode |
| 321 | * array |
| 322 | * |
| 323 | * @return the backward comparator or null if not needed |
| 324 | */ |
| 325 | public Comparator<GraphNode> getBackComparator() { |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Compare two graphNodes |
| 331 | * |
| 332 | * @param node |
| 333 | * the node to compare to |
| 334 | * @return true if equal false otherwise |
| 335 | */ |
| 336 | public boolean isSameAs(GraphNode node) { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Return the node type for all class instances. This id is used to store the same nodes kind in the same ordered |
| 342 | * array. |
| 343 | * |
| 344 | * @return the node type identifier |
| 345 | */ |
| 346 | abstract public String getArrayId(); |
| 347 | |
| 348 | /** |
| 349 | * Return true if the distance from the GraphNode to the given point is positive |
| 350 | * |
| 351 | * @param x the point x coordinate |
| 352 | * @param y the point y coordinate |
| 353 | * @return true if positive false otherwise |
| 354 | */ |
| 355 | public boolean positiveDistanceToPoint(int x, int y) { |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Returns the graph node which contains the point given in parameter WARNING: Only graph nodes in the current |
| 361 | * visible area can be returned |
| 362 | * |
| 363 | * @param x the x coordinate of the point to test |
| 364 | * @param y the y coordinate of the point to test |
| 365 | * @return the graph node containing the point given in parameter, null otherwise |
| 366 | */ |
| 367 | public GraphNode getNodeAt(int x, int y) { |
| 368 | GraphNode toReturn = null; |
| 369 | |
| 370 | if (!fHasChilden) { |
| 371 | return null; |
| 372 | } |
| 373 | |
| 374 | Iterator<String> it = fNodes.keySet().iterator(); |
| 375 | GraphNode node = null; |
| 376 | while (it.hasNext()) { |
| 377 | Object nodeType = it.next(); |
| 378 | List<GraphNode> list = fNodes.get(nodeType); |
| 379 | int index = fIndexes.get(nodeType).intValue(); |
| 380 | node = getNodeFromListAt(x, y, list, index); |
| 381 | if (toReturn == null) { |
| 382 | toReturn = node; |
| 383 | } |
| 384 | if (node != null) { |
| 385 | GraphNode internalNode = node.getNodeAt(x, y); |
| 386 | if (internalNode != null) { |
| 387 | return internalNode; |
| 388 | } else if (Math.abs(node.getWidth()) < Math.abs(toReturn.getWidth()) || Math.abs(node.getHeight()) < Math.abs(toReturn.getHeight())) { |
| 389 | toReturn = node; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | return toReturn; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Gets node list from node A to node B |
| 398 | |
| 399 | * @param from A from node |
| 400 | * @param to A to node |
| 401 | * @return the list of nodes |
| 402 | */ |
| 403 | public List<GraphNode> getNodeList(GraphNode from, GraphNode to) { |
| 404 | List<GraphNode> result = new ArrayList<GraphNode>(); |
| 405 | |
| 406 | if (from != null) { |
| 407 | result.add(from); |
| 408 | } else if (to != null) { |
| 409 | result.add(to); |
| 410 | } |
| 411 | |
| 412 | if ((from == null) || (to == null)) { |
| 413 | return result; |
| 414 | } |
| 415 | |
| 416 | if (from == to) { |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | int startX = Math.min(from.getX(), Math.min(to.getX(), Math.min(from.getX() + from.getWidth(), to.getX() + to.getWidth()))); |
| 421 | int endX = Math.max(from.getX(), Math.max(to.getX(), Math.max(from.getX() + from.getWidth(), to.getX() + to.getWidth()))); |
| 422 | int startY = Math.min(from.getY(), Math.min(to.getY(), Math.min(from.getY() + from.getHeight(), to.getY() + to.getHeight()))); |
| 423 | int endY = Math.max(from.getY(), Math.max(to.getY(), Math.max(from.getY() + from.getHeight(), to.getY() + to.getHeight()))); |
| 424 | |
| 425 | if (!fHasChilden) { |
| 426 | return result; |
| 427 | } |
| 428 | |
| 429 | Iterator<String> it = fNodes.keySet().iterator(); |
| 430 | while (it.hasNext()) { |
| 431 | Object nodeType = it.next(); |
| 432 | List<GraphNode> nodesList = fNodes.get(nodeType); |
| 433 | if (nodesList == null || nodesList.isEmpty()) { |
| 434 | return null; |
| 435 | } |
| 436 | for (int i = 0; i < nodesList.size(); i++) { |
| 437 | GraphNode node = nodesList.get(i); |
| 438 | int nw = node.getWidth(); |
| 439 | int nh = node.getHeight(); |
| 440 | int nx = node.getX(); |
| 441 | int ny = node.getY(); |
| 442 | 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)) { |
| 443 | result.add(node); |
| 444 | } |
| 445 | result.addAll(node.getNodeList(from, to)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (!result.contains(to)) { |
| 450 | result.add(to); |
| 451 | } |
| 452 | return result; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Returns the graph node which contains the point given in parameter for the given graph node list and starting the |
| 457 | * iteration at the given index<br> |
| 458 | * WARNING: Only graph nodes with smaller coordinates than the current visible area can be returned.<br> |
| 459 | * |
| 460 | * @param x the x coordinate of the point to test |
| 461 | * @param y the y coordinate of the point to test |
| 462 | * @param list the list to search in |
| 463 | * @param fromIndex list browsing starting point |
| 464 | * @return the graph node containing the point given in parameter, null otherwise |
| 465 | */ |
| 466 | protected GraphNode getNodeFromListAt(int x, int y, List<GraphNode> list, int fromIndex) { |
| 467 | if (list == null) { |
| 468 | return null; |
| 469 | } |
| 470 | for (int i = fromIndex; i < list.size(); i++) { |
| 471 | GraphNode node = list.get(i); |
| 472 | if (node.contains(x, y)) { |
| 473 | return node; |
| 474 | } |
| 475 | } |
| 476 | return null; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Returns the start event occurrence attached to this graphNode. |
| 481 | * |
| 482 | * @return the start event occurrence attached to the graphNode |
| 483 | */ |
| 484 | public int getStartOccurrence() { |
| 485 | return fStartEventOccurrence; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Returns the end event occurrence attached to this graphNode |
| 490 | * |
| 491 | * @return the start event occurrence attached to the graphNode |
| 492 | */ |
| 493 | public int getEndOccurrence() { |
| 494 | return fEndEventOccurrence; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Computes the index of the first visible GraphNode for each ordered graph node lists depending on the visible area |
| 499 | * given in parameter |
| 500 | * |
| 501 | * @param x visible area top left corner x coordinate |
| 502 | * @param y visible area top left corner y coordinate |
| 503 | * @param width visible area width |
| 504 | * @param height visible area height |
| 505 | */ |
| 506 | public void updateIndex(int x, int y, int width, int height) { |
| 507 | if (!fHasChilden) { |
| 508 | return; |
| 509 | } |
| 510 | if(TmfUiTracer.isIndexTraced()) { |
| 511 | TmfUiTracer.traceIndex("*****************************\n"); //$NON-NLS-1$ |
| 512 | TmfUiTracer.traceIndex("Visible area position in virtual screen (x,y)= " + x + " " + y + "\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 513 | } |
| 514 | |
| 515 | Iterator<String> it = fNodes.keySet().iterator(); |
| 516 | while (it.hasNext()) { |
| 517 | String nodeType = it.next(); |
| 518 | int direction = 1; |
| 519 | int drawIndex = fIndexes.get(nodeType).intValue(); |
| 520 | /* |
| 521 | * if (x==0) { drawIndex = 0; indexes.put(nodeType,new Integer(drawIndex)); } |
| 522 | */ |
| 523 | if ((fNodes.get(nodeType) != null) && (fNodes.get(nodeType).size() > 1)) { |
| 524 | if (fNodes.get(nodeType).get(drawIndex).positiveDistanceToPoint(x, y)) { |
| 525 | direction = -1; |
| 526 | } |
| 527 | |
| 528 | if (drawIndex == 0) { |
| 529 | direction = 1; |
| 530 | } |
| 531 | |
| 532 | if ((direction == -1) && (fBackwardNodes.get(nodeType) != null)) { |
| 533 | GraphNode currentNode = fNodes.get(nodeType).get(drawIndex); |
| 534 | drawIndex = Arrays.binarySearch(fBackwardNodes.get(nodeType).toArray(new GraphNode[fBackwardNodes.get(nodeType).size()]), |
| 535 | fNodes.get(nodeType).get(drawIndex), currentNode.getBackComparator()); |
| 536 | fNodes.put(nodeType, fBackwardNodes.get(nodeType)); |
| 537 | if (drawIndex < 0) { |
| 538 | drawIndex = 0; |
| 539 | direction = 1; |
| 540 | } else { |
| 541 | fNodes.put(nodeType, fBackwardNodes.get(nodeType)); |
| 542 | } |
| 543 | } |
| 544 | GraphNode prev = null; |
| 545 | |
| 546 | for (int i = drawIndex; i < fNodes.get(nodeType).size() && i >= 0; i = i + direction) { |
| 547 | drawIndex = i; |
| 548 | fIndexes.put(nodeType, Integer.valueOf(i)); |
| 549 | |
| 550 | GraphNode currentNode = fNodes.get(nodeType).get(i); |
| 551 | |
| 552 | if (prev == null) { |
| 553 | prev = currentNode; |
| 554 | } |
| 555 | |
| 556 | Comparator<GraphNode> comp = currentNode.getComparator(); |
| 557 | Map<String, Boolean> sort = fForwardSort; |
| 558 | |
| 559 | if ((direction == -1) && (currentNode.getBackComparator() != null)) { |
| 560 | comp = currentNode.getBackComparator(); |
| 561 | sort = fBackwardSort; |
| 562 | } |
| 563 | |
| 564 | if (i < fNodes.get(nodeType).size() - 1) { |
| 565 | GraphNode next = fNodes.get(nodeType).get(i + 1); |
| 566 | |
| 567 | if ((comp != null) && (comp.compare(currentNode, next) > 0)) { |
| 568 | sort.put(nodeType, Boolean.TRUE); |
| 569 | } |
| 570 | } |
| 571 | if (direction == 1) { |
| 572 | if (fNodes.get(nodeType).get(i).positiveDistanceToPoint(x, y)) { |
| 573 | break; |
| 574 | } |
| 575 | } else { |
| 576 | if (currentNode.getBackComparator() == null) { |
| 577 | if // (currentNode.isVisible(x,y,width,height) |
| 578 | (!currentNode.positiveDistanceToPoint(x, y)) { |
| 579 | break; |
| 580 | } |
| 581 | } else { |
| 582 | if (currentNode.isVisible(x, y, width, height) && !currentNode.positiveDistanceToPoint(x, y)) { |
| 583 | if ((comp != null) && (comp.compare(currentNode, prev) <= 0)) { |
| 584 | break; |
| 585 | } |
| 586 | } else if ((comp != null) && (comp.compare(currentNode, prev) <= 0)) { |
| 587 | prev = currentNode; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | fNodes.put(nodeType, fForwardNodes.get(nodeType)); |
| 594 | if ((fBackwardNodes.get(nodeType) != null) && (direction == -1)) { |
| 595 | // nodes.put(nodeType,fnodes.get(nodeType)); |
| 596 | int index = fIndexes.get(nodeType).intValue(); |
| 597 | List<GraphNode> list = fNodes.get(nodeType); |
| 598 | List<GraphNode> backList = fBackwardNodes.get(nodeType); |
| 599 | GraphNode currentNode = (backList.get(index)); |
| 600 | if (index > 0) { |
| 601 | index = Arrays.binarySearch(list.toArray(new GraphNode[list.size()]), backList.get(index), currentNode.getComparator()); |
| 602 | if (index < 0) { |
| 603 | index = 0; |
| 604 | } |
| 605 | fIndexes.put(nodeType, Integer.valueOf(index)); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | for (int i = drawIndex; i < fNodes.get(nodeType).size() && i >= 0; i++) { |
| 610 | GraphNode toDraw = fNodes.get(nodeType).get(i); |
| 611 | toDraw.updateIndex(x, y, width, height); |
| 612 | if (!toDraw.isVisible(x, y, width, height)) { |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | if (TmfUiTracer.isIndexTraced()) { |
| 618 | TmfUiTracer.traceIndex("First drawn " + nodeType + " index = " + drawIndex + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 619 | TmfUiTracer.traceIndex(nodeType + " found in " + 0 + " iterations\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (TmfUiTracer.isIndexTraced()) { |
| 624 | TmfUiTracer.traceIndex("*****************************\n"); //$NON-NLS-1$ |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Draws the children nodes on the given context.<br> |
| 630 | * This method start width GraphNodes ordering if needed.<br> |
| 631 | * After, depending on the visible area, only visible GraphNodes are drawn.<br> |
| 632 | * |
| 633 | * @param context the context to draw to |
| 634 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode#draw(IGC) |
| 635 | */ |
| 636 | protected void drawChildenNodes(IGC context) { |
| 637 | |
| 638 | if (!fHasChilden) { |
| 639 | return; |
| 640 | } |
| 641 | // If the nodes have not been added ordered, the array is ordered |
| 642 | Iterator<String> it = fForwardSort.keySet().iterator(); |
| 643 | while (it.hasNext()) { |
| 644 | String nodeType = it.next(); |
| 645 | boolean sort = fForwardSort.get(nodeType).booleanValue(); |
| 646 | if (sort) { |
| 647 | GraphNode[] temp = fForwardNodes.get(nodeType).toArray(new GraphNode[fForwardNodes.get(nodeType).size()]); |
| 648 | GraphNode node = fNodes.get(nodeType).get(0); |
| 649 | Arrays.sort(temp, node.getComparator()); |
| 650 | fForwardSort.put(nodeType, Boolean.FALSE); |
| 651 | fNodes.put(nodeType, Arrays.asList(temp)); |
| 652 | fForwardNodes.put(nodeType, Arrays.asList(temp)); |
| 653 | if (TmfUiTracer.isSortingTraced()) { |
| 654 | TmfUiTracer.traceSorting(nodeType + " array sorted\n"); //$NON-NLS-1$ |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | Iterator<String> it2 = fBackwardSort.keySet().iterator(); |
| 660 | while (it2.hasNext()) { |
| 661 | String nodeType = it2.next(); |
| 662 | boolean sort = fBackwardSort.get(nodeType).booleanValue(); |
| 663 | if (sort) { |
| 664 | GraphNode[] temp = fBackwardNodes.get(nodeType).toArray(new GraphNode[fBackwardNodes.get(nodeType).size()]); |
| 665 | GraphNode node = fNodes.get(nodeType).get(0); |
| 666 | Arrays.sort(temp, node.getBackComparator()); |
| 667 | fBackwardSort.put(nodeType, Boolean.FALSE); |
| 668 | fBackwardNodes.put(nodeType, Arrays.asList(temp)); |
| 669 | if (TmfUiTracer.isSortingTraced()) { |
| 670 | TmfUiTracer.traceSorting(nodeType + " back array sorted\n"); //$NON-NLS-1$ |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (TmfUiTracer.isDisplayTraced()) { |
| 676 | TmfUiTracer.traceDisplay("*****************************\n"); //$NON-NLS-1$ |
| 677 | } |
| 678 | |
| 679 | int arrayStep = 1; |
| 680 | if ((Metrics.getMessageFontHeigth() + Metrics.MESSAGES_NAME_SPACING * 2) * context.getZoom() < Metrics.MESSAGE_SIGNIFICANT_VSPACING) { |
| 681 | arrayStep = Math.round(Metrics.MESSAGE_SIGNIFICANT_VSPACING / ((Metrics.getMessageFontHeigth() + Metrics.MESSAGES_NAME_SPACING * 2) * context.getZoom())); |
| 682 | } |
| 683 | |
| 684 | int count = 0; |
| 685 | Iterator<String> it3 = fForwardSort.keySet().iterator(); |
| 686 | while (it3.hasNext()) { |
| 687 | count = 0; |
| 688 | Object nodeType = it3.next(); |
| 689 | GraphNode node = fNodes.get(nodeType).get(0); |
| 690 | context.setFont(SDViewPref.getInstance().getFont(node.fPrefId)); |
| 691 | int index = fIndexes.get(nodeType).intValue(); |
| 692 | count = drawNodes(context, fNodes.get(nodeType), index, arrayStep); |
| 693 | if (TmfUiTracer.isDisplayTraced()) { |
| 694 | TmfUiTracer.traceDisplay(count + " " + nodeType + " drawn, starting from index " + index + "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 695 | } |
| 696 | } |
| 697 | if (TmfUiTracer.isDisplayTraced()) { |
| 698 | TmfUiTracer.traceDisplay("*****************************\n"); //$NON-NLS-1$ |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Draw the GraphNode stored in the given list, starting at index startIndex with the given step |
| 705 | * |
| 706 | * @param context the context to draw to |
| 707 | * @param list the GraphNodes list |
| 708 | * @param startIndex the start index |
| 709 | * @param step the step to browse the list |
| 710 | * @return the number of GraphNodes drawn |
| 711 | */ |
| 712 | protected int drawNodes(IGC context, List<GraphNode> list, int startIndex, int step) { |
| 713 | if (!fHasChilden) { |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | GraphNode last = null; |
| 718 | int nodesCount = 0; |
| 719 | if (list.size() < 0) { |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | GraphNode node = list.get(0); |
| 724 | context.setFont(SDViewPref.getInstance().getFont(node.fPrefId)); |
| 725 | Comparator<GraphNode> comparator = node.getComparator(); |
| 726 | for (int i = startIndex; i < list.size(); i = i + step) { |
| 727 | GraphNode toDraw = list.get(i); |
| 728 | if (i < list.size() - 1) { |
| 729 | GraphNode next = list.get(i + 1); |
| 730 | if ((comparator != null) && (comparator.compare(toDraw, next) > 0)) { |
| 731 | fForwardSort.put(next.getArrayId(), Boolean.TRUE); |
| 732 | } |
| 733 | } |
| 734 | int cx = context.getContentsX(); |
| 735 | int cy = context.getContentsY(); |
| 736 | int cw = context.getVisibleWidth(); |
| 737 | int ch = context.getVisibleHeight(); |
| 738 | // The arrays should be ordered, no needs to continue for this one |
| 739 | if (!toDraw.isVisible(cx, cy, cw, ch) && toDraw.positiveDistanceToPoint(cx + cw, cy + ch)) { |
| 740 | break; |
| 741 | } |
| 742 | // ***Common*** nodes visibility |
| 743 | if ((!toDraw.isSameAs(last) || toDraw.isSelected()) && (toDraw.isVisible(context.getContentsX(), context.getContentsY(), context.getVisibleWidth(), context.getVisibleHeight()))) { |
| 744 | nodesCount++; |
| 745 | |
| 746 | toDraw.draw(context); |
| 747 | if (hasFocus()) { |
| 748 | toDraw.drawFocus(context); |
| 749 | } |
| 750 | } |
| 751 | last = toDraw; |
| 752 | } |
| 753 | return nodesCount; |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Draws the focus within the graphical context. |
| 758 | * |
| 759 | * @param context |
| 760 | * The context |
| 761 | */ |
| 762 | public void drawFocus(IGC context) { |
| 763 | context.drawFocus(getX(), getY(), getWidth(), getHeight()); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Determine if the given point (px,py) is contained in the rectangle (x,y,width,height) |
| 768 | * |
| 769 | * @param x the rectangle x coordinate |
| 770 | * @param y the rectangle y coordinate |
| 771 | * @param width the rectangle width |
| 772 | * @param height the rectangle height |
| 773 | * @param px the x coordinate of the point to test |
| 774 | * @param py the y coordinate of the point to test |
| 775 | * @return true if contained false otherwise |
| 776 | */ |
| 777 | public static boolean contains(int x, int y, int width, int height, int px, int py) { |
| 778 | int locX = x; |
| 779 | int locY = y; |
| 780 | int locWidth = width; |
| 781 | int locHeight = height; |
| 782 | |
| 783 | if (width < 0) { |
| 784 | locX = locX + width; |
| 785 | locWidth = -locWidth; |
| 786 | } |
| 787 | |
| 788 | if (height < 0) { |
| 789 | locY = locY + height; |
| 790 | locHeight = -locHeight; |
| 791 | } |
| 792 | return (px >= locX) && (py >= locY) && ((px - locX) <= locWidth) && ((py - locY) <= locHeight); |
| 793 | } |
| 794 | } |