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