KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FirstLine.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 import org.gjt.sp.jedit.Debug;
26 import org.gjt.sp.util.Log;
27
28 /**
29  * @author Slava Pestov
30  * @version $Id: FirstLine.java 7156 2006-10-02 21:33:17Z kpouer $
31  */

32 class FirstLine extends Anchor
33 {
34     int skew;
35
36     //{{{ FirstLine constructor
37
FirstLine(DisplayManager displayManager,
38         TextArea textArea)
39     {
40         super(displayManager,textArea);
41     } //}}}
42

43     //{{{ changed() method
44
public void changed()
45     {
46         //{{{ Debug code
47
if(Debug.SCROLL_DEBUG)
48         {
49             Log.log(Log.DEBUG,this,"changed() before: "
50                 + physicalLine + ":" + scrollLine
51                 + ":" + skew);
52         } //}}}
53

54         ensurePhysicalLineIsVisible();
55
56         int screenLines = displayManager
57             .getScreenLineCount(physicalLine);
58         if(skew >= screenLines)
59             skew = screenLines - 1;
60
61         //{{{ Debug code
62
if(Debug.SCROLL_VERIFY)
63         {
64             System.err.println("SCROLL_VERIFY");
65             int verifyScrollLine = 0;
66
67             for(int i = 0; i < displayManager.getBuffer()
68                 .getLineCount(); i++)
69             {
70                 if(!displayManager.isLineVisible(i))
71                     continue;
72
73                 if(i >= physicalLine)
74                     break;
75
76                 verifyScrollLine += displayManager
77                     .getScreenLineCount(i);
78             }
79
80             if(verifyScrollLine != scrollLine)
81             {
82                 Exception JavaDoc ex = new Exception JavaDoc(scrollLine + ":" + verifyScrollLine);
83                 Log.log(Log.ERROR,this,ex);
84             }
85         }
86
87         if(Debug.SCROLL_DEBUG)
88         {
89             Log.log(Log.DEBUG,this,"changed() after: "
90                 + physicalLine + ":" + scrollLine
91                 + ":" + skew);
92         } //}}}
93
} //}}}
94

95     //{{{ reset() method
96
public void reset()
97     {
98         if(Debug.SCROLL_DEBUG)
99             Log.log(Log.DEBUG,this,"reset()");
100
101         int oldPhysicalLine = physicalLine;
102         physicalLine = 0;
103         scrollLine = 0;
104
105         int i = displayManager.getFirstVisibleLine();
106
107         for(;;)
108         {
109             if(i >= oldPhysicalLine)
110                 break;
111
112             int before = scrollLine;
113             displayManager.updateScreenLineCount(i);
114             if(before != scrollLine)
115                 throw new RuntimeException JavaDoc(this + " nudged");
116             scrollLine += displayManager.getScreenLineCount(i);
117
118             int nextLine = displayManager.getNextVisibleLine(i);
119             if(nextLine == -1)
120                 break;
121             else
122                 i = nextLine;
123         }
124
125         physicalLine = i;
126
127         displayManager.updateScreenLineCount(i);
128         int screenLines = displayManager.getScreenLineCount(physicalLine);
129         if(skew >= screenLines)
130             skew = screenLines - 1;
131
132         textArea.updateScrollBar();
133     } //}}}
134

135     //{{{ physDown() method
136
// scroll down by physical line amount
137
void physDown(int amount, int screenAmount)
138     {
139         if(Debug.SCROLL_DEBUG)
140         {
141             Log.log(Log.DEBUG,this,"physDown() start: "
142                 + physicalLine + ":" + scrollLine);
143         }
144
145         skew = 0;
146
147         if(!displayManager.isLineVisible(physicalLine))
148         {
149             int lastVisibleLine = displayManager.getLastVisibleLine();
150             if(physicalLine > lastVisibleLine)
151                 physicalLine = lastVisibleLine;
152             else
153             {
154                 int nextPhysicalLine = displayManager.getNextVisibleLine(physicalLine);
155                 amount -= (nextPhysicalLine - physicalLine);
156                 scrollLine += displayManager.getScreenLineCount(physicalLine);
157                 physicalLine = nextPhysicalLine;
158             }
159         }
160
161         for(;;)
162         {
163             int nextPhysicalLine = displayManager.getNextVisibleLine(
164                 physicalLine);
165             if(nextPhysicalLine == -1)
166                 break;
167             else if(nextPhysicalLine > physicalLine + amount)
168                 break;
169             else
170             {
171                 scrollLine += displayManager.getScreenLineCount(physicalLine);
172                 amount -= (nextPhysicalLine - physicalLine);
173                 physicalLine = nextPhysicalLine;
174             }
175         }
176
177         if(Debug.SCROLL_DEBUG)
178         {
179             Log.log(Log.DEBUG,this,"physDown() end: "
180                 + physicalLine + ":" + scrollLine);
181         }
182
183         callChanged = true;
184
185         // JEditTextArea.scrollTo() needs this to simplify
186
// its code
187
if(screenAmount < 0)
188             scrollUp(-screenAmount);
189         else if(screenAmount > 0)
190             scrollDown(screenAmount);
191     } //}}}
192

193     //{{{ physUp() method
194
// scroll up by physical line amount
195
void physUp(int amount, int screenAmount)
196     {
197         if(Debug.SCROLL_DEBUG)
198         {
199             Log.log(Log.DEBUG,this,"physUp() start: "
200                 + physicalLine + ":" + scrollLine);
201         }
202
203         skew = 0;
204
205         if(!displayManager.isLineVisible(physicalLine))
206         {
207             int firstVisibleLine = displayManager.getFirstVisibleLine();
208             if(physicalLine < firstVisibleLine)
209                 physicalLine = firstVisibleLine;
210             else
211             {
212                 int prevPhysicalLine = displayManager.getPrevVisibleLine(physicalLine);
213                 amount -= (physicalLine - prevPhysicalLine);
214             }
215         }
216
217         for(;;)
218         {
219             int prevPhysicalLine = displayManager.getPrevVisibleLine(
220                 physicalLine);
221             if(prevPhysicalLine == -1)
222                 break;
223             else if(prevPhysicalLine < physicalLine - amount)
224                 break;
225             else
226             {
227                 amount -= (physicalLine - prevPhysicalLine);
228                 physicalLine = prevPhysicalLine;
229                 scrollLine -= displayManager.getScreenLineCount(
230                     prevPhysicalLine);
231             }
232         }
233
234         if(Debug.SCROLL_DEBUG)
235         {
236             Log.log(Log.DEBUG,this,"physUp() end: "
237                 + physicalLine + ":" + scrollLine);
238         }
239
240         callChanged = true;
241
242         // JEditTextArea.scrollTo() needs this to simplify
243
// its code
244
if(screenAmount < 0)
245             scrollUp(-screenAmount);
246         else if(screenAmount > 0)
247             scrollDown(screenAmount);
248     } //}}}
249

250     //{{{ scrollDown() method
251
// scroll down by screen line amount
252
void scrollDown(int amount)
253     {
254         if(Debug.SCROLL_DEBUG)
255             Log.log(Log.DEBUG,this,"scrollDown()");
256
257         ensurePhysicalLineIsVisible();
258
259         amount += skew;
260
261         skew = 0;
262
263         while(amount > 0)
264         {
265             int screenLines = displayManager.getScreenLineCount(physicalLine);
266             if(amount < screenLines)
267             {
268                 skew = amount;
269                 break;
270             }
271             else
272             {
273                 int nextLine = displayManager.getNextVisibleLine(physicalLine);
274                 if(nextLine == -1)
275                     break;
276                 boolean visible = displayManager.isLineVisible(physicalLine);
277                 physicalLine = nextLine;
278                 if(visible)
279                 {
280                     amount -= screenLines;
281                     scrollLine += screenLines;
282                 }
283             }
284         }
285
286         callChanged = true;
287     } //}}}
288

289     //{{{ scrollUp() method
290
// scroll up by screen line amount
291
void scrollUp(int amount)
292     {
293         if(Debug.SCROLL_DEBUG)
294             Log.log(Log.DEBUG,this,"scrollUp()");
295
296         ensurePhysicalLineIsVisible();
297
298         if(amount <= skew)
299         {
300             skew -= amount;
301         }
302         else
303         {
304             amount -= skew;
305             skew = 0;
306
307             while(amount > 0)
308             {
309                 int prevLine = displayManager.getPrevVisibleLine(physicalLine);
310                 if(prevLine == -1)
311                     break;
312                 physicalLine = prevLine;
313
314                 int screenLines = displayManager.getScreenLineCount(physicalLine);
315                 scrollLine -= screenLines;
316                 if(amount < screenLines)
317                 {
318                     skew = screenLines - amount;
319                     break;
320                 }
321                 else
322                     amount -= screenLines;
323             }
324         }
325
326         callChanged = true;
327     } //}}}
328

329     //{{{ ensurePhysicalLineIsVisible() method
330
void ensurePhysicalLineIsVisible()
331     {
332         if(!displayManager.isLineVisible(physicalLine))
333         {
334             if(physicalLine > displayManager.getLastVisibleLine())
335             {
336                 physicalLine = displayManager.getLastVisibleLine();
337                 scrollLine = displayManager.getScrollLineCount() - 1;
338             }
339             else if(physicalLine < displayManager.getFirstVisibleLine())
340             {
341                 physicalLine = displayManager.getFirstVisibleLine();
342                 scrollLine = 0;
343             }
344             else
345             {
346                 physicalLine = displayManager.getNextVisibleLine(physicalLine);
347                 scrollLine += displayManager.getScreenLineCount(physicalLine);
348             }
349         }
350     } //}}}
351
}
352
Popular Tags