1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 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;
17 import java.util.List;
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.widgets.*;
23 class DefaultContent implements StyledTextContent {
24 private final static String LineDelimiter = System.getProperty("line.separator");
26 List<StyledTextListener> textListeners = new ArrayList<>(); // stores text listeners for event sending
27 char[] textStore = new char[0]; // stores the actual text
28 int gapStart = -1; // the character position start of the gap
29 int gapEnd = -1; // the character position after the end of the gap
30 int gapLine = -1; // the line on which the gap exists, the gap will always be associated with one line
31 int highWatermark = 300;
32 int lowWatermark = 50;
34 int[][] lines = new int[50][2]; // array of character positions and lengths representing the lines of text
35 int lineCount = 0; // the number of lines of text
36 int expandExp = 1; // the expansion exponent, used to increase the lines array exponentially
37 int replaceExpandExp = 1; // the expansion exponent, used to increase the lines array exponentially
40 * Creates a new DefaultContent and initializes it. A <code>StyledTextContent</code> will always have
41 * at least one empty line.
48 * Adds a line to the end of the line indexes array. Increases the size of the array if necessary.
49 * <code>lineCount</code> is updated to reflect the new entry.
52 * @param start the start of the line
53 * @param length the length of the line
55 void addLineIndex(int start, int length) {
56 int size = lines.length;
57 if (lineCount == size) {
58 // expand the lines by powers of 2
59 int[][] newLines = new int[size+Compatibility.pow2(expandExp)][2];
60 System.arraycopy(lines, 0, newLines, 0, size);
64 int[] range = new int[] {start, length};
65 lines[lineCount] = range;
69 * Adds a line index to the end of <code>linesArray</code>. Increases the
70 * size of the array if necessary and returns a new array.
73 * @param start the start of the line
74 * @param length the length of the line
75 * @param linesArray the array to which to add the line index
76 * @param count the position at which to add the line
77 * @return a new array of line indexes
79 int[][] addLineIndex(int start, int length, int[][] linesArray, int count) {
80 int size = linesArray.length;
81 int[][] newLines = linesArray;
83 newLines = new int[size+Compatibility.pow2(replaceExpandExp)][2];
85 System.arraycopy(linesArray, 0, newLines, 0, size);
87 int[] range = new int[] {start, length};
88 newLines[count] = range;
92 * Adds a <code>TextChangeListener</code> listening for
93 * <code>TextChangingEvent</code> and <code>TextChangedEvent</code>. A
94 * <code>TextChangingEvent</code> is sent before changes to the text occur.
95 * A <code>TextChangedEvent</code> is sent after changes to the text
99 * @param listener the listener
100 * @exception IllegalArgumentException <ul>
101 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
105 public void addTextChangeListener(TextChangeListener listener) {
106 if (listener == null) error(SWT.ERROR_NULL_ARGUMENT);
107 StyledTextListener typedListener = new StyledTextListener(listener);
108 textListeners.add(typedListener);
111 * Adjusts the gap to accommodate a text change that is occurring.
114 * @param position the position at which a change is occurring
115 * @param sizeHint the size of the change
116 * @param line the line where the gap will go
118 void adjustGap(int position, int sizeHint, int line) {
119 if (position == gapStart) {
120 // text is being inserted at the gap position
121 int size = (gapEnd - gapStart) - sizeHint;
122 if (lowWatermark <= size && size <= highWatermark)
124 } else if ((position + sizeHint == gapStart) && (sizeHint < 0)) {
125 // text is being deleted at the gap position
126 int size = (gapEnd - gapStart) - sizeHint;
127 if (lowWatermark <= size && size <= highWatermark)
130 moveAndResizeGap(position, sizeHint, line);
133 * Calculates the indexes of each line in the text store. Assumes no gap exists.
134 * Optimized to do less checking.
139 int textLength = textStore.length;
141 for (i = start; i < textLength; i++) {
142 char ch = textStore[i];
144 // see if the next character is a LF
145 if (i + 1 < textLength) {
151 addLineIndex(start, i - start + 1);
153 } else if (ch == SWT.LF) {
154 addLineIndex(start, i - start + 1);
158 addLineIndex(start, i - start);
161 * Returns whether or not the given character is a line delimiter. Both CR and LF
162 * are valid line delimiters.
165 * @param ch the character to test
166 * @return true if ch is a delimiter, false otherwise
168 boolean isDelimiter(char ch) {
169 if (ch == SWT.CR) return true;
170 if (ch == SWT.LF) return true;
174 * Determine whether or not the replace operation is valid. DefaultContent will not allow
175 * the /r/n line delimiter to be split or partially deleted.
178 * @param start start offset of text to replace
179 * @param replaceLength start offset of text to replace
180 * @param newText start offset of text to replace
181 * @return a boolean specifying whether or not the replace operation is valid
183 protected boolean isValidReplace(int start, int replaceLength, String newText){
184 if (replaceLength == 0) {
185 // inserting text, see if the \r\n line delimiter is being split
186 if (start == 0) return true;
187 if (start == getCharCount()) return true;
188 char before = getTextRange(start - 1, 1).charAt(0);
189 if (before == '\r') {
190 char after = getTextRange(start, 1).charAt(0);
191 if (after == '\n') return false;
194 // deleting text, see if part of a \r\n line delimiter is being deleted
195 char startChar = getTextRange(start, 1).charAt(0);
196 if (startChar == '\n') {
197 // see if char before delete position is \r
199 char before = getTextRange(start - 1, 1).charAt(0);
200 if (before == '\r') return false;
203 char endChar = getTextRange(start + replaceLength - 1, 1).charAt(0);
204 if (endChar == '\r') {
205 // see if char after delete position is \n
206 if (start + replaceLength != getCharCount()) {
207 char after = getTextRange(start + replaceLength, 1).charAt(0);
208 if (after == '\n') return false;
215 * Calculates the indexes of each line of text in the given range.
218 * @param offset the logical start offset of the text lineate
219 * @param length the length of the text to lineate, includes gap
220 * @param numLines the number of lines to initially allocate for the line index array,
221 * passed in for efficiency (the exact number of lines may be known)
222 * @return a line indexes array where each line is identified by a start offset and
225 int[][] indexLines(int offset, int length, int numLines){
226 int[][] indexedLines = new int[numLines][2];
230 replaceExpandExp = 1;
231 for (i = start; i < length; i++) {
232 int location = i + offset;
233 if ((location >= gapStart) && (location < gapEnd)) {
236 char ch = textStore[location];
238 // see if the next character is a LF
239 if (location+1 < textStore.length) {
240 ch = textStore[location+1];
245 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount);
248 } else if (ch == SWT.LF) {
249 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount);
255 int[][] newLines = new int[lineCount+1][2];
256 System.arraycopy(indexedLines, 0, newLines, 0, lineCount);
257 int[] range = new int[] {start, i - start};
258 newLines[lineCount] = range;
265 * @param position the position at which to insert the text
266 * @param text the text to insert
268 void insert(int position, String text) {
269 if (text.length() == 0) return;
271 int startLine = getLineAtOffset(position);
272 int change = text.length();
273 boolean endInsert = position == getCharCount();
274 adjustGap(position, change, startLine);
276 // during an insert the gap will be adjusted to start at
277 // position and it will be associated with startline, the
278 // inserted text will be placed in the gap
279 int startLineOffset = getOffsetAtLine(startLine);
280 // at this point, startLineLength will include the start line
281 // and all of the newly inserted text
282 int startLineLength = getPhysicalLine(startLine).length();
286 gapStart += (change);
287 for (int i = 0; i < text.length(); i++) {
288 textStore[position + i]= text.charAt(i);
292 // figure out the number of new lines that have been inserted
293 int [][] newLines = indexLines(startLineOffset, startLineLength, 10);
294 // only insert an empty line if it is the last line in the text
295 int numNewLines = newLines.length - 1;
296 if (newLines[numNewLines][1] == 0) {
297 // last inserted line is a new line
299 // insert happening at end of the text, leave numNewLines as
300 // is since the last new line will not be concatenated with another
308 // make room for the new lines
309 expandLinesBy(numNewLines);
310 // shift down the lines after the replace line
311 for (int i = lineCount - 1; i > startLine; i--) {
312 lines[i + numNewLines]=lines[i];
314 // insert the new lines
315 for (int i = 0; i < numNewLines; i++) {
316 newLines[i][0] += startLineOffset;
317 lines[startLine + i]=newLines[i];
319 // update the last inserted line
320 if (numNewLines < newLines.length) {
321 newLines[numNewLines][0] += startLineOffset;
322 lines[startLine + numNewLines] = newLines[numNewLines];
325 lineCount += numNewLines;
326 gapLine = getLineAtPhysicalOffset(gapStart);
329 * Moves the gap and adjusts its size in anticipation of a text change.
330 * The gap is resized to actual size + the specified size and moved to the given
334 * @param position the position at which a change is occurring
335 * @param size the size of the change
336 * @param newGapLine the line where the gap should be put
338 void moveAndResizeGap(int position, int size, int newGapLine) {
339 char[] content = null;
340 int oldSize = gapEnd - gapStart;
343 newSize = highWatermark + size;
345 newSize = lowWatermark - size;
347 // remove the old gap from the lines information
349 // adjust the line length
350 lines[gapLine][1] = lines[gapLine][1] - oldSize;
351 // adjust the offsets of the lines after the gapLine
352 for (int i = gapLine + 1; i < lineCount; i++) {
353 lines[i][0] = lines[i][0] - oldSize;
360 content = new char[textStore.length - oldSize];
361 System.arraycopy(textStore, 0, content, 0, gapStart);
362 System.arraycopy(textStore, gapEnd, content, gapStart, content.length - gapStart);
365 gapStart = gapEnd = position;
368 content = new char[textStore.length + (newSize - oldSize)];
369 int newGapStart = position;
370 int newGapEnd = newGapStart + newSize;
372 System.arraycopy(textStore, 0, content, 0, newGapStart);
373 System.arraycopy(textStore, newGapStart, content, newGapEnd, content.length - newGapEnd);
374 } else if (newGapStart < gapStart) {
375 int delta = gapStart - newGapStart;
376 System.arraycopy(textStore, 0, content, 0, newGapStart);
377 System.arraycopy(textStore, newGapStart, content, newGapEnd, delta);
378 System.arraycopy(textStore, gapEnd, content, newGapEnd + delta, textStore.length - gapEnd);
380 int delta = newGapStart - gapStart;
381 System.arraycopy(textStore, 0, content, 0, gapStart);
382 System.arraycopy(textStore, gapEnd, content, gapStart, delta);
383 System.arraycopy(textStore, gapEnd + delta, content, newGapEnd, content.length - newGapEnd);
386 gapStart = newGapStart;
389 // add the new gap to the lines information
391 gapLine = newGapLine;
392 // adjust the line length
393 int gapLength = gapEnd - gapStart;
394 lines[gapLine][1] = lines[gapLine][1] + (gapLength);
395 // adjust the offsets of the lines after the gapLine
396 for (int i = gapLine + 1; i < lineCount; i++) {
397 lines[i][0] = lines[i][0] + gapLength;
402 * Returns the number of lines that are in the specified text.
405 * @param startOffset the start of the text to lineate
406 * @param length the length of the text to lineate
407 * @return number of lines
409 int lineCount(int startOffset, int length){
417 i += gapEnd - gapStart;
419 while (count < length) {
420 if ((i >= gapStart) && (i < gapEnd)) {
423 char ch = textStore[i];
425 // see if the next character is a LF
426 if (i + 1 < textStore.length) {
434 } else if (ch == SWT.LF) {
444 * Returns the number of lines that are in the specified text.
447 * @param text the text to lineate
448 * @return number of lines in the text
450 int lineCount(String text){
452 int length = text.length();
453 for (int i = 0; i < length; i++) {
454 char ch = text.charAt(i);
456 if (i + 1 < length && text.charAt(i + 1) == SWT.LF) {
460 } else if (ch == SWT.LF) {
467 * @return the logical length of the text store
470 public int getCharCount() {
471 int length = gapEnd - gapStart;
472 return (textStore.length - length);
475 * Returns the line at <code>index</code> without delimiters.
478 * @param index the index of the line to return
479 * @return the logical line text (i.e., without the gap)
480 * @exception IllegalArgumentException <ul>
481 * <li>ERROR_INVALID_ARGUMENT when index is out of range</li>
485 public String getLine(int index) {
486 if ((index >= lineCount) || (index < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
487 int start = lines[index][0];
488 int length = lines[index][1];
489 int end = start + length - 1;
490 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
491 // line is before or after the gap
492 while ((length - 1 >= 0) && isDelimiter(textStore[start+length-1])) {
495 return new String(textStore, start, length);
497 // gap is in the specified range, strip out the gap
498 StringBuilder buf = new StringBuilder();
499 int gapLength = gapEnd - gapStart;
500 buf.append(textStore, start, gapStart - start);
501 buf.append(textStore, gapEnd, length - gapLength - (gapStart - start));
502 length = buf.length();
503 while ((length - 1 >=0) && isDelimiter(buf.charAt(length - 1))) {
506 return buf.toString().substring(0, length);
510 * Returns the line delimiter that should be used by the StyledText
511 * widget when inserting new lines. This delimiter may be different than the
512 * delimiter that is used by the <code>StyledTextContent</code> interface.
515 * @return the platform line delimiter as specified in the line.separator
519 public String getLineDelimiter() {
520 return LineDelimiter;
523 * Returns the line at the given index with delimiters.
525 * @param index the index of the line to return
526 * @return the logical line text (i.e., without the gap) with delimiters
528 String getFullLine(int index) {
529 int start = lines[index][0];
530 int length = lines[index][1];
531 int end = start + length - 1;
532 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
533 // line is before or after the gap
534 return new String(textStore, start, length);
536 // gap is in the specified range, strip out the gap
537 StringBuilder buffer = new StringBuilder();
538 int gapLength = gapEnd - gapStart;
539 buffer.append(textStore, start, gapStart - start);
540 buffer.append(textStore, gapEnd, length - gapLength - (gapStart - start));
541 return buffer.toString();
545 * Returns the physical line at the given index (i.e., with delimiters and the gap).
548 * @param index the line index
549 * @return the physical line
551 String getPhysicalLine(int index) {
552 int start = lines[index][0];
553 int length = lines[index][1];
554 return getPhysicalText(start, length);
557 * @return the number of lines in the text store
560 public int getLineCount(){
564 * Returns the line at the given offset.
567 * @param charPosition logical character offset (i.e., does not include gap)
568 * @return the line index
569 * @exception IllegalArgumentException <ul>
570 * <li>ERROR_INVALID_ARGUMENT when charPosition is out of range</li>
574 public int getLineAtOffset(int charPosition){
575 if ((charPosition > getCharCount()) || (charPosition < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
577 if (charPosition < gapStart) {
578 // position is before the gap
579 position = charPosition;
581 // position includes the gap
582 position = charPosition + (gapEnd - gapStart);
585 // if last line and the line is not empty you can ask for
586 // a position that doesn't exist (the one to the right of the
587 // last character) - for inserting
589 int lastLine = lineCount - 1;
590 if (position == lines[lastLine][0] + lines[lastLine][1])
594 int high = lineCount;
596 int index = lineCount;
597 while (high - low > 1) {
598 index = (high + low) / 2;
599 int lineStart = lines[index][0];
600 int lineEnd = lineStart + lines[index][1] - 1;
601 if (position <= lineStart) {
603 } else if (position <= lineEnd) {
613 * Returns the line index at the given physical offset.
616 * @param position physical character offset (i.e., includes gap)
617 * @return the line index
619 int getLineAtPhysicalOffset(int position){
620 int high = lineCount;
622 int index = lineCount;
623 while (high - low > 1) {
624 index = (high + low) / 2;
625 int lineStart = lines[index][0];
626 int lineEnd = lineStart + lines[index][1] - 1;
627 if (position <= lineStart) {
629 } else if (position <= lineEnd) {
639 * Returns the logical offset of the given line.
642 * @param lineIndex index of line
643 * @return the logical starting offset of the line. When there are not any lines,
644 * getOffsetAtLine(0) is a valid call that should answer 0.
645 * @exception IllegalArgumentException <ul>
646 * <li>ERROR_INVALID_ARGUMENT when lineIndex is out of range</li>
650 public int getOffsetAtLine(int lineIndex) {
651 if (lineIndex == 0) return 0;
652 if ((lineIndex >= lineCount) || (lineIndex < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
653 int start = lines[lineIndex][0];
654 if (start > gapEnd) {
655 return start - (gapEnd - gapStart);
661 * Increases the line indexes array to accommodate more lines.
664 * @param numLines the number to increase the array by
666 void expandLinesBy(int numLines) {
667 int size = lines.length;
668 if (size - lineCount >= numLines) {
671 int[][] newLines = new int[size+Math.max(10, numLines)][2];
672 System.arraycopy(lines, 0, newLines, 0, size);
676 * Reports an SWT error.
679 * @param code the error code
681 void error (int code) {
685 * Returns whether or not a gap exists in the text store.
688 * @return true if gap exists, false otherwise
690 boolean gapExists() {
691 return gapStart != gapEnd;
694 * Returns a string representing the continuous content of
698 * @param start the physical start offset of the text to return
699 * @param length the physical length of the text to return
702 String getPhysicalText(int start, int length) {
703 return new String(textStore, start, length);
706 * Returns a string representing the logical content of
707 * the text store (i.e., gap stripped out).
710 * @param start the logical start offset of the text to return
711 * @param length the logical length of the text to return
715 public String getTextRange(int start, int length) {
716 if (textStore == null)
720 int end= start + length;
721 if (!gapExists() || (end < gapStart))
722 return new String(textStore, start, length);
723 if (gapStart < start) {
724 int gapLength= gapEnd - gapStart;
725 return new String(textStore, start + gapLength , length);
727 StringBuilder buf = new StringBuilder();
728 buf.append(textStore, start, gapStart - start);
729 buf.append(textStore, gapEnd, end - gapStart);
730 return buf.toString();
733 * Removes the specified <code>TextChangeListener</code>.
736 * @param listener the listener which should no longer be notified
738 * @exception IllegalArgumentException <ul>
739 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
743 public void removeTextChangeListener(TextChangeListener listener){
744 if (listener == null) error(SWT.ERROR_NULL_ARGUMENT);
745 for (int i = 0; i < textListeners.size(); i++) {
746 TypedListener typedListener = textListeners.get(i);
747 if (typedListener.getEventListener () == listener) {
748 textListeners.remove(i);
754 * Replaces the text with <code>newText</code> starting at position <code>start</code>
755 * for a length of <code>replaceLength</code>. Notifies the appropriate listeners.
758 * When sending the TextChangingEvent, <code>newLineCount</code> is the number of
759 * lines that are going to be inserted and <code>replaceLineCount</code> is
760 * the number of lines that are going to be deleted, based on the change
761 * that occurs visually. For example:
764 * <li>(replaceText,newText) ==> (replaceLineCount,newLineCount)
765 * <li>("","\n") ==> (0,1)
766 * <li>("\n\n","a") ==> (2,0)
769 * @param start start offset of text to replace
770 * @param replaceLength start offset of text to replace
771 * @param newText start offset of text to replace
773 * @exception SWTException <ul>
774 * <li>ERROR_INVALID_ARGUMENT when the text change results in a multi byte
775 * line delimiter being split or partially deleted. Splitting a line
776 * delimiter by inserting text between the CR and LF characters of the
777 * \r\n delimiter or deleting part of this line delimiter is not supported</li>
781 public void replaceTextRange(int start, int replaceLength, String newText){
782 // check for invalid replace operations
783 if (!isValidReplace(start, replaceLength, newText)) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
786 StyledTextEvent event = new StyledTextEvent(this);
787 event.type = ST.TextChanging;
789 event.replaceLineCount = lineCount(start, replaceLength);
790 event.text = newText;
791 event.newLineCount = lineCount(newText);
792 event.replaceCharCount = replaceLength;
793 event.newCharCount = newText.length();
794 sendTextEvent(event);
796 // first delete the text to be replaced
797 delete(start, replaceLength, event.replaceLineCount + 1);
798 // then insert the new text
799 insert(start, newText);
801 event = new StyledTextEvent(this);
802 event.type = ST.TextChanged;
803 sendTextEvent(event);
806 * Sends the text listeners the TextChanged event.
808 void sendTextEvent(StyledTextEvent event) {
809 for (int i = 0; i < textListeners.size(); i++) {
810 textListeners.get(i).handleEvent(event);
814 * Sets the content to text and removes the gap since there are no sensible predictions
815 * about where the next change will occur.
818 * @param text the text
821 public void setText (String text){
822 textStore = text.toCharArray();
827 StyledTextEvent event = new StyledTextEvent(this);
828 event.type = ST.TextSet;
830 sendTextEvent(event);
835 * @param position the position at which the text to delete starts
836 * @param length the length of the text to delete
837 * @param numLines the number of lines that are being deleted
839 void delete(int position, int length, int numLines) {
840 if (length == 0) return;
842 int startLine = getLineAtOffset(position);
843 int startLineOffset = getOffsetAtLine(startLine);
844 int endLine = getLineAtOffset(position + length);
847 boolean splittingDelimiter = false;
848 if (position + length < getCharCount()) {
849 endText = getTextRange(position + length - 1, 2);
850 if ((endText.charAt(0) == SWT.CR) && (endText.charAt(1) == SWT.LF)) {
851 splittingDelimiter = true;
855 adjustGap(position + length, -length, startLine);
856 int [][] oldLines = indexLines(position, length + (gapEnd - gapStart), numLines);
858 // enlarge the gap - the gap can be enlarged either to the
860 if (position + length == gapStart) {
866 // figure out the length of the new concatenated line, do so by
867 // finding the first line delimiter after position
870 while (j < textStore.length && !eol) {
871 if (j < gapStart || j >= gapEnd) {
872 char ch = textStore[j];
873 if (isDelimiter(ch)) {
874 if (j + 1 < textStore.length) {
875 if (ch == SWT.CR && (textStore[j+1] == SWT.LF)) {
884 // update the line where the deletion started
885 lines[startLine][1] = (position - startLineOffset) + (j - position);
886 // figure out the number of lines that have been deleted
887 int numOldLines = oldLines.length - 1;
888 if (splittingDelimiter) numOldLines -= 1;
889 // shift up the lines after the last deleted line, no need to update
890 // the offset or length of the lines
891 for (int i = endLine + 1; i < lineCount; i++) {
892 lines[i - numOldLines] = lines[i];
894 lineCount -= numOldLines;
895 gapLine = getLineAtPhysicalOffset(gapStart);