1. /*
  2. * @(#)LayoutComparator.java 1.7 04/02/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.Comparator;
  9. import java.util.LinkedList;
  10. import java.util.ListIterator;
  11. import java.awt.Component;
  12. import java.awt.ComponentOrientation;
  13. import java.awt.Window;
  14. /**
  15. * Comparator which attempts to sort Components based on their size and
  16. * position. Code adapted from original javax.swing.DefaultFocusManager
  17. * implementation.
  18. *
  19. * @version 1.7, 02/05/04
  20. * @author David Mendenhall
  21. */
  22. final class LayoutComparator implements Comparator, java.io.Serializable {
  23. private static final int ROW_TOLERANCE = 10;
  24. private boolean horizontal = true;
  25. private boolean leftToRight = true;
  26. void setComponentOrientation(ComponentOrientation orientation) {
  27. horizontal = orientation.isHorizontal();
  28. leftToRight = orientation.isLeftToRight();
  29. }
  30. public int compare(Object o1, Object o2) {
  31. Component a = (Component)o1;
  32. Component b = (Component)o2;
  33. if (a == b) {
  34. return 0;
  35. }
  36. // Row/Column algorithm only applies to siblings. If 'a' and 'b'
  37. // aren't siblings, then we need to find their most inferior
  38. // ancestors which share a parent. Compute the ancestory lists for
  39. // each Component and then search from the Window down until the
  40. // hierarchy branches.
  41. if (a.getParent() != b.getParent()) {
  42. LinkedList aAncestory, bAncestory;
  43. for(aAncestory = new LinkedList(); a != null; a = a.getParent()) {
  44. aAncestory.add(a);
  45. if (a instanceof Window) {
  46. break;
  47. }
  48. }
  49. if (a == null) {
  50. // 'a' is not part of a Window hierarchy. Can't cope.
  51. throw new ClassCastException();
  52. }
  53. for(bAncestory = new LinkedList(); b != null; b = b.getParent()) {
  54. bAncestory.add(b);
  55. if (b instanceof Window) {
  56. break;
  57. }
  58. }
  59. if (b == null) {
  60. // 'b' is not part of a Window hierarchy. Can't cope.
  61. throw new ClassCastException();
  62. }
  63. for (ListIterator
  64. aIter = aAncestory.listIterator(aAncestory.size()),
  65. bIter = bAncestory.listIterator(bAncestory.size()); ;) {
  66. if (aIter.hasPrevious()) {
  67. a = (Component)aIter.previous();
  68. } else {
  69. // a is an ancestor of b
  70. return -1;
  71. }
  72. if (bIter.hasPrevious()) {
  73. b = (Component)bIter.previous();
  74. } else {
  75. // b is an ancestor of a
  76. return 1;
  77. }
  78. if (a != b) {
  79. break;
  80. }
  81. }
  82. }
  83. int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
  84. int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
  85. if (horizontal) {
  86. if (leftToRight) {
  87. // LT - Western Europe (optional for Japanese, Chinese, Korean)
  88. if (Math.abs(ay - by) < ROW_TOLERANCE) {
  89. return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
  90. } else {
  91. return (ay < by) ? -1 : 1;
  92. }
  93. } else { // !leftToRight
  94. // RT - Middle East (Arabic, Hebrew)
  95. if (Math.abs(ay - by) < ROW_TOLERANCE) {
  96. return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
  97. } else {
  98. return (ay < by) ? -1 : 1;
  99. }
  100. }
  101. } else { // !horizontal
  102. if (leftToRight) {
  103. // TL - Mongolian
  104. if (Math.abs(ax - bx) < ROW_TOLERANCE) {
  105. return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
  106. } else {
  107. return (ax < bx) ? -1 : 1;
  108. }
  109. } else { // !leftToRight
  110. // TR - Japanese, Chinese, Korean
  111. if (Math.abs(ax - bx) < ROW_TOLERANCE) {
  112. return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
  113. } else {
  114. return (ax > bx) ? -1 : 1;
  115. }
  116. }
  117. }
  118. }
  119. }