001: /*
002: * FastRepaintManager.java
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.textarea;
024:
025: //{{{ Imports
026: import java.awt.*;
027:
028: //}}}
029:
030: /**
031: * Manages blitting the offscreen graphics context to speed up scrolling.
032: * The text area does not use Swing's built-in double buffering, so that
033: * we have access to the graphics context for fast scrolling.
034: * @author Slava Pestov
035: * @version $Id: FastRepaintManager.java 7156 2006-10-02 21:33:17Z kpouer $
036: */
037: class FastRepaintManager {
038: //{{{ FastRepaintManager constructor
039: FastRepaintManager(TextArea textArea, TextAreaPainter painter) {
040: this .textArea = textArea;
041: this .painter = painter;
042: } //}}}
043:
044: //{{{ updateGraphics() method
045: void updateGraphics() {
046: if (gfx != null)
047: gfx.dispose();
048:
049: int width = painter.getWidth();
050: int height = painter.getHeight();
051: /* A little hack */
052: if (width <= 0)
053: width = 1;
054: if (height <= 0)
055: height = 1;
056: img = painter.getGraphicsConfiguration().createCompatibleImage(
057: width, height, Transparency.OPAQUE);
058: gfx = (Graphics2D) img.getGraphics();
059: gfx.clipRect(0, 0, painter.getWidth(), painter.getHeight());
060: fastScroll = false;
061: } //}}}
062:
063: //{{{ getGraphics() method
064: Graphics2D getGraphics() {
065: return gfx;
066: } //}}}
067:
068: //{{{ prepareGraphics() method
069: static class RepaintLines {
070: final int first;
071: final int last;
072:
073: RepaintLines(int first, int last) {
074: this .first = first;
075: this .last = last;
076: }
077: } //}}}
078:
079: //{{{ prepareGraphics() method
080: RepaintLines prepareGraphics(Rectangle clipRect, int firstLine,
081: Graphics2D gfx) {
082: gfx.setFont(painter.getFont());
083: gfx.setColor(painter.getBackground());
084:
085: int height = gfx.getFontMetrics().getHeight();
086:
087: if (fastScroll) {
088: int lineDelta = this .firstLine - firstLine;
089: int yDelta = lineDelta * height;
090: int visibleLines = textArea.getVisibleLines();
091:
092: if (lineDelta > -visibleLines && lineDelta < visibleLines) {
093: if (lineDelta < 0) {
094: gfx.copyArea(0, -yDelta, painter.getWidth(),
095: painter.getHeight() + yDelta, 0, yDelta);
096: return new RepaintLines(visibleLines
097: + this .firstLine - firstLine - 1,
098: visibleLines - 1);
099: } else if (lineDelta > 0) {
100: gfx.copyArea(0, 0, painter.getWidth(), painter
101: .getHeight()
102: - yDelta, 0, yDelta);
103: return new RepaintLines(0, this .firstLine
104: - firstLine);
105: }
106: }
107: }
108:
109: // Because the clipRect's height is usually an even multiple
110: // of the font height, we subtract 1 from it, otherwise one
111: // too many lines will always be painted.
112: return new RepaintLines(clipRect.y / height, (clipRect.y
113: + clipRect.height - 1)
114: / height);
115: } //}}}
116:
117: //{{{ paint() method
118: void paint(Graphics g) {
119: firstLine = textArea.getFirstLine();
120: g.drawImage(img, 0, 0, null);
121: } //}}}
122:
123: //{{{ setFastScroll() method
124: void setFastScroll(boolean fastScroll) {
125: this .fastScroll = fastScroll;
126: } //}}}
127:
128: //{{{ Private members
129: private TextArea textArea;
130: private TextAreaPainter painter;
131: private Graphics2D gfx;
132: private Image img;
133: private boolean fastScroll;
134: /* Most recently rendered first line */
135: private int firstLine;
136: //}}}
137: }
|