1 /*******************************************************************************
2 * Copyright (c) 2000, 2020 IBM Corporation and others.
3 *
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/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
15
16
17 /**
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.
24 */
25 public interface StyledTextContent {
26
27 /**
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.
31 * <p>
32 *
33 * @param listener the listener
34 * @exception IllegalArgumentException <ul>
35 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
36 * </ul>
37 */
addTextChangeListener(TextChangeListener listener)38 public void addTextChangeListener(TextChangeListener listener);
39
40 /**
41 * Return the number of characters in the content.
42 * <p>
43 *
44 * @return the number of characters in the content.
45 */
getCharCount()46 public int getCharCount();
47
48 /**
49 * Return the line at the given line index without delimiters.
50 * <p>
51 *
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
54 * content.
55 * @return the line text without delimiters
56 * For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
57 * <ul>
58 * <li>getLine(0) == ""
59 * <li>getLine(1) == ""
60 * <li>getLine(2) == ""
61 * </ul>
62 * or if text = "A\nBC\nD", and delimiter = "\n", then:
63 * <ul>
64 * <li>getLine(0) == "A"
65 * <li>getLine(1) == "BC"
66 * <li>getLine(2) == "D"
67 * </ul>
68 */
getLine(int lineIndex)69 public String getLine(int lineIndex);
70
71 /**
72 * Return the line index at the given character offset.
73 * <p>
74 *
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
80 * that is delimited.
81 * For example, if text = "\r\n\r\n", and delimiter = "\r\n", then:
82 * <ul>
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
88 * </ul>
89 * or if text = "A\nBC\nD", and delimiter = "\n", then:
90 * <ul>
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
98 * </ul>
99 */
getLineAtOffset(int offset)100 public int getLineAtOffset(int offset);
101
102 /**
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.
105 * <p>
106 *
107 * @return the number of lines. For example:
108 * <ul>
109 * <li> text value ==> getLineCount
110 * <li> null ==> 1
111 * <li> "" ==> 1
112 * <li> "a\n" ==> 2
113 * <li> "\n\n" ==> 3
114 * </ul>
115 */
getLineCount()116 public int getLineCount();
117
118 /**
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.lineSeparator() to return
123 * the platform line delimiter.
124 * <p>
125 *
126 * @return the line delimiter that should be used by the StyledText widget
127 * when inserting new lines.
128 */
getLineDelimiter()129 public String getLineDelimiter();
130
131 /**
132 * Return the character offset of the first character of the given line.
133 * <p>
134 * <b>NOTE:</b> When there is no text (i.e., no lines), getOffsetAtLine(0)
135 * is a valid call that should return 0.
136 * </p>
137 *
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:
143 * <ul>
144 * <li>getOffsetAtLine(0) == 0
145 * <li>getOffsetAtLine(1) == 2
146 * <li>getOffsetAtLine(2) == 8
147 * </ul>
148 */
getOffsetAtLine(int lineIndex)149 public int getOffsetAtLine(int lineIndex);
150
151 /**
152 * Returns a string representing the content at the given range.
153 * <p>
154 *
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
159 */
getTextRange(int start, int length)160 public String getTextRange(int start, int length);
161
162 /**
163 * Remove the specified text changed listener.
164 * <p>
165 *
166 * @param listener the listener which should no longer be notified
167 *
168 * @exception IllegalArgumentException <ul>
169 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
170 * </ul>
171 */
removeTextChangeListener(TextChangeListener listener)172 public void removeTextChangeListener(TextChangeListener listener);
173
174 /**
175 * Replace the text with "newText" starting at position "start"
176 * for a length of "replaceLength".
177 * <p>
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.
189 * <p>
190 * The <code>TextChangingEvent</code> should be set as follows:
191 * </p>
192 * <ul>
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
200 * </ul>
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.
203 * For example:
204 * <ul>
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)
210 * </ul>
211 *
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
214 * document
215 * @param replaceLength length of text to replace
216 * @param text text to replace
217 * @see TextChangeListener
218 */
replaceTextRange(int start, int replaceLength, String text)219 public void replaceTextRange(int start, int replaceLength, String text);
220
221 /**
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>.
226 * <p>
227 *
228 * @param text the new text
229 * @see TextChangeListener
230 */
setText(String text)231 public void setText(String text);
232 }
233