KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > ListView


1 /*
2  * @(#)ListView.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.html;
8
9 import java.util.Enumeration JavaDoc;
10 import java.awt.*;
11 import javax.swing.text.*;
12
13 /**
14  * A view implementation to display an html list
15  *
16  * @author Timothy Prinzing
17  * @version 1.28 12/19/03
18  */

19 public class ListView extends BlockView JavaDoc {
20
21     /**
22      * Creates a new view that represents a list element.
23      *
24      * @param elem the element to create a view for
25      */

26     public ListView(Element elem) {
27     super(elem, View.Y_AXIS);
28     }
29
30     /**
31      * Calculates the desired shape of the list.
32      *
33      * @return the desired span
34      * @see View#getPreferredSpan
35      */

36     public float getAlignment(int axis) {
37         switch (axis) {
38         case View.X_AXIS:
39             return 0.5f;
40         case View.Y_AXIS:
41             return 0.5f;
42         default:
43             throw new IllegalArgumentException JavaDoc("Invalid axis: " + axis);
44         }
45     }
46
47     /**
48      * Renders using the given rendering surface and area on that
49      * surface.
50      *
51      * @param g the rendering surface to use
52      * @param allocation the allocated region to render into
53      * @see View#paint
54      */

55     public void paint(Graphics g, Shape allocation) {
56     super.paint(g, allocation);
57     Rectangle alloc = allocation.getBounds();
58     Rectangle clip = g.getClipBounds();
59     // Since listPainter paints in the insets we have to check for the
60
// case where the child is not painted because the paint region is
61
// to the left of the child. This assumes the ListPainter paints in
62
// the left margin.
63
if ((clip.x + clip.width) < (alloc.x + getLeftInset())) {
64         Rectangle childRect = alloc;
65         alloc = getInsideAllocation(allocation);
66         int n = getViewCount();
67         int endY = clip.y + clip.height;
68         for (int i = 0; i < n; i++) {
69         childRect.setBounds(alloc);
70         childAllocation(i, childRect);
71         if (childRect.y < endY) {
72             if ((childRect.y + childRect.height) >= clip.y) {
73             listPainter.paint(g, childRect.x, childRect.y,
74                       childRect.width, childRect.height,
75                       this, i);
76             }
77         }
78         else {
79             break;
80         }
81         }
82     }
83     }
84
85     /**
86      * Paints one of the children; called by paint(). By default
87      * that is all it does, but a subclass can use this to paint
88      * things relative to the child.
89      *
90      * @param g the graphics context
91      * @param alloc the allocated region to render the child into
92      * @param index the index of the child
93      */

94     protected void paintChild(Graphics g, Rectangle alloc, int index) {
95     listPainter.paint(g, alloc.x, alloc.y, alloc.width, alloc.height, this, index);
96     super.paintChild(g, alloc, index);
97     }
98
99     protected void setPropertiesFromAttributes() {
100     super.setPropertiesFromAttributes();
101     listPainter = getStyleSheet().getListPainter(getAttributes());
102     }
103
104     private StyleSheet.ListPainter JavaDoc listPainter;
105 }
106
Popular Tags