KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > LayoutComparator


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
9 import java.util.Comparator JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.ListIterator JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.ComponentOrientation JavaDoc;
14 import java.awt.Window JavaDoc;
15
16
17 /**
18  * Comparator which attempts to sort Components based on their size and
19  * position. Code adapted from original javax.swing.DefaultFocusManager
20  * implementation.
21  *
22  * @version 1.7, 02/05/04
23  * @author David Mendenhall
24  */

25 final class LayoutComparator implements Comparator JavaDoc, java.io.Serializable JavaDoc {
26
27     private static final int ROW_TOLERANCE = 10;
28
29     private boolean horizontal = true;
30     private boolean leftToRight = true;
31
32     void setComponentOrientation(ComponentOrientation JavaDoc orientation) {
33     horizontal = orientation.isHorizontal();
34     leftToRight = orientation.isLeftToRight();
35     }
36
37     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
38     Component JavaDoc a = (Component JavaDoc)o1;
39     Component JavaDoc b = (Component JavaDoc)o2;
40
41     if (a == b) {
42         return 0;
43     }
44
45     // Row/Column algorithm only applies to siblings. If 'a' and 'b'
46
// aren't siblings, then we need to find their most inferior
47
// ancestors which share a parent. Compute the ancestory lists for
48
// each Component and then search from the Window down until the
49
// hierarchy branches.
50
if (a.getParent() != b.getParent()) {
51         LinkedList JavaDoc aAncestory, bAncestory;
52
53         for(aAncestory = new LinkedList JavaDoc(); a != null; a = a.getParent()) {
54         aAncestory.add(a);
55         if (a instanceof Window JavaDoc) {
56             break;
57         }
58         }
59         if (a == null) {
60         // 'a' is not part of a Window hierarchy. Can't cope.
61
throw new ClassCastException JavaDoc();
62         }
63
64         for(bAncestory = new LinkedList JavaDoc(); b != null; b = b.getParent()) {
65         bAncestory.add(b);
66         if (b instanceof Window JavaDoc) {
67             break;
68         }
69         }
70         if (b == null) {
71         // 'b' is not part of a Window hierarchy. Can't cope.
72
throw new ClassCastException JavaDoc();
73         }
74
75         for (ListIterator JavaDoc
76              aIter = aAncestory.listIterator(aAncestory.size()),
77              bIter = bAncestory.listIterator(bAncestory.size()); ;) {
78         if (aIter.hasPrevious()) {
79             a = (Component JavaDoc)aIter.previous();
80         } else {
81             // a is an ancestor of b
82
return -1;
83         }
84
85         if (bIter.hasPrevious()) {
86             b = (Component JavaDoc)bIter.previous();
87         } else {
88             // b is an ancestor of a
89
return 1;
90         }
91
92         if (a != b) {
93             break;
94         }
95         }
96     }
97
98         int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
99
100     int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
101     if (horizontal) {
102         if (leftToRight) {
103
104         // LT - Western Europe (optional for Japanese, Chinese, Korean)
105

106         if (Math.abs(ay - by) < ROW_TOLERANCE) {
107             return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
108         } else {
109             return (ay < by) ? -1 : 1;
110         }
111         } else { // !leftToRight
112

113         // RT - Middle East (Arabic, Hebrew)
114

115         if (Math.abs(ay - by) < ROW_TOLERANCE) {
116             return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
117         } else {
118             return (ay < by) ? -1 : 1;
119         }
120         }
121     } else { // !horizontal
122
if (leftToRight) {
123
124         // TL - Mongolian
125

126         if (Math.abs(ax - bx) < ROW_TOLERANCE) {
127             return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
128         } else {
129             return (ax < bx) ? -1 : 1;
130         }
131         } else { // !leftToRight
132

133         // TR - Japanese, Chinese, Korean
134

135         if (Math.abs(ax - bx) < ROW_TOLERANCE) {
136             return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
137         } else {
138             return (ax > bx) ? -1 : 1;
139         }
140         }
141     }
142     }
143 }
144
Popular Tags