📄 textareapainter.java
字号:
/* * TextAreaPainter.java - Paints the text area * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.gjt.sp.jedit.textarea;//{{{ Importsimport javax.swing.text.*;import javax.swing.JComponent;import java.awt.event.MouseEvent;import java.awt.font.*;import java.awt.*;import java.util.HashMap;import org.gjt.sp.jedit.syntax.*;import org.gjt.sp.jedit.Buffer;import org.gjt.sp.util.Log;//}}}/** * The text area painter is the component responsible for displaying the * text of the current buffer. The only methods in this class that should * be called by plugins are those for adding and removing * text area extensions. * * @see #addExtension(TextAreaExtension) * @see #addExtension(int,TextAreaExtension) * @see #removeExtension(TextAreaExtension) * @see TextAreaExtension * @see JEditTextArea * * @author Slava Pestov * @version $Id: TextAreaPainter.java,v 1.61 2003/02/15 01:09:21 spestov Exp $ */public class TextAreaPainter extends JComponent implements TabExpander{ //{{{ Layers /** * The lowest possible layer. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int LOWEST_LAYER = Integer.MIN_VALUE; /** * Below selection layer. The JDiff plugin will use this. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int BACKGROUND_LAYER = -60; /** * The line highlight and collapsed fold highlight layer. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre7 */ public static final int LINE_BACKGROUND_LAYER = -50; /** * Below selection layer. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int BELOW_SELECTION_LAYER = -40; /** * Selection layer. Most extensions will be above this layer, but some * (eg, JDiff) will want to be below the selection. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int SELECTION_LAYER = -30; /** * Wrap guide layer. Most extensions will be above this layer. * @since jEdit 4.0pre4 */ public static final int WRAP_GUIDE_LAYER = -20; /** * Below most extensions layer. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int BELOW_MOST_EXTENSIONS_LAYER = -10; /** * Default extension layer. This is above the wrap guide but below the * bracket highlight. * @since jEdit 4.0pre4 */ public static final int DEFAULT_LAYER = 0; /** * Bracket highlight layer. Most extensions will be below this layer. * @since jEdit 4.0pre4 */ public static final int BRACKET_HIGHLIGHT_LAYER = 100; /** * Highest possible layer. * @since jEdit 4.0pre4 */ public static final int HIGHEST_LAYER = Integer.MAX_VALUE; //}}} //{{{ TextAreaPainter constructor /** * Creates a new painter. Do not create instances of this class * directly. */ public TextAreaPainter(JEditTextArea textArea) { enableEvents(AWTEvent.FOCUS_EVENT_MASK | AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK); this.textArea = textArea; extensionMgr = new ExtensionManager(); setAutoscrolls(true); setOpaque(true); setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); fontRenderContext = new FontRenderContext(null,false,false); addExtension(LINE_BACKGROUND_LAYER,lineBackground = new PaintLineBackground()); addExtension(SELECTION_LAYER,new PaintSelection()); addExtension(WRAP_GUIDE_LAYER,new WrapGuide()); addExtension(BRACKET_HIGHLIGHT_LAYER,new BracketHighlight()); } //}}} //{{{ setBounds() method /** * It is a bad idea to override this, but we need to get the component * event before the first repaint. */ public void setBounds(int x, int y, int width, int height) { if(x == getX() && y == getY() && width == getWidth() && height == getHeight()) { return; } super.setBounds(x,y,width,height); textArea.recalculateVisibleLines(); textArea.recalculateLastPhysicalLine(); textArea.propertiesChanged(); textArea.scrollBarsInitialized = true; } //}}} //{{{ isManagingFocus() method /** * Returns if this component can be traversed by pressing the * Tab key. This returns false. */ public boolean isManagingFocus() { return false; } //}}} //{{{ getFocusTraversalKeysEnabled() method /** * Makes the tab key work in Java 1.4. * @since jEdit 3.2pre4 */ public boolean getFocusTraversalKeysEnabled() { return false; } //}}} //{{{ Getters and setters //{{{ getStyles() method /** * Returns the syntax styles used to paint colorized text. Entry <i>n</i> * will be used to paint tokens with id = <i>n</i>. * @see org.gjt.sp.jedit.syntax.Token */ public final SyntaxStyle[] getStyles() { return styles; } //}}} //{{{ setStyles() method /** * Sets the syntax styles used to paint colorized text. Entry <i>n</i> * will be used to paint tokens with id = <i>n</i>. * @param styles The syntax styles * @see org.gjt.sp.jedit.syntax.Token */ public final void setStyles(SyntaxStyle[] styles) { this.styles = styles; styles[Token.NULL] = new SyntaxStyle(getForeground(),null,getFont()); repaint(); } //}}} //{{{ getCaretColor() method /** * Returns the caret color. */ public final Color getCaretColor() { return caretColor; } //}}} //{{{ setCaretColor() method /** * Sets the caret color. * @param caretColor The caret color */ public final void setCaretColor(Color caretColor) { this.caretColor = caretColor; if(textArea.getBuffer() != null) textArea.invalidateLine(textArea.getCaretLine()); } //}}} //{{{ getSelectionColor() method /** * Returns the selection color. */ public final Color getSelectionColor() { return selectionColor; } //}}} //{{{ setSelectionColor() method /** * Sets the selection color. * @param selectionColor The selection color */ public final void setSelectionColor(Color selectionColor) { this.selectionColor = selectionColor; if(textArea.getBuffer() != null) textArea.invalidateSelectedLines(); } //}}} //{{{ getLineHighlightColor() method /** * Returns the line highlight color. */ public final Color getLineHighlightColor() { return lineHighlightColor; } //}}} //{{{ setLineHighlightColor() method /** * Sets the line highlight color. * @param lineHighlightColor The line highlight color */ public final void setLineHighlightColor(Color lineHighlightColor) { this.lineHighlightColor = lineHighlightColor; if(textArea.getBuffer() != null) textArea.invalidateLine(textArea.getCaretLine()); } //}}} //{{{ isLineHighlightEnabled() method /** * Returns true if line highlight is enabled, false otherwise. */ public final boolean isLineHighlightEnabled() { return lineHighlight; } //}}} //{{{ setLineHighlightEnabled() method /** * Enables or disables current line highlighting. * @param lineHighlight True if current line highlight should be enabled, * false otherwise */ public final void setLineHighlightEnabled(boolean lineHighlight) { this.lineHighlight = lineHighlight; if(textArea.getBuffer() != null) textArea.invalidateSelectedLines(); } //}}} //{{{ getBracketHighlightColor() method /** * Returns the bracket highlight color. */ public final Color getBracketHighlightColor() { return bracketHighlightColor; } //}}} //{{{ setBracketHighlightColor() method /** * Sets the bracket highlight color. * @param bracketHighlightColor The bracket highlight color */ public final void setBracketHighlightColor(Color bracketHighlightColor) { this.bracketHighlightColor = bracketHighlightColor; if(textArea.getBuffer() != null) textArea.invalidateLine(textArea.getBracketLine()); } //}}} //{{{ isBracketHighlightEnabled() method /** * Returns true if bracket highlighting is enabled, false otherwise. * When bracket highlighting is enabled, the bracket matching the * one before the caret (if any) is highlighted. */ public final boolean isBracketHighlightEnabled() { return bracketHighlight; } //}}} //{{{ setBracketHighlightEnabled() method /** * Enables or disables bracket highlighting. * When bracket highlighting is enabled, the bracket matching the * one before the caret (if any) is highlighted. * @param bracketHighlight True if bracket highlighting should be * enabled, false otherwise */ public final void setBracketHighlightEnabled(boolean bracketHighlight) { this.bracketHighlight = bracketHighlight; if(textArea.getBuffer() != null) textArea.invalidateLine(textArea.getBracketLine()); } //}}} //{{{ isBlockCaretEnabled() method /** * Returns true if the caret should be drawn as a block, false otherwise. */ public final boolean isBlockCaretEnabled() { return blockCaret; } //}}} //{{{ setBlockCaretEnabled() method /** * Sets if the caret should be drawn as a block, false otherwise. * @param blockCaret True if the caret should be drawn as a block, * false otherwise. */ public final void setBlockCaretEnabled(boolean blockCaret) { this.blockCaret = blockCaret; if(textArea.getBuffer() != null) textArea.invalidateLine(textArea.getCaretLine()); } //}}} //{{{ getEOLMarkerColor() method /** * Returns the EOL marker color. */ public final Color getEOLMarkerColor() { return eolMarkerColor; } //}}} //{{{ setEOLMarkerColor() method /** * Sets the EOL marker color. * @param eolMarkerColor The EOL marker color */ public final void setEOLMarkerColor(Color eolMarkerColor) { this.eolMarkerColor = eolMarkerColor; repaint(); } //}}} //{{{ getEOLMarkersPainted() method /** * Returns true if EOL markers are drawn, false otherwise. */ public final boolean getEOLMarkersPainted() { return eolMarkers; } //}}} //{{{ setEOLMarkersPainted() method /** * Sets if EOL markers are to be drawn. * @param eolMarkers True if EOL markers should be drawn, false otherwise */ public final void setEOLMarkersPainted(boolean eolMarkers) { this.eolMarkers = eolMarkers; repaint(); } //}}} //{{{ getWrapGuideColor() method /** * Returns the wrap guide color. */ public final Color getWrapGuideColor() { return wrapGuideColor; } //}}} //{{{ setWrapGuideColor() method /** * Sets the wrap guide color. * @param wrapGuideColor The wrap guide color */ public final void setWrapGuideColor(Color wrapGuideColor) { this.wrapGuideColor = wrapGuideColor; repaint(); } //}}} //{{{ isWrapGuidePainted() method /** * Returns true if the wrap guide is drawn, false otherwise. * @since jEdit 4.0pre4 */ public final boolean isWrapGuidePainted() { return wrapGuide; } //}}} //{{{ setWrapGuidePainted() method /** * Sets if the wrap guide is to be drawn. * @param wrapGuide True if the wrap guide should be drawn, false otherwise */ public final void setWrapGuidePainted(boolean wrapGuide) { this.wrapGuide = wrapGuide; repaint(); } //}}} //{{{ getFoldLineStyle() method /** * Returns the fold line style. */ public final SyntaxStyle getFoldLineStyle() { return foldLineStyle; } //}}} //{{{ setFoldLineStyle() method /** * Sets the fold line style. * @param foldLineStyle The fold line style */ public final void setFoldLineStyle(SyntaxStyle foldLineStyle) { this.foldLineStyle = foldLineStyle; repaint(); } //}}} //{{{ setAntiAliasEnabled() method /** * Sets if anti-aliasing should be enabled. Has no effect when * running on Java 1.1. * @since jEdit 3.2pre6 */ public void setAntiAliasEnabled(boolean antiAlias) { this.antiAlias = antiAlias; updateRenderingHints(); } //}}} //{{{ isAntiAliasEnabled() method /** * Returns if anti-aliasing is enabled. * @since jEdit 3.2pre6 */ public boolean isAntiAliasEnabled() { return antiAlias; } //}}} //{{{ setFractionalFontMetricsEnabled() method /** * Sets if fractional font metrics should be enabled. Has no effect when * running on Java 1.1. * @since jEdit 3.2pre6 */ public void setFractionalFontMetricsEnabled(boolean fracFontMetrics) { this.fracFontMetrics = fracFontMetrics; updateRenderingHints(); } //}}} //{{{ isFractionalFontMetricsEnabled() method /** * Returns if fractional font metrics are enabled. * @since jEdit 3.2pre6 */ public boolean isFractionalFontMetricsEnabled() { return fracFontMetrics; } //}}} //{{{ getFontRenderContext() method /** * Returns the font render context. * @since jEdit 4.0pre4 */ public FontRenderContext getFontRenderContext() { return fontRenderContext; } //}}} //}}} //{{{ addExtension() method /** * Adds a text area extension, which can perform custom painting and * tool tip handling. * @param extension The extension * @since jEdit 4.0pre4 */ public void addExtension(TextAreaExtension extension) { extensionMgr.addExtension(DEFAULT_LAYER,extension); repaint(); } //}}} //{{{ addExtension() method /** * Adds a text area extension, which can perform custom painting and * tool tip handling. * @param layer The layer to add the extension to. Note that more than * extension can share the same layer. * @param extension The extension * @since jEdit 4.0pre4 */ public void addExtension(int layer, TextAreaExtension extension) { extensionMgr.addExtension(layer,extension); repaint(); } //}}} //{{{ removeExtension() method /** * Removes a text area extension. It will no longer be asked to * perform custom painting and tool tip handling. * @param extension The extension * @since jEdit 4.0pre4 */ public void removeExtension(TextAreaExtension extension) { extensionMgr.removeExtension(extension); repaint(); } //}}} //{{{ getExtensions() method /** * Returns an array of registered text area extensions. Useful for * debugging purposes. * @since jEdit 4.1pre5 */ public TextAreaExtension[] getExtensions()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -