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

📄 styledtext.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.custom;import java.util.*;import org.eclipse.swt.*;import org.eclipse.swt.accessibility.*;import org.eclipse.swt.dnd.*;import org.eclipse.swt.events.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.internal.*;import org.eclipse.swt.printing.*;import org.eclipse.swt.widgets.*;/** * A StyledText is an editable user interface object that displays lines  * of text.  The following style attributes can be defined for the text:  * <ul> * <li>foreground color  * <li>background color * <li>font style (bold, regular) * </ul> * <p> * In addition to text style attributes, the background color of a line may  * be specified. * </p> * <p> * There are two ways to use this widget when specifying text style information.   * You may use the API that is defined for StyledText or you may define your own  * LineStyleListener.  If you define your own listener, you will be responsible  * for maintaining the text style information for the widget.  IMPORTANT: You may  * not define your own listener and use the StyledText API.  The following * StyledText API is not supported if you have defined a LineStyleListener: * <ul> * <li>getStyleRangeAtOffset(int) * <li>getStyleRanges() * <li>replaceStyleRanges(int,int,StyleRange[]) * <li>setStyleRange(StyleRange) * <li>setStyleRanges(StyleRange[]) * </ul> * </p> * <p> * There are two ways to use this widget when specifying line background colors. * You may use the API that is defined for StyledText or you may define your own  * LineBackgroundListener.  If you define your own listener, you will be responsible  * for maintaining the line background color information for the widget.   * IMPORTANT: You may not define your own listener and use the StyledText API.   * The following StyledText API is not supported if you have defined a  * LineBackgroundListener: * <ul> * <li>getLineBackground(int) * <li>setLineBackground(int,int,Color) * </ul> * </p> * <p> * The content implementation for this widget may also be user-defined.  To do so, * you must implement the StyledTextContent interface and use the StyledText API * setContent(StyledTextContent) to initialize the widget.  * </p> * <p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> * <dl> * <dt><b>Styles:</b><dd>FULL_SELECTION, MULTI, READ_ONLY, SINGLE, WRAP * <dt><b>Events:</b><dd>ExtendedModify, LineGetBackground, LineGetSegments, LineGetStyle, Modify, Selection, Verify, VerifyKey * </dl> */public class StyledText extends Canvas {	static final char TAB = '\t';	static final String PlatformLineDelimiter = System.getProperty("line.separator");	static final int BIDI_CARET_WIDTH = 3;	static final int DEFAULT_WIDTH	= 64;	static final int DEFAULT_HEIGHT = 64;		static final int ExtendedModify = 3000;	static final int LineGetBackground = 3001;	static final int LineGetStyle = 3002;	static final int TextChanging = 3003;	static final int TextSet = 3004;	static final int VerifyKey = 3005;	static final int TextChanged = 3006;	static final int LineGetSegments = 3007;		Color selectionBackground;	// selection background color	Color selectionForeground;	// selection foreground color	StyledTextContent logicalContent;	// native content (default or user specified)	StyledTextContent content;			// line wrapping content, same as logicalContent if word wrap is off	DisplayRenderer renderer;	Listener listener;	TextChangeListener textChangeListener;	// listener for TextChanging, TextChanged and TextSet events from StyledTextContent	DefaultLineStyler defaultLineStyler;// used for setStyles API when no LineStyleListener is registered	LineCache lineCache;	boolean userLineStyle = false;		// true=widget is using a user defined line style listener for line styles. false=widget is using the default line styler to store line styles	boolean userLineBackground = false;	// true=widget is using a user defined line background listener for line backgrounds. false=widget is using the default line styler to store line backgrounds	int verticalScrollOffset = 0;		// pixel based	int horizontalScrollOffset = 0;		// pixel based	int topIndex = 0;					// top visible line	int lastPaintTopIndex = -1;	int topOffset = 0;					// offset of first character in top line	int clientAreaHeight = 0;			// the client area height. Needed to calculate content width for new 										// visible lines during Resize callback	int clientAreaWidth = 0;			// the client area width. Needed during Resize callback to determine 										// if line wrap needs to be recalculated	int lineHeight;						// line height=font height	int tabLength = 4;					// number of characters in a tab	int leftMargin;	int topMargin;	int rightMargin;	int bottomMargin;	Cursor ibeamCursor;			int columnX;							// keep track of the horizontal caret position										// when changing lines/pages. Fixes bug 5935	int caretOffset = 0;	Point selection = new Point(0, 0);	// x and y are start and end caret offsets of selection	int selectionAnchor;				// position of selection anchor. 0 based offset from beginning of text	Point doubleClickSelection;			// selection after last mouse double click	boolean editable = true;	boolean wordWrap = false;	boolean doubleClickEnabled = true;	// see getDoubleClickEnabled 	boolean overwrite = false;			// insert/overwrite edit mode	int textLimit = -1;					// limits the number of characters the user can type in the widget. Unlimited by default.	Hashtable keyActionMap = new Hashtable();	Color background = null;			// workaround for bug 4791	Color foreground = null;			//	Clipboard clipboard;	boolean mouseDoubleClick = false;	// true=a double click ocurred. Don't do mouse swipe selection.	int autoScrollDirection = SWT.NULL;	// the direction of autoscrolling (up, down, right, left)	int lastTextChangeStart;			// cache data of the 	int lastTextChangeNewLineCount;		// last text changing 	int lastTextChangeNewCharCount;		// event for use in the 	int lastTextChangeReplaceLineCount;	// text changed handler	int lastTextChangeReplaceCharCount;		boolean isBidi;	boolean isMirrored;	boolean bidiColoring = false;		// apply the BIDI algorithm on text segments of the same color	Image leftCaretBitmap = null;	Image rightCaretBitmap = null;	int caretDirection = SWT.NULL;	boolean advancing = true;	Caret defaultCaret = null;	boolean updateCaretDirection = true;	final static boolean IS_CARBON;	final static boolean DOUBLE_BUFFERED;		static {		String platform = SWT.getPlatform();		IS_CARBON = "carbon".equals(platform);		DOUBLE_BUFFERED = !("carbon".equals(platform) || "gtk".equals(platform));	}	/**	 * The Printing class implements printing of a range of text.	 * An instance of <class>Printing </class> is returned in the 	 * StyledText#print(Printer) API. The run() method may be 	 * invoked from any thread.	 */	static class Printing implements Runnable {		final static int LEFT = 0;						// left aligned header/footer segment		final static int CENTER = 1;					// centered header/footer segment		final static int RIGHT = 2;						// right aligned header/footer segment		StyledText parent;		Printer printer;		PrintRenderer renderer;		StyledTextPrintOptions printOptions;		StyledTextContent printerContent;				// copy of the widget content		Rectangle clientArea;							// client area to print on		Font printerFont;		FontData displayFontData;		Hashtable printerColors;						// printer color cache for line backgrounds and style		Hashtable lineBackgrounds = new Hashtable();	// cached line backgrounds		Hashtable lineStyles = new Hashtable();			// cached line styles		Hashtable bidiSegments = new Hashtable();		// cached bidi segments when running on a bidi platform		GC gc;											// printer GC		int pageWidth;									// width of a printer page in pixels		int startPage;									// first page to print		int endPage;									// last page to print		int pageSize;									// number of lines on a page		int startLine;									// first (wrapped) line to print		int endLine;									// last (wrapped) line to print		boolean singleLine;								// widget single line mode		Point selection = null;					// selected text	/**	 * Creates an instance of <class>Printing</class>.	 * Copies the widget content and rendering data that needs 	 * to be requested from listeners.	 * </p>	 * @param parent StyledText widget to print.	 * @param printer printer device to print on.	 * @param printOptions print options	 */			Printing(StyledText parent, Printer printer, StyledTextPrintOptions printOptions) {		PrinterData data = printer.getPrinterData();		this.parent = parent;		this.printer = printer;		this.printOptions = printOptions;		singleLine = parent.isSingleLine();		startPage = 1;		endPage = Integer.MAX_VALUE;		if (data.scope == PrinterData.PAGE_RANGE) {			startPage = data.startPage;			endPage = data.endPage;			if (endPage < startPage) {				int temp = endPage;				endPage = startPage;				startPage = temp;			}					} 		else 		if (data.scope == PrinterData.SELECTION) {			selection = parent.getSelectionRange();		}		displayFontData = parent.getFont().getFontData()[0];		copyContent(parent.getContent());		cacheLineData(printerContent);	}	/**	 * Caches the bidi segments of the given line.	 * </p>	 * @param lineOffset offset of the line to cache bidi segments for. 	 * 	Relative to the start of the document.	 * @param line line to cache bidi segments for. 	 */	void cacheBidiSegments(int lineOffset, String line) {		int[] segments = parent.getBidiSegments(lineOffset, line);				if (segments != null) {			bidiSegments.put(new Integer(lineOffset), segments);		}	}	/**	 * Caches the line background color of the given line.	 * </p>	 * @param lineOffset offset of the line to cache the background 	 * 	color for. Relative to the start of the document.	 * @param line line to cache the background color for	 */	void cacheLineBackground(int lineOffset, String line) {		StyledTextEvent event = parent.getLineBackgroundData(lineOffset, line);				if (event != null) {			lineBackgrounds.put(new Integer(lineOffset), event);		}	}	/**	 * Caches all line data that needs to be requested from a listener.	 * </p>	 * @param printerContent <class>StyledTextContent</class> to request 	 * 	line data for.	 */	void cacheLineData(StyledTextContent printerContent) {			for (int i = 0; i < printerContent.getLineCount(); i++) {			int lineOffset = printerContent.getOffsetAtLine(i);			String line = printerContent.getLine(i);				if (printOptions.printLineBackground) {				cacheLineBackground(lineOffset, line);			}			if (printOptions.printTextBackground ||				printOptions.printTextForeground ||				printOptions.printTextFontStyle) {				cacheLineStyle(lineOffset, line);			}			if (parent.isBidi()) {				cacheBidiSegments(lineOffset, line);			}		}	}	/**	 * Caches all line styles of the given line.	 * </p>	 * @param lineOffset offset of the line to cache the styles for.	 * 	Relative to the start of the document.	 * @param line line to cache the styles for.	 */	void cacheLineStyle(int lineOffset, String line) {		StyledTextEvent event = parent.getLineStyleData(lineOffset, line);				if (event != null) {			StyleRange[] styles = event.styles;			for (int i = 0; i < styles.length; i++) {				StyleRange styleCopy = null;				if (printOptions.printTextBackground == false && styles[i].background != null) {					styleCopy = (StyleRange) styles[i].clone();					styleCopy.background = null;				}				if (printOptions.printTextForeground == false && styles[i].foreground != null) {					if (styleCopy == null) {						styleCopy = (StyleRange) styles[i].clone();					}					styleCopy.foreground = null;				}				if (printOptions.printTextFontStyle == false && styles[i].fontStyle != SWT.NORMAL) {					if (styleCopy == null) {						styleCopy = (StyleRange) styles[i].clone();					}					styleCopy.fontStyle = SWT.NORMAL;				}				if (styleCopy != null) {					styles[i] = styleCopy;				}			}				lineStyles.put(new Integer(lineOffset), event);		}	}	/**	 * Copies the text of the specified <class>StyledTextContent</class>.	 * </p>	 * @param original the <class>StyledTextContent</class> to copy.	 */	void copyContent(StyledTextContent original) {		int insertOffset = 0;				printerContent = new DefaultContent();		for (int i = 0; i < original.getLineCount(); i++) {			int insertEndOffset;			if (i < original.getLineCount() - 1) {				insertEndOffset = original.getOffsetAtLine(i + 1);			}			else {				insertEndOffset = original.getCharCount();			}			printerContent.replaceTextRange(insertOffset, 0, original.getTextRange(insertOffset, insertEndOffset - insertOffset));			insertOffset = insertEndOffset;		}	}	/**	 * Replaces all display colors in the cached line backgrounds and 	 * line styles with printer colors.	 */	void createPrinterColors() {		Enumeration values = lineBackgrounds.elements();		printerColors = new Hashtable();		while (values.hasMoreElements()) {			StyledTextEvent event = (StyledTextEvent) values.nextElement();			event.lineBackground = getPrinterColor(event.lineBackground);		}				values = lineStyles.elements();		while (values.hasMoreElements()) {			StyledTextEvent event = (StyledTextEvent) values.nextElement();			for (int i = 0; i < event.styles.length; i++) {				StyleRange style = event.styles[i];				Color printerBackground = getPrinterColor(style.background);				Color printerForeground = getPrinterColor(style.foreground);								if (printerBackground != style.background || 					printerForeground != style.foreground) {					style = (StyleRange) style.clone();					style.background = printerBackground;					style.foreground = printerForeground;					event.styles[i] = style;				}			}		}			}	/**	 * Disposes of the resources and the <class>PrintRenderer</class>.	 */	void dispose() {		if (printerColors != null) {			Enumeration colors = printerColors.elements();						while (colors.hasMoreElements()) {				Color color = (Color) colors.nextElement();				color.dispose();			}			printerColors = null;		}		if (gc != null) {			gc.dispose();			gc = null;		}		if (printerFont != null) {			printerFont.dispose();			printerFont = null;		}		if (renderer != null) {

⌨️ 快捷键说明

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