jdk/src/share/classes/javax/swing/LayoutComparator.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 23697 e556a715949f
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
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
 */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    42
final class LayoutComparator implements Comparator<Component>, java.io.Serializable {
2
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
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    54
    public int compare(Component a, Component b) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (a == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // Row/Column algorithm only applies to siblings. If 'a' and 'b'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // aren't siblings, then we need to find their most inferior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // ancestors which share a parent. Compute the ancestory lists for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // each Component and then search from the Window down until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // hierarchy branches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (a.getParent() != b.getParent()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    65
            LinkedList<Component> aAncestory = new LinkedList<Component>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    67
            for(; a != null; a = a.getParent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                aAncestory.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (a instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                // 'a' is not part of a Window hierarchy. Can't cope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    78
            LinkedList<Component> bAncestory = new LinkedList<Component>();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    79
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    80
            for(; b != null; b = b.getParent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                bAncestory.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (b instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                // 'b' is not part of a Window hierarchy. Can't cope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    91
            for (ListIterator<Component>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                     aIter = aAncestory.listIterator(aAncestory.size()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                     bIter = bAncestory.listIterator(bAncestory.size()); ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (aIter.hasPrevious()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    95
                    a = aIter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    // a is an ancestor of b
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (bIter.hasPrevious()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   102
                    b = bIter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    // b is an ancestor of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (a != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
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
        int ax = a.getX(), ay = a.getY(), bx = b.getX(), by = b.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int zOrder = a.getParent().getComponentZOrder(a) - b.getParent().getComponentZOrder(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // LT - Western Europe (optional for Japanese, Chinese, Korean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (Math.abs(ay - by) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    return (ay < by) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else { // !leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // RT - Middle East (Arabic, Hebrew)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (Math.abs(ay - by) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    return (ay < by) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else { // !horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // TL - Mongolian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (Math.abs(ax - bx) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    return (ax < bx) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else { // !leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // TR - Japanese, Chinese, Korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (Math.abs(ax - bx) < ROW_TOLERANCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    return (ax > bx) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
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
}