KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ScreenLineManager.java - Manage screen line counts
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2004 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 org.gjt.sp.jedit.buffer.*;
27 import org.gjt.sp.jedit.Debug;
28 import org.gjt.sp.util.Log;
29 //}}}
30

31 /**
32  * Performs the Mapping between physical lines and screen lines.
33  *
34  * @since jEdit 4.3pre1
35  * @author Slava Pestov
36  * @version $Id: ScreenLineManager.java 5459 2006-06-20 21:53:46Z kpouer $
37  */

38 class ScreenLineManager
39 {
40     //{{{ ScreenLineManager constructor
41
ScreenLineManager(DisplayManager displayManager, JEditBuffer buffer)
42     {
43         this.displayManager = displayManager;
44         this.buffer = buffer;
45         if(!buffer.isLoading())
46             reset();
47     } //}}}
48

49     //{{{ isScreenLineCountValid() method
50
boolean isScreenLineCountValid(int line)
51     {
52         return (screenLines[line] & SCREEN_LINES_VALID_MASK) != 0;
53     } //}}}
54

55     //{{{ getScreenLineCount() method
56
int getScreenLineCount(int line)
57     {
58         return screenLines[line] >> SCREEN_LINES_SHIFT;
59     } //}}}
60

61     //{{{ setScreenLineCount() method
62
void setScreenLineCount(int line, int count)
63     {
64         if(count > Short.MAX_VALUE)
65         {
66             // limitations...
67
count = Short.MAX_VALUE;
68         }
69
70         if(Debug.SCREEN_LINES_DEBUG)
71             Log.log(Log.DEBUG,this,new Exception JavaDoc("setScreenLineCount(" + line + "," + count + ")"));
72         screenLines[line] = (short)(count << SCREEN_LINES_SHIFT
73             | SCREEN_LINES_VALID_MASK);
74     } //}}}
75

76     //{{{ invalidateScreenLineCounts() method
77
void invalidateScreenLineCounts()
78     {
79         int lineCount = buffer.getLineCount();
80         for(int i = 0; i < lineCount; i++)
81             screenLines[i] &= ~SCREEN_LINES_VALID_MASK;
82     } //}}}
83

84     //{{{ reset() method
85
void reset()
86     {
87         screenLines = new short[buffer.getLineCount()];
88         for(int i = 0; i < screenLines.length; i++)
89             screenLines[i] = 0;
90     } //}}}
91

92     //{{{ contentInserted() method
93
public void contentInserted(int startLine, int numLines)
94     {
95         int endLine = startLine + numLines;
96         screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
97
98         int lineCount = buffer.getLineCount();
99
100         if(numLines > 0)
101         {
102             if(screenLines.length <= lineCount)
103             {
104                 short[] screenLinesN = new short[(lineCount + 1) * 2];
105                 System.arraycopy(screenLines,0,screenLinesN,0,
106                          screenLines.length);
107                 screenLines = screenLinesN;
108             }
109
110             System.arraycopy(screenLines,startLine,screenLines,
111                 endLine,lineCount - endLine);
112
113             for(int i = 0; i < numLines; i++)
114                 screenLines[startLine + i] = 0;
115         }
116     } //}}}
117

118     //{{{ contentRemoved() method
119
public void contentRemoved(int startLine, int numLines)
120     {
121         int endLine = startLine + numLines;
122         screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
123
124         if(numLines > 0 && endLine != screenLines.length)
125         {
126             System.arraycopy(screenLines,endLine + 1,screenLines,
127                 startLine + 1,screenLines.length - endLine - 1);
128         }
129     } //}}}
130

131     //{{{ Private members
132
private static final int SCREEN_LINES_SHIFT = 1;
133     private static final int SCREEN_LINES_VALID_MASK = 1;
134
135     private DisplayManager displayManager;
136     private JEditBuffer buffer;
137     private short[] screenLines;
138     //}}}
139
}
140
Popular Tags