org.fife.ui.rsyntaxtextarea.FoldingAwareIconRowHeader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rsyntaxtextarea Show documentation
Show all versions of rsyntaxtextarea Show documentation
RSyntaxTextArea is the syntax highlighting text editor for Swing applications. Features include syntax highlighting for 40+ languages, code folding, code completion, regex find and replace, macros, code templates, undo/redo, line numbering and bracket matching.
/*
* 03/07/2012
*
* FoldingAwareIconRowHeader - Icon row header that paints itself correctly
* even when code folding is enabled.
*
* This library is distributed under a modified BSD license. See the included
* LICENSE file for details.
*/
package org.fife.ui.rsyntaxtextarea;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.Icon;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import org.fife.ui.rsyntaxtextarea.folding.FoldManager;
import org.fife.ui.rtextarea.GutterIconInfo;
import org.fife.ui.rtextarea.IconRowHeader;
/**
* A row header component that takes code folding into account when painting
* itself.
*
* @author Robert Futrell
* @version 1.0
*/
public class FoldingAwareIconRowHeader extends IconRowHeader {
/**
* Constructor.
*
* @param textArea The parent text area.
*/
public FoldingAwareIconRowHeader(RSyntaxTextArea textArea) {
super(textArea);
}
@Override
protected void paintComponent(Graphics g) {
// When line wrap is not enabled, take the faster code path.
if (textArea==null) {
return;
}
RSyntaxTextArea rsta = (RSyntaxTextArea)textArea;
FoldManager fm = rsta.getFoldManager();
if (!fm.isCodeFoldingSupportedAndEnabled()) {
super.paintComponent(g);
return;
}
visibleRect = g.getClipBounds(visibleRect);
if (visibleRect==null) { // ???
visibleRect = getVisibleRect();
}
//System.out.println("IconRowHeader repainting: " + visibleRect);
if (visibleRect==null) {
return;
}
paintBackgroundImpl(g, visibleRect);
if (textArea.getLineWrap()) {
paintComponentWrapped(g);
return;
}
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
textAreaInsets = textArea.getInsets(textAreaInsets);
if (visibleRect.y-1 && activeLineRangeEnd>-1) {
Color activeLineRangeColor = getActiveLineRangeColor();
g.setColor(activeLineRangeColor);
try {
int realY1 = rsta.yForLine(activeLineRangeStart);
if (realY1>-1) { // Not in a collapsed fold...
int y1 = realY1;//Math.max(y, realY1);
int y2 = rsta.yForLine(activeLineRangeEnd);
if (y2==-1) { // In a collapsed fold
y2 = y1;
}
y2 += cellHeight - 1;
if (y2visibleRect.y+visibleRect.height) {
//System.out.println("... nothing to paint, bailing...");
return;
}
y1 = Math.max(y, realY1);
y2 = Math.min(y2, visibleRect.y+visibleRect.height);
//System.out.println(y1 + "... " + y2 + "; " + realY1 + ", " + visibleRect);
int j = y1;
while (j<=y2) {
int yEnd = Math.min(y2, j+getWidth());
int xEnd = yEnd-j;
g.drawLine(0,j, xEnd,yEnd);
j += 2;
}
int i = 2;
while (i=y && realY1=y && y2=0; i--) { // Last to first
GutterIconInfo ti = getTrackingIcon(i);
int offs = ti.getMarkedOffset();
if (offs>=0 && offs<=doc.getLength()) {
int line = root.getElementIndex(offs);
if (line<=lastLine && line>=topLine) {
try {
Icon icon = ti.getIcon();
if (icon!=null) {
int lineY = rsta.yForLine(line);
if (lineY>=y && lineY<=visibleRect.y+visibleRect.height) {
int y2 = lineY + (cellHeight-icon.getIconHeight())/2;
icon.paintIcon(this, g, 0, y2);
lastLine = line-1; // Paint only 1 icon per line
}
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
}
else if (line=0; i--) { // Last to first
GutterIconInfo ti = getTrackingIcon(i);
Icon icon = ti.getIcon();
if (icon!=null) {
int iconH = icon.getIconHeight();
int offs = ti.getMarkedOffset();
if (offs>=0 && offs<=doc.getLength()) {
int line = root.getElementIndex(offs);
if (line<=lastLine && line>=topLine) {
try {
int lineY = rsta.yForLine(line);
if (lineY<=bottomY && (lineY+iconH>=topY)) {
int y2 = lineY + (cellHeight-iconH)/2;
ti.getIcon().paintIcon(this, g, 0, y2);
lastLine = line-1; // Paint only 1 icon per line
}
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
}
else if (line