KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > buffer > ContentManager


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
23 package org.gjt.sp.jedit.buffer;
24
25 import javax.swing.text.Segment JavaDoc;
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 4524 2003-03-09 19:26:05Z spestov $
36  * @since jEdit 4.0pre1
37  */

38 public class ContentManager
39 {
40     //{{{ getLength() method
41
public final int getLength()
42     {
43         return length;
44     } //}}}
45

46     //{{{ getText() method
47
public String JavaDoc getText(int start, int len)
48     {
49         if(start >= gapStart)
50             return new String JavaDoc(text,start + gapEnd - gapStart,len);
51         else if(start + len <= gapStart)
52             return new String JavaDoc(text,start,len);
53         else
54         {
55             return new String JavaDoc(text,start,gapStart - start)
56                 .concat(new String JavaDoc(text,gapEnd,start + len - gapStart));
57         }
58     } //}}}
59

60     //{{{ getText() method
61
public void getText(int start, int len, Segment JavaDoc seg)
62     {
63         if(start >= gapStart)
64         {
65             seg.array = text;
66             seg.offset = start + gapEnd - gapStart;
67             seg.count = len;
68         }
69         else if(start + len <= gapStart)
70         {
71             seg.array = text;
72             seg.offset = start;
73             seg.count = len;
74         }
75         else
76         {
77             seg.array = new char[len];
78
79             // copy text before gap
80
System.arraycopy(text,start,seg.array,0,gapStart - start);
81
82             // copy text after gap
83
System.arraycopy(text,gapEnd,seg.array,gapStart - start,
84                 len + start - gapStart);
85
86             seg.offset = 0;
87             seg.count = len;
88         }
89     } //}}}
90

91     //{{{ insert() method
92
public void insert(int start, String JavaDoc str)
93     {
94         int len = str.length();
95         moveGapStart(start);
96         if(gapEnd - gapStart < len)
97         {
98             ensureCapacity(length + len + 1024);
99             moveGapEnd(start + len + 1024);
100         }
101
102         str.getChars(0,len,text,start);
103         gapStart += len;
104         length += len;
105     } //}}}
106

107     //{{{ insert() method
108
public void insert(int start, Segment JavaDoc seg)
109     {
110         moveGapStart(start);
111         if(gapEnd - gapStart < seg.count)
112         {
113             ensureCapacity(length + seg.count + 1024);
114             moveGapEnd(start + seg.count + 1024);
115         }
116
117         System.arraycopy(seg.array,seg.offset,text,start,seg.count);
118         gapStart += seg.count;
119         length += seg.count;
120     } //}}}
121

122     //{{{ _setContent() method
123
public void _setContent(char[] text, int length)
124     {
125         this.text = text;
126         this.gapStart = this.gapEnd = 0;
127         this.length = length;
128     } //}}}
129

130     //{{{ remove() method
131
public void remove(int start, int len)
132     {
133         moveGapStart(start);
134         gapEnd += len;
135         length -= len;
136     } //}}}
137

138     //{{{ Private members
139
private char[] text;
140     private int gapStart;
141     private int gapEnd;
142     private int length;
143
144     //{{{ moveGapStart() method
145
private void moveGapStart(int newStart)
146     {
147         int newEnd = gapEnd + (newStart - gapStart);
148
149         if(newStart == gapStart)
150         {
151             // nothing to do
152
}
153         else if(newStart > gapStart)
154         {
155             System.arraycopy(text,gapEnd,text,gapStart,
156                 newStart - gapStart);
157         }
158         else if(newStart < gapStart)
159         {
160             System.arraycopy(text,newStart,text,newEnd,
161                 gapStart - newStart);
162         }
163
164         gapStart = newStart;
165         gapEnd = newEnd;
166     } //}}}
167

168     //{{{ moveGapEnd() method
169
private void moveGapEnd(int newEnd)
170     {
171         System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
172         gapEnd = newEnd;
173     } //}}}
174

175     //{{{ ensureCapacity() method
176
private void ensureCapacity(int capacity)
177     {
178         if(capacity >= text.length)
179         {
180             char[] textN = new char[capacity * 2];
181             System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
182             text = textN;
183         }
184     } //}}}
185

186     //}}}
187
}
188
Popular Tags