 |
Gridarta Editor
|
Go to the documentation of this file.
11 package net.sf.gridarta.textedit.textarea;
13 import java.awt.Color;
14 import java.awt.Cursor;
15 import java.awt.Dimension;
17 import java.awt.FontMetrics;
18 import java.awt.Graphics;
19 import java.awt.Rectangle;
20 import java.awt.font.FontRenderContext;
21 import java.awt.font.TextLayout;
22 import java.util.Collections;
23 import java.util.LinkedList;
24 import java.util.List;
25 import javax.swing.JComponent;
26 import javax.swing.ToolTipManager;
27 import javax.swing.text.Segment;
28 import javax.swing.text.TabExpander;
29 import javax.swing.text.Utilities;
31 import org.apache.log4j.Category;
32 import org.apache.log4j.Logger;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
58 private final FontRenderContext
fontRenderContext =
new FontRenderContext(
null,
false,
false);
247 setAutoscrolls(
true);
248 setDoubleBuffered(
true);
251 ToolTipManager.sharedInstance().registerComponent(
this);
253 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
255 final Font font =
new Font(
"Monospaced", Font.PLAIN, 14);
258 setForeground(Color.black);
259 setBackground(Color.white);
262 styles = defaults.getStyles();
263 cols = defaults.getCols();
264 rows = defaults.getRows();
281 final Font font = getFont();
359 public final void setFont(@NotNull
final Font font) {
370 public void paint(@NotNull
final Graphics g) {
373 final Rectangle clipRectangle = g.getClipBounds();
375 g.setColor(getBackground());
376 g.fillRect(clipRectangle.x, clipRectangle.y, clipRectangle.width, clipRectangle.height);
381 final int firstInvalid =
firstLine + clipRectangle.y / height;
385 final int lastInvalid =
firstLine + (clipRectangle.y + clipRectangle.height - 1) / height;
390 for (
int line = firstInvalid; line <= lastInvalid; line++) {
395 final int h = clipRectangle.y + clipRectangle.height;
396 repaint(0, h, getWidth(), getHeight() - h);
398 }
catch (
final Exception e) {
399 LOG.error(
"Error repainting line range {" + firstInvalid +
", " + lastInvalid +
"}", e);
436 final Dimension dim = super.getPreferredSize();
443 final Dimension dim = super.getMinimumSize();
465 private void paintLine(@NotNull
final Graphics gfx, @Nullable
final TokenMarker tokenMarker,
final int line,
final int x) {
469 final Font font = getFont();
470 final Color foreground = getForeground();
475 gfx.drawString(
"~", 0, y +
fontMetrics.getHeight());
477 }
else if (tokenMarker ==
null) {
493 private void paintPlainLine(@NotNull
final Graphics gfx,
final int line, @NotNull
final Font
defaultFont, @NotNull
final Color defaultColor,
final int x,
final int y) {
498 gfx.setColor(defaultColor);
501 final int xx = Utilities.drawTabbedText(
currentLine, x, yy, gfx,
this, 0);
505 gfx.drawString(
".", xx, yy);
519 private void paintSyntaxLine(@NotNull
final Graphics gfx, @NotNull
final TokenMarker tokenMarker,
final int line, @NotNull
final Font
defaultFont, @NotNull
final Color defaultColor,
final int x,
final int y) {
526 gfx.setColor(defaultColor);
533 gfx.drawString(
".", xx, yy);
544 private void paintHighlight(@NotNull
final Graphics gfx,
final int line,
final int y) {
571 if (selectionStart == selectionEnd) {
574 gfx.fillRect(0, yy, getWidth(), height);
589 x2 = x3 + (x1 == x3 ? 1 : 0);
590 }
else if (selectionStartLine == selectionEndLine) {
591 x1 =
offsetToX2(line, selectionStart - lineStart);
592 x2 =
offsetToX2(line, selectionEnd - lineStart);
593 }
else if (line == selectionStartLine) {
594 x1 =
offsetToX2(line, selectionStart - lineStart);
596 }
else if (line == selectionEndLine) {
598 x2 =
offsetToX2(line, selectionEnd - lineStart);
604 gfx.fillRect(Math.min(x1, x2), yy, Math.abs(x1 - x2), height);
617 if (position == -1) {
636 private void paintCaret(@NotNull
final Graphics gfx,
final int line,
final int y) {
647 gfx.fillRect(caretX, yy + height - 1, caretWidth, 1);
649 if (caretWidth <= 1) {
653 gfx.drawLine(caretX, yy, caretX, yy + height - 1);
655 gfx.drawRect(caretX, yy, caretWidth - 1, height - 1);
699 final int height = getHeight();
702 final int lineHeight;
776 if (tokenMarker ==
null) {
778 return x + Utilities.getTabbedTextWidth(
lineSegment, fm, x,
this, 0);
782 final List<Token> tokens;
792 for (
final Token token : tokens) {
793 final byte id = token.getId();
796 final int length = token.getLength();
798 if (offset + segmentOffset <
lineSegment.offset + length) {
800 return x + Utilities.getTabbedTextWidth(
lineSegment, fm, x,
this, 0);
803 x += Utilities.getTabbedTextWidth(
lineSegment, fm, x,
this, 0);
815 public int offsetToX(
final int line,
final int offset) {
827 final int height = fm.getHeight();
850 if (tokenMarker ==
null) {
851 for (
int i = 0; i < segmentCount; i++) {
852 final char c = segmentArray[i + segmentOffset];
857 charWidth = fm.charWidth(c);
861 if (x - charWidth <= width) {
865 if (x - charWidth / 2 <= width) {
875 final List<Token> tokens;
886 for (
final Token token : tokens) {
887 final byte id = token.getId();
890 final int length = token.getLength();
892 for (
int i = 0; i < length; i++) {
893 final char c = segmentArray[segmentOffset + offset + i];
896 charWidth = (int)
nextTabStop(width, offset + i) - width;
898 charWidth = fm.charWidth(c);
902 if (x - charWidth <= width) {
906 if (x - charWidth / 2 <= width) {
final TextAreaBrackets brackets
The brackets to paint.
static final Category LOG
The Logger for printing log messages.
boolean setHorizontalOffset(final int horizontalOffset)
TokenMarker getTokenMarker()
Returns the token marker that is to be used to split lines of this document up into tokens.
Encapsulates default settings for a text area.
void setGraphicsFlags(final Graphics gfx, final Font font)
Sets the text color and font of the specified graphics context to that specified in this style.
final boolean paintInvalid
Whether invalid lines should be painted as red tildes.
final JEditTextArea textArea
The associated text area that is painted.
final Color selectionColor
The selection color.
jEdit's text area component.
int getSelectionStart()
Returns the selection start offset.
float nextTabStop(final float x, final int tabOffset)
int getSelectionEnd()
Returns the selection end offset.
int getSelectionEndLine()
Returns the selection end line.
void setCurrentLineTokens(@Nullable final List< Token > tokens)
Sets the tokens of the currently painted line.
This package contains the other part of the script editor.
Base package of all Gridarta classes.
void invalidateLineRange(final int firstLine, final int lastLine)
Marks a range of lines as needing a repaint.
boolean isBlockCaretEnabled()
Returns whether the caret should be drawn as a block.
boolean isSelectionRectangular()
Returns true if the selection is rectangular, false otherwise.
static final byte NULL
Normal text token id.
int getCurrentLineIndex()
Returns the currently painted line.
final FontRenderContext fontRenderContext
The font render context for this instance.
TokenMarker getTokenMarker()
Returns the document's token marker.
int getHorizontalOffset()
Returns the horizontal offset of drawn lines.
final TextAreaConfig config
The TextAreaConfig options to use.
Font defaultFont
The Font from which defaultLineHeight and {} have been calculated.
final Color caretColor
The caret color.
void setStyles(@NotNull final SyntaxStyles styles)
Sets the syntax styles used to paint colorized text.
void paintPlainLine(@NotNull final Graphics gfx, final int line, @NotNull final Font defaultFont, @NotNull final Color defaultColor, final int x, final int y)
Paint a line without token highlighting.
void invalidateSelectedLines()
Repaints the lines containing the selection.
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
final Color lineHighlightColor
The color for line highlighting.
void paint(@NotNull final Graphics g)
Repaints the text.
SyntaxDocument getDocument()
Returns the document this text area is editing.
FontMetrics getFontMetrics()
Returns the font metrics used by this component.
void recalculateVisibleLines()
Recalculates the number of visible lines.
int getBracketPosition()
Returns the position of the highlighted bracket (the bracket matching the one before the caret).
int currentLineIndex
The currently painted line.
boolean setFirstLine(final int firstLine)
final boolean lineHighlight
Whether line highlighting is enabled.
The text area repaint manager.
TextAreaPainter(@NotNull final JEditTextArea textArea, @NotNull final TextAreaSelection selection, @NotNull final TextAreaCaret caret, @NotNull final TextAreaDefaults defaults, @NotNull final TextAreaBrackets brackets, @NotNull final TextAreaConfig config, final boolean paintInvalid)
Creates a new repaint manager.
void paintCaret(@NotNull final Graphics gfx, final int line, final int y)
Paints the caret highlight.
boolean isNextLineRequested()
Returns true if the next line should be repainted.
void setCurrentLineIndex(final int lineIndex)
Sets the currently painted line.
int getLineCount()
Returns the number of lines in the document.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
int xToOffset(final int line, final int x)
Converts a x co-ordinate to an offset within a line.
int getCaretPosition()
Returns the caret position.
SyntaxStyles getStyles()
Returns the syntax styles used to paint colorized text.
int offsetToX2(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
static final byte INVALID
Invalid token id.
void paintBracketHighlight(@NotNull final Graphics gfx, final int line, final int y)
Paints the bracket highlight.
int tabSize
The tab size in pixels.
final TextAreaCaret caret
int getFirstLine()
Returns the line displayed at the text area's origin.
SyntaxStyle getStyle(final byte id)
Returns a style for a token id.
boolean isOverwrite()
Returns whether overwrite mode is active.
int yToLine(final int y)
Converts a y co-ordinate to a line index.
final boolean eolMarkers
Whether end of line markers should be painted.
void updateScrollBars()
Updates the state of the scroll bars.
FontMetrics fontMetrics
The font metrics for this instance.
int lineToY(final int line)
Converts a line index to a y co-ordinate.
FontMetrics getFontMetrics(final Font font, final Graphics g)
Returns the font metrics for the styled font.
Class with several utility functions used by jEdit's syntax colorizing subsystem.
List< Token > getCurrentLineTokens()
Returns the tokens of the currently painted line.
List< Token > currentLineTokens
The tokens of the currently painted line.
static final long serialVersionUID
The serial version UID.
int defaultCharWidth
The character width of defaultFont.
Maintains information about the highlighted pairs of brackets.
void paintHighlight(@NotNull final Graphics gfx, final int line, final int y)
Adds highlights for a line: selections, custom highlights from client code, brackets,...
boolean isBracketHighlightEnabled()
Returns whether bracket highlighting is enabled.
final Segment currentLine
Holds the currently painted line.
final int rows
The number of rows.
int getDefaultLineHeight()
This works only for fonts with fixed line height.
void paintLine(@NotNull final Graphics gfx, @Nullable final TokenMarker tokenMarker, final int line, final int x)
Paints one line.
final Color eolMarkerColor
The color for painting eol markers.
final void setFont(@NotNull final Font font)
final TextAreaSelection selection
Dimension newDimension(final int columns, final int rows)
Returns a Dimension measured in default character sizes.
Dimension getPreferredSize()
A token marker that splits lines of text into tokens.
void updateLineInfo()
Make sure defaultCharWidth and defaultLineHeight are up-to-date.
static int paintSyntaxLine(@NotNull final Segment line, @NotNull final Iterable< Token > tokens, @NotNull final SyntaxStyles styles, @NotNull final TabExpander expander, @NotNull final Graphics gfx, final int x, final int y)
Paints the specified line onto the graphics context.
int offsetToX(final int line, final int offset)
Converts an offset in a line into an x co-ordinate.
Dimension getMinimumSize()
boolean isCaretVisible()
Returns true if the caret is visible, false otherwise.
final boolean blockCaret
Whether the caret should be wide even in insert mode.
Miscellaneous configuration settings for JEditTextArea.
int defaultLineHeight
The line height of defaultFont.
final Color bracketHighlightColor
The color for bracket highlighting.
int getLineLength(final int line)
Returns the length of the specified line.
void paintLineHighlight(@NotNull final Graphics gfx, final int line, final int y)
Paints the selection highlight.
void invalidateLine(final int line)
Marks a line as needing a repaint.
void paintSyntaxLine(@NotNull final Graphics gfx, @NotNull final TokenMarker tokenMarker, final int line, @NotNull final Font defaultFont, @NotNull final Color defaultColor, final int x, final int y)
Paint a line with token highlighting.
final int cols
The number of columns.
final Segment lineSegment
List< Token > markTokens(@NotNull final Segment line, final int lineIndex)
A wrapper for the lower-level.
int getSelectionStartLine()
Returns the selection start line.
SyntaxStyles styles
The syntax styles used to paint colorized text.
final boolean bracketHighlight
Whether bracket highlighting is enabled.
int xyToOffset(final int x, final int y)
Converts a point to an offset, from the start of the text.
int getCaretLine()
Returns the caret line.
A set of SyntaxStyle instances for painting colorized text.
int getBracketLine()
Returns the line of the highlighted bracket (the bracket matching the one before the caret).
int getDefaultCharWidth()
This works only for fonts with fixed line height.