001: /*
002: * ScreenLineManager.java - Manage screen line counts
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.textarea;
024:
025: //{{{ Imports
026: import org.gjt.sp.jedit.buffer.*;
027: import org.gjt.sp.jedit.Debug;
028: import org.gjt.sp.util.Log;
029:
030: //}}}
031:
032: /**
033: * Performs the Mapping between physical lines and screen lines.
034: *
035: * @since jEdit 4.3pre1
036: * @author Slava Pestov
037: * @version $Id: ScreenLineManager.java 10273 2007-08-01 21:20:45Z kpouer $
038: */
039: class ScreenLineManager {
040: //{{{ ScreenLineManager constructor
041: ScreenLineManager(JEditBuffer buffer) {
042: this .buffer = buffer;
043: if (!buffer.isLoading())
044: reset();
045: } //}}}
046:
047: //{{{ isScreenLineCountValid() method
048: boolean isScreenLineCountValid(int line) {
049: return (screenLines[line] & SCREEN_LINES_VALID_MASK) != 0;
050: } //}}}
051:
052: //{{{ getScreenLineCount() method
053: /**
054: * Returns how many screen lines contains the given physical line.
055: * It can be greater than 1 when using soft wrap
056: *
057: * @param line the physical line
058: * @return the screen line count
059: */
060: int getScreenLineCount(int line) {
061: return screenLines[line] >> SCREEN_LINES_SHIFT;
062: } //}}}
063:
064: //{{{ setScreenLineCount() method
065: /**
066: * Sets the number of screen lines that the specified physical line
067: * is split into.
068: * @param line the line number
069: * @param count the line count (1 if no wrap)
070: */
071: void setScreenLineCount(int line, int count) {
072: if (count > Short.MAX_VALUE) {
073: // limitations...
074: count = Short.MAX_VALUE;
075: }
076:
077: if (Debug.SCREEN_LINES_DEBUG)
078: Log.log(Log.DEBUG, this , new Exception(
079: "setScreenLineCount(" + line + ',' + count + ')'));
080: screenLines[line] = (short) (count << SCREEN_LINES_SHIFT | SCREEN_LINES_VALID_MASK);
081: } //}}}
082:
083: //{{{ invalidateScreenLineCounts() method
084: void invalidateScreenLineCounts() {
085: int lineCount = buffer.getLineCount();
086: for (int i = 0; i < lineCount; i++)
087: screenLines[i] &= ~SCREEN_LINES_VALID_MASK;
088: } //}}}
089:
090: //{{{ reset() method
091: void reset() {
092: screenLines = new short[buffer.getLineCount()];
093: } //}}}
094:
095: //{{{ contentInserted() method
096: public void contentInserted(int startLine, int numLines) {
097: int endLine = startLine + numLines;
098: screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
099:
100: int lineCount = buffer.getLineCount();
101:
102: if (numLines > 0) {
103: if (screenLines.length <= lineCount) {
104: short[] screenLinesN = new short[((lineCount + 1) << 1)];
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: int endLine = startLine + numLines;
121: screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
122:
123: if (numLines > 0 && endLine != screenLines.length) {
124: System.arraycopy(screenLines, endLine + 1, screenLines,
125: startLine + 1, screenLines.length - endLine - 1);
126: }
127: } //}}}
128:
129: //{{{ Private members
130: private static final int SCREEN_LINES_SHIFT = 1;
131: private static final int SCREEN_LINES_VALID_MASK = 1;
132:
133: private final JEditBuffer buffer;
134: /** This array contains the line count for each physical line. */
135: private short[] screenLines;
136: //}}}
137: }
|