KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > textarea > FastRepaintManager


1 /*
2  * FastRepaintManager.java
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.textarea;
24
25 //{{{ Imports
26
import java.awt.*;
27 //}}}
28

29 /**
30  * Manages blitting the offscreen graphics context to speed up scrolling.
31  * The text area does not use Swing's built-in double buffering, so that
32  * we have access to the graphics context for fast scrolling.
33  * @author Slava Pestov
34  * @version $Id: FastRepaintManager.java 7156 2006-10-02 21:33:17Z kpouer $
35  */

36 class FastRepaintManager
37 {
38     //{{{ FastRepaintManager constructor
39
FastRepaintManager(TextArea textArea,
40         TextAreaPainter painter)
41     {
42         this.textArea = textArea;
43         this.painter = painter;
44     } //}}}
45

46     //{{{ updateGraphics() method
47
void updateGraphics()
48     {
49         if(gfx != null)
50             gfx.dispose();
51
52         int width = painter.getWidth();
53         int height = painter.getHeight();
54         /* A little hack */
55         if(width <= 0)
56             width = 1;
57         if(height <= 0)
58             height = 1;
59         img = painter.getGraphicsConfiguration()
60             .createCompatibleImage(width,height,
61             Transparency.OPAQUE);
62         gfx = (Graphics2D)img.getGraphics();
63         gfx.clipRect(0,0,painter.getWidth(),painter.getHeight());
64         fastScroll = false;
65     } //}}}
66

67     //{{{ getGraphics() method
68
Graphics2D getGraphics()
69     {
70         return gfx;
71     } //}}}
72

73     //{{{ prepareGraphics() method
74
static class RepaintLines
75     {
76         final int first;
77         final int last;
78
79         RepaintLines(int first, int last)
80         {
81             this.first = first;
82             this.last = last;
83         }
84     } //}}}
85

86     //{{{ prepareGraphics() method
87
RepaintLines prepareGraphics(Rectangle clipRect, int firstLine,
88         Graphics2D gfx)
89     {
90         gfx.setFont(painter.getFont());
91         gfx.setColor(painter.getBackground());
92
93         int height = gfx.getFontMetrics().getHeight();
94
95         if(fastScroll)
96         {
97             int lineDelta = this.firstLine - firstLine;
98             int yDelta = lineDelta * height;
99             int visibleLines = textArea.getVisibleLines();
100
101             if(lineDelta > -visibleLines
102                 && lineDelta < visibleLines)
103             {
104                 if(lineDelta < 0)
105                 {
106                     gfx.copyArea(0,-yDelta,painter.getWidth(),
107                         painter.getHeight() + yDelta,0,yDelta);
108                     return new RepaintLines(
109                         visibleLines + this.firstLine
110                         - firstLine - 1,
111                         visibleLines - 1);
112                 }
113                 else if(lineDelta > 0)
114                 {
115                     gfx.copyArea(0,0,painter.getWidth(),
116                         painter.getHeight() - yDelta,0,yDelta);
117                     return new RepaintLines(0,
118                         this.firstLine - firstLine);
119                 }
120             }
121         }
122
123         // Because the clipRect's height is usually an even multiple
124
// of the font height, we subtract 1 from it, otherwise one
125
// too many lines will always be painted.
126
return new RepaintLines(
127             clipRect.y / height,
128             (clipRect.y + clipRect.height - 1) / height);
129     } //}}}
130

131     //{{{ paint() method
132
void paint(Graphics g)
133     {
134         firstLine = textArea.getFirstLine();
135         g.drawImage(img,0,0,null);
136     } //}}}
137

138     //{{{ setFastScroll() method
139
void setFastScroll(boolean fastScroll)
140     {
141         this.fastScroll = fastScroll;
142     } //}}}
143

144     //{{{ Private members
145
private TextArea textArea;
146     private TextAreaPainter painter;
147     private Graphics2D gfx;
148     private Image img;
149     private boolean fastScroll;
150     /* Most recently rendered first line */
151     private int firstLine;
152     //}}}
153
}
154
Popular Tags