jdk/src/share/classes/java/awt/CardLayout.java
author darcy
Mon, 05 May 2014 23:38:30 -0700
changeset 24549 147a5c8b7793
parent 22584 eed64ee05369
child 24865 09b1d992ca72
permissions -rw-r--r--
8039109: Fix unchecked and raw lint warnings in java.awt Reviewed-by: pchelko, forax, flar, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
     2
 * Copyright (c) 1995, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A <code>CardLayout</code> object is a layout manager for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * container. It treats each component in the container as a card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Only one card is visible at a time, and the container acts as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a stack of cards. The first component added to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>CardLayout</code> object is the visible component when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * container is first displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The ordering of cards is determined by the container's own internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * ordering of its component objects. <code>CardLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * defines a set of methods that allow an application to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * through these cards sequentially, or to show a specified card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The {@link CardLayout#addLayoutComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * method can be used to associate a string identifier with a given card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * for fast random access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see         java.awt.Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since       JDK1.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
public class CardLayout implements LayoutManager2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                   Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final long serialVersionUID = -4328196481005934313L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This creates a Vector to store associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * pairs of components and their names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    69
    Vector<Card> vector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * A pair of Component and String that represents its name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    class Card implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        static final long serialVersionUID = 6640330810709497518L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public Component comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public Card(String cardName, Component cardComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            name = cardName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            comp = cardComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Index of Component currently displayed by CardLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int currentCard = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    * A cards horizontal Layout gap (inset). It specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    * the space between the left and right edges of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    * container and the current component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    * This should be a non negative Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    * @see getHgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    * @see setHgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int hgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    * A cards vertical Layout gap (inset). It specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    * the space between the top and bottom edges of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    * container and the current component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    * This should be a non negative Integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    * @see getVgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    * @see setVgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @serialField tab         Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *      deprectated, for forward compatibility only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @serialField hgap        int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @serialField vgap        int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @serialField vector      Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @serialField currentCard int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        new ObjectStreamField("tab", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        new ObjectStreamField("hgap", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        new ObjectStreamField("vgap", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        new ObjectStreamField("vector", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        new ObjectStreamField("currentCard", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates a new card layout with gaps of size zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public CardLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Creates a new card layout with the specified horizontal and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * vertical gaps. The horizontal gaps are placed at the left and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * right edges. The vertical gaps are placed at the top and bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * edges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param     hgap   the horizontal gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param     vgap   the vertical gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public CardLayout(int hgap, int vgap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.hgap = hgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.vgap = vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Gets the horizontal gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return    the horizontal gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see       java.awt.CardLayout#setHgap(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see       java.awt.CardLayout#getVgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public int getHgap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return hgap;
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
     * Sets the horizontal gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param hgap the horizontal gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @see       java.awt.CardLayout#getHgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see       java.awt.CardLayout#setVgap(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setHgap(int hgap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.hgap = hgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Gets the vertical gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return the vertical gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see       java.awt.CardLayout#setVgap(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see       java.awt.CardLayout#getHgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int getVgap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Sets the vertical gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param     vgap the vertical gap between components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see       java.awt.CardLayout#getVgap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see       java.awt.CardLayout#setHgap(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void setVgap(int vgap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.vgap = vgap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Adds the specified component to this card layout's internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * table of names. The object specified by <code>constraints</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * must be a string. The card layout stores this string as a key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * pair that can be used for random access to a particular card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * By calling the <code>show</code> method, an application can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * display the component with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param     comp          the component to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param     constraints   a tag that identifies a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *                                        card in the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see       java.awt.CardLayout#show(java.awt.Container, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception  IllegalArgumentException  if the constraint is not a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public void addLayoutComponent(Component comp, Object constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
      synchronized (comp.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
          if (constraints == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
              constraints = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (constraints instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            addLayoutComponent((String)constraints, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new IllegalArgumentException("cannot add to layout: constraint must be a string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @deprecated   replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *      <code>addLayoutComponent(Component, Object)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void addLayoutComponent(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        synchronized (comp.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (!vector.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            for (int i=0; i < vector.size(); i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   226
                if ((vector.get(i)).name.equals(name)) {
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   227
                    (vector.get(i)).comp = comp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            vector.add(new Card(name, comp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Removes the specified component from the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * If the card was visible on top, the next card underneath it is shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param   comp   the component to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see     java.awt.Container#remove(java.awt.Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see     java.awt.Container#removeAll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void removeLayoutComponent(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        synchronized (comp.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            for (int i = 0; i < vector.size(); i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   245
                if ((vector.get(i)).comp == comp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    // if we remove current component we should show next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (comp.isVisible() && (comp.getParent() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        next(comp.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    vector.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    // correct currentCard if this is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    if (currentCard > i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        currentCard--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Determines the preferred size of the container argument using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * this card layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param   parent the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return  the preferred dimensions to lay out the subcomponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *                of the specified container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see     java.awt.Container#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see     java.awt.CardLayout#minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public Dimension preferredLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                Dimension d = comp.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (d.width > w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    w = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (d.height > h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    h = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return new Dimension(insets.left + insets.right + w + hgap*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                 insets.top + insets.bottom + h + vgap*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Calculates the minimum size for the specified panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param     parent the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return    the minimum dimensions required to lay out the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *                subcomponents of the specified container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see       java.awt.Container#doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see       java.awt.CardLayout#preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public Dimension minimumLayoutSize(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                Dimension d = comp.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (d.width > w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    w = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                if (d.height > h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    h = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return new Dimension(insets.left + insets.right + w + hgap*2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                 insets.top + insets.bottom + h + vgap*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns the maximum dimensions for this layout given the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * in the specified target container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param target the component which needs to be laid out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @see Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @see #minimumLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @see #preferredLayoutSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public Dimension maximumLayoutSize(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns the alignment along the x axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public float getLayoutAlignmentX(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the alignment along the y axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public float getLayoutAlignmentY(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Invalidates the layout, indicating that if the layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * has cached information it should be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void invalidateLayout(Container target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Lays out the specified container using this card layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Each component in the <code>parent</code> container is reshaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * to be the size of the container, minus space for surrounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * insets, horizontal gaps, and vertical gaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param     parent the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see       java.awt.Container#doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void layoutContainer(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Insets insets = parent.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            Component comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            boolean currentFound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                comp.setBounds(hgap + insets.left, vgap + insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                               parent.width - (hgap*2 + insets.left + insets.right),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                               parent.height - (vgap*2 + insets.top + insets.bottom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    currentFound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (!currentFound && ncomponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                parent.getComponent(0).setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Make sure that the Container really has a CardLayout installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Otherwise havoc can ensue!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    void checkLayout(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (parent.getLayout() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new IllegalArgumentException("wrong parent for CardLayout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Flips to the first card of the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param     parent   the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see       java.awt.CardLayout#last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public void first(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            checkLayout(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (ncomponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                currentCard = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                parent.getComponent(0).setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Flips to the next card of the specified container. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * currently visible card is the last one, this method flips to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * first card in the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param     parent   the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @see       java.awt.CardLayout#previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void next(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            checkLayout(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    currentCard = (i + 1) % ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    comp = parent.getComponent(currentCard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    comp.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            showDefaultComponent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Flips to the previous card of the specified container. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * currently visible card is the first one, this method flips to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * last card in the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param     parent   the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @see       java.awt.CardLayout#next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void previous(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            checkLayout(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    currentCard = ((i > 0) ? i-1 : ncomponents-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    comp = parent.getComponent(currentCard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    comp.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            showDefaultComponent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    void showDefaultComponent(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (parent.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            currentCard = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            parent.getComponent(0).setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Flips to the last card of the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param     parent   the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see       java.awt.CardLayout#first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public void last(Container parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            checkLayout(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            int ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (ncomponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                currentCard = ncomponents - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                parent.getComponent(currentCard).setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Flips to the component that was added to this layout with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * specified <code>name</code>, using <code>addLayoutComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * If no such component exists, then nothing happens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param     parent   the parent container in which to do the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param     name     the component name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @see       java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public void show(Container parent, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        synchronized (parent.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            checkLayout(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            Component next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            int ncomponents = vector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            for (int i = 0; i < ncomponents; i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   530
                Card card = vector.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (card.name.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    next = card.comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    currentCard = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if ((next != null) && !next.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                ncomponents = parent.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                for (int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    Component comp = parent.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        comp.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                next.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                parent.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Returns a string representation of the state of this card layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return    a string representation of this card layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Reads serializable fields from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 22584
diff changeset
   563
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        ObjectInputStream.GetField f = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        hgap = f.get("hgap", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        vgap = f.get("vgap", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (f.defaulted("vector")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            //  pre-1.4 stream
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   574
            Hashtable<String, Component> tab = (Hashtable)f.get("tab", null);
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   575
            vector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (tab != null && !tab.isEmpty()) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   577
                for (Enumeration<String> e = tab.keys() ; e.hasMoreElements() ; ) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   578
                    String key = e.nextElement();
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   579
                    Component comp = tab.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    vector.add(new Card(key, comp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    if (comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        currentCard = vector.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            vector = (Vector)f.get("vector", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            currentCard = f.get("currentCard", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Writes serializable fields to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   598
        Hashtable<String, Component> tab = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int ncomponents = vector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        for (int i = 0; i < ncomponents; i++) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   601
            Card card = vector.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            tab.put(card.name, card.comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        ObjectOutputStream.PutField f = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        f.put("hgap", hgap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        f.put("vgap", vgap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        f.put("vector", vector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        f.put("currentCard", currentCard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        f.put("tab", tab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
}