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