PageRenderTime 115ms CodeModel.GetById 105ms app.highlight 8ms 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
  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
 23package org.gjt.sp.jedit.textarea;
 24
 25//{{{ Imports
 26import org.gjt.sp.jedit.buffer.*;
 27import org.gjt.sp.jedit.Debug;
 28import 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 15570 2009-06-25 00:43:57Z ezust $
 37 */
 38class ScreenLineManager
 39{
 40	//{{{ ScreenLineManager constructor
 41	ScreenLineManager(JEditBuffer buffer)
 42	{
 43		this.buffer = buffer;
 44		if(!buffer.isLoading())
 45			reset();
 46	} //}}}
 47	
 48	//{{{ isScreenLineCountValid() method
 49	boolean isScreenLineCountValid(int line)
 50	{
 51		return (screenLines[line] & SCREEN_LINES_VALID_MASK) != 0;
 52	} //}}}
 53
 54	//{{{ getScreenLineCount() method
 55	/**
 56	 * Returns how many screen lines contains the given physical line.
 57	 * It can be greater than 1 when using soft wrap
 58	 *
 59	 * @param line the physical line
 60	 * @return the screen line count
 61	 */
 62	int getScreenLineCount(int line)
 63	{
 64		return screenLines[line] >> SCREEN_LINES_SHIFT;
 65	} //}}}
 66
 67	//{{{ setScreenLineCount() method
 68	/**
 69	 * Sets the number of screen lines that the specified physical line
 70	 * is split into.
 71	 * @param line the line number
 72	 * @param count the line count (1 if no wrap)
 73	 */
 74	void setScreenLineCount(int line, int count)
 75	{
 76		if(count > Short.MAX_VALUE)
 77		{
 78			// limitations...
 79			count = Short.MAX_VALUE;
 80		}
 81
 82		if(Debug.SCREEN_LINES_DEBUG)
 83			Log.log(Log.DEBUG,this,new Exception("setScreenLineCount(" + line + ',' + count + ')'));
 84		screenLines[line] = (short)(count << SCREEN_LINES_SHIFT
 85			| SCREEN_LINES_VALID_MASK);
 86	} //}}}
 87
 88	//{{{ invalidateScreenLineCounts() method
 89	void invalidateScreenLineCounts()
 90	{
 91		int lineCount = buffer.getLineCount();
 92		for(int i = 0; i < lineCount; i++)
 93			screenLines[i] &= ~SCREEN_LINES_VALID_MASK;
 94	} //}}}
 95
 96	//{{{ reset() method
 97	void reset()
 98	{
 99		screenLines = new short[buffer.getLineCount()];
100	} //}}}
101
102	//{{{ contentInserted() method
103	public void contentInserted(int startLine, int numLines)
104	{
105		int endLine = startLine + numLines;
106		screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
107
108		int lineCount = buffer.getLineCount();
109
110		if(numLines > 0)
111		{
112			if(screenLines.length <= lineCount)
113			{
114				short[] screenLinesN = new short[((lineCount + 1) << 1)];
115				System.arraycopy(screenLines,0,screenLinesN,0,
116						 screenLines.length);
117				screenLines = screenLinesN;
118			}
119
120			System.arraycopy(screenLines,startLine,screenLines,
121				endLine,lineCount - endLine);
122
123			for(int i = 0; i < numLines; i++)
124				screenLines[startLine + i] = 0;
125		}
126	} //}}}
127
128	//{{{ contentRemoved() method
129	public void contentRemoved(int startLine, int numLines)
130	{
131		int endLine = startLine + numLines;
132		screenLines[startLine] &= ~SCREEN_LINES_VALID_MASK;
133
134		if(numLines > 0 && endLine != screenLines.length)
135		{
136			System.arraycopy(screenLines,endLine + 1,screenLines,
137				startLine + 1,screenLines.length - endLine - 1);
138		}
139	} //}}}
140
141	//{{{ Private members
142	private static final int SCREEN_LINES_SHIFT = 1;
143	private static final int SCREEN_LINES_VALID_MASK = 1;
144
145	private final JEditBuffer buffer;
146	/** This array contains the line count for each physical line. */
147	private short[] screenLines;
148	//}}}
149}