001: /*
002: * ContentManager.java - Manages text content
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.buffer;
024:
025: import javax.swing.text.Segment;
026:
027: /**
028: * A class internal to jEdit's document model. You should not use it
029: * directly. To improve performance, none of the methods in this class
030: * check for out of bounds access, nor are they thread-safe. The
031: * <code>Buffer</code> class, through which these methods must be
032: * called through, implements such protection.
033: *
034: * @author Slava Pestov
035: * @version $Id: ContentManager.java 4524 2003-03-09 19:26:05Z spestov $
036: * @since jEdit 4.0pre1
037: */
038: public class ContentManager {
039: //{{{ getLength() method
040: public final int getLength() {
041: return length;
042: } //}}}
043:
044: //{{{ getText() method
045: public String getText(int start, int len) {
046: if (start >= gapStart)
047: return new String(text, start + gapEnd - gapStart, len);
048: else if (start + len <= gapStart)
049: return new String(text, start, len);
050: else {
051: return new String(text, start, gapStart - start)
052: .concat(new String(text, gapEnd, start + len
053: - gapStart));
054: }
055: } //}}}
056:
057: //{{{ getText() method
058: public void getText(int start, int len, Segment seg) {
059: if (start >= gapStart) {
060: seg.array = text;
061: seg.offset = start + gapEnd - gapStart;
062: seg.count = len;
063: } else if (start + len <= gapStart) {
064: seg.array = text;
065: seg.offset = start;
066: seg.count = len;
067: } else {
068: seg.array = new char[len];
069:
070: // copy text before gap
071: System.arraycopy(text, start, seg.array, 0, gapStart
072: - start);
073:
074: // copy text after gap
075: System.arraycopy(text, gapEnd, seg.array, gapStart - start,
076: len + start - gapStart);
077:
078: seg.offset = 0;
079: seg.count = len;
080: }
081: } //}}}
082:
083: //{{{ insert() method
084: public void insert(int start, String str) {
085: int len = str.length();
086: moveGapStart(start);
087: if (gapEnd - gapStart < len) {
088: ensureCapacity(length + len + 1024);
089: moveGapEnd(start + len + 1024);
090: }
091:
092: str.getChars(0, len, text, start);
093: gapStart += len;
094: length += len;
095: } //}}}
096:
097: //{{{ insert() method
098: public void insert(int start, Segment seg) {
099: moveGapStart(start);
100: if (gapEnd - gapStart < seg.count) {
101: ensureCapacity(length + seg.count + 1024);
102: moveGapEnd(start + seg.count + 1024);
103: }
104:
105: System.arraycopy(seg.array, seg.offset, text, start, seg.count);
106: gapStart += seg.count;
107: length += seg.count;
108: } //}}}
109:
110: //{{{ _setContent() method
111: public void _setContent(char[] text, int length) {
112: this .text = text;
113: this .gapStart = this .gapEnd = 0;
114: this .length = length;
115: } //}}}
116:
117: //{{{ remove() method
118: public void remove(int start, int len) {
119: moveGapStart(start);
120: gapEnd += len;
121: length -= len;
122: } //}}}
123:
124: //{{{ Private members
125: private char[] text;
126: private int gapStart;
127: private int gapEnd;
128: private int length;
129:
130: //{{{ moveGapStart() method
131: private void moveGapStart(int newStart) {
132: int newEnd = gapEnd + (newStart - gapStart);
133:
134: if (newStart == gapStart) {
135: // nothing to do
136: } else if (newStart > gapStart) {
137: System.arraycopy(text, gapEnd, text, gapStart, newStart
138: - gapStart);
139: } else if (newStart < gapStart) {
140: System.arraycopy(text, newStart, text, newEnd, gapStart
141: - newStart);
142: }
143:
144: gapStart = newStart;
145: gapEnd = newEnd;
146: } //}}}
147:
148: //{{{ moveGapEnd() method
149: private void moveGapEnd(int newEnd) {
150: System.arraycopy(text, gapEnd, text, newEnd, length - gapStart);
151: gapEnd = newEnd;
152: } //}}}
153:
154: //{{{ ensureCapacity() method
155: private void ensureCapacity(int capacity) {
156: if (capacity >= text.length) {
157: char[] textN = new char[capacity * 2];
158: System.arraycopy(text, 0, textN, 0, length
159: + (gapEnd - gapStart));
160: text = textN;
161: }
162: } //}}}
163:
164: //}}}
165: }
|