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. import java.util.Enumeration;
  9. import java.awt.*;
  10. import javax.swing.text.*;
  11. /**
  12. * A view implementation to display an html list
  13. *
  14. * @author Timothy Prinzing
  15. * @version 1.28 12/19/03
  16. */
  17. public class ListView extends BlockView {
  18. /**
  19. * Creates a new view that represents a list element.
  20. *
  21. * @param elem the element to create a view for
  22. */
  23. public ListView(Element elem) {
  24. super(elem, View.Y_AXIS);
  25. }
  26. /**
  27. * Calculates the desired shape of the list.
  28. *
  29. * @return the desired span
  30. * @see View#getPreferredSpan
  31. */
  32. public float getAlignment(int axis) {
  33. switch (axis) {
  34. case View.X_AXIS:
  35. return 0.5f;
  36. case View.Y_AXIS:
  37. return 0.5f;
  38. default:
  39. throw new IllegalArgumentException("Invalid axis: " + axis);
  40. }
  41. }
  42. /**
  43. * Renders using the given rendering surface and area on that
  44. * surface.
  45. *
  46. * @param g the rendering surface to use
  47. * @param allocation the allocated region to render into
  48. * @see View#paint
  49. */
  50. public void paint(Graphics g, Shape allocation) {
  51. super.paint(g, allocation);
  52. Rectangle alloc = allocation.getBounds();
  53. Rectangle clip = g.getClipBounds();
  54. // Since listPainter paints in the insets we have to check for the
  55. // case where the child is not painted because the paint region is
  56. // to the left of the child. This assumes the ListPainter paints in
  57. // the left margin.
  58. if ((clip.x + clip.width) < (alloc.x + getLeftInset())) {
  59. Rectangle childRect = alloc;
  60. alloc = getInsideAllocation(allocation);
  61. int n = getViewCount();
  62. int endY = clip.y + clip.height;
  63. for (int i = 0; i < n; i++) {
  64. childRect.setBounds(alloc);
  65. childAllocation(i, childRect);
  66. if (childRect.y < endY) {
  67. if ((childRect.y + childRect.height) >= clip.y) {
  68. listPainter.paint(g, childRect.x, childRect.y,
  69. childRect.width, childRect.height,
  70. this, i);
  71. }
  72. }
  73. else {
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * Paints one of the children; called by paint(). By default
  81. * that is all it does, but a subclass can use this to paint
  82. * things relative to the child.
  83. *
  84. * @param g the graphics context
  85. * @param alloc the allocated region to render the child into
  86. * @param index the index of the child
  87. */
  88. protected void paintChild(Graphics g, Rectangle alloc, int index) {
  89. listPainter.paint(g, alloc.x, alloc.y, alloc.width, alloc.height, this, index);
  90. super.paintChild(g, alloc, index);
  91. }
  92. protected void setPropertiesFromAttributes() {
  93. super.setPropertiesFromAttributes();
  94. listPainter = getStyleSheet().getListPainter(getAttributes());
  95. }
  96. private StyleSheet.ListPainter listPainter;
  97. }