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