PageRenderTime 50ms CodeModel.GetById 42ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/buffer/ContentManager.java

#
Java | 193 lines | 123 code | 22 blank | 48 comment | 15 complexity | 8ca3cd5f5c302497fb227c1401ba0dd4 MD5 | raw file
  1/*
  2 * ContentManager.java - Manages text content
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 2002 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.buffer;
 24
 25import javax.swing.text.Segment;
 26
 27/**
 28 * A class internal to jEdit's document model. You should not use it
 29 * directly. To improve performance, none of the methods in this class
 30 * check for out of bounds access, nor are they thread-safe. The
 31 * <code>Buffer</code> class, through which these methods must be
 32 * called through, implements such protection.
 33 *
 34 * @author Slava Pestov
 35 * @version $Id: ContentManager.java 4227 2002-06-02 05:40:43Z spestov $
 36 * @since jEdit 4.0pre1
 37 */
 38public class ContentManager
 39{
 40	//{{{ ContentManager constructor
 41	public ContentManager()
 42	{
 43		text = new char[1024];
 44	} //}}}
 45
 46	//{{{ getLength() method
 47	public final int getLength()
 48	{
 49		return length;
 50	} //}}}
 51
 52	//{{{ getText() method
 53	public String getText(int start, int len)
 54	{
 55		if(start >= gapStart)
 56			return new String(text,start + gapEnd - gapStart,len);
 57		else if(start + len <= gapStart)
 58			return new String(text,start,len);
 59		else
 60		{
 61			return new String(text,start,gapStart - start)
 62				.concat(new String(text,gapEnd,start + len - gapStart));
 63		}
 64	} //}}}
 65
 66	//{{{ getText() method
 67	public void getText(int start, int len, Segment seg)
 68	{
 69		if(start >= gapStart)
 70		{
 71			seg.array = text;
 72			seg.offset = start + gapEnd - gapStart;
 73			seg.count = len;
 74		}
 75		else if(start + len <= gapStart)
 76		{
 77			seg.array = text;
 78			seg.offset = start;
 79			seg.count = len;
 80		}
 81		else
 82		{
 83			seg.array = new char[len];
 84
 85			// copy text before gap
 86			System.arraycopy(text,start,seg.array,0,gapStart - start);
 87
 88			// copy text after gap
 89			System.arraycopy(text,gapEnd,seg.array,gapStart - start,
 90				len + start - gapStart);
 91
 92			seg.offset = 0;
 93			seg.count = len;
 94		}
 95	} //}}}
 96
 97	//{{{ insert() method
 98	public void insert(int start, String str)
 99	{
100		int len = str.length();
101		moveGapStart(start);
102		if(gapEnd - gapStart < len)
103		{
104			ensureCapacity(length + len + 1024);
105			moveGapEnd(start + len + 1024);
106		}
107
108		str.getChars(0,len,text,start);
109		gapStart += len;
110		length += len;
111	} //}}}
112
113	//{{{ insert() method
114	public void insert(int start, Segment seg)
115	{
116		moveGapStart(start);
117		if(gapEnd - gapStart < seg.count)
118		{
119			ensureCapacity(length + seg.count + 1024);
120			moveGapEnd(start + seg.count + 1024);
121		}
122
123		System.arraycopy(seg.array,seg.offset,text,start,seg.count);
124		gapStart += seg.count;
125		length += seg.count;
126	} //}}}
127
128	//{{{ _setContent() method
129	public void _setContent(char[] text, int length)
130	{
131		this.text = text;
132		this.gapStart = this.gapEnd = 0;
133		this.length = length;
134	} //}}}
135
136	//{{{ remove() method
137	public void remove(int start, int len)
138	{
139		moveGapStart(start);
140		gapEnd += len;
141		length -= len;
142	} //}}}
143
144	//{{{ Private members
145	private char[] text;
146	private int gapStart;
147	private int gapEnd;
148	private int length;
149
150	//{{{ moveGapStart() method
151	private void moveGapStart(int newStart)
152	{
153		int newEnd = gapEnd + (newStart - gapStart);
154
155		if(newStart == gapStart)
156		{
157			// nothing to do
158		}
159		else if(newStart > gapStart)
160		{
161			System.arraycopy(text,gapEnd,text,gapStart,
162				newStart - gapStart);
163		}
164		else if(newStart < gapStart)
165		{
166			System.arraycopy(text,newStart,text,newEnd,
167				gapStart - newStart);
168		}
169
170		gapStart = newStart;
171		gapEnd = newEnd;
172	} //}}}
173
174	//{{{ moveGapEnd() method
175	private void moveGapEnd(int newEnd)
176	{
177		System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
178		gapEnd = newEnd;
179	} //}}}
180
181	//{{{ ensureCapacity() method
182	private void ensureCapacity(int capacity)
183	{
184		if(capacity >= text.length)
185		{
186			char[] textN = new char[capacity * 2];
187			System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
188			text = textN;
189		}
190	} //}}}
191
192	//}}}
193}