37 * position. Code adapted from original javax.swing.DefaultFocusManager |
37 * position. Code adapted from original javax.swing.DefaultFocusManager |
38 * implementation. |
38 * implementation. |
39 * |
39 * |
40 * @author David Mendenhall |
40 * @author David Mendenhall |
41 */ |
41 */ |
42 final class LayoutComparator implements Comparator, java.io.Serializable { |
42 final class LayoutComparator implements Comparator<Component>, java.io.Serializable { |
43 |
43 |
44 private static final int ROW_TOLERANCE = 10; |
44 private static final int ROW_TOLERANCE = 10; |
45 |
45 |
46 private boolean horizontal = true; |
46 private boolean horizontal = true; |
47 private boolean leftToRight = true; |
47 private boolean leftToRight = true; |
49 void setComponentOrientation(ComponentOrientation orientation) { |
49 void setComponentOrientation(ComponentOrientation orientation) { |
50 horizontal = orientation.isHorizontal(); |
50 horizontal = orientation.isHorizontal(); |
51 leftToRight = orientation.isLeftToRight(); |
51 leftToRight = orientation.isLeftToRight(); |
52 } |
52 } |
53 |
53 |
54 public int compare(Object o1, Object o2) { |
54 public int compare(Component a, Component b) { |
55 Component a = (Component)o1; |
|
56 Component b = (Component)o2; |
|
57 |
|
58 if (a == b) { |
55 if (a == b) { |
59 return 0; |
56 return 0; |
60 } |
57 } |
61 |
58 |
62 // Row/Column algorithm only applies to siblings. If 'a' and 'b' |
59 // Row/Column algorithm only applies to siblings. If 'a' and 'b' |
63 // aren't siblings, then we need to find their most inferior |
60 // aren't siblings, then we need to find their most inferior |
64 // ancestors which share a parent. Compute the ancestory lists for |
61 // ancestors which share a parent. Compute the ancestory lists for |
65 // each Component and then search from the Window down until the |
62 // each Component and then search from the Window down until the |
66 // hierarchy branches. |
63 // hierarchy branches. |
67 if (a.getParent() != b.getParent()) { |
64 if (a.getParent() != b.getParent()) { |
68 LinkedList aAncestory, bAncestory; |
65 LinkedList<Component> aAncestory = new LinkedList<Component>(); |
69 |
66 |
70 for(aAncestory = new LinkedList(); a != null; a = a.getParent()) { |
67 for(; a != null; a = a.getParent()) { |
71 aAncestory.add(a); |
68 aAncestory.add(a); |
72 if (a instanceof Window) { |
69 if (a instanceof Window) { |
73 break; |
70 break; |
74 } |
71 } |
75 } |
72 } |
76 if (a == null) { |
73 if (a == null) { |
77 // 'a' is not part of a Window hierarchy. Can't cope. |
74 // 'a' is not part of a Window hierarchy. Can't cope. |
78 throw new ClassCastException(); |
75 throw new ClassCastException(); |
79 } |
76 } |
80 |
77 |
81 for(bAncestory = new LinkedList(); b != null; b = b.getParent()) { |
78 LinkedList<Component> bAncestory = new LinkedList<Component>(); |
|
79 |
|
80 for(; b != null; b = b.getParent()) { |
82 bAncestory.add(b); |
81 bAncestory.add(b); |
83 if (b instanceof Window) { |
82 if (b instanceof Window) { |
84 break; |
83 break; |
85 } |
84 } |
86 } |
85 } |
87 if (b == null) { |
86 if (b == null) { |
88 // 'b' is not part of a Window hierarchy. Can't cope. |
87 // 'b' is not part of a Window hierarchy. Can't cope. |
89 throw new ClassCastException(); |
88 throw new ClassCastException(); |
90 } |
89 } |
91 |
90 |
92 for (ListIterator |
91 for (ListIterator<Component> |
93 aIter = aAncestory.listIterator(aAncestory.size()), |
92 aIter = aAncestory.listIterator(aAncestory.size()), |
94 bIter = bAncestory.listIterator(bAncestory.size()); ;) { |
93 bIter = bAncestory.listIterator(bAncestory.size()); ;) { |
95 if (aIter.hasPrevious()) { |
94 if (aIter.hasPrevious()) { |
96 a = (Component)aIter.previous(); |
95 a = aIter.previous(); |
97 } else { |
96 } else { |
98 // a is an ancestor of b |
97 // a is an ancestor of b |
99 return -1; |
98 return -1; |
100 } |
99 } |
101 |
100 |
102 if (bIter.hasPrevious()) { |
101 if (bIter.hasPrevious()) { |
103 b = (Component)bIter.previous(); |
102 b = bIter.previous(); |
104 } else { |
103 } else { |
105 // b is an ancestor of a |
104 // b is an ancestor of a |
106 return 1; |
105 return 1; |
107 } |
106 } |
108 |
107 |