All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.rtextarea.LineHighlightManager Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 3.5.1
Show newest version
/*
 * 02/10/2009
 *
 * LineHighlightManager - Manages line highlights.
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSyntaxTextArea.License.txt file for details.
 */
package org.fife.ui.rtextarea;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.text.BadLocationException;
import javax.swing.text.Position;


/**
 * Manages line highlights in an RTextArea.
 *
 * @author Robert Futrell
 * @version 1.0
 */
class LineHighlightManager {

	private RTextArea textArea;
	private List lineHighlights;
	private LineHighlightInfoComparator comparator;

	/**
	 * Constructor.
	 *
	 * @param textArea The parent text area.
	 */
	LineHighlightManager(RTextArea textArea) {
		this.textArea = textArea;
		comparator = new LineHighlightInfoComparator();
	}


	/**
	 * Highlights the specified line.
	 *
	 * @param line The line to highlight.
	 * @param color The color to highlight with.
	 * @return A tag for the highlight.
	 * @throws BadLocationException If line is not a valid line
	 *         number.
	 * @see #removeLineHighlight(Object)
	 */
	public Object addLineHighlight(int line, Color color)
									throws BadLocationException {
		int offs = textArea.getLineStartOffset(line);
		LineHighlightInfo lhi = new LineHighlightInfo(
						textArea.getDocument().createPosition(offs), color);
		if (lineHighlights==null) {
			lineHighlights = new ArrayList(1);
		}
		int index = Collections.binarySearch(lineHighlights, lhi, comparator);
		if (index<0) { // Common case
			index = -(index+1);
		}
		lineHighlights.add(index, lhi);
		repaintLine(lhi);
		return lhi;
	}


	/**
	 * Returns the current line highlights' tags.
	 *
	 * @return The current line highlights' tags, or an empty list if there
	 *         are none.
	 */
	protected List getCurrentLineHighlightTags() {
		return lineHighlights == null ? Collections.emptyList() :
			new ArrayList(lineHighlights);
	}


	/**
	 * Returns the current number of line highlights.  Useful for testing.
	 *
	 * @return The current number of line highlights.
	 */
	protected int getLineHighlightCount() {
		return lineHighlights == null ? 0 : lineHighlights.size();
	}


	/**
	 * Paints any highlighted lines in the specified line range.
	 *
	 * @param g The graphics context.
	 */
	public void paintLineHighlights(Graphics g) {

		int count = lineHighlights==null ? 0 : lineHighlights.size();
		if (count>0) {

			int docLen = textArea.getDocument().getLength();
			Rectangle vr = textArea.getVisibleRect();
			int lineHeight = textArea.getLineHeight();

			try {

				for (int i=0; i=0 && offs<=docLen) {
						int y = textArea.yForLineContaining(offs);
						if (y>vr.y-lineHeight) {
							if (y length if they deleted text including the highlight
		if (offs>=0 && offs<=textArea.getDocument().getLength()) {
			try {
				int y = textArea.yForLineContaining(offs);
				if (y>-1) {
					textArea.repaint(0, y,
								textArea.getWidth(), textArea.getLineHeight());
				}
			} catch (BadLocationException ble) {
				ble.printStackTrace(); // Never happens
			}
		}
	}


	/**
	 * Information about a line highlight.
	 */
	private static class LineHighlightInfo {

		private Position offs;
		private Color color;

		LineHighlightInfo(Position offs, Color c) {
			this.offs = offs;
			this.color = c;
		}

		public Color getColor() {
			return color;
		}

		public int getOffset() {
			return offs.getOffset();
		}

		@Override
		public int hashCode() {
			return getOffset();
		}

	}


	/**
	 * Comparator used when adding new highlights.  This is done here instead
	 * of making LineHighlightInfo implement
	 * Comparable as correctly implementing the latter prevents
	 * two LHI's pointing to the same line from correctly being distinguished
	 * from one another.  See:
	 * https://github.com/bobbylight/RSyntaxTextArea/issues/161
	 */
	private static class LineHighlightInfoComparator
			implements Comparator {

		@Override
		public int compare(LineHighlightInfo lhi1, LineHighlightInfo lhi2) {
			if (lhi1.getOffset() < lhi2.getOffset()) {
				return -1;
			}
			return lhi1.getOffset() == lhi2.getOffset() ? 0 : 1;
		}

	}


}