jdk/src/share/classes/javax/swing/LayoutComparator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.ComponentOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Comparator which attempts to sort Components based on their size and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * position. Code adapted from original javax.swing.DefaultFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class LayoutComparator implements Comparator, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int ROW_TOLERANCE = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean horizontal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean leftToRight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void setComponentOrientation(ComponentOrientation orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        horizontal = orientation.isHorizontal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        leftToRight = orientation.isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Component a = (Component)o1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Component b = (Component)o2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (a == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Row/Column algorithm only applies to siblings. If 'a' and 'b'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // aren't siblings, then we need to find their most inferior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // ancestors which share a parent. Compute the ancestory lists for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // each Component and then search from the Window down until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // hierarchy branches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (a.getParent() != b.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            LinkedList aAncestory, bAncestory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            for(aAncestory = new LinkedList(); a != null; a = a.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                aAncestory.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                if (a instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // 'a' is not part of a Window hierarchy. Can't cope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            for(bAncestory = new LinkedList(); b != null; b = b.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                bAncestory.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (b instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // 'b' is not part of a Window hierarchy. Can't cope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            for (ListIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                     aIter = aAncestory.listIterator(aAncestory.size()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                     bIter = bAncestory.listIterator(bAncestory.size()); ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (aIter.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    a = (Component)aIter.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    // a is an ancestor of b
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (bIter.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    b = (Component)bIter.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    // b is an ancestor of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (a != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // LT - Western Europe (optional for Japanese, Chinese, Korean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                if (Math.abs(ay - by) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    return (ay < by) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } else { // !leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // RT - Middle East (Arabic, Hebrew)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (Math.abs(ay - by) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    return (ay < by) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else { // !horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // TL - Mongolian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (Math.abs(ax - bx) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    return (ax < bx) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else { // !leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // TR - Japanese, Chinese, Korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (Math.abs(ax - bx) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    return (ax > bx) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}