PageRenderTime 279ms CodeModel.GetById 259ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/buffer/ContentManager.java

#
Java | 245 lines | 147 code | 24 blank | 74 comment | 20 complexity | 49f360fb91c6dd10ead4c2c86b5b10c0 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 17813 2010-05-12 14:20:37Z k_satoda $
 36 * @since jEdit 4.0pre1
 37 */
 38class ContentManager
 39{
 40	//{{{ getLength() method
 41	public final int getLength()
 42	{
 43		return length;
 44	} //}}}
 45
 46	//{{{ getText() methods
 47	public String getText(int start, int len)
 48	{
 49		if(start >= gapStart)
 50			return new String(text,start + gapEnd - gapStart,len);
 51		else if(start + len <= gapStart)
 52			return new String(text,start,len);
 53		else
 54		{
 55			return new String(text,start,gapStart - start)
 56				.concat(new String(text,gapEnd,start + len - gapStart));
 57		}
 58	}
 59
 60	/**
 61	 * Returns the specified text range in a <code>Segment</code>.<p>
 62	 *
 63	 * Using a <classname>Segment</classname> is generally more
 64	 * efficient than using a <classname>String</classname> because it
 65	 * results in less memory allocation and array copying.<p>
 66	 *
 67	 *
 68	 * @param start The start offset
 69	 * @param len The number of characters to get
 70	 * @param seg The segment to copy the text to
 71	 * @see JEditBuffer#getText(int, int, Segment)
 72	 */
 73	public void getText(int start, int len, Segment seg)
 74	{
 75		if(start >= gapStart)
 76		{
 77			seg.array = text;
 78			seg.offset = start + gapEnd - gapStart;
 79			seg.count = len;
 80		}
 81		else if(start + len <= gapStart)
 82		{
 83			seg.array = text;
 84			seg.offset = start;
 85			seg.count = len;
 86		}
 87		else
 88		{
 89			seg.array = new char[len];
 90
 91			// copy text before gap
 92			System.arraycopy(text,start,seg.array,0,gapStart - start);
 93
 94			// copy text after gap
 95			System.arraycopy(text,gapEnd,seg.array,gapStart - start,
 96				len + start - gapStart);
 97
 98			seg.offset = 0;
 99			seg.count = len;
100		}
101	} //}}}
102
103	//{{{ getSegment() method
104	/**
105	 * Returns a read-only segment of the buffer.
106	 * It doesn't copy the text
107	 *
108	 * @param start The start offset
109	 * @param len The number of characters to get
110	 *
111	 * @return a CharSequence that contains the text wanted text
112	 * @since jEdit 4.3pre15
113	 */
114	public CharSequence getSegment(int start, int len)
115	{
116		if(start >= gapStart)
117			return new BufferSegment(text,start + gapEnd - gapStart,len);
118		else if(start + len <= gapStart)
119			return new BufferSegment(text,start,len);
120		else
121		{
122			return new BufferSegment(text,start,gapStart - start,
123				new BufferSegment(text,gapEnd,start + len - gapStart));
124		}
125	} //}}}
126
127	//{{{ insert() methods
128	public void insert(int start, String str)
129	{
130		int len = str.length();
131		moveGapStart(start);
132		if(gapEnd - gapStart < len)
133		{
134			ensureCapacity(length + len + 1024);
135			moveGapEnd(start + len + 1024);
136		}
137
138		str.getChars(0,len,text,start);
139		gapStart += len;
140		length += len;
141	}
142
143	/**
144	 * Inserts the given data into the buffer.
145	 *
146	 * @since jEdit 4.3pre15
147	 */
148	public void insert(int start, CharSequence str)
149	{
150		int len = str.length();
151		moveGapStart(start);
152		if(gapEnd - gapStart < len)
153		{
154			ensureCapacity(length + len + 1024);
155			moveGapEnd(start + len + 1024);
156		}
157
158		for (int i = 0; i < len; i++)
159		{
160			text[start+i] = str.charAt(i);
161		}
162		gapStart += len;
163		length += len;
164	}
165
166	public void insert(int start, Segment seg)
167	{
168		moveGapStart(start);
169		if(gapEnd - gapStart < seg.count)
170		{
171			ensureCapacity(length + seg.count + 1024);
172			moveGapEnd(start + seg.count + 1024);
173		}
174
175		System.arraycopy(seg.array,seg.offset,text,start,seg.count);
176		gapStart += seg.count;
177		length += seg.count;
178	} //}}}
179
180	//{{{ _setContent() method
181	public void _setContent(char[] text, int length)
182	{
183		this.text = text;
184		this.gapStart = this.gapEnd = 0;
185		this.length = length;
186	} //}}}
187
188	//{{{ remove() method
189	public void remove(int start, int len)
190	{
191		moveGapStart(start);
192		gapEnd += len;
193		length -= len;
194	} //}}}
195
196	//{{{ Private members
197	private char[] text;
198	private int gapStart;
199	private int gapEnd;
200	private int length;
201
202	//{{{ moveGapStart() method
203	private void moveGapStart(int newStart)
204	{
205		int newEnd = gapEnd + (newStart - gapStart);
206
207		if(newStart == gapStart)
208		{
209			// nothing to do
210		}
211		else if(newStart > gapStart)
212		{
213			System.arraycopy(text,gapEnd,text,gapStart,
214				newStart - gapStart);
215		}
216		else if(newStart < gapStart)
217		{
218			System.arraycopy(text,newStart,text,newEnd,
219				gapStart - newStart);
220		}
221
222		gapStart = newStart;
223		gapEnd = newEnd;
224	} //}}}
225
226	//{{{ moveGapEnd() method
227	private void moveGapEnd(int newEnd)
228	{
229		System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
230		gapEnd = newEnd;
231	} //}}}
232
233	//{{{ ensureCapacity() method
234	private void ensureCapacity(int capacity)
235	{
236		if(capacity >= text.length)
237		{
238			char[] textN = new char[capacity * 2];
239			System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
240			text = textN;
241		}
242	} //}}}
243
244	//}}}
245}