/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/buffer/ContentManager.java
Java | 177 lines | 113 code | 18 blank | 46 comment | 23 complexity | 9665706266c490d45a1c180ab62ad32d 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
1/*
2 * ContentManager.java - Manages text content
3 * :tabSize=8:indentSize=8:noTabs=false:
4 * :folding=explicit:collapseFolds=1:
5 *
6 * Copyright (C) 2001 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 3909 2001-11-24 02:27:08Z 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 if(gapStart != start || gapEnd - gapStart < len)
102 {
103 ensureCapacity(length + len + 200);
104 close(start,start + len + 200);
105 }
106 str.getChars(0,len,text,start);
107 gapStart += len;
108 length += len;
109 } //}}}
110
111 //{{{ insert() method
112 public void insert(int start, Segment seg)
113 {
114 if(gapStart != start || gapEnd - gapStart < seg.count)
115 {
116 ensureCapacity(length + seg.count + 200);
117 close(start,start + seg.count + 200);
118 }
119 System.arraycopy(seg.array,seg.offset,text,start,seg.count);
120 gapStart += seg.count;
121 length += seg.count;
122 } //}}}
123
124 //{{{ remove() method
125 public void remove(int start, int len)
126 {
127 close(start,start);
128 gapEnd += len;
129 length -= len;
130 } //}}}
131
132 //{{{ Private members
133 private char[] text;
134 private int gapStart;
135 private int gapEnd;
136 private int length;
137
138 //{{{ close() method
139 private void close(int newStart, int newEnd)
140 {
141 // Optimization
142 if(gapStart == newStart)
143 {
144 System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
145 }
146 else
147 {
148 if(gapStart != gapEnd && gapStart != length)
149 {
150 System.arraycopy(text,gapEnd,text,gapStart,
151 length - gapStart);
152 }
153
154 if(newStart != newEnd && newStart != length)
155 {
156 System.arraycopy(text,newStart,text,newEnd,
157 length - newStart);
158 }
159 }
160
161 gapStart = newStart;
162 gapEnd = newEnd;
163 } //}}}
164
165 //{{{ ensureCapacity() method
166 private void ensureCapacity(int capacity)
167 {
168 if(capacity >= text.length)
169 {
170 char[] textN = new char[capacity * 2];
171 System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
172 text = textN;
173 }
174 } //}}}
175
176 //}}}
177}