1. /*
  2. * @(#)View.java 1.70 04/05/18
  3. *
  4. * Copyright 2004 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.SwingConstants;
  10. import javax.swing.event.*;
  11. /**
  12. * <p>
  13. * A very important part of the text package is the <code>View</code> class.
  14. * As the name suggests it represents a view of the text model,
  15. * or a piece of the text model.
  16. * It is this class that is responsible for the look of the text component.
  17. * The view is not intended to be some completely new thing that one must
  18. * learn, but rather is much like a lightweight component.
  19. * In fact, the original <code>View</code> implementation was a
  20. * lightweight component. There were several reasons why
  21. * the <code>Component</code> implementation was abandoned in favor of
  22. * an alternative.
  23. * <ol>
  24. * <li>
  25. * <p>
  26. * There was barely time to get the lightweight component support in the
  27. * 1.1 version of the JDK. There simply wasn't time to lighten up the
  28. * component further to where it would need to be to be used for text purposes.
  29. * The additions made to <code>JComponent</code> increased the memory
  30. * consumption, and as it currently stands its much too heavy for
  31. * representing text.
  32. * </p>
  33. * <li>
  34. * <p>
  35. * The layout semantics aren't quite right for text,
  36. * and changing the current layout
  37. * semantics of component might break existing applications.
  38. * </p>
  39. * <li>
  40. * <p>
  41. * The component api uses integers, but in 1.2 one can use floating point
  42. * device independent coordinates. An api that works in both 1.1 and 1.2
  43. * would be convenient for minimizing transition difficulties. The
  44. * <code>View</code> class uses the <code>Shape</code> interface and
  45. * float arguments to enable View implementations for the Java 2 platform
  46. * v1.2 and later while still functioning in the older 1.1 JDK.
  47. * </p>
  48. * </ol>
  49. * <p>
  50. By default, a view is very light. It contains a reference to the parent
  51. view from which it can fetch many things without holding state, and it
  52. contains a reference to a portion of the model (</code>Element</code>).
  53. A view does not
  54. have to exactly represent an element in the model, that is simply a typical
  55. and therefore convenient mapping. A view can alternatively maintain a couple
  56. of Position objects to maintain its location in the model (i.e. represent
  57. a fragment of an element). This is typically the result of formatting where
  58. views have been broken down into pieces. The convenience of a substantial
  59. relationship to the element makes it easier to build factories to produce the
  60. views, and makes it easier to keep track of the view pieces as the model is
  61. changed and the view must be changed to reflect the model. Simple views
  62. therefore represent an Element directly and complex views do not.
  63. <p>
  64. A view has the following responsibilities:
  65. <dl>
  66. <dt><b>Participate in layout.</b>
  67. <dd>
  68. <p>The view has a <code>setSize</code> method which is like
  69. <code>doLayout</code> and <code>setSize</code> in <code>Component</code> combined.
  70. The view has a <code>preferenceChanged</code> method which is
  71. like <code>invalidate</code> in <code>Component</code> except that one can
  72. invalidate just one axis
  73. and the child requesting the change is identified.
  74. <p>A View expresses the size that it would like to be in terms of three
  75. values, a minimum, a preferred, and a maximum span. Layout in a view is
  76. can be done independently upon each axis. For a properly functioning View
  77. implementation, the minimum span will be <= the preferred span which in turn
  78. will be <= the maximum span.
  79. </p>
  80. <p align=center><img src="doc-files/View-flexibility.jpg"
  81. alt="The above text describes this graphic.">
  82. <p>The minimum set of methods for layout are:
  83. <ul>
  84. <li><a href="#getMinimumSpan(int)">getMinimumSpan</a>
  85. <li><a href="#getPreferredSpan(int)">getPreferredSpan</a>
  86. <li><a href="#getMaximumSpan(int)">getMaximumSpan</a>
  87. <li><a href="#getAlignment(int)">getAlignment</a>
  88. <li><a href="#preferenceChanged(javax.swing.text.View, boolean, boolean)">preferenceChanged</a>
  89. <li><a href="#setSize(float, float)">setSize</a>
  90. </ul>
  91. <p>The <code>setSize</code> method should be prepared to be called a number of times
  92. (i.e. It may be called even if the size didn't change).
  93. The <code>setSize</code> method
  94. is generally called to make sure the View layout is complete prior to trying
  95. to perform an operation on it that requires an up-to-date layout. A view's
  96. size should <em>always</em> be set to a value within the minimum and maximum
  97. span specified by that view. Additionally, the view must always call the
  98. <code>preferenceChanged</code> method on the parent if it has changed the
  99. values for the
  100. layout it would like, and expects the parent to honor. The parent View is
  101. not required to recognize a change until the <code>preferenceChanged</code>
  102. has been sent.
  103. This allows parent View implementations to cache the child requirements if
  104. desired. The calling sequence looks something like the following:
  105. </p>
  106. <p align=center>
  107. <img src="doc-files/View-layout.jpg"
  108. alt="Sample calling sequence between parent view and child view:
  109. setSize, getMinimum, getPreferred, getMaximum, getAlignment, setSize">
  110. <p>The exact calling sequence is up to the layout functionality of
  111. the parent view (if the view has any children). The view may collect
  112. the preferences of the children prior to determining what it will give
  113. each child, or it might iteratively update the children one at a time.
  114. </p>
  115. <dt><b>Render a portion of the model.</b>
  116. <dd>
  117. <p>This is done in the paint method, which is pretty much like a component
  118. paint method. Views are expected to potentially populate a fairly large
  119. tree. A <code>View</code> has the following semantics for rendering:
  120. </p>
  121. <ul>
  122. <li>The view gets its allocation from the parent at paint time, so it
  123. must be prepared to redo layout if the allocated area is different from
  124. what it is prepared to deal with.
  125. <li>The coordinate system is the same as the hosting <code>Component</code>
  126. (i.e. the <code>Component</code> returned by the
  127. <a href="#getContainer">getContainer</a> method).
  128. This means a child view lives in the same coordinate system as the parent
  129. view unless the parent has explicitly changed the coordinate system.
  130. To schedule itself to be repainted a view can call repaint on the hosting
  131. <code>Component</code>.
  132. <li>The default is to <em>not clip</em> the children. It is more efficient
  133. to allow a view to clip only if it really feels it needs clipping.
  134. <li>The <code>Graphics</code> object given is not initialized in any way.
  135. A view should set any settings needed.
  136. <li>A <code>View</code> is inherently transparent. While a view may render into its
  137. entire allocation, typically a view does not. Rendering is performed by
  138. tranversing down the tree of <code>View</code> implementations.
  139. Each <code>View</code> is responsible
  140. for rendering its children. This behavior is depended upon for thread
  141. safety. While view implementations do not necessarily have to be implemented
  142. with thread safety in mind, other view implementations that do make use of
  143. concurrency can depend upon a tree traversal to guarantee thread safety.
  144. <li>The order of views relative to the model is up to the implementation.
  145. Although child views will typically be arranged in the same order that they
  146. occur in the model, they may be visually arranged in an entirely different
  147. order. View implementations may have Z-Order associated with them if the
  148. children are overlapping.
  149. </ul>
  150. <p>The methods for rendering are:
  151. <ul>
  152. <li><a href="#paint(java.awt.Graphics, java.awt.Shape)">paint</a>
  153. </ul>
  154. <p>
  155. <dt><b>Translate between the model and view coordinate systems.</b>
  156. <dd>
  157. <p>Because the view objects are produced from a factory and therefore cannot
  158. necessarily be counted upon to be in a particular pattern, one must be able
  159. to perform translation to properly locate spatial representation of the model.
  160. The methods for doing this are:
  161. <ul>
  162. <li><a href="#modelToView(int, javax.swing.text.Position.Bias, int, javax.swing.text.Position.Bias, java.awt.Shape)">modelToView</a>
  163. <li><a href="#viewToModel(float, float, java.awt.Shape, javax.swing.text.Position.Bias[])">viewToModel</a>
  164. <li><a href="#getDocument()">getDocument</a>
  165. <li><a href="#getElement()">getElement</a>
  166. <li><a href="#getStartOffset()">getStartOffset</a>
  167. <li><a href="#getEndOffset()">getEndOffset</a>
  168. </ul>
  169. <p>The layout must be valid prior to attempting to make the translation.
  170. The translation is not valid, and must not be attempted while changes
  171. are being broadcasted from the model via a <code>DocumentEvent</code>.
  172. </p>
  173. <dt><b>Respond to changes from the model.</b>
  174. <dd>
  175. <p>If the overall view is represented by many pieces (which is the best situation
  176. if one want to be able to change the view and write the least amount of new code),
  177. it would be impractical to have a huge number of <code>DocumentListener</code>s.
  178. If each
  179. view listened to the model, only a few would actually be interested in the
  180. changes broadcasted at any given time. Since the model has no knowledge of
  181. views, it has no way to filter the broadcast of change information. The view
  182. hierarchy itself is instead responsible for propagating the change information.
  183. At any level in the view hierarchy, that view knows enough about its children to
  184. best distribute the change information further. Changes are therefore broadcasted
  185. starting from the root of the view hierarchy.
  186. The methods for doing this are:
  187. <ul>
  188. <li><a href="#insertUpdate">insertUpdate</a>
  189. <li><a href="#removeUpdate">removeUpdate</a>
  190. <li><a href="#changedUpdate">changedUpdate</a>
  191. </ul>
  192. <p>
  193. </dl>
  194. *
  195. * @author Timothy Prinzing
  196. * @version 1.70 05/18/04
  197. */
  198. public abstract class View implements SwingConstants {
  199. /**
  200. * Creates a new <code>View</code> object.
  201. *
  202. * @param elem the <code>Element</code> to represent
  203. */
  204. public View(Element elem) {
  205. this.elem = elem;
  206. }
  207. /**
  208. * Returns the parent of the view.
  209. *
  210. * @return the parent, or <code>null</code> if none exists
  211. */
  212. public View getParent() {
  213. return parent;
  214. }
  215. /**
  216. * Returns a boolean that indicates whether
  217. * the view is visible or not. By default
  218. * all views are visible.
  219. *
  220. * @return always returns true
  221. */
  222. public boolean isVisible() {
  223. return true;
  224. }
  225. /**
  226. * Determines the preferred span for this view along an
  227. * axis.
  228. *
  229. * @param axis may be either <code>View.X_AXIS</code> or
  230. * <code>View.Y_AXIS</code>
  231. * @return the span the view would like to be rendered into.
  232. * Typically the view is told to render into the span
  233. * that is returned, although there is no guarantee.
  234. * The parent may choose to resize or break the view
  235. * @see View#getPreferredSpan
  236. */
  237. public abstract float getPreferredSpan(int axis);
  238. /**
  239. * Determines the minimum span for this view along an
  240. * axis.
  241. *
  242. * @param axis may be either <code>View.X_AXIS</code> or
  243. * <code>View.Y_AXIS</code>
  244. * @return the minimum span the view can be rendered into
  245. * @see View#getPreferredSpan
  246. */
  247. public float getMinimumSpan(int axis) {
  248. int w = getResizeWeight(axis);
  249. if (w == 0) {
  250. // can't resize
  251. return getPreferredSpan(axis);
  252. }
  253. return 0;
  254. }
  255. /**
  256. * Determines the maximum span for this view along an
  257. * axis.
  258. *
  259. * @param axis may be either <code>View.X_AXIS</code> or
  260. * <code>View.Y_AXIS</code>
  261. * @return the maximum span the view can be rendered into
  262. * @see View#getPreferredSpan
  263. */
  264. public float getMaximumSpan(int axis) {
  265. int w = getResizeWeight(axis);
  266. if (w == 0) {
  267. // can't resize
  268. return getPreferredSpan(axis);
  269. }
  270. return Integer.MAX_VALUE;
  271. }
  272. /**
  273. * Child views can call this on the parent to indicate that
  274. * the preference has changed and should be reconsidered
  275. * for layout. By default this just propagates upward to
  276. * the next parent. The root view will call
  277. * <code>revalidate</code> on the associated text component.
  278. *
  279. * @param child the child view
  280. * @param width true if the width preference has changed
  281. * @param height true if the height preference has changed
  282. * @see javax.swing.JComponent#revalidate
  283. */
  284. public void preferenceChanged(View child, boolean width, boolean height) {
  285. View parent = getParent();
  286. if (parent != null) {
  287. parent.preferenceChanged(this, width, height);
  288. }
  289. }
  290. /**
  291. * Determines the desired alignment for this view along an
  292. * axis. The desired alignment is returned. This should be
  293. * a value >= 0.0 and <= 1.0, where 0 indicates alignment at
  294. * the origin and 1.0 indicates alignment to the full span
  295. * away from the origin. An alignment of 0.5 would be the
  296. * center of the view.
  297. *
  298. * @param axis may be either <code>View.X_AXIS</code> or
  299. * <code>View.Y_AXIS</code>
  300. * @return the value 0.5
  301. */
  302. public float getAlignment(int axis) {
  303. return 0.5f;
  304. }
  305. /**
  306. * Renders using the given rendering surface and area on that
  307. * surface. The view may need to do layout and create child
  308. * views to enable itself to render into the given allocation.
  309. *
  310. * @param g the rendering surface to use
  311. * @param allocation the allocated region to render into
  312. * @see View#paint
  313. */
  314. public abstract void paint(Graphics g, Shape allocation);
  315. /**
  316. * Establishes the parent view for this view. This is
  317. * guaranteed to be called before any other methods if the
  318. * parent view is functioning properly. This is also
  319. * the last method called, since it is called to indicate
  320. * the view has been removed from the hierarchy as
  321. * well. When this method is called to set the parent to
  322. * null, this method does the same for each of its children,
  323. * propogating the notification that they have been
  324. * disconnected from the view tree. If this is
  325. * reimplemented, <code>super.setParent()</code> should
  326. * be called.
  327. *
  328. * @param parent the new parent, or <code>null</code> if the view is
  329. * being removed from a parent
  330. */
  331. public void setParent(View parent) {
  332. // if the parent is null then propogate down the view tree
  333. if (parent == null) {
  334. for (int i = 0; i < getViewCount(); i++) {
  335. if (getView(i).getParent() == this) {
  336. // in FlowView.java view might be referenced
  337. // from two super-views as a child. see logicalView
  338. getView(i).setParent(null);
  339. }
  340. }
  341. }
  342. this.parent = parent;
  343. }
  344. /**
  345. * Returns the number of views in this view. Since
  346. * the default is to not be a composite view this
  347. * returns 0.
  348. *
  349. * @return the number of views >= 0
  350. * @see View#getViewCount
  351. */
  352. public int getViewCount() {
  353. return 0;
  354. }
  355. /**
  356. * Gets the <i>n</i>th child view. Since there are no
  357. * children by default, this returns <code>null</code>.
  358. *
  359. * @param n the number of the view to get, >= 0 && < getViewCount()
  360. * @return the view
  361. */
  362. public View getView(int n) {
  363. return null;
  364. }
  365. /**
  366. * Removes all of the children. This is a convenience
  367. * call to <code>replace</code>.
  368. *
  369. * @since 1.3
  370. */
  371. public void removeAll() {
  372. replace(0, getViewCount(), null);
  373. }
  374. /**
  375. * Removes one of the children at the given position.
  376. * This is a convenience call to <code>replace</code>.
  377. * @since 1.3
  378. */
  379. public void remove(int i) {
  380. replace(i, 1, null);
  381. }
  382. /**
  383. * Inserts a single child view. This is a convenience
  384. * call to <code>replace</code>.
  385. *
  386. * @param offs the offset of the view to insert before >= 0
  387. * @param v the view
  388. * @see #replace
  389. * @since 1.3
  390. */
  391. public void insert(int offs, View v) {
  392. View[] one = new View[1];
  393. one[0] = v;
  394. replace(offs, 0, one);
  395. }
  396. /**
  397. * Appends a single child view. This is a convenience
  398. * call to <code>replace</code>.
  399. *
  400. * @param v the view
  401. * @see #replace
  402. * @since 1.3
  403. */
  404. public void append(View v) {
  405. View[] one = new View[1];
  406. one[0] = v;
  407. replace(getViewCount(), 0, one);
  408. }
  409. /**
  410. * Replaces child views. If there are no views to remove
  411. * this acts as an insert. If there are no views to
  412. * add this acts as a remove. Views being removed will
  413. * have the parent set to <code>null</code>, and the internal reference
  414. * to them removed so that they can be garbage collected.
  415. * This is implemented to do nothing, because by default
  416. * a view has no children.
  417. *
  418. * @param offset the starting index into the child views to insert
  419. * the new views. This should be a value >= 0 and <= getViewCount
  420. * @param length the number of existing child views to remove
  421. * This should be a value >= 0 and <= (getViewCount() - offset).
  422. * @param views the child views to add. This value can be
  423. * <code>null</code> to indicate no children are being added
  424. * (useful to remove).
  425. * @since 1.3
  426. */
  427. public void replace(int offset, int length, View[] views) {
  428. }
  429. /**
  430. * Returns the child view index representing the given position in
  431. * the model. By default a view has no children so this is implemented
  432. * to return -1 to indicate there is no valid child index for any
  433. * position.
  434. *
  435. * @param pos the position >= 0
  436. * @return index of the view representing the given position, or
  437. * -1 if no view represents that position
  438. * @since 1.3
  439. */
  440. public int getViewIndex(int pos, Position.Bias b) {
  441. return -1;
  442. }
  443. /**
  444. * Fetches the allocation for the given child view.
  445. * This enables finding out where various views
  446. * are located, without assuming how the views store
  447. * their location. This returns <code>null</code> since the
  448. * default is to not have any child views.
  449. *
  450. * @param index the index of the child, >= 0 && <
  451. * <code>getViewCount()</code>
  452. * @param a the allocation to this view
  453. * @return the allocation to the child
  454. */
  455. public Shape getChildAllocation(int index, Shape a) {
  456. return null;
  457. }
  458. /**
  459. * Provides a way to determine the next visually represented model
  460. * location at which one might place a caret.
  461. * Some views may not be visible,
  462. * they might not be in the same order found in the model, or they just
  463. * might not allow access to some of the locations in the model.
  464. *
  465. * @param pos the position to convert >= 0
  466. * @param a the allocated region in which to render
  467. * @param direction the direction from the current position that can
  468. * be thought of as the arrow keys typically found on a keyboard.
  469. * This will be one of the following values:
  470. * <ul>
  471. * <li>SwingConstants.WEST
  472. * <li>SwingConstants.EAST
  473. * <li>SwingConstants.NORTH
  474. * <li>SwingConstants.SOUTH
  475. * </ul>
  476. * @return the location within the model that best represents the next
  477. * location visual position
  478. * @exception BadLocationException
  479. * @exception IllegalArgumentException if <code>direction</code>
  480. * doesn't have one of the legal values above
  481. */
  482. public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
  483. int direction, Position.Bias[] biasRet)
  484. throws BadLocationException {
  485. biasRet[0] = Position.Bias.Forward;
  486. switch (direction) {
  487. case NORTH:
  488. case SOUTH:
  489. {
  490. if (pos == -1) {
  491. pos = (direction == NORTH) ? Math.max(0, getEndOffset() - 1) :
  492. getStartOffset();
  493. break;
  494. }
  495. JTextComponent target = (JTextComponent) getContainer();
  496. Caret c = (target != null) ? target.getCaret() : null;
  497. // YECK! Ideally, the x location from the magic caret position
  498. // would be passed in.
  499. Point mcp;
  500. if (c != null) {
  501. mcp = c.getMagicCaretPosition();
  502. }
  503. else {
  504. mcp = null;
  505. }
  506. int x;
  507. if (mcp == null) {
  508. Rectangle loc = target.modelToView(pos);
  509. x = (loc == null) ? 0 : loc.x;
  510. }
  511. else {
  512. x = mcp.x;
  513. }
  514. if (direction == NORTH) {
  515. pos = Utilities.getPositionAbove(target, pos, x);
  516. }
  517. else {
  518. pos = Utilities.getPositionBelow(target, pos, x);
  519. }
  520. }
  521. break;
  522. case WEST:
  523. if(pos == -1) {
  524. pos = Math.max(0, getEndOffset() - 1);
  525. }
  526. else {
  527. pos = Math.max(0, pos - 1);
  528. }
  529. break;
  530. case EAST:
  531. if(pos == -1) {
  532. pos = getStartOffset();
  533. }
  534. else {
  535. pos = Math.min(pos + 1, getDocument().getLength());
  536. }
  537. break;
  538. default:
  539. throw new IllegalArgumentException("Bad direction: " + direction);
  540. }
  541. return pos;
  542. }
  543. /**
  544. * Provides a mapping, for a given character,
  545. * from the document model coordinate space
  546. * to the view coordinate space.
  547. *
  548. * @param pos the position of the desired character (>=0)
  549. * @param a the area of the view, which encompasses the requested character
  550. * @param b the bias toward the previous character or the
  551. * next character represented by the offset, in case the
  552. * position is a boundary of two views; <code>b</code> will have one
  553. * of these values:
  554. * <ul>
  555. * <li> <code>Position.Bias.Forward</code>
  556. * <li> <code>Position.Bias.Backward</code>
  557. * </ul>
  558. * @return the bounding box, in view coordinate space,
  559. * of the character at the specified position
  560. * @exception BadLocationException if the specified position does
  561. * not represent a valid location in the associated document
  562. * @exception IllegalArgumentException if <code>b</code> is not one of the
  563. * legal <code>Position.Bias</code> values listed above
  564. * @see View#viewToModel
  565. */
  566. public abstract Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException;
  567. /**
  568. * Provides a mapping, for a given region,
  569. * from the document model coordinate space
  570. * to the view coordinate space. The specified region is
  571. * created as a union of the first and last character positions.
  572. *
  573. * @param p0 the position of the first character (>=0)
  574. * @param b0 the bias of the first character position,
  575. * toward the previous character or the
  576. * next character represented by the offset, in case the
  577. * position is a boundary of two views; <code>b0</code> will have one
  578. * of these values:
  579. * <ul>
  580. * <li> <code>Position.Bias.Forward</code>
  581. * <li> <code>Position.Bias.Backward</code>
  582. * </ul>
  583. * @param p1 the position of the last character (>=0)
  584. * @param b1 the bias for the second character position, defined
  585. * one of the legal values shown above
  586. * @param a the area of the view, which encompasses the requested region
  587. * @return the bounding box which is a union of the region specified
  588. * by the first and last character positions
  589. * @exception BadLocationException if the given position does
  590. * not represent a valid location in the associated document
  591. * @exception IllegalArgumentException if <code>b0</code> or
  592. * <code>b1</code> are not one of the
  593. * legal <code>Position.Bias</code> values listed above
  594. * @see View#viewToModel
  595. */
  596. public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException {
  597. Shape s0 = modelToView(p0, a, b0);
  598. Shape s1;
  599. if (p1 == getEndOffset()) {
  600. try {
  601. s1 = modelToView(p1, a, b1);
  602. } catch (BadLocationException ble) {
  603. s1 = null;
  604. }
  605. if (s1 == null) {
  606. // Assume extends left to right.
  607. Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
  608. a.getBounds();
  609. s1 = new Rectangle(alloc.x + alloc.width - 1, alloc.y,
  610. 1, alloc.height);
  611. }
  612. }
  613. else {
  614. s1 = modelToView(p1, a, b1);
  615. }
  616. Rectangle r0 = s0.getBounds();
  617. Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle) s1 :
  618. s1.getBounds();
  619. if (r0.y != r1.y) {
  620. // If it spans lines, force it to be the width of the view.
  621. Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
  622. a.getBounds();
  623. r0.x = alloc.x;
  624. r0.width = alloc.width;
  625. }
  626. r0.add(r1);
  627. return r0;
  628. }
  629. /**
  630. * Provides a mapping from the view coordinate space to the logical
  631. * coordinate space of the model. The <code>biasReturn</code>
  632. * argument will be filled in to indicate that the point given is
  633. * closer to the next character in the model or the previous
  634. * character in the model.
  635. *
  636. * @param x the X coordinate >= 0
  637. * @param y the Y coordinate >= 0
  638. * @param a the allocated region in which to render
  639. * @return the location within the model that best represents the
  640. * given point in the view >= 0. The <code>biasReturn</code>
  641. * argument will be
  642. * filled in to indicate that the point given is closer to the next
  643. * character in the model or the previous character in the model.
  644. */
  645. public abstract int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn);
  646. /**
  647. * Gives notification that something was inserted into
  648. * the document in a location that this view is responsible for.
  649. * To reduce the burden to subclasses, this functionality is
  650. * spread out into the following calls that subclasses can
  651. * reimplement:
  652. * <ol>
  653. * <li><a href="#updateChildren">updateChildren</a> is called
  654. * if there were any changes to the element this view is
  655. * responsible for. If this view has child views that are
  656. * represent the child elements, then this method should do
  657. * whatever is necessary to make sure the child views correctly
  658. * represent the model.
  659. * <li><a href="#forwardUpdate">forwardUpdate</a> is called
  660. * to forward the DocumentEvent to the appropriate child views.
  661. * <li><a href="#updateLayout">updateLayout</a> is called to
  662. * give the view a chance to either repair its layout, to reschedule
  663. * layout, or do nothing.
  664. * </ol>
  665. *
  666. * @param e the change information from the associated document
  667. * @param a the current allocation of the view
  668. * @param f the factory to use to rebuild if the view has children
  669. * @see View#insertUpdate
  670. */
  671. public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  672. if (getViewCount() > 0) {
  673. Element elem = getElement();
  674. DocumentEvent.ElementChange ec = e.getChange(elem);
  675. if (ec != null) {
  676. if (! updateChildren(ec, e, f)) {
  677. // don't consider the element changes they
  678. // are for a view further down.
  679. ec = null;
  680. }
  681. }
  682. forwardUpdate(ec, e, a, f);
  683. updateLayout(ec, e, a);
  684. }
  685. }
  686. /**
  687. * Gives notification that something was removed from the document
  688. * in a location that this view is responsible for.
  689. * To reduce the burden to subclasses, this functionality is
  690. * spread out into the following calls that subclasses can
  691. * reimplement:
  692. * <ol>
  693. * <li><a href="#updateChildren">updateChildren</a> is called
  694. * if there were any changes to the element this view is
  695. * responsible for. If this view has child views that are
  696. * represent the child elements, then this method should do
  697. * whatever is necessary to make sure the child views correctly
  698. * represent the model.
  699. * <li><a href="#forwardUpdate">forwardUpdate</a> is called
  700. * to forward the DocumentEvent to the appropriate child views.
  701. * <li><a href="#updateLayout">updateLayout</a> is called to
  702. * give the view a chance to either repair its layout, to reschedule
  703. * layout, or do nothing.
  704. * </ol>
  705. *
  706. * @param e the change information from the associated document
  707. * @param a the current allocation of the view
  708. * @param f the factory to use to rebuild if the view has children
  709. * @see View#removeUpdate
  710. */
  711. public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  712. if (getViewCount() > 0) {
  713. Element elem = getElement();
  714. DocumentEvent.ElementChange ec = e.getChange(elem);
  715. if (ec != null) {
  716. if (! updateChildren(ec, e, f)) {
  717. // don't consider the element changes they
  718. // are for a view further down.
  719. ec = null;
  720. }
  721. }
  722. forwardUpdate(ec, e, a, f);
  723. updateLayout(ec, e, a);
  724. }
  725. }
  726. /**
  727. * Gives notification from the document that attributes were changed
  728. * in a location that this view is responsible for.
  729. * To reduce the burden to subclasses, this functionality is
  730. * spread out into the following calls that subclasses can
  731. * reimplement:
  732. * <ol>
  733. * <li><a href="#updateChildren">updateChildren</a> is called
  734. * if there were any changes to the element this view is
  735. * responsible for. If this view has child views that are
  736. * represent the child elements, then this method should do
  737. * whatever is necessary to make sure the child views correctly
  738. * represent the model.
  739. * <li><a href="#forwardUpdate">forwardUpdate</a> is called
  740. * to forward the DocumentEvent to the appropriate child views.
  741. * <li><a href="#updateLayout">updateLayout</a> is called to
  742. * give the view a chance to either repair its layout, to reschedule
  743. * layout, or do nothing.
  744. * </ol>
  745. *
  746. * @param e the change information from the associated document
  747. * @param a the current allocation of the view
  748. * @param f the factory to use to rebuild if the view has children
  749. * @see View#changedUpdate
  750. */
  751. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  752. if (getViewCount() > 0) {
  753. Element elem = getElement();
  754. DocumentEvent.ElementChange ec = e.getChange(elem);
  755. if (ec != null) {
  756. if (! updateChildren(ec, e, f)) {
  757. // don't consider the element changes they
  758. // are for a view further down.
  759. ec = null;
  760. }
  761. }
  762. forwardUpdate(ec, e, a, f);
  763. updateLayout(ec, e, a);
  764. }
  765. }
  766. /**
  767. * Fetches the model associated with the view.
  768. *
  769. * @return the view model, <code>null</code> if none
  770. * @see View#getDocument
  771. */
  772. public Document getDocument() {
  773. return elem.getDocument();
  774. }
  775. /**
  776. * Fetches the portion of the model for which this view is
  777. * responsible.
  778. *
  779. * @return the starting offset into the model >= 0
  780. * @see View#getStartOffset
  781. */
  782. public int getStartOffset() {
  783. return elem.getStartOffset();
  784. }
  785. /**
  786. * Fetches the portion of the model for which this view is
  787. * responsible.
  788. *
  789. * @return the ending offset into the model >= 0
  790. * @see View#getEndOffset
  791. */
  792. public int getEndOffset() {
  793. return elem.getEndOffset();
  794. }
  795. /**
  796. * Fetches the structural portion of the subject that this
  797. * view is mapped to. The view may not be responsible for the
  798. * entire portion of the element.
  799. *
  800. * @return the subject
  801. * @see View#getElement
  802. */
  803. public Element getElement() {
  804. return elem;
  805. }
  806. /**
  807. * Fetch a <code>Graphics</code> for rendering.
  808. * This can be used to determine
  809. * font characteristics, and will be different for a print view
  810. * than a component view.
  811. *
  812. * @return a <code>Graphics</code> object for rendering
  813. * @since 1.3
  814. */
  815. public Graphics getGraphics() {
  816. // PENDING(prinz) this is a temporary implementation
  817. Component c = getContainer();
  818. return c.getGraphics();
  819. }
  820. /**
  821. * Fetches the attributes to use when rendering. By default
  822. * this simply returns the attributes of the associated element.
  823. * This method should be used rather than using the element
  824. * directly to obtain access to the attributes to allow
  825. * view-specific attributes to be mixed in or to allow the
  826. * view to have view-specific conversion of attributes by
  827. * subclasses.
  828. * Each view should document what attributes it recognizes
  829. * for the purpose of rendering or layout, and should always
  830. * access them through the <code>AttributeSet</code> returned
  831. * by this method.
  832. */
  833. public AttributeSet getAttributes() {
  834. return elem.getAttributes();
  835. }
  836. /**
  837. * Tries to break this view on the given axis. This is
  838. * called by views that try to do formatting of their
  839. * children. For example, a view of a paragraph will
  840. * typically try to place its children into row and
  841. * views representing chunks of text can sometimes be
  842. * broken down into smaller pieces.
  843. * <p>
  844. * This is implemented to return the view itself, which
  845. * represents the default behavior on not being
  846. * breakable. If the view does support breaking, the
  847. * starting offset of the view returned should be the
  848. * given offset, and the end offset should be less than
  849. * or equal to the end offset of the view being broken.
  850. *
  851. * @param axis may be either <code>View.X_AXIS</code> or
  852. * <code>View.Y_AXIS</code>
  853. * @param offset the location in the document model
  854. * that a broken fragment would occupy >= 0. This
  855. * would be the starting offset of the fragment
  856. * returned
  857. * @param pos the position along the axis that the
  858. * broken view would occupy >= 0. This may be useful for
  859. * things like tab calculations
  860. * @param len specifies the distance along the axis
  861. * where a potential break is desired >= 0
  862. * @return the fragment of the view that represents the
  863. * given span, if the view can be broken. If the view
  864. * doesn't support breaking behavior, the view itself is
  865. * returned.
  866. * @see ParagraphView
  867. */
  868. public View breakView(int axis, int offset, float pos, float len) {
  869. return this;
  870. }
  871. /**
  872. * Creates a view that represents a portion of the element.
  873. * This is potentially useful during formatting operations
  874. * for taking measurements of fragments of the view. If
  875. * the view doesn't support fragmenting (the default), it
  876. * should return itself.
  877. *
  878. * @param p0 the starting offset >= 0. This should be a value
  879. * greater or equal to the element starting offset and
  880. * less than the element ending offset.
  881. * @param p1 the ending offset > p0. This should be a value
  882. * less than or equal to the elements end offset and
  883. * greater than the elements starting offset.
  884. * @return the view fragment, or itself if the view doesn't
  885. * support breaking into fragments
  886. * @see LabelView
  887. */
  888. public View createFragment(int p0, int p1) {
  889. return this;
  890. }
  891. /**
  892. * Determines how attractive a break opportunity in
  893. * this view is. This can be used for determining which
  894. * view is the most attractive to call <code>breakView</code>
  895. * on in the process of formatting. A view that represents
  896. * text that has whitespace in it might be more attractive
  897. * than a view that has no whitespace, for example. The
  898. * higher the weight, the more attractive the break. A
  899. * value equal to or lower than <code>BadBreakWeight</code>
  900. * should not be considered for a break. A value greater
  901. * than or equal to <code>ForcedBreakWeight</code> should
  902. * be broken.
  903. * <p>
  904. * This is implemented to provide the default behavior
  905. * of returning <code>BadBreakWeight</code> unless the length
  906. * is greater than the length of the view in which case the
  907. * entire view represents the fragment. Unless a view has
  908. * been written to support breaking behavior, it is not
  909. * attractive to try and break the view. An example of
  910. * a view that does support breaking is <code>LabelView</code>.
  911. * An example of a view that uses break weight is
  912. * <code>ParagraphView</code>.
  913. *
  914. * @param axis may be either <code>View.X_AXIS</code> or
  915. * <code>View.Y_AXIS</code>
  916. * @param pos the potential location of the start of the
  917. * broken view >= 0. This may be useful for calculating tab
  918. * positions
  919. * @param len specifies the relative length from <em>pos</em>
  920. * where a potential break is desired >= 0
  921. * @return the weight, which should be a value between
  922. * ForcedBreakWeight and BadBreakWeight
  923. * @see LabelView
  924. * @see ParagraphView
  925. * @see #BadBreakWeight
  926. * @see #GoodBreakWeight
  927. * @see #ExcellentBreakWeight
  928. * @see #ForcedBreakWeight
  929. */
  930. public int getBreakWeight(int axis, float pos, float len) {
  931. if (len > getPreferredSpan(axis)) {
  932. return GoodBreakWeight;
  933. }
  934. return BadBreakWeight;
  935. }
  936. /**
  937. * Determines the resizability of the view along the
  938. * given axis. A value of 0 or less is not resizable.
  939. *
  940. * @param axis may be either <code>View.X_AXIS</code> or
  941. * <code>View.Y_AXIS</code>
  942. * @return the weight
  943. */
  944. public int getResizeWeight(int axis) {
  945. return 0;
  946. }
  947. /**
  948. * Sets the size of the view. This should cause
  949. * layout of the view along the given axis, if it
  950. * has any layout duties.
  951. *
  952. * @param width the width >= 0
  953. * @param height the height >= 0
  954. */
  955. public void setSize(float width, float height) {
  956. }
  957. /**
  958. * Fetches the container hosting the view. This is useful for
  959. * things like scheduling a repaint, finding out the host
  960. * components font, etc. The default implementation
  961. * of this is to forward the query to the parent view.
  962. *
  963. * @return the container, <code>null</code> if none
  964. */
  965. public Container getContainer() {
  966. View v = getParent();
  967. return (v != null) ? v.getContainer() : null;
  968. }
  969. /**
  970. * Fetches the <code>ViewFactory</code> implementation that is feeding
  971. * the view hierarchy. Normally the views are given this
  972. * as an argument to updates from the model when they
  973. * are most likely to need the factory, but this
  974. * method serves to provide it at other times.
  975. *
  976. * @return the factory, <code>null</code> if none
  977. */
  978. public ViewFactory getViewFactory() {
  979. View v = getParent();
  980. return (v != null) ? v.getViewFactory() : null;
  981. }
  982. /**
  983. * Returns the tooltip text at the specified location. The default
  984. * implementation returns the value from the child View identified by
  985. * the passed in location.
  986. *
  987. * @since 1.4
  988. * @see JTextComponent#getToolTipText
  989. */
  990. public String getToolTipText(float x, float y, Shape allocation) {
  991. int viewIndex = getViewIndex(x, y, allocation);
  992. if (viewIndex >= 0) {
  993. allocation = getChildAllocation(viewIndex, allocation);
  994. Rectangle rect = (allocation instanceof Rectangle) ?
  995. (Rectangle)allocation : allocation.getBounds();
  996. if (rect.contains(x, y)) {
  997. return getView(viewIndex).getToolTipText(x, y, allocation);
  998. }
  999. }
  1000. return null;
  1001. }
  1002. /**
  1003. * Returns the child view index representing the given position in
  1004. * the view. This iterates over all the children returning the
  1005. * first with a bounds that contains <code>x</code>, <code>y</code>.
  1006. *
  1007. * @param x the x coordinate
  1008. * @param y the y coordinate
  1009. * @param allocation current allocation of the View.
  1010. * @return index of the view representing the given location, or
  1011. * -1 if no view represents that position
  1012. * @since 1.4
  1013. */
  1014. public int getViewIndex(float x, float y, Shape allocation) {
  1015. for (int counter = getViewCount() - 1; counter >= 0; counter--) {
  1016. Shape childAllocation = getChildAllocation(counter, allocation);
  1017. if (childAllocation != null) {
  1018. Rectangle rect = (childAllocation instanceof Rectangle) ?
  1019. (Rectangle)childAllocation : allocation.getBounds();
  1020. if (rect.contains(x, y)) {
  1021. return counter;
  1022. }
  1023. }
  1024. }
  1025. return -1;
  1026. }
  1027. /**
  1028. * Updates the child views in response to receiving notification
  1029. * that the model changed, and there is change record for the
  1030. * element this view is responsible for. This is implemented
  1031. * to assume the child views are directly responsible for the
  1032. * child elements of the element this view represents. The
  1033. * <code>ViewFactory</code> is used to create child views for each element
  1034. * specified as added in the <code>ElementChange</code>, starting at the
  1035. * index specified in the given <code>ElementChange</code>. The number of
  1036. * child views representing the removed elements specified are
  1037. * removed.
  1038. *
  1039. * @param ec the change information for the element this view
  1040. * is responsible for. This should not be <code>null</code> if
  1041. * this method gets called
  1042. * @param e the change information from the associated document
  1043. * @param f the factory to use to build child views
  1044. * @return whether or not the child views represent the
  1045. * child elements of the element this view is responsible
  1046. * for. Some views create children that represent a portion
  1047. * of the element they are responsible for, and should return
  1048. * false. This information is used to determine if views
  1049. * in the range of the added elements should be forwarded to
  1050. * or not
  1051. * @see #insertUpdate
  1052. * @see #removeUpdate
  1053. * @see #changedUpdate
  1054. * @since 1.3
  1055. */
  1056. protected boolean updateChildren(DocumentEvent.ElementChange ec,
  1057. DocumentEvent e, ViewFactory f) {
  1058. Element[] removedElems = ec.getChildrenRemoved();
  1059. Element[] addedElems = ec.getChildrenAdded();
  1060. View[] added = null;
  1061. if (addedElems != null) {
  1062. added = new View[addedElems.length];
  1063. for (int i = 0; i < addedElems.length; i++) {
  1064. added[i] = f.create(addedElems[i]);
  1065. }
  1066. }
  1067. int nremoved = 0;
  1068. int index = ec.getIndex();
  1069. if (removedElems != null) {
  1070. nremoved = removedElems.length;
  1071. }
  1072. replace(index, nremoved, added);
  1073. return true;
  1074. }
  1075. /**
  1076. * Forwards the given <code>DocumentEvent</code> to the child views
  1077. * that need to be notified of the change to the model.
  1078. * If there were changes to the element this view is
  1079. * responsible for, that should be considered when
  1080. * forwarding (i.e. new child views should not get
  1081. * notified).
  1082. *
  1083. * @param ec changes to the element this view is responsible
  1084. * for (may be <code>null</code> if there were no changes).
  1085. * @param e the change information from the associated document
  1086. * @param a the current allocation of the view
  1087. * @param f the factory to use to rebuild if the view has children
  1088. * @see #insertUpdate
  1089. * @see #removeUpdate
  1090. * @see #changedUpdate
  1091. * @since 1.3
  1092. */
  1093. protected void forwardUpdate(DocumentEvent.ElementChange ec,
  1094. DocumentEvent e, Shape a, ViewFactory f) {
  1095. Element elem = getElement();
  1096. int pos = e.getOffset();
  1097. int index0 = getViewIndex(pos, Position.Bias.Forward);
  1098. if (index0 == -1 && e.getType() == DocumentEvent.EventType.REMOVE &&
  1099. pos >= getEndOffset()) {
  1100. // Event beyond our offsets. We may have represented this, that is
  1101. // the remove may have removed one of our child Elements that
  1102. // represented this, so, we should foward to last element.
  1103. index0 = getViewCount() - 1;
  1104. }
  1105. int index1 = index0;
  1106. View v = (index0 >= 0) ? getView(index0) : null;
  1107. if (v != null) {
  1108. if ((v.getStartOffset() == pos) && (pos > 0)) {
  1109. // If v is at a boundary, forward the event to the previous
  1110. // view too.
  1111. index0 = Math.max(index0 - 1, 0);
  1112. }
  1113. }
  1114. if (e.getType() != DocumentEvent.EventType.REMOVE) {
  1115. index1 = getViewIndex(pos + e.getLength(), Position.Bias.Forward);
  1116. if (index1 < 0) {
  1117. index1 = getViewCount() - 1;
  1118. }
  1119. }
  1120. int hole0 = index1 + 1;
  1121. int hole1 = hole0;
  1122. Element[] addedElems = (ec != null) ? ec.getChildrenAdded() : null;
  1123. if ((addedElems != null) && (addedElems.length > 0)) {
  1124. hole0 = ec.getIndex();
  1125. hole1 = hole0 + addedElems.length - 1;
  1126. }
  1127. // forward to any view not in the forwarding hole
  1128. // formed by added elements (i.e. they will be updated
  1129. // by initialization.
  1130. index0 = Math.max(index0, 0);
  1131. for (int i = index0; i <= index1; i++) {
  1132. if (! ((i >= hole0) && (i <= hole1))) {
  1133. v = getView(i);
  1134. if (v != null) {
  1135. Shape childAlloc = getChildAllocation(i, a);
  1136. forwardUpdateToView(v, e, childAlloc, f);
  1137. }
  1138. }
  1139. }
  1140. }
  1141. /**
  1142. * Forwards the <code>DocumentEvent</code> to the give child view. This
  1143. * simply messages the view with a call to <code>insertUpdate</code>,
  1144. * <code>removeUpdate</code>, or <code>changedUpdate</code> depending
  1145. * upon the type of the event. This is called by
  1146. * <a href="#forwardUpdate">forwardUpdate</a> to forward
  1147. * the event to children that need it.
  1148. *
  1149. * @param v the child view to forward the event to
  1150. * @param e the change information from the associated document
  1151. * @param a the current allocation of the view
  1152. * @param f the factory to use to rebuild if the view has children
  1153. * @see #forwardUpdate
  1154. * @since 1.3
  1155. */
  1156. protected void forwardUpdateToView(View v, DocumentEvent e,
  1157. Shape a, ViewFactory f) {
  1158. DocumentEvent.EventType type = e.getType();
  1159. if (type == DocumentEvent.EventType.INSERT) {
  1160. v.insertUpdate(e, a, f);
  1161. } else if (type == DocumentEvent.EventType.REMOVE) {
  1162. v.removeUpdate(e, a, f);
  1163. } else {
  1164. v.changedUpdate(e, a, f);
  1165. }
  1166. }
  1167. /**
  1168. * Updates the layout in response to receiving notification of
  1169. * change from the model. This is implemented to call
  1170. * <code>preferenceChanged</code> to reschedule a new layout
  1171. * if the <code>ElementChange</code> record is not <code>null</code>.
  1172. *
  1173. * @param ec changes to the element this view is responsible
  1174. * for (may be <code>null</code> if there were no changes)
  1175. * @param e the change information from the associated document
  1176. * @param a the current allocation of the view
  1177. * @see #insertUpdate
  1178. * @see #removeUpdate
  1179. * @see #changedUpdate
  1180. * @since 1.3
  1181. */
  1182. protected void updateLayout(DocumentEvent.ElementChange ec,
  1183. DocumentEvent e, Shape a) {
  1184. if ((ec != null) && (a != null)) {
  1185. // should damage more intelligently
  1186. preferenceChanged(null, true, true);
  1187. Container host = getContainer();
  1188. if (host != null) {
  1189. host.repaint();
  1190. }
  1191. }
  1192. }
  1193. /**
  1194. * The weight to indicate a view is a bad break
  1195. * opportunity for the purpose of formatting. This
  1196. * value indicates that no attempt should be made to
  1197. * break the view into fragments as the view has
  1198. * not been written to support fragmenting.
  1199. *
  1200. * @see #getBreakWeight
  1201. * @see #GoodBreakWeight
  1202. * @see #ExcellentBreakWeight
  1203. * @see #ForcedBreakWeight
  1204. */
  1205. public static final int BadBreakWeight = 0;
  1206. /**
  1207. * The weight to indicate a view supports breaking,
  1208. * but better opportunities probably exist.
  1209. *
  1210. * @see #getBreakWeight
  1211. * @see #BadBreakWeight
  1212. * @see #ExcellentBreakWeight
  1213. * @see #ForcedBreakWeight
  1214. */
  1215. public static final int GoodBreakWeight = 1000;
  1216. /**
  1217. * The weight to indicate a view supports breaking,
  1218. * and this represents a very attractive place to
  1219. * break.
  1220. *
  1221. * @see #getBreakWeight
  1222. * @see #BadBreakWeight
  1223. * @see #GoodBreakWeight
  1224. * @see #ForcedBreakWeight
  1225. */
  1226. public static final int ExcellentBreakWeight = 2000;
  1227. /**
  1228. * The weight to indicate a view supports breaking,
  1229. * and must be broken to be represented properly
  1230. * when placed in a view that formats its children
  1231. * by breaking them.
  1232. *
  1233. * @see #getBreakWeight
  1234. * @see #BadBreakWeight
  1235. * @see #GoodBreakWeight
  1236. * @see #ExcellentBreakWeight
  1237. */
  1238. public static final int ForcedBreakWeight = 3000;
  1239. /**
  1240. * Axis for format/break operations.
  1241. */
  1242. public static final int X_AXIS = HORIZONTAL;
  1243. /**
  1244. * Axis for format/break operations.
  1245. */
  1246. public static final int Y_AXIS = VERTICAL;
  1247. /**
  1248. * Provides a mapping from the document model coordinate space
  1249. * to the coordinate space of the view mapped to it. This is
  1250. * implemented to default the bias to <code>Position.Bias.Forward</code>
  1251. * which was previously implied.
  1252. *
  1253. * @param pos the position to convert >= 0
  1254. * @param a the allocated region in which to render
  1255. * @return the bounding box of the given position is returned
  1256. * @exception BadLocationException if the given position does
  1257. * not represent a valid location in the associated document
  1258. * @see View#modelToView
  1259. * @deprecated
  1260. */
  1261. @Deprecated
  1262. public Shape modelToView(int pos, Shape a) throws BadLocationException {
  1263. return modelToView(pos, a, Position.Bias.Forward);
  1264. }
  1265. /**
  1266. * Provides a mapping from the view coordinate space to the logical
  1267. * coordinate space of the model.
  1268. *
  1269. * @param x the X coordinate >= 0
  1270. * @param y the Y coordinate >= 0
  1271. * @param a the allocated region in which to render
  1272. * @return the location within the model that best represents the
  1273. * given point in the view >= 0
  1274. * @see View#viewToModel
  1275. * @deprecated
  1276. */
  1277. @Deprecated
  1278. public int viewToModel(float x, float y, Shape a) {
  1279. sharedBiasReturn[0] = Position.Bias.Forward;
  1280. return viewToModel(x, y, a, sharedBiasReturn);
  1281. }
  1282. // static argument available for viewToModel calls since only
  1283. // one thread at a time may call this method.
  1284. static final Position.Bias[] sharedBiasReturn = new Position.Bias[1];
  1285. private View parent;
  1286. private Element elem;
  1287. };