1 /*******************************************************************************
2 * Copyright (c) 2000, 2019 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
18 * Clients may implement the StyledTextContent interface to provide a
19 * custom store for the StyledText widget content. The StyledText widget
20 * interacts with its StyledTextContent in order to access and update
21 * the text that is being displayed and edited in the widget.
22 * A custom content implementation can be set in the widget using the
23 * StyledText.setContent API.
25 public interface StyledTextContent {
28 * Called by StyledText to add itself as an Observer to content changes.
29 * See TextChangeListener for a description of the listener methods that
30 * are called when text changes occur.
33 * @param listener the listener
34 * @exception IllegalArgumentException <ul>
35 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
38 public void addTextChangeListener(TextChangeListener listener);
41 * Return the number of characters in the content.
44 * @return the number of characters in the content.
46 public int getCharCount();
49 * Return the line at the given line index without delimiters.
52 * @param lineIndex index of the line to return. Does not include
53 * delimiters of preceding lines. Index 0 is the first line of the
55 * @return the line text without delimiters
56 * For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
58 * <li>getLine(0) == ""
59 * <li>getLine(1) == ""
60 * <li>getLine(2) == ""
62 * or if text = "A\nBC\nD", and delimiter = "\n", then:
64 * <li>getLine(0) == "A"
65 * <li>getLine(1) == "BC"
66 * <li>getLine(2) == "D"
69 public String getLine(int lineIndex);
72 * Return the line index at the given character offset.
75 * @param offset offset of the line to return. The first character of the
76 * document is at offset 0. An offset of {@link #getCharCount()} is valid
77 * and should answer the line index of the last line.
78 * @return the line index. The first line is at index 0. If the character
79 * at offset is a delimiter character, answer the line index of the line
81 * For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
83 * <li>getLineAtOffset(0) == 0
84 * <li>getLineAtOffset(1) == 0
85 * <li>getLineAtOffset(2) == 1
86 * <li>getLineAtOffset(3) == 1
87 * <li>getLineAtOffset(4) == 2
89 * or if text = "A\nBC\nD", and delimiter = "\n", then:
91 * <li>getLineAtOffset(0) == 0
92 * <li>getLineAtOffset(1) == 0
93 * <li>getLineAtOffset(2) == 1
94 * <li>getLineAtOffset(3) == 1
95 * <li>getLineAtOffset(4) == 1
96 * <li>getLineAtOffset(5) == 2
97 * <li>getLineAtOffset(6) == 2
100 public int getLineAtOffset(int offset);
103 * Return the number of lines. Should answer 1 when no text is specified.
104 * The StyledText widget relies on this behavior for drawing the cursor.
107 * @return the number of lines. For example:
109 * <li> text value ==> getLineCount
112 * <li> "a\n" ==> 2
113 * <li> "\n\n" ==> 3
116 public int getLineCount();
119 * Return the line delimiter that should be used by the StyledText
120 * widget when inserting new lines. New lines entered using key strokes
121 * and paste operations use this line delimiter.
122 * Implementors may use System.getProperty("line.separator") to return
123 * the platform line delimiter.
126 * @return the line delimiter that should be used by the StyledText widget
127 * when inserting new lines.
129 public String getLineDelimiter();
132 * Return the character offset of the first character of the given line.
134 * <b>NOTE:</b> When there is no text (i.e., no lines), getOffsetAtLine(0)
135 * is a valid call that should return 0.
138 * @param lineIndex index of the line. The first line is at index 0.
139 * @return offset offset of the first character of the line. The first
140 * character of the document is at offset 0. The return value should
141 * include line delimiters.
142 * For example, if text = "\r\ntest\r\n" and delimiter = "\r\n", then:
144 * <li>getOffsetAtLine(0) == 0
145 * <li>getOffsetAtLine(1) == 2
146 * <li>getOffsetAtLine(2) == 8
149 public int getOffsetAtLine(int lineIndex);
152 * Returns a string representing the content at the given range.
155 * @param start the start offset of the text to return. Offset 0 is the
156 * first character of the document.
157 * @param length the length of the text to return
158 * @return the text at the given range
160 public String getTextRange(int start, int length);
163 * Remove the specified text changed listener.
166 * @param listener the listener which should no longer be notified
168 * @exception IllegalArgumentException <ul>
169 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
172 public void removeTextChangeListener(TextChangeListener listener);
175 * Replace the text with "newText" starting at position "start"
176 * for a length of "replaceLength".
178 * Implementors have to notify the TextChangeListeners that were added
179 * using <code>addTextChangeListener</code> before and after the content
180 * is changed. A <code>TextChangingEvent</code> has to be sent to the
181 * textChanging method before the content is changed and a
182 * <code>TextChangedEvent</code> has to be sent to the textChanged method
183 * after the content has changed.
184 * The text change that occurs after the <code>TextChangingEvent</code>
185 * has been sent has to be consistent with the data provided in the
186 * <code>TextChangingEvent</code>.
187 * This data will be cached by the widget and will be used when the
188 * <code>TextChangedEvent</code> is received.
190 * The <code>TextChangingEvent</code> should be set as follows:
193 * <li>event.start = start of the replaced text
194 * <li>event.newText = text that is going to be inserted or empty String
195 * if no text will be inserted
196 * <li>event.replaceCharCount = length of text that is going to be replaced
197 * <li>event.newCharCount = length of text that is going to be inserted
198 * <li>event.replaceLineCount = number of lines that are going to be replaced
199 * <li>event.newLineCount = number of new lines that are going to be inserted
201 * <b>NOTE:</b> newLineCount is the number of inserted lines and replaceLineCount
202 * is the number of deleted lines based on the change that occurs visually.
205 * <li>(replaceText, newText) ==> (replaceLineCount, newLineCount)
206 * <li>("", "\n") ==> (0, 1)
207 * <li>("\n\n", "a") ==> (2, 0)
208 * <li>("a", "\n\n") ==> (0, 2)
209 * <li>("\n", "") ==> (1, 0)
212 * @param start start offset of text to replace, none of the offsets include
213 * delimiters of preceding lines, offset 0 is the first character of the
215 * @param replaceLength length of text to replace
216 * @param text text to replace
217 * @see TextChangeListener
219 public void replaceTextRange(int start, int replaceLength, String text);
222 * Set text to "text".
223 * Implementors have to send a <code>TextChangedEvent</code> to the
224 * textSet method of the TextChangeListeners that were added using
225 * <code>addTextChangeListener</code>.
228 * @param text the new text
229 * @see TextChangeListener
231 public void setText(String text);