PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 149 lines | 76 code | 18 blank | 55 comment | 11 complexity | 3353d4448630f7451a38341b4f86dd3e 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. * 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. package org.gjt.sp.jedit.textarea;
  23. //{{{ Imports
  24. import org.gjt.sp.jedit.buffer.*;
  25. import org.gjt.sp.jedit.Debug;
  26. import org.gjt.sp.util.Log;
  27. //}}}
  28. /**
  29. * Performs the Mapping between physical lines and screen lines.
  30. *
  31. * @since jEdit 4.3pre1
  32. * @author Slava Pestov
  33. * @version $Id: ScreenLineManager.java 15570 2009-06-25 00:43:57Z ezust $
  34. */
  35. class ScreenLineManager
  36. {
  37. //{{{ ScreenLineManager constructor
  38. ScreenLineManager(JEditBuffer buffer)
  39. {
  40. this.buffer = buffer;
  41. if(!buffer.isLoading())
  42. reset();
  43. } //}}}
  44. //{{{ isScreenLineCountValid() method
  45. boolean isScreenLineCountValid(int line)
  46. {
  47. return (screenLines[line] & SCREEN_LINES_VALID_MASK) != 0;
  48. } //}}}
  49. //{{{ getScreenLineCount() method
  50. /**
  51. * Returns how many screen lines contains the given physical line.
  52. * It can be greater than 1 when using soft wrap
  53. *
  54. * @param line the physical line
  55. * @return the screen line count
  56. */
  57. int getScreenLineCount(int line)
  58. {
  59. return screenLines[line] >> SCREEN_LINES_SHIFT;
  60. } //}}}
  61. //{{{ setScreenLineCount() method
  62. /**
  63. * Sets the number of screen lines that the specified physical line
  64. * is split into.
  65. * @param line the line number
  66. * @param count the line count (1 if no wrap)
  67. */
  68. void setScreenLineCount(int line, int count)
  69. {
  70. if(count > Short.MAX_VALUE)
  71. {
  72. // limitations...
  73. count = Short.MAX_VALUE;
  74. }
  75. if(Debug.SCREEN_LINES_DEBUG)
  76. Log.log(Log.DEBUG,this,new Exception("setScreenLineCount(" + line + ',' + count + ')'));
  77. screenLines[line] = (short)(count << SCREEN_LINES_SHIFT
  78. | SCREEN_LINES_VALID_MASK);
  79. } //}}}
  80. //{{{ invalidateScreenLineCounts() method
  81. void invalidateScreenLineCounts()
  82. {
  83. int lineCount = buffer.getLineCount();
  84. for(int i = 0; i < lineCount; i++)
  85. screenLines[i] &= ~SCREEN_LINES_VALID_MASK;
  86. } //}}}
  87. //{{{ reset() method
  88. void reset()
  89. {
  90. screenLines = new short[buffer.getLineCount()];
  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. int lineCount = buffer.getLineCount();
  98. if(numLines > 0)
  99. {
  100. if(screenLines.length <= lineCount)
  101. {
  102. short[] screenLinesN = new short[((lineCount + 1) << 1)];
  103. System.arraycopy(screenLines,0,screenLinesN,0,
  104. screenLines.length);
  105. screenLines = screenLinesN;
  106. }
  107. System.arraycopy(screenLines,startLine,screenLines,
  108. endLine,lineCount - endLine);
  109. for(int i = 0; i < numLines; i++)
  110. screenLines[startLine + i] = 0;
  111. }
  112. } //}}}
  113. //{{{ contentRemoved() method
  114. public void contentRemoved(int startLine, int numLines)
  115. {
  116. int endLine = startLine + numLines;
  117. screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
  118. if(numLines > 0 && endLine != screenLines.length)
  119. {
  120. System.arraycopy(screenLines,endLine + 1,screenLines,
  121. startLine + 1,screenLines.length - endLine - 1);
  122. }
  123. } //}}}
  124. //{{{ Private members
  125. private static final int SCREEN_LINES_SHIFT = 1;
  126. private static final int SCREEN_LINES_VALID_MASK = 1;
  127. private final JEditBuffer buffer;
  128. /** This array contains the line count for each physical line. */
  129. private short[] screenLines;
  130. //}}}
  131. }