📄 defaultcontent.java
字号:
*/int lineCount(String text){ int lineCount = 0; int length = text.length(); for (int i = 0; i < length; i++) { char ch = text.charAt(i); if (ch == SWT.CR) { if (i + 1 < length && text.charAt(i + 1) == SWT.LF) { i++; } lineCount++; } else if (ch == SWT.LF) { lineCount++; } } return lineCount; }/** * @return the logical length of the text store */public int getCharCount() { int length = gapEnd - gapStart; return (textStore.length - length);}/** * Returns the line at <code>index</code> without delimiters. * <p> * * @param index the index of the line to return * @return the logical line text (i.e., without the gap) * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT when index is out of range</li> * </ul> */public String getLine(int index) { if ((index >= lineCount) || (index < 0)) error(SWT.ERROR_INVALID_ARGUMENT); int start = lines[index][0]; int length = lines[index][1]; int end = start + length - 1; if (!gapExists() || (end < gapStart) || (start >= gapEnd)) { // line is before or after the gap while ((length - 1 >= 0) && isDelimiter(textStore[start+length-1])) { length--; } return new String(textStore, start, length); } else { // gap is in the specified range, strip out the gap StringBuffer buf = new StringBuffer(); int gapLength = gapEnd - gapStart; buf.append(textStore, start, gapStart - start); buf.append(textStore, gapEnd, length - gapLength - (gapStart - start)); length = buf.length(); while ((length - 1 >=0) && isDelimiter(buf.charAt(length-1))) { length--; } return buf.toString().substring(0, length); }}/** * Returns the line delimiter that should be used by the StyledText * widget when inserting new lines. This delimiter may be different than the * delimiter that is used by the <code>StyledTextContent</code> interface. * <p> * * @return the platform line delimiter as specified in the line.separator * system property. */public String getLineDelimiter() { return LineDelimiter;}/** * Returns the line at the given index with delimiters. * <p> * @param index the index of the line to return * @return the logical line text (i.e., without the gap) with delimiters */String getFullLine(int index) { int start = lines[index][0]; int length = lines[index][1]; int end = start + length - 1; if (!gapExists() || (end < gapStart) || (start >= gapEnd)) { // line is before or after the gap return new String(textStore, start, length); } else { // gap is in the specified range, strip out the gap StringBuffer buf = new StringBuffer(); int gapLength = gapEnd - gapStart; buf.append(textStore, start, gapStart - start); buf.append(textStore, gapEnd, length - gapLength - (gapStart - start)); return buf.toString(); }}/** * Returns the physical line at the given index (i.e., with delimiters and the gap). * <p> * * @param index the line index * @return the physical line */String getPhysicalLine(int index) { int start = lines[index][0]; int length = lines[index][1]; return getPhysicalText(start, length);}/** * @return the number of lines in the text store */public int getLineCount(){ return lineCount;}/** * Returns the line at the given offset. * <p> * * @param charPosition logical character offset (i.e., does not include gap) * @return the line index * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT when charPosition is out of range</li> * </ul> */public int getLineAtOffset(int charPosition){ int position; if ((charPosition > getCharCount()) || (charPosition < 0)) error(SWT.ERROR_INVALID_ARGUMENT); if (charPosition < gapStart) { // position is before the gap position = charPosition; } else { // position includes the gap position = charPosition + (gapEnd - gapStart); } // if last line and the line is not empty you can ask for // a position that doesn't exist (the one to the right of the // last character) - for inserting if (lineCount > 0) { int lastLine = lineCount - 1; if (position == lines[lastLine][0] + lines[lastLine][1]) return lastLine; } int high = lineCount; int low = -1; int index = lineCount; while (high - low > 1) { index = (high + low) / 2; int lineStart = lines[index][0]; int lineEnd = lineStart + lines[index][1] - 1; if (position <= lineStart) { high = index; } else if (position <= lineEnd) { high = index; break; } else { low = index; } } return high;}/** * Returns the line index at the given physical offset. * <p> * * @param position physical character offset (i.e., includes gap) * @return the line index */int getLineAtPhysicalOffset(int position){ int high = lineCount; int low = -1; int index = lineCount; while (high - low > 1) { index = (high + low) / 2; int lineStart = lines[index][0]; int lineEnd = lineStart + lines[index][1] - 1; if (position <= lineStart) { high = index; } else if (position <= lineEnd) { high = index; break; } else { low = index; } } return high;}/** * Returns the logical offset of the given line. * <p> * * @param lineIndex index of line * @return the logical starting offset of the line. When there are not any lines, * getOffsetAtLine(0) is a valid call that should answer 0. * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT when lineIndex is out of range</li> * </ul> */public int getOffsetAtLine(int lineIndex) { if (lineIndex == 0) return 0; if ((lineIndex >= lineCount) || (lineIndex < 0)) error(SWT.ERROR_INVALID_ARGUMENT); int start = lines[lineIndex][0]; if (start > gapEnd) { return start - (gapEnd - gapStart); } else { return start; }} /** * Increases the line indexes array to accomodate more lines. * <p> * * @param numLines the number to increase the array by */void expandLinesBy(int numLines) { int size = lines.length; if (size - lineCount >= numLines) { return; } int[][] newLines = new int[size+Math.max(10, numLines)][2]; System.arraycopy(lines, 0, newLines, 0, size); lines = newLines;}/** * Reports an SWT error. * <p> * * @param code the error code */void error (int code) { SWT.error(code);}/** * Returns whether or not a gap exists in the text store. * <p> * * @return true if gap exists, false otherwise */boolean gapExists() { return gapStart != gapEnd;} /** * Returns a string representing the continous content of * the text store. * <p> * * @param start the physical start offset of the text to return * @param length the physical length of the text to return * @return the text */String getPhysicalText(int start, int length) { return new String(textStore, start, length);}/** * Returns a string representing the logical content of * the text store (i.e., gap stripped out). * <p> * * @param start the logical start offset of the text to return * @param length the logical length of the text to return * @return the text */public String getTextRange(int start, int length) { if (textStore == null) return ""; if (length == 0) return ""; int end= start + length; if (!gapExists() || (end < gapStart)) return new String(textStore, start, length); if (gapStart < start) { int gapLength= gapEnd - gapStart; return new String(textStore, start + gapLength , length); } StringBuffer buf = new StringBuffer(); buf.append(textStore, start, gapStart - start); buf.append(textStore, gapEnd, end - gapStart); return buf.toString();}/** * Removes the specified <code>TextChangeListener</code>. * <p> * * @param listener the listener * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void removeTextChangeListener(TextChangeListener listener){ if (listener == null) error(SWT.ERROR_NULL_ARGUMENT); for (int i=0; i<textListeners.size(); i++) { TypedListener typedListener = (TypedListener) textListeners.elementAt(i); if (typedListener.getEventListener () == listener) { textListeners.removeElementAt(i); break; } }}/** * Replaces the text with <code>newText</code> starting at position <code>start</code> * for a length of <code>replaceLength</code>. Notifies the appropriate listeners. * <p> * * When sending the TextChangingEvent, <code>newLineCount</code> is the number of * lines that are going to be inserted and <code>replaceLineCount</code> is * the number of lines that are going to be deleted, based on the change * that occurs visually. For example: * <ul> * <li>(replaceText,newText) ==> (replaceLineCount,newLineCount) * <li>("","\n") ==> (0,1) * <li>("\n\n","a") ==> (2,0) * </ul> * </p> * * @param start start offset of text to replace * @param replaceLength start offset of text to replace * @param newText start offset of text to replace * * @exception SWTException <ul> * <li>ERROR_INVALID_ARGUMENT when the text change results in a multi byte * line delimiter being split or partially deleted. Splitting a line * delimiter by inserting text between the CR and LF characters of the * \r\n delimiter or deleting part of this line delimiter is not supported</li> * </ul> */public void replaceTextRange(int start, int replaceLength, String newText){ // check for invalid replace operations if (!isValidReplace(start, replaceLength, newText)) SWT.error(SWT.ERROR_INVALID_ARGUMENT); // inform listeners StyledTextEvent event = new StyledTextEvent(this); event.type = StyledText.TextChanging; event.start = start; event.replaceLineCount = lineCount(start, replaceLength); event.text = newText; event.newLineCount = lineCount(newText); event.replaceCharCount = replaceLength; event.newCharCount = newText.length(); sendTextEvent(event); // first delete the text to be replaced delete(start, replaceLength, event.replaceLineCount + 1); // then insert the new text insert(start, newText); // inform listeners event = new StyledTextEvent(this); event.type = StyledText.TextChanged; sendTextEvent(event); // printLines();}/** * Sends the text listeners the TextChanged event. */void sendTextEvent(StyledTextEvent event) { for (int i=0; i<textListeners.size(); i++) { ((StyledTextListener)textListeners.elementAt(i)).handleEvent(event); }} /** * Sets the content to text and removes the gap since there are no sensible predictions * about where the next change will occur. * <p> * * @param text the text */public void setText (String text){ textStore = text.toCharArray(); gapStart = -1; gapEnd = -1; expandExp = 1; indexLines(); StyledTextEvent event = new StyledTextEvent(this); event.type = StyledText.TextSet; event.text = ""; sendTextEvent(event);}/** * Deletes text. * <p> * @param position the position at which the text to delete starts * @param length the length of the text to delete * @param numLines the number of lines that are being deleted */void delete(int position, int length, int numLines) { if (length == 0) return; int startLine = getLineAtOffset(position); int startLineOffset = getOffsetAtLine(startLine); int endLine = getLineAtOffset(position + length); String endText = ""; boolean splittingDelimiter = false; if (position + length < getCharCount()) { endText = getTextRange(position + length - 1, 2); if ((endText.charAt(0) == SWT.CR) && (endText.charAt(1) == SWT.LF)) { splittingDelimiter = true; } } adjustGap(position + length, -length, startLine); int [][] oldLines = indexLines(position, length + (gapEnd - gapStart), numLines); // enlarge the gap - the gap can be enlarged either to the // right or left if (position + length == gapStart) { gapStart -= length; } else { gapEnd += length; } // figure out the length of the new concatenated line, do so by // finding the first line delmiter after position int j = position; boolean eol = false; while (j < textStore.length && !eol) { if (j < gapStart || j >= gapEnd) { char ch = textStore[j]; if (isDelimiter(ch)) { if (j + 1 < textStore.length) if (ch == SWT.CR && (textStore[j+1] == SWT.LF)) j++; eol = true; } } j++; } // update the line where the deletion started lines[startLine][1] = (position - startLineOffset) + (j - position); // figure out the number of lines that have been deleted int numOldLines = oldLines.length - 1; if (splittingDelimiter) numOldLines -= 1; // shift up the lines after the last deleted line, no need to update // the offset or length of the lines for (int i = endLine + 1; i < lineCount; i++) { lines[i - numOldLines]=lines[i]; } lineCount -= numOldLines; gapLine = getLineAtPhysicalOffset(gapStart); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -