1. /*
  2. * @(#)IconView.java 1.23 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;
  8. import java.awt.*;
  9. import javax.swing.Icon;
  10. import javax.swing.event.*;
  11. /**
  12. * Icon decorator that implements the view interface. The
  13. * entire element is used to represent the icon. This acts
  14. * as a gateway from the display-only View implementations to
  15. * interactive lightweight icons (that is, it allows icons
  16. * to be embedded into the View hierarchy. The parent of the icon
  17. * is the container that is handed out by the associated view
  18. * factory.
  19. *
  20. * @author Timothy Prinzing
  21. * @version 1.23 11/29/01
  22. */
  23. public class IconView extends View {
  24. /**
  25. * Creates a new icon view that represents an element.
  26. *
  27. * @param elem the element to create a view for
  28. */
  29. public IconView(Element elem) {
  30. super(elem);
  31. AttributeSet attr = elem.getAttributes();
  32. c = StyleConstants.getIcon(attr);
  33. }
  34. // --- View methods ---------------------------------------------
  35. /**
  36. * Paints the icon.
  37. * The real paint behavior occurs naturally from the association
  38. * that the icon has with its parent container (the same
  39. * container hosting this view), so this simply allows us to
  40. * position the icon properly relative to the view. Since
  41. * the coordinate system for the view is simply the parent
  42. * containers, positioning the child icon is easy.
  43. *
  44. * @param g the rendering surface to use
  45. * @param a the allocated region to render into
  46. * @see View#paint
  47. */
  48. public void paint(Graphics g, Shape a) {
  49. Rectangle alloc = a.getBounds();
  50. c.paintIcon(getContainer(), g, alloc.x, alloc.y);
  51. }
  52. /**
  53. * Determines the preferred span for this view along an
  54. * axis.
  55. *
  56. * @param axis may be either View.X_AXIS or View.Y_AXIS
  57. * @returns the span the view would like to be rendered into.
  58. * Typically the view is told to render into the span
  59. * that is returned, although there is no guarantee.
  60. * The parent may choose to resize or break the view.
  61. * @exception IllegalArgumentException for an invalid axis
  62. */
  63. public float getPreferredSpan(int axis) {
  64. switch (axis) {
  65. case View.X_AXIS:
  66. return c.getIconWidth();
  67. case View.Y_AXIS:
  68. return c.getIconHeight();
  69. default:
  70. throw new IllegalArgumentException("Invalid axis: " + axis);
  71. }
  72. }
  73. /**
  74. * Determines the desired alignment for this view along an
  75. * axis. This is implemented to give the alignment to the
  76. * bottom of the icon along the y axis, and the default
  77. * along the x axis.
  78. *
  79. * @param axis may be either View.X_AXIS or View.Y_AXIS
  80. * @returns the desired alignment >= 0.0f && <= 1.0f. This should be
  81. * a value between 0.0 and 1.0 where 0 indicates alignment at the
  82. * origin and 1.0 indicates alignment to the full span
  83. * away from the origin. An alignment of 0.5 would be the
  84. * center of the view.
  85. */
  86. public float getAlignment(int axis) {
  87. switch (axis) {
  88. case View.Y_AXIS:
  89. return 1;
  90. default:
  91. return super.getAlignment(axis);
  92. }
  93. }
  94. /**
  95. * Provides a mapping from the document model coordinate space
  96. * to the coordinate space of the view mapped to it.
  97. *
  98. * @param pos the position to convert >= 0
  99. * @param a the allocated region to render into
  100. * @return the bounding box of the given position
  101. * @exception BadLocationException if the given position does not
  102. * represent a valid location in the associated document
  103. * @see View#modelToView
  104. */
  105. public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
  106. int p0 = getStartOffset();
  107. int p1 = getEndOffset();
  108. if ((pos >= p0) && (pos <= p1)) {
  109. Rectangle r = a.getBounds();
  110. if (pos == p1) {
  111. r.x += r.width;
  112. }
  113. r.width = 0;
  114. return r;
  115. }
  116. throw new BadLocationException(pos + " not in range " + p0 + "," + p1, pos);
  117. }
  118. /**
  119. * Provides a mapping from the view coordinate space to the logical
  120. * coordinate space of the model.
  121. *
  122. * @param x the X coordinate >= 0
  123. * @param y the Y coordinate >= 0
  124. * @param a the allocated region to render into
  125. * @return the location within the model that best represents the
  126. * given point of view >= 0
  127. * @see View#viewToModel
  128. */
  129. public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
  130. Rectangle alloc = (Rectangle) a;
  131. if (x < alloc.x + (alloc.width / 2)) {
  132. bias[0] = Position.Bias.Forward;
  133. return getStartOffset();
  134. }
  135. bias[0] = Position.Bias.Backward;
  136. return getEndOffset();
  137. }
  138. /**
  139. * Sets the size of the view. Since Icon doesn't
  140. * support this functionality, there is nothing
  141. * we can do.
  142. *
  143. * @param width the width
  144. * @param height the height
  145. */
  146. public void setSize(float width, float height) {
  147. }
  148. // --- member variables ------------------------------------------------
  149. private Icon c;
  150. }