1. /*
  2. * @(#)LayoutComparator.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 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.3, 01/23/03
  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(a.toString());
  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(b.toString());
  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. if (horizontal) {
  85. if (leftToRight) {
  86. // LT - Western Europe (optional for Japanese, Chinese, Korean)
  87. if (Math.abs(ay - by) < ROW_TOLERANCE) {
  88. return (ax < bx) ? -1 : 1;
  89. } else {
  90. return (ay < by) ? -1 : 1;
  91. }
  92. } else { // !leftToRight
  93. // RT - Middle East (Arabic, Hebrew)
  94. if (Math.abs(ay - by) < ROW_TOLERANCE) {
  95. return (ax > bx) ? -1 : 1;
  96. } else {
  97. return (ay < by) ? -1 : 1;
  98. }
  99. }
  100. } else { // !horizontal
  101. if (leftToRight) {
  102. // TL - Mongolian
  103. if (Math.abs(ax - bx) < ROW_TOLERANCE) {
  104. return (ay < by) ? -1 : 1;
  105. } else {
  106. return (ax < bx) ? -1 : 1;
  107. }
  108. } else { // !leftToRight
  109. // TR - Japanese, Chinese, Korean
  110. if (Math.abs(ax - bx) < ROW_TOLERANCE) {
  111. return (ay < by) ? -1 : 1;
  112. } else {
  113. return (ax > bx) ? -1 : 1;
  114. }
  115. }
  116. }
  117. }
  118. }