jdk/src/share/classes/javax/swing/LegacyGlueFocusTraversalPolicy.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-2002 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.awt.FocusTraversalPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A FocusTraversalPolicy which provides support for legacy applications which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * handle focus traversal via JComponent.setNextFocusableComponent or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * installing a custom DefaultFocusManager. If a specific traversal has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * been hard coded, then that traversal is provided either by the custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * DefaultFocusManager, or by a wrapped FocusTraversalPolicy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
final class LegacyGlueFocusTraversalPolicy extends FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private transient FocusTraversalPolicy delegatePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private transient DefaultFocusManager delegateManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private HashMap forwardMap = new HashMap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        backwardMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    LegacyGlueFocusTraversalPolicy(FocusTraversalPolicy delegatePolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.delegatePolicy = delegatePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    LegacyGlueFocusTraversalPolicy(DefaultFocusManager delegateManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.delegateManager = delegateManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void setNextFocusableComponent(Component left, Component right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        forwardMap.put(left, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        backwardMap.put(right, left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    void unsetNextFocusableComponent(Component left, Component right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        forwardMap.remove(left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        backwardMap.remove(right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public Component getComponentAfter(Container focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                       Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        Component hardCoded = aComponent, prevHardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        HashSet sanity = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            prevHardCoded = hardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            hardCoded = (Component)forwardMap.get(hardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (hardCoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (delegatePolicy != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    prevHardCoded.isFocusCycleRoot(focusCycleRoot)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    return delegatePolicy.getComponentAfter(focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                                            prevHardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                } else if (delegateManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    return delegateManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        getComponentAfter(focusCycleRoot, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (sanity.contains(hardCoded)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                // cycle detected; bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sanity.add(hardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } while (!accept(hardCoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return hardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public Component getComponentBefore(Container focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                        Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Component hardCoded = aComponent, prevHardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        HashSet sanity = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            prevHardCoded = hardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            hardCoded = (Component)backwardMap.get(hardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (hardCoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (delegatePolicy != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    prevHardCoded.isFocusCycleRoot(focusCycleRoot)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    return delegatePolicy.getComponentBefore(focusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                                       prevHardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                } else if (delegateManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    return delegateManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        getComponentBefore(focusCycleRoot, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (sanity.contains(hardCoded)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                // cycle detected; bail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            sanity.add(hardCoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } while (!accept(hardCoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return hardCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public Component getFirstComponent(Container focusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (delegatePolicy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return delegatePolicy.getFirstComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else if (delegateManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return delegateManager.getFirstComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public Component getLastComponent(Container focusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (delegatePolicy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return delegatePolicy.getLastComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else if (delegateManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return delegateManager.getLastComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Component getDefaultComponent(Container focusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (delegatePolicy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return delegatePolicy.getDefaultComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return getFirstComponent(focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
              aComponent.isFocusable() && aComponent.isEnabled())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Verify that the Component is recursively enabled. Disabling a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // heavyweight Container disables its children, whereas disabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // a lightweight Container does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (!(aComponent instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            for (Container enableTest = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                 enableTest != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                 enableTest = enableTest.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (enableTest instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        out.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (delegatePolicy instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            out.writeObject(delegatePolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            out.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (delegateManager instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            out.writeObject(delegateManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            out.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        delegatePolicy = (FocusTraversalPolicy)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        delegateManager = (DefaultFocusManager)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}