1. /*
  2. * @(#)FieldView.java 1.16 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.*;
  10. import javax.swing.event.*;
  11. /**
  12. * Extends the multi-line plain text view to be suitable
  13. * for a single-line editor view. If the view is
  14. * allocated extra space, the field must adjust for it.
  15. * If the hosting component is a JTextField, this view
  16. * will manage the ranges of the associated BoundedRangeModel
  17. * and will adjust the horizontal allocation to match the
  18. * current visibility settings of the JTextField.
  19. *
  20. * @author Timothy Prinzing
  21. * @version 1.16 11/29/01
  22. * @see View
  23. */
  24. public class FieldView extends PlainView {
  25. /**
  26. * Constructs a new FieldView wrapped on an element.
  27. *
  28. * @param elem the element
  29. */
  30. public FieldView(Element elem) {
  31. super(elem);
  32. }
  33. /**
  34. * Fetches the font metrics associated with the component hosting
  35. * this view.
  36. *
  37. * @return the metrics
  38. */
  39. protected FontMetrics getFontMetrics() {
  40. Component c = getContainer();
  41. return c.getFontMetrics(c.getFont());
  42. }
  43. /**
  44. * Adjusts the allocation given to the view
  45. * to be a suitable allocation for a text field.
  46. * If the view has been allocated more than the
  47. * preferred span vertically, the allocation is
  48. * changed to be centered vertically. Horizontally
  49. * the view is adjusted according to the horizontal
  50. * alignment property set on the associated JTextField
  51. * (if that is the type of the hosting component).
  52. *
  53. * @param a the allocation given to the view, which may need
  54. * to be adjusted.
  55. * @return the allocation that the superclass should use.
  56. */
  57. protected Shape adjustAllocation(Shape a) {
  58. if (a != null) {
  59. Rectangle bounds = a.getBounds();
  60. int vspan = (int) getPreferredSpan(Y_AXIS);
  61. int hspan = (int) getPreferredSpan(X_AXIS);
  62. if (bounds.height != vspan) {
  63. int slop = bounds.height - vspan;
  64. bounds.y += slop / 2;
  65. bounds.height -= slop;
  66. }
  67. // horizontal adjustments
  68. Component c = getContainer();
  69. if (c instanceof JTextField) {
  70. JTextField field = (JTextField) c;
  71. BoundedRangeModel vis = field.getHorizontalVisibility();
  72. int max = Math.max(hspan, bounds.width);
  73. int value = vis.getValue();
  74. int extent = Math.min(max, bounds.width - 1);
  75. if ((value + extent) > max) {
  76. value = max - extent;
  77. }
  78. vis.setRangeProperties(value, extent, vis.getMinimum(),
  79. max, false);
  80. if (hspan < bounds.width) {
  81. // horizontally align the interior
  82. int slop = bounds.width - 1 - hspan;
  83. switch (((JTextField)c).getHorizontalAlignment()) {
  84. case SwingConstants.CENTER:
  85. bounds.x += slop / 2;
  86. bounds.width -= slop;
  87. break;
  88. case SwingConstants.RIGHT:
  89. bounds.x += slop;
  90. bounds.width -= slop;
  91. break;
  92. }
  93. } else {
  94. // adjust the allocation to match the bounded range.
  95. bounds.width = hspan;
  96. bounds.x -= vis.getValue();
  97. }
  98. }
  99. return bounds;
  100. }
  101. return null;
  102. }
  103. /**
  104. * Update the visibility model with the associated JTextField
  105. * (if there is one) to reflect the current visibility as a
  106. * result of changes to the document model. The bounded
  107. * range properties are updated. If the view hasn't yet been
  108. * shown the extent will be zero and we just set it to be full
  109. * until determined otherwise.
  110. */
  111. void updateVisibilityModel() {
  112. Component c = getContainer();
  113. if (c instanceof JTextField) {
  114. JTextField field = (JTextField) c;
  115. BoundedRangeModel vis = field.getHorizontalVisibility();
  116. int hspan = (int) getPreferredSpan(X_AXIS);
  117. int extent = vis.getExtent();
  118. int maximum = Math.max(hspan, extent);
  119. extent = (extent == 0) ? maximum : extent;
  120. int value = maximum - extent;
  121. int oldValue = vis.getValue();
  122. if ((oldValue + extent) > maximum) {
  123. oldValue = maximum - extent;
  124. }
  125. value = Math.max(0, Math.min(value, oldValue));
  126. vis.setRangeProperties(value, extent, 0, maximum, false);
  127. }
  128. }
  129. // --- View methods -------------------------------------------
  130. /**
  131. * Renders using the given rendering surface and area on that surface.
  132. * The view may need to do layout and create child views to enable
  133. * itself to render into the given allocation.
  134. *
  135. * @param g the rendering surface to use
  136. * @param a the allocated region to render into
  137. *
  138. * @see View#paint
  139. */
  140. public void paint(Graphics g, Shape a) {
  141. Rectangle r = (Rectangle) a;
  142. g.clipRect(r.x, r.y, r.width, r.height);
  143. super.paint(g, a);
  144. }
  145. /**
  146. * Adjusts <code>a</code> based on the visible region and returns it.
  147. */
  148. Shape adjustPaintRegion(Shape a) {
  149. return adjustAllocation(a);
  150. }
  151. /**
  152. * Determines the preferred span for this view along an
  153. * axis.
  154. *
  155. * @param axis may be either View.X_AXIS or View.Y_AXIS
  156. * @returns the span the view would like to be rendered into >= 0.
  157. * Typically the view is told to render into the span
  158. * that is returned, although there is no guarantee.
  159. * The parent may choose to resize or break the view.
  160. */
  161. public float getPreferredSpan(int axis) {
  162. switch (axis) {
  163. case View.X_AXIS:
  164. Segment buff = getLineBuffer();
  165. Document doc = getDocument();
  166. int width;
  167. try {
  168. doc.getText(0, doc.getLength(), buff);
  169. width = Utilities.getTabbedTextWidth(buff, getFontMetrics(), 0, this, 0);
  170. } catch (BadLocationException bl) {
  171. width = 0;
  172. }
  173. return width;
  174. default:
  175. return super.getPreferredSpan(axis);
  176. }
  177. }
  178. /**
  179. * Determines the resizability of the view along the
  180. * given axis. A value of 0 or less is not resizable.
  181. *
  182. * @param axis View.X_AXIS or View.Y_AXIS
  183. * @return the weight -> 1 for View.X_AXIS, else 0
  184. */
  185. public int getResizeWeight(int axis) {
  186. if (axis == View.X_AXIS) {
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Provides a mapping from the document model coordinate space
  193. * to the coordinate space of the view mapped to it.
  194. *
  195. * @param pos the position to convert >= 0
  196. * @param a the allocated region to render into
  197. * @return the bounding box of the given position
  198. * @exception BadLocationException if the given position does not
  199. * represent a valid location in the associated document
  200. * @see View#modelToView
  201. */
  202. public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
  203. return super.modelToView(pos, adjustAllocation(a), b);
  204. }
  205. /**
  206. * Provides a mapping from the view coordinate space to the logical
  207. * coordinate space of the model.
  208. *
  209. * @param fx the X coordinate >= 0.0f
  210. * @param fy the Y coordinate >= 0.0f
  211. * @param a the allocated region to render into
  212. * @return the location within the model that best represents the
  213. * given point in the view
  214. * @see View#viewToModel
  215. */
  216. public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
  217. return super.viewToModel(fx, fy, adjustAllocation(a), bias);
  218. }
  219. /**
  220. * Gives notification that something was inserted into the document
  221. * in a location that this view is responsible for.
  222. *
  223. * @param changes the change information from the associated document
  224. * @param a the current allocation of the view
  225. * @param f the factory to use to rebuild if the view has children
  226. * @see View#insertUpdate
  227. */
  228. public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
  229. super.insertUpdate(changes, adjustAllocation(a), f);
  230. updateVisibilityModel();
  231. }
  232. /**
  233. * Gives notification that something was removed from the document
  234. * in a location that this view is responsible for.
  235. *
  236. * @param changes the change information from the associated document
  237. * @param a the current allocation of the view
  238. * @param f the factory to use to rebuild if the view has children
  239. * @see View#removeUpdate
  240. */
  241. public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
  242. super.removeUpdate(changes, adjustAllocation(a), f);
  243. updateVisibilityModel();
  244. }
  245. }