1. /*
  2. * @(#)Document.java 1.31 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 javax.swing.event.*;
  9. /**
  10. * <p>
  11. * The Document is a container for text that serves as the model
  12. * for swing text components. The goal for this interface is to
  13. * scale from very simple needs (plain text textfield) to
  14. * complex needs (HTML or XML documents for example).
  15. *
  16. * <p><b><font size=+1>Content</font></b>
  17. * <p>
  18. * At the simplest level, text can be
  19. * modeled as a linear sequence of characters. To support
  20. * internationalization, the Swing text model uses
  21. * <a href="http://www.unicode.org/">unicode</a> characters.
  22. * The sequence of characters displayed in a text component is
  23. * generally referred to as the component's <em>content</em>.
  24. * <p>
  25. * To refer to locations within the sequence, the coordinates
  26. * used are the location between two characters. As the diagram
  27. * below shows, a location in a text document can be referred to
  28. * as a position, or an offset. This position is zero-based.
  29. * <p align=center><img src="doc-files/Document-coord.gif">
  30. * <p>
  31. * In the example, if the content of a document is the
  32. * sequence "The quick brown fox," as shown in the preceding diagram,
  33. * the location just before the word "The" is 0, and the location after
  34. * the word "The" and before the whitespace that follows it is 3.
  35. * The entire sequence of characters in the sequence "The" is called a
  36. * <em>range</em>.
  37. * <p>The following methods give access to the character data
  38. * that makes up the content.
  39. * <ul>
  40. * <li><a href="#getLength">getLength</a>
  41. * <li><a href="#getText(int, int)">getText(int, int)</a>
  42. * <li><a href="#getText(int, int, javax.swing.text.Segment)">getText(int, int, Segment)</a>
  43. * </ul>
  44. * <p><b><font size=+1>Structure</font></b>
  45. * <p>
  46. * Text is rarely represented simply as featureless content. Rather,
  47. * text typically has some sort of structure associated with it.
  48. * Exactly what structure is modeled is up to a particular Document
  49. * implementation. It might be as simple as no structure (i.e. a
  50. * simple text field), or it might be something like diagram below.
  51. * <p align=center><img src="doc-files/Document-structure.gif">
  52. * <p>
  53. * The unit of structure (i.e. a node of the tree) is referred to
  54. * by the <a href="Element.html">Element</a> interface. Each Element
  55. * can be tagged with a set of attributes. These attributes
  56. * (name/value pairs) are defined by the
  57. * <a href="AttributeSet.html">AttributeSet</a> interface.
  58. * <p>The following methods give access to the document structure.
  59. * <ul>
  60. * <li><a href="#getDefaultRootElement">getDefaultRootElement</a>
  61. * <li><a href="#getRootElements">getRootElements</a>
  62. * </ul>
  63. *
  64. * <p><b><font size=+1>Mutations</font></b>
  65. * <p>
  66. * All documents need to be able to add and remove simple text.
  67. * Typically, text is inserted and removed via gestures from
  68. * a keyboard or a mouse. What effect the insertion or removal
  69. * has upon the document structure is entirely up to the
  70. * implementation of the document.
  71. * <p>The following methods are related to mutation of the
  72. * document content:
  73. * <ul>
  74. * <li><a href="#insertString">insertString</a>
  75. * <li><a href="#remove">remove</a>
  76. * <li><a href="#createPosition">createPosition</a>
  77. * </ul>
  78. *
  79. * <p><b><font size=+1>Notification</font></b>
  80. * <p>
  81. * Mutations to the Document must be communicated to interested
  82. * observers. The notification of change follows the event model
  83. * guidelines that are specified for JavaBeans. In the JavaBeans
  84. * event model, once an event notification is dispatched, all listeners
  85. * must be notified before any further mutations occur to the source
  86. * of the event. Further, order of delivery is not guaranteed.
  87. * <p>
  88. * Notification is provided as two seperate events,
  89. * <a href="../event/DocumentEvent.html">DocumentEvent<a>, and
  90. * <a href="../event/UndoableEditEvent.html">UndoableEditEvent</a>.
  91. * If a mutation is made to a Document through its api,
  92. * a DocumentEvent will be sent to all of the registered
  93. * DocumentListeners. If the Document implementation supports
  94. * undo/redo capabilities, an UndoableEditEvent will be sent
  95. * to all of the registered UndoableEditListeners.
  96. * If an undoable edit is undone, a DocumentEvent should be
  97. * fired from the Document to indicate it has changed again.
  98. * In this case however, there should be no UndoableEditEvent
  99. * generated since that edit is actually the source of the change
  100. * rather than a mutation to the Document made through it's
  101. * api.
  102. * <p align=center><img src="doc-files/Document-notification.gif">
  103. * <p>
  104. * Referring to the above diagram, suppose that the component shown
  105. * on the left mutates the document object represented by the blue
  106. * rectangle. The document responds by dispatching a DocumentEvent to
  107. * both component views and sends an UndoableEditEvent to the listening
  108. * logic, which maintains a history buffer.
  109. * <p>
  110. * Now suppose that the component shown on the right mutates the same
  111. * document. Again, the document dispatches a DocumentEvent to both
  112. * component views and sends an UndoableEditEvent to the listening logic
  113. * that is maintaining the history buffer.
  114. * <p>
  115. * If the history buffer is then rolled back (i.e. the last UndoableEdit
  116. * undone), a DocumentEvent is sent to both views, causing both of them to
  117. * reflect the undone mutation to the document (that is, the
  118. * removal of the right component's mutation). If the history buffer again
  119. * rolls back another change, another DocumentEvent is sent to both views,
  120. * causing them to reflect the undone mutation to the document -- that is,
  121. * the removal of the left component's mutation.
  122. * <p>
  123. * The methods related to observing mutations to the document are:
  124. * <ul>
  125. * <li><a href="#addDocumentListener">addDocumentListener</a>
  126. * <li><a href="#removeDocumentListener">removeDocumentListener</a>
  127. * <li><a href="#addUndoableEditListener">addUndoableEditListener</a>
  128. * <li><a href="#removeUndoableEditListener">removeUndoableEditListener</a>
  129. * </ul>
  130. *
  131. * <p><b><font size=+1>Properties</font></b>
  132. * <p>
  133. * Document implementations will generally have some set of properties
  134. * associated with them at runtime. Two well known properties are the
  135. * <a href="#StreamDescriptionProperty">StreamDescriptionProperty</a>
  136. * which can be used to describe where the Document came from, and
  137. * <a href="#TitleProperty">TitleProperty</a> which can be used to
  138. * name the Document. The methods related to the properties are:
  139. * <ul>
  140. * <li><a href="#getProperty">getProperty</a>
  141. * <li><a href="#putProperty">putProperty</a>
  142. * </ul>
  143. *
  144. * @author Timothy Prinzing
  145. * @version 1.31 11/29/01
  146. *
  147. * @see javax.swing.event.DocumentEvent
  148. * @see javax.swing.event.DocumentListener
  149. * @see javax.swing.event.UndoableEditEvent
  150. * @see javax.swing.event.UndoableEditListener
  151. * @see Element
  152. * @see Position
  153. * @see AttributeSet
  154. */
  155. public interface Document {
  156. /**
  157. * Returns number of characters of content currently
  158. * in the document.
  159. *
  160. * @return number of characters >= 0
  161. */
  162. public int getLength();
  163. /**
  164. * Registers the given observer to begin receiving notifications
  165. * when changes are made to the document.
  166. *
  167. * @param listener the observer to register
  168. * @see Document#removeDocumentListener
  169. */
  170. public void addDocumentListener(DocumentListener listener);
  171. /**
  172. * Unregisters the given observer from the notification list
  173. * so it will no longer receive change updates.
  174. *
  175. * @param listener the observer to register
  176. * @see Document#addDocumentListener
  177. */
  178. public void removeDocumentListener(DocumentListener listener);
  179. /**
  180. * Registers the given observer to begin receiving notifications
  181. * when undoable edits are made to the document.
  182. *
  183. * @param listener the observer to register
  184. * @see javax.swing.event.UndoableEditEvent
  185. */
  186. public void addUndoableEditListener(UndoableEditListener listener);
  187. /**
  188. * Unregisters the given observer from the notification list
  189. * so it will no longer receive updates.
  190. *
  191. * @param listener the observer to register
  192. * @see javax.swing.event.UndoableEditEvent
  193. */
  194. public void removeUndoableEditListener(UndoableEditListener listener);
  195. /**
  196. * Gets properties associated with the document. Allows one to
  197. * store things like the document title, author, etc.
  198. *
  199. * @param key a non-null property
  200. * @return the properties
  201. */
  202. public Object getProperty(Object key);
  203. /**
  204. * Puts a new property on the list.
  205. *
  206. * @param key the non-null property key
  207. * @param value the property value
  208. */
  209. public void putProperty(Object key, Object value);
  210. /**
  211. * Removes a portion of the content of the document.
  212. * This will cause a DocumentEvent of type
  213. * DocumentEvent.EventType.REMOVE to be sent to the
  214. * registered DocumentListeners, unless an exception
  215. * is thrown. The notification will be sent to the
  216. * listeners by calling the removeUpdate method on the
  217. * DocumentListeners.
  218. * <p>
  219. * To ensure reasonable behavior in the face
  220. * of concurrency, the event is dispatched after the
  221. * mutation has occurred. This means that by the time a
  222. * notification of removal is dispatched, the document
  223. * has already been updated and any marks created by
  224. * createPosition have already changed.
  225. * For a removal, the end of the removal range is collapsed
  226. * down to the start of the range, and any marks in the removal
  227. * range are collapsed down to the start of the range.
  228. * <p align=center><img src="doc-files/Document-remove.gif">
  229. * <p>
  230. * If the Document structure changed as result of the removal,
  231. * the details of what Elements were inserted and removed in
  232. * response to the change will also be contained in the generated
  233. * DocumentEvent. It is up to the implementation of a Document
  234. * to decide how the structure should change in response to a
  235. * remove.
  236. * <p>
  237. * If the Document supports undo/redo, an UndoableEditEvent will
  238. * also be generated.
  239. *
  240. * @param offs the offset from the begining >= 0
  241. * @param len the number of characters to remove >= 0
  242. * @exception BadLocationException some portion of the removal range
  243. * was not a valid part of the document. The location in the exception
  244. * is the first bad position encountered.
  245. * @see javax.swing.event.DocumentEvent
  246. * @see javax.swing.event.DocumentListener
  247. * @see javax.swing.event.UndoableEditEvent
  248. * @see javax.swing.event.UndoableEditListener
  249. */
  250. public void remove(int offs, int len) throws BadLocationException;
  251. /**
  252. * Inserts a string of content. This will cause a DocumentEvent
  253. * of type DocumentEvent.EventType.INSERT to be sent to the
  254. * registered DocumentListers, unless an exception is thrown.
  255. * The DocumentEvent will be delivered by calling the
  256. * insertUpdate method on the DocumentListener.
  257. * The offset and length of the generated DocumentEvent
  258. * will indicate what change was actually made to the Document.
  259. * <p align=center><img src="doc-files/Document-insert.gif">
  260. * <p>
  261. * If the Document structure changed as result of the insertion,
  262. * the details of what Elements were inserted and removed in
  263. * response to the change will also be contained in the generated
  264. * DocumentEvent. It is up to the implementation of a Document
  265. * to decide how the structure should change in response to an
  266. * insertion.
  267. * <p>
  268. * If the Document supports undo/redo, an UndoableEditEvent will
  269. * also be generated.
  270. *
  271. * @param offset the offset into the document to insert the content >= 0.
  272. * All positions that track change at or after the given location
  273. * will move.
  274. * @param str the string to insert
  275. * @param a the attributes to associate with the inserted
  276. * content. This may be null if there are no attributes.
  277. * @exception BadLocationException the given insert position is not a valid
  278. * position within the document
  279. * @see javax.swing.event.DocumentEvent
  280. * @see javax.swing.event.DocumentListener
  281. * @see javax.swing.event.UndoableEditEvent
  282. * @see javax.swing.event.UndoableEditListener
  283. */
  284. public void insertString(int offset, String str, AttributeSet a) throws BadLocationException;
  285. /**
  286. * Fetches the text contained within the given portion
  287. * of the document.
  288. *
  289. * @param offset the offset into the document representing the desired
  290. * start of the text >= 0
  291. * @param length the length of the desired string >= 0
  292. * @return the text, in a String of length >= 0
  293. * @exception BadLocationException some portion of the given range
  294. * was not a valid part of the document. The location in the exception
  295. * is the first bad position encountered.
  296. */
  297. public String getText(int offset, int length) throws BadLocationException;
  298. /**
  299. * Fetches the text contained within the given portion
  300. * of the document.
  301. *
  302. * @param offset the offset into the document representing the desired
  303. * start of the text >= 0
  304. * @param length the length of the desired string >= 0
  305. * @param txt the Segment object to return the text in
  306. *
  307. * @exception BadLocationException Some portion of the given range
  308. * was not a valid part of the document. The location in the exception
  309. * is the first bad position encountered.
  310. */
  311. public void getText(int offset, int length, Segment txt) throws BadLocationException;
  312. /**
  313. * Returns a position that represents the start of the document. The
  314. * position returned can be counted on to track change and stay
  315. * located at the beginning of the document.
  316. *
  317. * @return the position
  318. */
  319. public Position getStartPosition();
  320. /**
  321. * Returns a position that represents the end of the document. The
  322. * position returned can be counted on to track change and stay
  323. * located at the end of the document.
  324. *
  325. * @return the position
  326. */
  327. public Position getEndPosition();
  328. /**
  329. * This method allows an application to mark a place in
  330. * a sequence of character content. This mark can then be
  331. * used to tracks change as insertions and removals are made
  332. * in the content. The policy is that insertions always
  333. * occur prior to the current position (the most common case)
  334. * unless the insertion location is zero, in which case the
  335. * insertion is forced to a position that follows the
  336. * original position.
  337. *
  338. * @param offs the offset from the start of the document >= 0
  339. * @return the position
  340. * @exception BadLocationException if the given position does not
  341. * represent a valid location in the associated document
  342. */
  343. public Position createPosition(int offs) throws BadLocationException;
  344. /**
  345. * Returns all of the root elements that are defined.
  346. * <p>
  347. * Typically there will be only one document structure, but the interface
  348. * supports building an arbitrary number of structural projections over the
  349. * text data. The document can have multiple root elements to support
  350. * multiple document structures. Some examples might be:
  351. * </p>
  352. * <ul>
  353. * <li>Text direction.
  354. * <li>Lexical token streams.
  355. * <li>Parse trees.
  356. * <li>Conversions to formats other than the native format.
  357. * <li>Modification specifications.
  358. * <li>Annotations.
  359. * </ul>
  360. *
  361. * @return the root element
  362. */
  363. public Element[] getRootElements();
  364. /**
  365. * Returns the root element that views should be based upon,
  366. * unless some other mechanism for assigning views to element
  367. * structures is provided.
  368. *
  369. * @return the root element
  370. */
  371. public Element getDefaultRootElement();
  372. /**
  373. * This allows the model to be safely rendered in the presence
  374. * of currency, if the model supports being updated asynchronously.
  375. * The given runnable will be executed in a way that allows it
  376. * to safely read the model with no changes while the runnable
  377. * is being executed. The runnable itself may <em>not</em>
  378. * make any mutations.
  379. *
  380. * @param r a Runnable used to render the model
  381. */
  382. public void render(Runnable r);
  383. /**
  384. * The property name for the description of the stream
  385. * used to initialize the document. This should be used
  386. * if the document was initialized from a stream and
  387. * anything is known about the stream.
  388. */
  389. public static final String StreamDescriptionProperty = "stream";
  390. /**
  391. * The property name for the title of the document, if
  392. * there is one.
  393. */
  394. public static final String TitleProperty = "title";
  395. }