jdk/src/share/classes/javax/swing/text/html/ListView.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 5506 202f599c92aa
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 1999, 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
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A view implementation to display an html list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ListView extends BlockView  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * Creates a new view that represents a list element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * @param elem the element to create a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public ListView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Calculates the desired shape of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @return the desired span
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Renders using the given rendering surface and area on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param allocation the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super.paint(g, allocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Rectangle alloc = allocation.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Since listPainter paints in the insets we have to check for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // case where the child is not painted because the paint region is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // to the left of the child. This assumes the ListPainter paints in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // the left margin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if ((clip.x + clip.width) < (alloc.x + getLeftInset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            Rectangle childRect = alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            alloc = getInsideAllocation(allocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int endY = clip.y + clip.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                childRect.setBounds(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                childAllocation(i, childRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (childRect.y < endY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    if ((childRect.y + childRect.height) >= clip.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        listPainter.paint(g, childRect.x, childRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                          childRect.width, childRect.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                          this, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Paints one of the children; called by paint().  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * that is all it does, but a subclass can use this to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * things relative to the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param alloc the allocated region to render the child into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param index the index of the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected void paintChild(Graphics g, Rectangle alloc, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        listPainter.paint(g, alloc.x, alloc.y, alloc.width, alloc.height, this, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        super.paintChild(g, alloc, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        super.setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        listPainter = getStyleSheet().getListPainter(getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private StyleSheet.ListPainter listPainter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}