PageRenderTime 99ms CodeModel.GetById 90ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 155 lines | 63 code | 11 blank | 81 comment | 10 complexity | d71510c399543e8a108e1951c64de52a MD5 | raw file
  1/*
  2 * Anchor.java - A base point for physical line <-> screen line conversion
  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
 23package org.gjt.sp.jedit.textarea;
 24
 25import org.gjt.sp.jedit.Debug;
 26import org.gjt.sp.util.Log;
 27
 28/**
 29 * A base point for physical line/screen line conversion.
 30 * @author Slava Pestov
 31 * @version $Id: Anchor.java 15570 2009-06-25 00:43:57Z ezust $
 32 */
 33abstract class Anchor
 34{
 35	protected final DisplayManager displayManager;
 36	protected final TextArea textArea;
 37	/** The physical line. */
 38	int physicalLine;
 39	/**
 40	 * The visible line index. (from the top of the buffer). It can be different from physical line
 41	 * when using soft wrap.
 42	 */
 43	int scrollLine;
 44	/** 
 45	 * If this is set to true, the changed() method will be called in
 46	 * {@link DisplayManager#notifyScreenLineChanges()}
 47	 */
 48	boolean callChanged;
 49	/** 
 50	 * If this is set to true, the reset() method will be called in
 51	 * {@link DisplayManager#notifyScreenLineChanges()}
 52	 */
 53	boolean callReset;
 54
 55	//{{{ Anchor constructor
 56	protected Anchor(DisplayManager displayManager,
 57		TextArea textArea)
 58	{
 59		this.displayManager = displayManager;
 60		this.textArea = textArea;
 61	} //}}}
 62
 63	/** This method recalculates the scrollLine from the beginning. */
 64	abstract void reset();
 65	abstract void changed();
 66
 67	//{{{ toString() method
 68	@Override
 69	public String toString()
 70	{
 71		return getClass().getName() + '[' + physicalLine + ','
 72		       + scrollLine + ']';
 73	} //}}}
 74
 75	//{{{ contentInserted() method
 76	/**
 77	 * Some content is inserted.
 78	 *
 79	 * @param startLine the start of the insert
 80	 * @param numLines the number of insterted lines
 81	 */
 82	void contentInserted(int startLine, int numLines)
 83	{
 84		// The Anchor is changed only if the content was inserted before
 85		if(physicalLine >= startLine)
 86		{
 87			if(physicalLine != startLine)
 88				physicalLine += numLines;
 89			callChanged = true;
 90		}
 91	} //}}}
 92
 93	//{{{ preContentRemoved() method
 94	/**
 95	 * Method called before a content is removed from a buffer.
 96	 *
 97	 * @param startLine the first line of the removed content
 98	 * @param offset the offset in the start line
 99	 * @param numLines the number of removed lines
100	 */
101	void preContentRemoved(int startLine, int offset, int numLines)
102	{
103		if(Debug.SCROLL_DEBUG)
104			Log.log(Log.DEBUG,this,"preContentRemoved() before:" + this);
105		// The removed content starts before the Anchor, we need to pull the anchor up
106		if(physicalLine >= startLine)
107		{
108			if(physicalLine == startLine)
109				callChanged = true;
110			else
111			{
112				int end = Math.min(startLine + numLines, physicalLine);
113				//Check the lines from the beginning of the removed content to the end (or the physical
114				//line of the Anchor if it is before the end of the removed content
115
116				//int loopStart = startLine + 1;
117
118				//{{{ treatment if the beginning of the deleted content is inside a physical line that has several line counts
119				/*if (displayManager.isLineVisible(startLine))
120				{
121					int screenLineCount = displayManager.screenLineMgr.getScreenLineCount(startLine);
122					if (screenLineCount > 1)
123					{
124						int lineStartOffset = textArea.getLineStartOffset(startLine);
125
126						int startScreenLine = textArea.getScreenLineOfOffset(lineStartOffset);
127						int deleteStartScreenLine = textArea.getScreenLineOfOffset(offset);
128						if (startScreenLine != deleteStartScreenLine)
129						{
130							loopStart = startLine + 2;
131							scrollLine -= screenLineCount - deleteStartScreenLine + startScreenLine;
132						}
133					}
134				}*/
135				//}}}
136
137				for(int i = startLine + 1; i <= end; i++)
138				{
139					//XXX
140					if(displayManager.isLineVisible(i))
141					{
142						scrollLine -=
143							displayManager
144								.screenLineMgr
145								.getScreenLineCount(i);
146					}
147				}
148				physicalLine -= end - startLine;
149				callChanged = true;
150			}
151		}
152		if(Debug.SCROLL_DEBUG)
153			Log.log(Log.DEBUG,this,"preContentRemoved() after:" + this);
154	} //}}}
155}