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