1. /*
  2. * @(#)View.java 1.36 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.SwingConstants;
  10. import javax.swing.event.*;
  11. /**
  12. <p>
  13. A very important part of the text package is the View class. As the name
  14. suggests it represents a view of the text model, or a piece of the text
  15. model. It is this class that is responsible for the look of the text component.
  16. The view is not intended to be some completely new thing that one must learn,
  17. but rather is much like a lightweight component. In fact, the original View
  18. implementation was a lightweight component. There were several reasons why
  19. the Component implementation was abandoned in favor of an alternative.
  20. <ol>
  21. <li>
  22. <p>There was barely had time to get the lightweight component support in the
  23. 1.1 version of the JDK. There simply wasn't time to lighten up the component
  24. further to where it would need to be to be used for text purposes. The additions
  25. made to JComponent increased the memory consumption, and as it currently stands
  26. it's much too heavy for representing text.
  27. </p>
  28. <li>
  29. <p>The layout semantics aren't quite right for text, and changing the current layout
  30. semantics of component might break existing applications.
  31. </p>
  32. <li>
  33. <p>The component api uses integers, but in 1.2 one can use floating point device
  34. independent coordinates. An api that works in both 1.1 and 1.2 would be convenient
  35. for minimizing transition difficulties. The View class uses the Shape interface
  36. and float arguments to enable View implementations in JDK 1.2 and later while
  37. still functioning in the older 1.1 JDK.
  38. </p>
  39. </ol>
  40. <p>
  41. By default, a view is very light. It contains a reference to the parent
  42. view from which it can fetch many things without holding state, and it
  43. contains a reference to a portion of the model (Element). A view does not
  44. have to exactly represent an element in the model, that is simply a typical
  45. and therefore convenient mapping. A view can alternatively maintain a couple
  46. of Position objects to maintain it's location in the model (i.e. represent
  47. a fragment of an element). This is typically the result of formatting where
  48. views have been broken down into pieces. The convenience of a substantial
  49. relationship to the element makes it easier to build factories to produce the
  50. views, and makes it easier to keep track of the view pieces as the model is
  51. changed and the view must be changed to reflect the model. Simple views
  52. therefore represent an Element directly and complex views do not.
  53. <p>
  54. A view has the following responsibilities:
  55. <dl>
  56. <dt><b>Participate in layout.</b>
  57. <dd>
  58. <p>The view has a setSize method which is like doLayout and setSize in
  59. Component combined. The view has a preferenceChanged method which is
  60. like invalidate in Component except that one can invalidate just one axis
  61. and the child requesting the change is identified.
  62. <p>A View expresses the size that it would like to be in terms of three
  63. values, a minimum, a preferred, and a maximum span. Layout in a view is
  64. can be done independantly upon each axis. For a properly functioning View
  65. implementation, the minimum span will be <= the preferred span which in turn
  66. will be <= the maximum span.
  67. </p>
  68. <p align=center><img src="View-flexibility.jpg">
  69. <p>The minimum set of methods for layout are:
  70. <ul>
  71. <li><a href="#getMinimumSpan">getMinimumSpan</a>
  72. <li><a href="#getPreferredSpan">getPreferredSpan</a>
  73. <li><a href="#getMaximumSpan">getMaximumSpan</a>
  74. <li><a href="#getAlignment">getAlignment</a>
  75. <li><a href="#preferenceChanged">preferenceChanged</a>
  76. <li><a href="#setSize">setSize</a>
  77. </ul>
  78. <p>The setSize method should be prepared to be called a number of times
  79. (i.e. It may be called even if the size didn't change). The setSize method
  80. is generally called to make sure the View layout is complete prior to trying
  81. to perform an operation on it that requires an up-to-date layout. A views
  82. size should <em>always</em> be set to a value within the minimum and maximum
  83. span specified by that view. Additionally, the view must always call the
  84. preferenceChanged method on the parent if it has changed the values for the
  85. layout it would like, and expects the parent to honor. The parent View is
  86. not required to recognize a change until the preferenceChanged has been sent.
  87. This allows parent View implementations to cache the child requirements if
  88. desired. The calling sequence looks something like the following:
  89. </p>
  90. <p align=center><img src="View-layout.jpg">
  91. <p>The exact calling sequence is up to the layout functionality of
  92. the parent view (if the view has any children). The view may collect
  93. the preferences of the children prior to determining what it will give
  94. each child, or it might iteratively update the children one at a time.
  95. </p>
  96. <dt><b>Render a portion of the model.</b>
  97. <dd>
  98. <p>This is done in the paint method, which is pretty much like a component
  99. paint method. Views are expected to potentially populate a fairly large
  100. tree. A View has the following semantics for rendering:
  101. </p>
  102. <ul>
  103. <li>The view gets it's allocation from the parent at paint time, so it
  104. must be prepared to redo layout if the allocated area is different from
  105. what it is prepared to deal with.
  106. <li>The coordinate system is the same as the hosting Component (i.e. the
  107. Component returned by the <a href="#getContainer">getContainer</a> method).
  108. This means a child view lives in the same coordinate system as the parent
  109. view unless the parent has explicitly changed the coordinate system.
  110. To schedule itself to be repainted a view can call repaint on the hosting
  111. Component.
  112. <li>The default is to <em>not clip</em> the children. It is more effecient
  113. to allow a view to clip only if it really feels it needs clipping.
  114. <li>The Graphics object given is not initialized in any way. A view should
  115. set any settings needed.
  116. <li>A View is inherently transparent. While a view may render into it's
  117. entire allocation, typically a view does not. Rendering is performed by
  118. tranversing down the tree of View implementations. Each View is responsible
  119. for rendering it's children. This behavior is depended upon for thread
  120. safety. While view implementations do not necessarily have to be implemented
  121. with thread safety in mind, other view implementations that do make use of
  122. concurrency can depend upon a tree traversal to guarantee thread safety.
  123. <li>The order of views relative to the model is up to the implementation.
  124. Although child views will typically be arranged in the same order that they
  125. occur in the model, they may be visually arranged in an entirely different
  126. order. View implementations may have Z-Order associated with them if the
  127. children are overlapping.
  128. </ul>
  129. <p>The methods for rendering are:
  130. <ul>
  131. <li><a href="#paint">paint</a>
  132. </ul>
  133. <p>
  134. <dt><b>Translate between the model and view coordinate systems.</b>
  135. <dd>
  136. <p>Because the view objects are produced from a factory and therefore cannot
  137. necessarily be counted upon to be in a particular pattern, one must be able
  138. to perform translation to properly locate spatial representation of the model.
  139. The methods for doing this are:
  140. <ul>
  141. <li><a href="#modelToView">modelToView</a>
  142. <li><a href="#viewToModel">viewToModel</a>
  143. <li><a href="#getDocument">getDocument</a>
  144. <li><a href="#getElement">getElement</a>
  145. <li><a href="#getStartOffset">getStartOffset</a>
  146. <li><a href="#getEndOffset">getEndOffset</a>
  147. </ul>
  148. <p>The layout must be valid prior to attempting to make the translation.
  149. The translation is not valid, and must not be attempted while changes
  150. are being broadcasted from the model via a DocumentEvent.
  151. </p>
  152. <dt><b>Respond to changes from the model.</b>
  153. <dd>
  154. <p>If the overall view is represented by many pieces (which is the best situation
  155. if one want to be able to change the view and write the least amount of new code),
  156. it would be impractical to have a huge number of DocumentListeners. If each
  157. view listened to the model, only a few would actually be interested in the
  158. changes broadcasted at any given time. Since the model has no knowledge of
  159. views, it has no way to filter the broadcast of change information. The view
  160. hierarchy itself is instead responsible for propagating the change information.
  161. At any level in the view hierarchy, that view knows enough about it's children to
  162. best distribute the change information further. Changes are therefore broadcasted
  163. starting from the root of the view hierarchy.
  164. The methods for doing this are:
  165. <ul>
  166. <li><a href="#insertUpdate">insertUpdate</a>
  167. <li><a href="#removeUpdate">removeUpdate</a>
  168. <li><a href="#changedUpdate">changedUpdate</a>
  169. </ul>
  170. <p>
  171. *
  172. * @author Timothy Prinzing
  173. * @version 1.36 11/29/01
  174. */
  175. public abstract class View implements SwingConstants {
  176. /**
  177. * Creates a new View object.
  178. *
  179. * @param elem the element to represent
  180. */
  181. public View(Element elem) {
  182. this.elem = elem;
  183. }
  184. /**
  185. * Returns the parent of the view.
  186. *
  187. * @return the parent, null if none
  188. */
  189. public View getParent() {
  190. return parent;
  191. }
  192. /**
  193. * Returns a boolean that indicates whether
  194. * the view is visible or not. By default
  195. * all views are visible.
  196. *
  197. * @return boolean value.
  198. */
  199. public boolean isVisible() {
  200. return true;
  201. }
  202. /**
  203. * Determines the preferred span for this view along an
  204. * axis.
  205. *
  206. * @param axis may be either View.X_AXIS or View.Y_AXIS
  207. * @returns the span the view would like to be rendered into.
  208. * Typically the view is told to render into the span
  209. * that is returned, although there is no guarantee.
  210. * The parent may choose to resize or break the view.
  211. * @see View#getPreferredSpan
  212. */
  213. public abstract float getPreferredSpan(int axis);
  214. /**
  215. * Determines the minimum span for this view along an
  216. * axis.
  217. *
  218. * @param axis may be either View.X_AXIS or View.Y_AXIS
  219. * @returns the minimum span the view can be rendered into.
  220. * @see View#getPreferredSpan
  221. */
  222. public float getMinimumSpan(int axis) {
  223. int w = getResizeWeight(axis);
  224. if (w == 0) {
  225. // can't resize
  226. return getPreferredSpan(axis);
  227. }
  228. return 0;
  229. }
  230. /**
  231. * Determines the maximum span for this view along an
  232. * axis.
  233. *
  234. * @param axis may be either View.X_AXIS or View.Y_AXIS
  235. * @returns the maximum span the view can be rendered into.
  236. * @see View#getPreferredSpan
  237. */
  238. public float getMaximumSpan(int axis) {
  239. int w = getResizeWeight(axis);
  240. if (w == 0) {
  241. // can't resize
  242. return getPreferredSpan(axis);
  243. }
  244. return Integer.MAX_VALUE;
  245. }
  246. /**
  247. * Child views can call this on the parent to indicate that
  248. * the preference has changed and should be reconsidered
  249. * for layout. By default this just propagates upward to
  250. * the next parent. The root view will call
  251. * <code>revalidate</code> on the associated text component.
  252. *
  253. * @param child the child view
  254. * @param width true if the width preference has changed
  255. * @param height true if the height preference has changed
  256. * @see javax.swing.JComponent#revalidate
  257. */
  258. public void preferenceChanged(View child, boolean width, boolean height) {
  259. View parent = getParent();
  260. if (parent != null) {
  261. parent.preferenceChanged(this, width, height);
  262. }
  263. }
  264. /**
  265. * Determines the desired alignment for this view along an
  266. * axis. By default this is simply centered.
  267. *
  268. * @param axis may be either View.X_AXIS or View.Y_AXIS
  269. * @returns The desired alignment. This should be a value
  270. * >= 0.0 and <= 1.0 where 0 indicates alignment at the
  271. * origin and 1.0 indicates alignment to the full span
  272. * away from the origin. An alignment of 0.5 would be the
  273. * center of the view.
  274. */
  275. public float getAlignment(int axis) {
  276. return 0.5f;
  277. }
  278. /**
  279. * Renders using the given rendering surface and area on that
  280. * surface. The view may need to do layout and create child
  281. * views to enable itself to render into the given allocation.
  282. *
  283. * @param g the rendering surface to use
  284. * @param allocation the allocated region to render into
  285. * @see View#paint
  286. */
  287. public abstract void paint(Graphics g, Shape allocation);
  288. /**
  289. * Establishes the parent view for this view. This is
  290. * guaranteed to be called before any other methods if the
  291. * parent view is functioning properly. This is also
  292. * the last method called, since it is called to indicate
  293. * the view has been removed from the hierarchy as
  294. * well. If this is reimplemented,
  295. * <code>super.setParent()</code> should be called.
  296. *
  297. * @param parent the new parent, or null if the view is
  298. * being removed from a parent it was previously added
  299. * to
  300. */
  301. public void setParent(View parent) {
  302. this.parent = parent;
  303. }
  304. /**
  305. * Returns the number of views in this view. Since
  306. * the default is to not be a composite view this
  307. * returns 0.
  308. *
  309. * @return the number of views >= 0
  310. * @see View#getViewCount
  311. */
  312. public int getViewCount() {
  313. return 0;
  314. }
  315. /**
  316. * Gets the nth child view. Since there are no
  317. * children by default, this returns null.
  318. *
  319. * @param n the number of the view to get, >= 0 && < getViewCount()
  320. * @return the view
  321. */
  322. public View getView(int n) {
  323. return null;
  324. }
  325. /**
  326. * Fetches the allocation for the given child view.
  327. * This enables finding out where various views
  328. * are located, without assuming the views store
  329. * their location. This returns null since the
  330. * default is to not have any child views.
  331. *
  332. * @param index the index of the child, >= 0 && < getViewCount()
  333. * @param a the allocation to this view.
  334. * @return the allocation to the child
  335. */
  336. public Shape getChildAllocation(int index, Shape a) {
  337. return null;
  338. }
  339. /**
  340. * Provides a way to determine the next visually represented model
  341. * location that one might place a caret. Some views may not be visible,
  342. * they might not be in the same order found in the model, or they just
  343. * might not allow access to some of the locations in the model.
  344. *
  345. * @param pos the position to convert >= 0
  346. * @param a the allocated region to render into
  347. * @param direction the direction from the current position that can
  348. * be thought of as the arrow keys typically found on a keyboard.
  349. * This may be SwingConstants.WEST, SwingConstants.EAST,
  350. * SwingConstants.NORTH, or SwingConstants.SOUTH.
  351. * @return the location within the model that best represents the next
  352. * location visual position.
  353. * @exception BadLocationException
  354. * @exception IllegalArgumentException for an invalid direction
  355. */
  356. public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
  357. int direction, Position.Bias[] biasRet)
  358. throws BadLocationException {
  359. biasRet[0] = Position.Bias.Forward;
  360. switch (direction) {
  361. case NORTH:
  362. {
  363. JTextComponent target = (JTextComponent) getContainer();
  364. Rectangle r = target.modelToView(pos);
  365. pos = Utilities.getPositionAbove(target, pos, r.x);
  366. }
  367. break;
  368. case SOUTH:
  369. {
  370. JTextComponent target = (JTextComponent) getContainer();
  371. Rectangle r = target.modelToView(pos);
  372. pos = Utilities.getPositionBelow(target, pos, r.x);
  373. }
  374. break;
  375. case WEST:
  376. if(pos == -1) {
  377. pos = Math.max(0, getEndOffset() - 1);
  378. }
  379. else {
  380. pos = Math.max(0, pos - 1);
  381. }
  382. break;
  383. case EAST:
  384. if(pos == -1) {
  385. pos = getStartOffset();
  386. }
  387. else {
  388. pos = Math.min(pos + 1, getDocument().getLength());
  389. }
  390. break;
  391. default:
  392. throw new IllegalArgumentException("Bad direction: " + direction);
  393. }
  394. return pos;
  395. }
  396. /**
  397. * Provides a mapping from the document model coordinate space
  398. * to the coordinate space of the view mapped to it.
  399. *
  400. * @param pos the position to convert >= 0
  401. * @param a the allocated region to render into
  402. * @param b the bias toward the previous character or the
  403. * next character represented by the offset, in case the
  404. * position is a boundary of two views.
  405. * @return the bounding box of the given position is returned
  406. * @exception BadLocationException if the given position does
  407. * not represent a valid location in the associated document
  408. * @exception IllegalArgumentException for an invalid bias argument
  409. * @see View#viewToModel
  410. */
  411. public abstract Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException;
  412. /**
  413. * Provides a mapping from the document model coordinate space
  414. * to the coordinate space of the view mapped to it.
  415. *
  416. * @param p0 the position to convert >= 0
  417. * @param b0 the bias toward the previous character or the
  418. * next character represented by p0, in case the
  419. * position is a boundary of two views.
  420. * @param p1 the position to convert >= 0
  421. * @param b1 the bias toward the previous character or the
  422. * next character represented by p1, in case the
  423. * position is a boundary of two views.
  424. * @param a the allocated region to render into
  425. * @return the bounding box of the given position is returned
  426. * @exception BadLocationException if the given position does
  427. * not represent a valid location in the associated document
  428. * @exception IllegalArgumentException for an invalid bias argument
  429. * @see View#viewToModel
  430. */
  431. public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException {
  432. Shape s0 = modelToView(p0, a, b0);
  433. Shape s1;
  434. if (p1 == getEndOffset()) {
  435. try {
  436. s1 = modelToView(p1, a, b1);
  437. } catch (BadLocationException ble) {
  438. s1 = null;
  439. }
  440. if (s1 == null) {
  441. // Assume extends left to right.
  442. Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
  443. a.getBounds();
  444. s1 = new Rectangle(alloc.x + alloc.width - 1, alloc.y,
  445. 1, alloc.height);
  446. }
  447. }
  448. else {
  449. s1 = modelToView(p1, a, b1);
  450. }
  451. Rectangle r0 = s0.getBounds();
  452. Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle) s1 :
  453. s1.getBounds();
  454. if (r0.y != r1.y) {
  455. // If it spans lines, force it to be the width of the view.
  456. Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
  457. a.getBounds();
  458. r0.x = alloc.x;
  459. r0.width = alloc.width;
  460. }
  461. r0.add(r1);
  462. return r0;
  463. }
  464. /**
  465. * Provides a mapping from the view coordinate space to the logical
  466. * coordinate space of the model. The biasReturn argument will be
  467. * filled in to indicate that the point given is closer to the next
  468. * character in the model or the previous character in the model.
  469. *
  470. * @param x the X coordinate >= 0
  471. * @param y the Y coordinate >= 0
  472. * @param a the allocated region to render into
  473. * @return the location within the model that best represents the
  474. * given point in the view >= 0. The biasReturn argument will be
  475. * filled in to indicate that the point given is closer to the next
  476. * character in the model or the previous character in the model.
  477. */
  478. public abstract int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn);
  479. /**
  480. * Gives notification that something was inserted into the document
  481. * in a location that this view is responsible for.
  482. *
  483. * @param e the change information from the associated document
  484. * @param a the current allocation of the view
  485. * @param f the factory to use to rebuild if the view has children
  486. * @see View#insertUpdate
  487. */
  488. public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  489. }
  490. /**
  491. * Gives notification from the document that attributes were removed
  492. * in a location that this view is responsible for.
  493. *
  494. * @param e the change information from the associated document
  495. * @param a the current allocation of the view
  496. * @param f the factory to use to rebuild if the view has children
  497. * @see View#removeUpdate
  498. */
  499. public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  500. }
  501. /**
  502. * Gives notification from the document that attributes were changed
  503. * in a location that this view is responsible for.
  504. *
  505. * @param e the change information from the associated document
  506. * @param a the current allocation of the view
  507. * @param f the factory to use to rebuild if the view has children
  508. * @see View#changedUpdate
  509. */
  510. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  511. }
  512. /**
  513. * Fetches the model associated with the view.
  514. *
  515. * @return the view model, null if none
  516. * @see View#getDocument
  517. */
  518. public Document getDocument() {
  519. return elem.getDocument();
  520. }
  521. /**
  522. * Fetches the portion of the model that this view is
  523. * responsible for.
  524. *
  525. * @return the starting offset into the model >= 0
  526. * @see View#getStartOffset
  527. */
  528. public int getStartOffset() {
  529. return elem.getStartOffset();
  530. }
  531. /**
  532. * Fetches the portion of the model that this view is
  533. * responsible for.
  534. *
  535. * @return the ending offset into the model >= 0
  536. * @see View#getEndOffset
  537. */
  538. public int getEndOffset() {
  539. return elem.getEndOffset();
  540. }
  541. /**
  542. * Fetches the structural portion of the subject that this
  543. * view is mapped to. The view may not be responsible for the
  544. * entire portion of the element.
  545. *
  546. * @return the subject
  547. * @see View#getElement
  548. */
  549. public Element getElement() {
  550. return elem;
  551. }
  552. /**
  553. * Fetches the attributes to use when rendering. By default
  554. * this simply returns the attributes of the associated element.
  555. * This method should be used rather than using the element
  556. * directly to obtain access to the attributes to allow
  557. * view-specific attributes to be mixed in or to allow the
  558. * view to have view-specific conversion of attributes by
  559. * subclasses.
  560. * Each view should document what attributes it recognizes
  561. * for the purpose of rendering or layout, and should always
  562. * access them through the AttributeSet returned by this method.
  563. */
  564. public AttributeSet getAttributes() {
  565. return elem.getAttributes();
  566. }
  567. /**
  568. * Tries to break this view on the given axis. This is
  569. * called by views that try to do formatting of their
  570. * children. For example, a view of a paragraph will
  571. * typically try to place its children into row and
  572. * views representing chunks of text can sometimes be
  573. * broken down into smaller pieces.
  574. * <p>
  575. * This is implemented to return the view itself, which
  576. * represents the default behavior on not being
  577. * breakable. If the view does support breaking, the
  578. * starting offset of the view returned should be the
  579. * given offset, and the end offset should be less than
  580. * or equal to the end offset of the view being broken.
  581. *
  582. * @param axis may be either View.X_AXIS or View.Y_AXIS
  583. * @param offset the location in the document model
  584. * that a broken fragment would occupy >= 0. This
  585. * would be the starting offset of the fragment
  586. * returned.
  587. * @param pos the position along the axis that the
  588. * broken view would occupy >= 0. This may be useful for
  589. * things like tab calculations.
  590. * @param len specifies the distance along the axis
  591. * where a potential break is desired >= 0.
  592. * @return the fragment of the view that represents the
  593. * given span, if the view can be broken. If the view
  594. * doesn't support breaking behavior, the view itself is
  595. * returned.
  596. * @see ParagraphView
  597. */
  598. public View breakView(int axis, int offset, float pos, float len) {
  599. return this;
  600. }
  601. /**
  602. * Create a view that represents a portion of the element.
  603. * This is potentially useful during formatting operations
  604. * for taking measurements of fragments of the view. If
  605. * the view doesn't support fragmenting (the default), it
  606. * should return itself.
  607. *
  608. * @param p0 the starting offset >= 0. This should be a value
  609. * greater or equal to the element starting offset and
  610. * less than the element ending offset.
  611. * @param p1 the ending offset > p0. This should be a value
  612. * less than or equal to the elements end offset and
  613. * greater than the elements starting offset.
  614. * @returns the view fragment, or itself if the view doesn't
  615. * support breaking into fragments.
  616. * @see LabelView
  617. */
  618. public View createFragment(int p0, int p1) {
  619. return this;
  620. }
  621. /**
  622. * Determines how attractive a break opportunity in
  623. * this view is. This can be used for determining which
  624. * view is the most attractive to call <code>breakView</code>
  625. * on in the process of formatting. A view that represents
  626. * text that has whitespace in it might be more attractive
  627. * than a view that has no whitespace, for example. The
  628. * higher the weight, the more attractive the break. A
  629. * value equal to or lower than <code>BadBreakWeight</code>
  630. * should not be considered for a break. A value greater
  631. * than or equal to <code>ForcedBreakWeight</code> should
  632. * be broken.
  633. * <p>
  634. * This is implemented to provide the default behavior
  635. * of returning <code>BadBreakWeight</code> unless the length
  636. * is greater than the length of the view in which case the
  637. * entire view represents the fragment. Unless a view has
  638. * been written to support breaking behavior, it is not
  639. * attractive to try and break the view. An example of
  640. * a view that does support breaking is <code>LabelView</code>.
  641. * An example of a view that uses break weight is
  642. * <code>ParagraphView</code>.
  643. *
  644. * @param axis may be either View.X_AXIS or View.Y_AXIS
  645. * @param pos the potential location of the start of the
  646. * broken view >= 0. This may be useful for calculating tab
  647. * positions.
  648. * @param len specifies the relative length from <em>pos</em>
  649. * where a potential break is desired >= 0.
  650. * @return the weight, which should be a value between
  651. * ForcedBreakWeight and BadBreakWeight.
  652. * @see LabelView
  653. * @see ParagraphView
  654. * @see BadBreakWeight
  655. * @see GoodBreakWeight
  656. * @see ExcellentBreakWeight
  657. * @see ForcedBreakWeight
  658. */
  659. public int getBreakWeight(int axis, float pos, float len) {
  660. if (len > getPreferredSpan(axis)) {
  661. return GoodBreakWeight;
  662. }
  663. return BadBreakWeight;
  664. }
  665. /**
  666. * Determines the resizability of the view along the
  667. * given axis. A value of 0 or less is not resizable.
  668. *
  669. * @param axis View.X_AXIS or View.Y_AXIS
  670. * @return the weight
  671. */
  672. public int getResizeWeight(int axis) {
  673. return 0;
  674. }
  675. /**
  676. * Sets the size of the view. This should cause
  677. * layout of the view, if it has any layout duties.
  678. * The default is to do nothing.
  679. *
  680. * @param width the width >= 0
  681. * @param height the height >= 0
  682. */
  683. public void setSize(float width, float height) {
  684. }
  685. /**
  686. * Fetches the container hosting the view. This is useful for
  687. * things like scheduling a repaint, finding out the host
  688. * components font, etc. The default implementation
  689. * of this is to forward the query to the parent view.
  690. *
  691. * @return the container, null if none
  692. */
  693. public Container getContainer() {
  694. View v = getParent();
  695. return (v != null) ? v.getContainer() : null;
  696. }
  697. /**
  698. * Fetches the ViewFactory implementation that is feeding
  699. * the view hierarchy. Normally the views are given this
  700. * as an argument to updates from the model when they
  701. * are most likely to need the factory, but this
  702. * method serves to provide it at other times.
  703. *
  704. * @return the factory, null if none
  705. */
  706. public ViewFactory getViewFactory() {
  707. View v = getParent();
  708. return (v != null) ? v.getViewFactory() : null;
  709. }
  710. /**
  711. * The weight to indicate a view is a bad break
  712. * opportunity for the purpose of formatting. This
  713. * value indicates that no attempt should be made to
  714. * break the view into fragments as the view has
  715. * not been written to support fragmenting.
  716. * @see #getBreakWeight
  717. * @see GoodBreakWeight
  718. * @see ExcellentBreakWeight
  719. * @see ForcedBreakWeight
  720. */
  721. public static final int BadBreakWeight = 0;
  722. /**
  723. * The weight to indicate a view supports breaking,
  724. * but better opportunities probably exist.
  725. *
  726. * @see #getBreakWeight
  727. * @see BadBreakWeight
  728. * @see GoodBreakWeight
  729. * @see ExcellentBreakWeight
  730. * @see ForcedBreakWeight
  731. */
  732. public static final int GoodBreakWeight = 1000;
  733. /**
  734. * The weight to indicate a view supports breaking,
  735. * and this represents a very attractive place to
  736. * break.
  737. *
  738. * @see #getBreakWeight
  739. * @see BadBreakWeight
  740. * @see GoodBreakWeight
  741. * @see ExcellentBreakWeight
  742. * @see ForcedBreakWeight
  743. */
  744. public static final int ExcellentBreakWeight = 2000;
  745. /**
  746. * The weight to indicate a view supports breaking,
  747. * and must be broken to be represented properly
  748. * when placed in a view that formats it's children
  749. * by breaking them.
  750. *
  751. * @see #getBreakWeight
  752. * @see BadBreakWeight
  753. * @see GoodBreakWeight
  754. * @see ExcellentBreakWeight
  755. * @see ForcedBreakWeight
  756. */
  757. public static final int ForcedBreakWeight = 3000;
  758. /**
  759. * Axis for format/break operations.
  760. */
  761. public static final int X_AXIS = HORIZONTAL;
  762. /**
  763. * Axis for format/break operations.
  764. */
  765. public static final int Y_AXIS = VERTICAL;
  766. /**
  767. * Provides a mapping from the document model coordinate space
  768. * to the coordinate space of the view mapped to it. This is
  769. * implemented to default the bias to Position.Bias.Forward
  770. * which was previously implied.
  771. *
  772. * @param pos the position to convert >= 0
  773. * @param a the allocated region to render into
  774. * @return the bounding box of the given position is returned
  775. * @exception BadLocationException if the given position does
  776. * not represent a valid location in the associated document
  777. * @see View#modelToView
  778. * @deprecated
  779. */
  780. public Shape modelToView(int pos, Shape a) throws BadLocationException {
  781. return modelToView(pos, a, Position.Bias.Forward);
  782. }
  783. /**
  784. * Provides a mapping from the view coordinate space to the logical
  785. * coordinate space of the model.
  786. *
  787. * @param x the X coordinate >= 0
  788. * @param y the Y coordinate >= 0
  789. * @param a the allocated region to render into
  790. * @return the location within the model that best represents the
  791. * given point in the view >= 0
  792. * @see View#viewToModel
  793. * @deprecated
  794. */
  795. public int viewToModel(float x, float y, Shape a) {
  796. sharedBiasReturn[0] = Position.Bias.Forward;
  797. return viewToModel(x, y, a, sharedBiasReturn);
  798. }
  799. // static argument available for viewToModel calls since only
  800. // one thread at a time may call this method.
  801. static final Position.Bias[] sharedBiasReturn = new Position.Bias[1];
  802. private View parent;
  803. private Element elem;
  804. };