KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > swt > BufferContent


1 //This is free software; for terms and warranty disclaimer see ./COPYING.
2

3 package gnu.jemacs.swt;
4
5 import gnu.lists.Consumer;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.io.Writer JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import gnu.lists.CharSeq;
15
16 import org.eclipse.swt.custom.StyledTextContent;
17 import org.eclipse.swt.custom.TextChangeListener;
18 import org.eclipse.swt.custom.TextChangedEvent;
19 import org.eclipse.swt.custom.TextChangingEvent;
20
21 /**
22  * @author Christian Surlykke
23  * 12-07-2004
24  */

25 public class BufferContent extends SwtCharBuffer
26   implements StyledTextContent, CharSeq
27 {
28   public BufferContent()
29   {
30     this(1000);
31   }
32
33   /**
34    * @param initialSize
35    */

36   public BufferContent(int initialSize)
37   {
38     super(initialSize);
39   }
40
41
42   private Set JavaDoc textChangeListeners = new HashSet JavaDoc();
43   
44   /**
45    * @see org.eclipse.swt.custom.StyledTextContent#addTextChangeListener(org.eclipse.swt.custom.TextChangeListener)
46    */

47   public void addTextChangeListener(TextChangeListener textChangeListener)
48   {
49     if (textChangeListener != null)
50     {
51       textChangeListeners.add(textChangeListener);
52     }
53
54   }
55
56   /**
57    * @see org.eclipse.swt.custom.StyledTextContent#removeTextChangeListener(org.eclipse.swt.custom.TextChangeListener)
58    */

59   public void removeTextChangeListener(TextChangeListener textChangeListener)
60   {
61     textChangeListeners.remove(textChangeListener);
62   }
63
64   
65   /**
66    * @see org.eclipse.swt.custom.StyledTextContent#getCharCount()
67    */

68   public int getCharCount()
69   {
70     return size();
71   }
72
73   /**
74    * @see org.eclipse.swt.custom.StyledTextContent#getLine(int)
75    */

76   public String JavaDoc getLine(int lineIndex)
77   {
78     int startPos = offset2pos(lineOffsets.index2offset(lineIndex));
79     int endPos;
80     if (lineIndex + 1 == lineOffsets.size())
81     {
82       endPos = size();
83     }
84     else
85     {
86       endPos = offset2pos(lineOffsets.index2offset(lineIndex + 1));
87       // Strip linedelimiters
88
while (endPos > startPos && lineOffsets.isLineDelimiter(charAt(endPos - 1)))
89       {
90         endPos--;
91       }
92     }
93     
94     char[] tmp = new char[endPos - startPos];
95     getChars(startPos, endPos, tmp, 0);
96     return new String JavaDoc(tmp);
97   }
98
99   /**
100    * @see org.eclipse.swt.custom.StyledTextContent#getLineAtOffset(int)
101    */

102   public int getLineAtOffset(int pos)
103   {
104     return (lineOffsets.offset2index(pos2offset(pos)));
105   }
106
107   /**
108    * @see org.eclipse.swt.custom.StyledTextContent#getLineCount()
109    */

110   public int getLineCount()
111   {
112     return lineOffsets.size();
113   }
114
115   /**
116    * @see org.eclipse.swt.custom.StyledTextContent#getLineDelimiter()
117    */

118   public String JavaDoc getLineDelimiter()
119   {
120     return "\n";
121   }
122
123   /**
124    * @see org.eclipse.swt.custom.StyledTextContent#getOffsetAtLine(int)
125    */

126   public int getOffsetAtLine(int lineIndex)
127   {
128     int result = offset2pos(lineOffsets.index2offset(lineIndex));
129     return result;
130   }
131
132   /**
133    * @see org.eclipse.swt.custom.StyledTextContent#getTextRange(int, int)
134    */

135   public String JavaDoc getTextRange(int start, int length)
136   {
137     char[] tmp = new char[length];
138     getChars(start, start + length, tmp, 0);
139     return new String JavaDoc(tmp);
140   }
141   
142   /**
143    * @see org.eclipse.swt.custom.StyledTextContent#replaceTextRange(int, int, java.lang.String)
144    */

145   public void replaceTextRange(int start, int length, String JavaDoc newText)
146   {
147     newText = newText == null ? "" : newText;
148     notifyListeners(makeTextChangingEvent(start, length, newText));
149     delete(start, length);
150     insert(start, newText);
151     notifyListeners(new TextChangedEvent(this));
152   }
153
154   private void show(TextChangingEvent changingEvent)
155   {
156     System.out.println("start: " + changingEvent.start + "\n" +
157     "newCharCount: " + changingEvent.newCharCount + "\n" +
158     "newLineCount: " + changingEvent.newLineCount + "\n" +
159     "replaceCharCount: " + changingEvent.replaceCharCount + "\n" +
160     "replaceLineCount: " + changingEvent.replaceLineCount + "\n" +
161     "text: " + printable(changingEvent.newText) + "\n");
162   }
163   /**
164    * @param start
165    * @param length
166    * @param newText
167    * @return
168    */

169   private TextChangingEvent makeTextChangingEvent(int start, int length, String JavaDoc newText)
170   {
171     TextChangingEvent result = new TextChangingEvent(this);
172     result.start = start;
173     result.newCharCount = newText.length();
174     result.newLineCount = lineOffsets.countLines(newText);
175     result.newText = newText;
176     result.replaceCharCount = length;
177     result.replaceLineCount = lineOffsets.linesInRange(pos2offset(start), pos2offset(start + length));
178     
179     return result;
180   }
181
182   /**
183    * @see org.eclipse.swt.custom.StyledTextContent#setText(java.lang.String)
184    */

185   public void setText(String JavaDoc newText)
186   {
187     delete(0, size());
188     insert(0, newText);
189     TextChangedEvent evt = new TextChangedEvent(this);
190     for (Iterator JavaDoc iter = textChangeListeners.iterator(); iter.hasNext();)
191     {
192      ((TextChangeListener) iter.next()).textSet(evt);
193     }
194   }
195   
196   /**
197    * @param changedEvent
198    */

199   private void notifyListeners(TextChangedEvent changedEvent)
200   {
201     for (Iterator JavaDoc iter = textChangeListeners.iterator(); iter.hasNext();)
202     {
203       TextChangeListener element = (TextChangeListener) iter.next();
204       element.textChanged(changedEvent);
205     }
206     
207   }
208
209   /**
210    * @param changingEvent
211    */

212   private void notifyListeners(TextChangingEvent changingEvent)
213   {
214     for (Iterator JavaDoc iter = textChangeListeners.iterator(); iter.hasNext();)
215     {
216       TextChangeListener element = (TextChangeListener) iter.next();
217       element.textChanging(changingEvent);
218     }
219     
220   }
221
222   
223   /**
224    * For testing purposes
225    * @param args
226    */

227   public static void main(String JavaDoc[] args)
228   {
229   }
230
231   public int lineStartPos(int pos)
232   {
233     int offset = pos2offset(pos);
234     int lineStartOffset = lineOffsets.index2offset(lineOffsets.offset2index(offset));
235     return offset2pos(lineStartOffset);
236   }
237
238   /**
239    * @param start
240    * @param count
241    * @param out
242    */

243   public void consume(int start, int count, Consumer out)
244   {
245     // TODO Auto-generated method stub
246

247   }
248
249   /**
250    * @param in
251    * @throws IOException
252    */

253   public void insertFile(Reader JavaDoc in, int pos) throws IOException JavaDoc
254   {
255     char[] buf = new char[65536];
256     for (;;)
257     {
258       int charsRead = in.read(buf);
259       if (charsRead < 0)
260       {
261         break;
262       }
263       replaceTextRange(pos, 0, new String JavaDoc(buf, 0, charsRead));
264     }
265   }
266
267   public void setCharAt (int index, char value)
268   {
269     char[] arr = { value };
270     replaceTextRange(index, index+1, new String JavaDoc(arr));
271   }
272
273   public void fill(char value)
274   {
275     fill(0, size(), value);
276   }
277
278   public void fill(int fromIndex, int toIndex, char value)
279   {
280     for (int i = fromIndex; i < toIndex; i++)
281       setCharAt(i, value);
282   }
283
284   /* #ifdef use:java.lang.CharSequence */
285   public CharSequence JavaDoc subSequence(int start, int end)
286   {
287     throw new UnsupportedOperationException JavaDoc("subSequence not implemented");
288   }
289   /* #endif */
290
291   /* #ifdef JAVA5 */
292   // public void writeTo(int start, int count, Appendable dest)
293
// throws java.io.IOException
294
// {
295
// dest.append(this, start, start+count);
296
// }
297

298   // public void writeTo(Appendable dest)
299
// throws java.io.IOException
300
// {
301
// dest.append(this, 0, size());
302
// }
303
/* #else */
304   public void writeTo(int start, int count, java.io.Writer JavaDoc dest)
305     throws java.io.IOException JavaDoc
306   {
307     while (--count >= 0)
308       dest.write(charAt(start++));
309   }
310
311   public void writeTo(java.io.Writer JavaDoc dest)
312     throws java.io.IOException JavaDoc
313   {
314     writeTo(0, size(), dest);
315   }
316   /* #endif */
317
318   /**
319    * @param out
320    * @throws IOException
321    */

322   public void save(Writer JavaDoc out) throws IOException JavaDoc
323   {
324     out.write(chars.data, 0, gapStart);
325     out.write(chars.data, gapEnd, chars.data.length - gapEnd);
326   }
327 }
328
Popular Tags