jdk/src/share/classes/javax/swing/LayoutComparator.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23697 e556a715949f
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2014, 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
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    42
@SuppressWarnings("serial") // JDK-implementation class
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    43
final class LayoutComparator implements Comparator<Component>, java.io.Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int ROW_TOLERANCE = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean horizontal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private boolean leftToRight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    void setComponentOrientation(ComponentOrientation orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        horizontal = orientation.isHorizontal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        leftToRight = orientation.isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    55
    public int compare(Component a, Component b) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (a == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Row/Column algorithm only applies to siblings. If 'a' and 'b'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // aren't siblings, then we need to find their most inferior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // ancestors which share a parent. Compute the ancestory lists for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // each Component and then search from the Window down until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // hierarchy branches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (a.getParent() != b.getParent()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    66
            LinkedList<Component> aAncestory = new LinkedList<Component>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    68
            for(; a != null; a = a.getParent()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                aAncestory.add(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (a instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                // 'a' is not part of a Window hierarchy. Can't cope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    79
            LinkedList<Component> bAncestory = new LinkedList<Component>();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    80
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    81
            for(; b != null; b = b.getParent()) {
2
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
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    92
            for (ListIterator<Component>
2
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()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    96
                    a = aIter.previous();
2
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()) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   103
                    b = bIter.previous();
2
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
}