⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultlinestyler.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	if (newStyle == null) {		styles = new StyleRange[0];		styleExpandExp = 1;		styleCount = 0;		return;	}	if (newStyle.length ==0) return;	if (newStyle.isUnstyled()) {		clearStyle(newStyle);		return;	}		Point pt = getOverlappingStyles(newStyle.start, newStyle.length);	int newStyleEnd = newStyle.start + newStyle.length - 1;		// no styles exist	if (pt == null) {		insertStyle(newStyle, 0);		return;	}		// newStyle does not overlap any other styles	if (pt.y == 0) {		insertMergeStyle(newStyle, pt.x);		return;	} 	// the newStyle overlaps one or more of the existing styles	boolean added = false; // indicates whether or not the new style has been added	int count = 0;	// pt.x is the index of the first overlapped style, pt.y is the number of overlapped	// styles	for (int i=pt.x; count<pt.y; i++) {		StyleRange overlap = styles[i];		int overlapEnd = overlap.start + overlap.length - 1;		if (overlap.start < newStyle.start) {			if (overlapEnd <= newStyleEnd) {				// the end of overlap needs to be replaced by newStyle				if (newStyle.similarTo(overlap)) {					// update overlap to accomodate the new style					overlap.length = newStyle.start + newStyle.length - overlap.start;				} else {					overlap.length=newStyle.start - overlap.start;					// see if newStyle can be merged with the style after overlap, if so,					// processing is done					if (mergeStyleAfter(newStyle, i+1)) break;					// otherwise, insert the newStyle, newStyle may still overlap other					// styles after it so continue processing						insertStyle(newStyle, i+1);					i++;				}				added = true;			} else {				// middle of overlap needs to be replaced by newStyle, this will				// cause overlap to be broken into two				if (newStyle.similarTo(overlap)) break;				StyleRange endStyle = (StyleRange)overlap.clone();				endStyle.start = newStyleEnd + 1;				endStyle.length = overlapEnd - newStyleEnd;				overlap.length = newStyle.start - overlap.start;				insertStyle(newStyle, i+1);				i++;				insertStyle(endStyle, i+1);				// when newStyle overlaps the middle of a style, this implies that				// processing is done (no more overlapped styles)				break;			}		} else {			if (overlapEnd <= newStyleEnd) {					// overlap will be replaced by the newStyle, make sure newStyle				// hasn't already been added, if it has just delete overlap				if (!added) {					styles[i] = newStyle;					added = true;				} else {					deleteStyle(i);					i--;				}			} else {				// beginning of overlap needs to be replaced by newStyle				overlap.start=newStyleEnd + 1;				overlap.length=overlapEnd - overlap.start + 1;				if (!added) {					insertMergeStyle(newStyle, i);				}				// when newStyle overlaps only the beginning of a style, this implies 				// that processing is done (no more overlapped styles)				break;			}		}					count++;	}}/**  * Replace the styles for the given range. * <p> * * @param styles the new styles, must be in order and non-overlapping */void replaceStyleRanges(int start, int length, StyleRange[] ranges) {	clearStyle(new StyleRange(start, length, null, null));	// find insert point	int high = styleCount;	int low = -1;	int index = high;	while (high - low > 1) {		index = (high + low) / 2;		StyleRange style = styles[index];		if (start <= style.start) {			high = index;					}		else {			low = index;		}	}	insertStyles(ranges, high);}/**  * Sets the array of styles and discards old styles.  Called by StyledText. * <p> * * @param styles the new styles, must be in order and non-overlapping */void setStyleRanges(StyleRange[] styles) {	this.styles = new StyleRange[styles.length];	System.arraycopy(styles, 0, this.styles, 0, styles.length);	styleCount = styles.length;	styleExpandExp = 1;}/**  * Updates the style ranges and line backgrounds to reflect a pending text  * change. * Called by StyledText when a TextChangingEvent is received. * <p> * * @param event	the event with the text change information */  public void textChanging(TextChangingEvent event) {	int startLine = content.getLineAtOffset(event.start);	int startLineOffset = content.getOffsetAtLine(startLine);		textChanging(event.start, -event.replaceCharCount);	textChanging(event.start, event.newCharCount);		if (event.replaceCharCount == content.getCharCount()) {		// all text is going to be replaced, clear line backgrounds		linesChanging(0, -lineCount);		linesChanging(0, content.getLineCount() - event.replaceLineCount + event.newLineCount);		return;	}	if (event.start != startLineOffset) {		startLine = startLine + 1;	}				linesChanging(startLine, -event.replaceLineCount);	linesChanging(startLine, event.newLineCount);}/* * Updates the line backgrounds to reflect a pending text change. * <p> * * @param start	the starting line of the change that is about to take place * @param delta	the number of lines in the change, > 0 indicates lines inserted, * 	< 0 indicates lines deleted */void linesChanging(int start, int delta) {	if (delta == 0) return;	boolean inserting = delta > 0;	if (inserting) {		// shift the lines down to make room for new lines		expandLinesBy(delta);		for (int i = lineCount-1; i >= start; i--) {			lineBackgrounds[i + delta]=lineBackgrounds[i];		}		for (int i=start; i<start + delta; i++) {			lineBackgrounds[i]=null;		}	} else {		// shift up the lines		for (int i = start - delta; i < lineCount; i++) {			lineBackgrounds[i+delta]=lineBackgrounds[i];		}	}	lineCount += delta;}/* * Updates the style ranges to reflect a text change. * <p> * * @param start	the starting offset of the change that is about to  *	take place * @param delta	the length of the change, > 0 indicates text inserted, * 	< 0 indicates text deleted */void textChanging(int start, int delta) {	if (delta == 0) return;	StyleRange style;	// find the index of the first style for the given offset, use a binary search	// to find the index	int end;	int deleteStart = -1;	int deleteCount = 0;	boolean inserting = delta > 0;	if (inserting) {		end = (start + delta) - 1;	} else {		end = (start - delta) - 1;	}	int high = searchForStyle(start, end);	int index;	// update the styles that are in the affected range	for (index = high; index < styleCount; index++) {		style = styles[index];		if (inserting) {			if (style.start >= start) break;			// in the insert case only one style range will be directly affected,			// it will need to be split into two and then the newStyle inserted			StyleRange beforeStyle = (StyleRange)style.clone();			beforeStyle.length = start - style.start;			style.start = start;			style.length = style.length - beforeStyle.length;			if (beforeStyle.length != 0) insertStyle(beforeStyle, index);			index++;			break;		} else {			int styleEnd = style.start + style.length - 1;			if (style.start > end) break;			// in the delete case, any style that overlaps the change range will be			// affected			if (style.start < start) {				if (styleEnd <= end) {					// style starts before change range, ends in change range					style.length = start - style.start;				} else {					// style starts before change range, ends after change range					style.length = style.length + delta;					index++;					break;				}			} else {				if (styleEnd <= end) {					// style starts in change range, ends in change range					if (deleteStart == -1)	{						deleteStart = index;					} 					deleteCount++;				} else {					// style starts in change range, ends after change range					style.start = start;					style.length = styleEnd - end;					index++;					break;				}			}			}	}	deleteStyles(deleteStart, deleteCount);	// change the offsets of the styles after the affected styles	for (int i = index - deleteCount; i < styleCount; i++) {		style = styles[i];		style.start = style.start + delta;	}}/**  * Returns the indexes of the styles that overlap the given range.  Styles that partially * or fully overlap the range will be returned. * <p> * * @return Point where x is the index of the starting overlap style, y is the number of * 	styles that overlap the range */ Point getOverlappingStyles(int start, int length) {		StyleRange style;	if (styleCount == 0) return null;	// find the index of the first style for the given offset, use a binary search	// to find the index	int end = start + length - 1;	int high = searchForStyle(start, end);	int count = 0;	for (int index = high; index < styleCount; index++) {		style = styles[index];		int styleEnd = style.start + style.length - 1;		if (style.start > end) break;		if (styleEnd >= start) count++;		}	return new Point(high, count);}/**  * Returns the background color of a line.  Called by StyledText.  It is safe to return  * the existing Color object since the colors are set and managed by the client. * <p> * * @param index	the line index * @return the background color of the line at the given index */ Color getLineBackground(int index) {	return lineBackgrounds[index];}/**  * Returns the style for the character at <code>offset</code>.  Called by StyledText.   * Returns a new style.  Does not return the existing style. * <p> * * @param offset the character position in the text * @return a cloned StyleRange with start == offset and length == 1 if a style is * 	specified or null if no style is specified */ StyleRange getStyleRangeAtOffset(int offset) {	if (styleCount == 0) return null;	Point pt = getOverlappingStyles(offset, 1);	if (pt == null || pt.y == 0) return null;	StyleRange newStyle = (StyleRange)styles[pt.x].clone();	newStyle.start = offset;	newStyle.length = 1;	return newStyle;}/**  * Returns the styles for the given range. Returns the existing styles, * so be careful not to modify the return value.  Styles are not cloned * in order to make this method as efficient as possible.  * <p> * * @param offset the start position of the text range * @param length the length of the text range * @return a StyleRange array or null if no styles are specified for the text * 	range */ StyleRange[] getStyleRangesFor(int offset, int length) {	if (styleCount == 0) return null;	Point pt = getOverlappingStyles(offset, length);	if (pt == null || pt.y == 0) return null;	StyleRange[] ranges = new StyleRange[pt.y];	for (int i=0; i<pt.y; i++) {		StyleRange newStyle = styles[pt.x + i];		ranges[i]=newStyle;	}	return ranges;}void release () {	styles = null;}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -