PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/textarea/FirstLine.java

#
Java | 364 lines | 263 code | 50 blank | 51 comment | 54 complexity | df1ad9b4decc804b8d824eeaeec8122d MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.textarea;
  23. import org.gjt.sp.jedit.Debug;
  24. import org.gjt.sp.util.Log;
  25. /**
  26. * This Anchor is the first visible line of the textarea.
  27. *
  28. * @author Slava Pestov
  29. * @version $Id: FirstLine.java 15570 2009-06-25 00:43:57Z ezust $
  30. */
  31. class FirstLine extends Anchor
  32. {
  33. /** The skew is the scroll count from the beginning of the line. Used with soft wrap. */
  34. int skew;
  35. //{{{ FirstLine constructor
  36. FirstLine(DisplayManager displayManager,
  37. TextArea textArea)
  38. {
  39. super(displayManager,textArea);
  40. } //}}}
  41. //{{{ changed() method
  42. @Override
  43. public void changed()
  44. {
  45. //{{{ Debug code
  46. if(Debug.SCROLL_DEBUG)
  47. {
  48. Log.log(Log.DEBUG,this,"changed() before: "
  49. + physicalLine + ':' + scrollLine
  50. + ':' + skew);
  51. } //}}}
  52. ensurePhysicalLineIsVisible();
  53. int screenLines = displayManager
  54. .getScreenLineCount(physicalLine);
  55. if(skew >= screenLines)
  56. skew = screenLines - 1;
  57. //{{{ Debug code
  58. if(Debug.SCROLL_VERIFY)
  59. {
  60. System.err.println("SCROLL_VERIFY");
  61. int verifyScrollLine = 0;
  62. for(int i = 0; i < displayManager.getBuffer()
  63. .getLineCount(); i++)
  64. {
  65. if(!displayManager.isLineVisible(i))
  66. continue;
  67. if(i >= physicalLine)
  68. break;
  69. verifyScrollLine += displayManager
  70. .getScreenLineCount(i);
  71. }
  72. if(verifyScrollLine != scrollLine)
  73. {
  74. Exception ex = new Exception(scrollLine + ":" + verifyScrollLine);
  75. Log.log(Log.ERROR,this,ex);
  76. }
  77. }
  78. if(Debug.SCROLL_DEBUG)
  79. {
  80. Log.log(Log.DEBUG,this,"changed() after: "
  81. + physicalLine + ':' + scrollLine
  82. + ':' + skew);
  83. } //}}}
  84. } //}}}
  85. //{{{ reset() method
  86. @Override
  87. public void reset()
  88. {
  89. if(Debug.SCROLL_DEBUG)
  90. Log.log(Log.DEBUG,this,"reset()");
  91. int oldPhysicalLine = physicalLine;
  92. physicalLine = 0;
  93. scrollLine = 0;
  94. int i = displayManager.getFirstVisibleLine();
  95. for(;;)
  96. {
  97. if(i >= oldPhysicalLine)
  98. break;
  99. scrollLine += displayManager.getScreenLineCount(i);
  100. int nextLine = displayManager.getNextVisibleLine(i);
  101. if(nextLine == -1)
  102. break;
  103. else
  104. i = nextLine;
  105. }
  106. physicalLine = i;
  107. int screenLines = displayManager.getScreenLineCount(physicalLine);
  108. if(skew >= screenLines)
  109. skew = screenLines - 1;
  110. textArea.updateScrollBar();
  111. } //}}}
  112. //{{{ physDown() method
  113. // scroll down by physical line amount
  114. void physDown(int amount, int screenAmount)
  115. {
  116. if(Debug.SCROLL_DEBUG)
  117. {
  118. Log.log(Log.DEBUG,this,"physDown() start: "
  119. + physicalLine + ':' + scrollLine);
  120. }
  121. skew = 0;
  122. if(!displayManager.isLineVisible(physicalLine))
  123. {
  124. int lastVisibleLine = displayManager.getLastVisibleLine();
  125. if(physicalLine > lastVisibleLine)
  126. physicalLine = lastVisibleLine;
  127. else
  128. {
  129. int nextPhysicalLine = displayManager.getNextVisibleLine(physicalLine);
  130. amount -= nextPhysicalLine - physicalLine;
  131. scrollLine += displayManager.getScreenLineCount(physicalLine);
  132. physicalLine = nextPhysicalLine;
  133. }
  134. }
  135. for(;;)
  136. {
  137. int nextPhysicalLine = displayManager.getNextVisibleLine(
  138. physicalLine);
  139. if(nextPhysicalLine == -1)
  140. break;
  141. else if(nextPhysicalLine > physicalLine + amount)
  142. break;
  143. else
  144. {
  145. scrollLine += displayManager.getScreenLineCount(physicalLine);
  146. amount -= nextPhysicalLine - physicalLine;
  147. physicalLine = nextPhysicalLine;
  148. }
  149. }
  150. if(Debug.SCROLL_DEBUG)
  151. {
  152. Log.log(Log.DEBUG,this,"physDown() end: "
  153. + physicalLine + ':' + scrollLine);
  154. }
  155. callChanged = true;
  156. // JEditTextArea.scrollTo() needs this to simplify
  157. // its code
  158. if(screenAmount < 0)
  159. scrollUp(-screenAmount);
  160. else if(screenAmount > 0)
  161. scrollDown(screenAmount);
  162. } //}}}
  163. //{{{ physUp() method
  164. // scroll up by physical line amount
  165. void physUp(int amount, int screenAmount)
  166. {
  167. if(Debug.SCROLL_DEBUG)
  168. {
  169. Log.log(Log.DEBUG,this,"physUp() start: "
  170. + physicalLine + ':' + scrollLine);
  171. }
  172. skew = 0;
  173. if(!displayManager.isLineVisible(physicalLine))
  174. {
  175. int firstVisibleLine = displayManager.getFirstVisibleLine();
  176. if(physicalLine < firstVisibleLine)
  177. physicalLine = firstVisibleLine;
  178. else
  179. {
  180. int prevPhysicalLine = displayManager.getPrevVisibleLine(physicalLine);
  181. amount -= physicalLine - prevPhysicalLine;
  182. }
  183. }
  184. for(;;)
  185. {
  186. int prevPhysicalLine = displayManager.getPrevVisibleLine(
  187. physicalLine);
  188. if(prevPhysicalLine == -1)
  189. break;
  190. else if(prevPhysicalLine < physicalLine - amount)
  191. break;
  192. else
  193. {
  194. amount -= physicalLine - prevPhysicalLine;
  195. physicalLine = prevPhysicalLine;
  196. scrollLine -= displayManager.getScreenLineCount(
  197. prevPhysicalLine);
  198. }
  199. }
  200. if(Debug.SCROLL_DEBUG)
  201. {
  202. Log.log(Log.DEBUG,this,"physUp() end: "
  203. + physicalLine + ':' + scrollLine);
  204. }
  205. callChanged = true;
  206. // JEditTextArea.scrollTo() needs this to simplify
  207. // its code
  208. if(screenAmount < 0)
  209. scrollUp(-screenAmount);
  210. else if(screenAmount > 0)
  211. scrollDown(screenAmount);
  212. } //}}}
  213. //{{{ scrollDown() method
  214. // scroll down by screen line amount
  215. void scrollDown(int amount)
  216. {
  217. if(Debug.SCROLL_DEBUG)
  218. Log.log(Log.DEBUG,this,"scrollDown()");
  219. ensurePhysicalLineIsVisible();
  220. amount += skew;
  221. skew = 0;
  222. while(amount > 0)
  223. {
  224. int screenLines = displayManager.getScreenLineCount(physicalLine);
  225. if(amount < screenLines)
  226. {
  227. skew = amount;
  228. break;
  229. }
  230. else
  231. {
  232. int nextLine = displayManager.getNextVisibleLine(physicalLine);
  233. if(nextLine == -1)
  234. break;
  235. boolean visible = displayManager.isLineVisible(physicalLine);
  236. physicalLine = nextLine;
  237. if(visible)
  238. {
  239. amount -= screenLines;
  240. scrollLine += screenLines;
  241. }
  242. }
  243. }
  244. callChanged = true;
  245. } //}}}
  246. //{{{ scrollUp() method
  247. // scroll up by screen line amount
  248. void scrollUp(int amount)
  249. {
  250. if(Debug.SCROLL_DEBUG)
  251. Log.log(Log.DEBUG,this,"scrollUp() before:" + this);
  252. ensurePhysicalLineIsVisible();
  253. if(amount <= skew)
  254. {
  255. // the amount is less than the skew, so we stay in the same like, just going
  256. // upper
  257. skew -= amount;
  258. }
  259. else
  260. {
  261. // moving to the first screen line of the current physical line
  262. amount -= skew;
  263. skew = 0;
  264. while(amount > 0)
  265. {
  266. int prevLine = displayManager.getPrevVisibleLine(physicalLine);
  267. if(prevLine == -1)
  268. break;
  269. // moving to the previous visible physical line
  270. physicalLine = prevLine;
  271. int screenLines = displayManager.getScreenLineCount(physicalLine);
  272. scrollLine -= screenLines;
  273. if(amount < screenLines)
  274. {
  275. skew = screenLines - amount;
  276. break;
  277. }
  278. else
  279. amount -= screenLines;
  280. }
  281. }
  282. if(Debug.SCROLL_DEBUG)
  283. Log.log(Log.DEBUG,this,"scrollUp() after:" + this);
  284. callChanged = true;
  285. } //}}}
  286. //{{{ ensurePhysicalLineIsVisible() method
  287. void ensurePhysicalLineIsVisible()
  288. {
  289. if(!displayManager.isLineVisible(physicalLine))
  290. {
  291. if(physicalLine > displayManager.getLastVisibleLine())
  292. {
  293. physicalLine = displayManager.getLastVisibleLine();
  294. scrollLine = displayManager.getScrollLineCount() - 1;
  295. }
  296. else if(physicalLine < displayManager.getFirstVisibleLine())
  297. {
  298. physicalLine = displayManager.getFirstVisibleLine();
  299. scrollLine = 0;
  300. }
  301. else
  302. {
  303. physicalLine = displayManager.getNextVisibleLine(physicalLine);
  304. scrollLine += displayManager.getScreenLineCount(physicalLine);
  305. }
  306. }
  307. } //}}}
  308. //{{{ toString() method
  309. @Override
  310. public String toString()
  311. {
  312. return "FirstLine["+physicalLine+','+scrollLine+','+skew+']';
  313. } //}}}
  314. }