📄 textareapainter.java
字号:
package org.jfree.designer.text.jedit;
/*
* TextAreaPainter.java - Paints the text area
* Copyright (C) 1999 Slava Pestov
*
* You may use and modify this package for any purpose. Redistribution is
* permitted, in both source and binary form, provided that this notice
* remains intact in all source distributions of this package.
*/
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.ToolTipManager;
import javax.swing.text.PlainDocument;
import javax.swing.text.Segment;
import javax.swing.text.TabExpander;
import javax.swing.text.Utilities;
import org.jfree.designer.text.jedit.syntax.SyntaxStyle;
import org.jfree.designer.text.jedit.syntax.SyntaxUtilities;
import org.jfree.designer.text.jedit.syntax.Token;
import org.jfree.designer.text.jedit.syntax.TokenMarker;
/**
* The text area repaint manager. It performs double buffering and paints lines of text.
*
* @author Slava Pestov
* @version $Id: TextAreaPainter.java,v 1.2 2004/04/20 18:54:51 taqua Exp $
*/
public final class TextAreaPainter
extends JComponent
implements TabExpander
{
/**
* Creates a new repaint manager. This should be not be called directly.
*/
public TextAreaPainter (final JEditTextArea textArea, final TextAreaDefaults defaults)
{
this.textArea = textArea;
setAutoscrolls(true);
setDoubleBuffered(true);
setOpaque(true);
ToolTipManager.sharedInstance().registerComponent(this);
currentLine = new Segment();
currentLineIndex = -1;
setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
setFont(new Font("Monospaced", Font.PLAIN, 14));
setForeground(Color.black);
setBackground(Color.white);
blockCaret = defaults.blockCaret;
styles = defaults.styles;
cols = defaults.cols;
rows = defaults.rows;
caretColor = defaults.caretColor;
selectionColor = defaults.selectionColor;
lineHighlightColor = defaults.lineHighlightColor;
lineHighlight = defaults.lineHighlight;
bracketHighlightColor = defaults.bracketHighlightColor;
bracketHighlight = defaults.bracketHighlight;
paintInvalid = defaults.paintInvalid;
eolMarkerColor = defaults.eolMarkerColor;
eolMarkers = defaults.eolMarkers;
}
/**
* 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 com.efs.transfix.gui.components.jEditTextArea.Token
*/
public final SyntaxStyle[] getStyles ()
{
return styles;
}
/**
* 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 com.efs.transfix.gui.components.jEditTextArea.Token
*/
public final void setStyles (final SyntaxStyle[] styles)
{
this.styles = styles;
repaint();
}
/**
* Returns the caret color.
*/
public final Color getCaretColor ()
{
return caretColor;
}
/**
* Sets the caret color.
*
* @param caretColor The caret color
*/
public final void setCaretColor (final Color caretColor)
{
this.caretColor = caretColor;
invalidateSelectedLines();
}
/**
* Returns the selection color.
*/
public final Color getSelectionColor ()
{
return selectionColor;
}
/**
* Sets the selection color.
*
* @param selectionColor The selection color
*/
public final void setSelectionColor (final Color selectionColor)
{
this.selectionColor = selectionColor;
invalidateSelectedLines();
}
/**
* Returns the line highlight color.
*/
public final Color getLineHighlightColor ()
{
return lineHighlightColor;
}
/**
* Sets the line highlight color.
*
* @param lineHighlightColor The line highlight color
*/
public final void setLineHighlightColor (final Color lineHighlightColor)
{
this.lineHighlightColor = lineHighlightColor;
invalidateSelectedLines();
}
/**
* Returns true if line highlight is enabled, false otherwise.
*/
public final boolean isLineHighlightEnabled ()
{
return lineHighlight;
}
/**
* Enables or disables current line highlighting.
*
* @param lineHighlight True if current line highlight should be enabled, false
* otherwise
*/
public final void setLineHighlightEnabled (final boolean lineHighlight)
{
this.lineHighlight = lineHighlight;
invalidateSelectedLines();
}
/**
* Returns the bracket highlight color.
*/
public final Color getBracketHighlightColor ()
{
return bracketHighlightColor;
}
/**
* Sets the bracket highlight color.
*
* @param bracketHighlightColor The bracket highlight color
*/
public final void setBracketHighlightColor (final Color bracketHighlightColor)
{
this.bracketHighlightColor = bracketHighlightColor;
invalidateLine(textArea.getBracketLine());
}
/**
* 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;
}
/**
* 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 (final boolean bracketHighlight)
{
this.bracketHighlight = bracketHighlight;
invalidateLine(textArea.getBracketLine());
}
/**
* Returns true if the caret should be drawn as a block, false otherwise.
*/
public final boolean isBlockCaretEnabled ()
{
return blockCaret;
}
/**
* 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 (final boolean blockCaret)
{
this.blockCaret = blockCaret;
invalidateSelectedLines();
}
/**
* Returns the EOL marker color.
*/
public final Color getEOLMarkerColor ()
{
return eolMarkerColor;
}
/**
* Sets the EOL marker color.
*
* @param eolMarkerColor The EOL marker color
*/
public final void setEOLMarkerColor (final Color eolMarkerColor)
{
this.eolMarkerColor = eolMarkerColor;
repaint();
}
/**
* Returns true if EOL markers are drawn, false otherwise.
*/
public final boolean getEOLMarkersPainted ()
{
return eolMarkers;
}
/**
* Sets if EOL markers are to be drawn.
*
* @param eolMarkers True if EOL markers should be drawn, false otherwise
*/
public final void setEOLMarkersPainted (final boolean eolMarkers)
{
this.eolMarkers = eolMarkers;
repaint();
}
/**
* Returns true if invalid lines are painted as red tildes (~), false otherwise.
*/
public final boolean getInvalidLinesPainted ()
{
return paintInvalid;
}
/**
* Sets if invalid lines are to be painted as red tildes.
*
* @param paintInvalid True if invalid lines should be drawn, false otherwise
*/
public final void setInvalidLinesPainted (final boolean paintInvalid)
{
this.paintInvalid = paintInvalid;
}
/**
* Adds a custom highlight painter.
*
* @param highlight The highlight
*/
public final void addCustomHighlight (final Highlight highlight)
{
highlight.init(textArea, highlights);
highlights = highlight;
}
/**
* Highlight interface.
*/
public interface Highlight
{
/**
* Called after the highlight painter has been added.
*
* @param textArea The text area
* @param next The painter this one should delegate to
*/
void init (JEditTextArea textArea, Highlight next);
/**
* This should paint the highlight and delgate to the next highlight painter.
*
* @param gfx The graphics context
* @param line The line number
* @param y The y co-ordinate of the line
*/
void paintHighlight (Graphics gfx, int line, int y);
/**
* Returns the tool tip to display at the specified location. If this highlighter
* doesn't know what to display, it should delegate to the next highlight painter.
*
* @param evt The mouse event
*/
String getToolTipText (MouseEvent evt);
}
/**
* Returns the tool tip to display at the specified location.
*
* @param evt The mouse event
*/
public final String getToolTipText (final MouseEvent evt)
{
if (highlights != null)
{
return highlights.getToolTipText(evt);
}
else
{
return null;
}
}
/**
* Returns the font metrics used by this component.
*/
public final FontMetrics getFontMetrics ()
{
return fm;
}
/**
* Sets the font for this component. This is overridden to update the cached font
* metrics and to recalculate which lines are visible.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -