/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * ContentManager.java - Manages text content
- * :tabSize=8:indentSize=8:noTabs=false:
- * :folding=explicit:collapseFolds=1:
- *
- * Copyright (C) 2001, 2002 Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.jedit.buffer;
- import javax.swing.text.Segment;
- /**
- * A class internal to jEdit's document model. You should not use it
- * directly. To improve performance, none of the methods in this class
- * check for out of bounds access, nor are they thread-safe. The
- * <code>Buffer</code> class, through which these methods must be
- * called through, implements such protection.
- *
- * @author Slava Pestov
- * @version $Id: ContentManager.java 4227 2002-06-02 05:40:43Z spestov $
- * @since jEdit 4.0pre1
- */
- public class ContentManager
- {
- //{{{ ContentManager constructor
- public ContentManager()
- {
- text = new char[1024];
- } //}}}
- //{{{ getLength() method
- public final int getLength()
- {
- return length;
- } //}}}
- //{{{ getText() method
- public String getText(int start, int len)
- {
- if(start >= gapStart)
- return new String(text,start + gapEnd - gapStart,len);
- else if(start + len <= gapStart)
- return new String(text,start,len);
- else
- {
- return new String(text,start,gapStart - start)
- .concat(new String(text,gapEnd,start + len - gapStart));
- }
- } //}}}
- //{{{ getText() method
- public void getText(int start, int len, Segment seg)
- {
- if(start >= gapStart)
- {
- seg.array = text;
- seg.offset = start + gapEnd - gapStart;
- seg.count = len;
- }
- else if(start + len <= gapStart)
- {
- seg.array = text;
- seg.offset = start;
- seg.count = len;
- }
- else
- {
- seg.array = new char[len];
- // copy text before gap
- System.arraycopy(text,start,seg.array,0,gapStart - start);
- // copy text after gap
- System.arraycopy(text,gapEnd,seg.array,gapStart - start,
- len + start - gapStart);
- seg.offset = 0;
- seg.count = len;
- }
- } //}}}
- //{{{ insert() method
- public void insert(int start, String str)
- {
- int len = str.length();
- moveGapStart(start);
- if(gapEnd - gapStart < len)
- {
- ensureCapacity(length + len + 1024);
- moveGapEnd(start + len + 1024);
- }
- str.getChars(0,len,text,start);
- gapStart += len;
- length += len;
- } //}}}
- //{{{ insert() method
- public void insert(int start, Segment seg)
- {
- moveGapStart(start);
- if(gapEnd - gapStart < seg.count)
- {
- ensureCapacity(length + seg.count + 1024);
- moveGapEnd(start + seg.count + 1024);
- }
- System.arraycopy(seg.array,seg.offset,text,start,seg.count);
- gapStart += seg.count;
- length += seg.count;
- } //}}}
- //{{{ _setContent() method
- public void _setContent(char[] text, int length)
- {
- this.text = text;
- this.gapStart = this.gapEnd = 0;
- this.length = length;
- } //}}}
- //{{{ remove() method
- public void remove(int start, int len)
- {
- moveGapStart(start);
- gapEnd += len;
- length -= len;
- } //}}}
- //{{{ Private members
- private char[] text;
- private int gapStart;
- private int gapEnd;
- private int length;
- //{{{ moveGapStart() method
- private void moveGapStart(int newStart)
- {
- int newEnd = gapEnd + (newStart - gapStart);
- if(newStart == gapStart)
- {
- // nothing to do
- }
- else if(newStart > gapStart)
- {
- System.arraycopy(text,gapEnd,text,gapStart,
- newStart - gapStart);
- }
- else if(newStart < gapStart)
- {
- System.arraycopy(text,newStart,text,newEnd,
- gapStart - newStart);
- }
- gapStart = newStart;
- gapEnd = newEnd;
- } //}}}
- //{{{ moveGapEnd() method
- private void moveGapEnd(int newEnd)
- {
- System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
- gapEnd = newEnd;
- } //}}}
- //{{{ ensureCapacity() method
- private void ensureCapacity(int capacity)
- {
- if(capacity >= text.length)
- {
- char[] textN = new char[capacity * 2];
- System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
- text = textN;
- }
- } //}}}
- //}}}
- }