KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package gnu.jemacs.swt;
4
5 import gnu.lists.FString;
6 import gnu.lists.GapVector;
7
8 /**
9  * TODO Explain that this is all about.
10  * @author Christian Surlykke
11  * 18-07-2004
12  */

13 public class SwtCharBuffer extends GapVector implements CharSequence JavaDoc
14 {
15   protected FString chars;
16   protected LineOffsets lineOffsets;
17   
18   public SwtCharBuffer(int initialSize)
19   {
20     super(new FString(new char[initialSize]));
21     chars = (FString) base;
22     this.lineOffsets = new LineOffsets(initialSize/50);
23   }
24     
25   /* #ifdef use:java.lang.CharSequence */
26   public CharSequence JavaDoc subSequence(int start, int end)
27   {
28     return new SubSequence(start, end);
29   }
30
31   class SubSequence implements CharSequence JavaDoc
32   {
33     private int start;
34     private int end;
35
36     public SubSequence(int start, int end)
37     {
38       this.start = start;
39       this.end = end;
40     }
41     
42     /**
43      * @see java.lang.CharSequence#length()
44      */

45     public int length()
46     {
47       return end - start;
48     }
49
50     /**
51      * @see java.lang.CharSequence#charAt(int)
52      */

53     public char charAt(int index)
54     {
55       return SwtCharBuffer.this.charAt(index + start);
56     }
57
58     /**
59      * @see java.lang.CharSequence#subSequence(int, int)
60      */

61     public CharSequence JavaDoc subSequence(int start, int end)
62     {
63       return SwtCharBuffer.this.subSequence(this.start + start, this.start + end);
64     }
65   }
66   /* #endif */
67
68   public int pos2offset(int pos)
69   {
70     return pos < gapStart ? pos : pos + gapEnd - gapStart;
71   }
72   
73   public int offset2pos(int offset)
74   {
75     return offset <= gapStart ? offset : offset - gapEnd + gapStart;
76   }
77   
78   /**
79    * @see gnu.lists.GapVector#gapReserve(int)
80    */

81   protected void gapReserve(int size)
82   {
83     int oldGapSize = gapEnd - gapStart;
84     super.gapReserve(size);
85     int newGapSize = gapEnd - gapStart;
86     if (newGapSize > oldGapSize) // It never shrinks
87
{
88       lineOffsets.textRegionMoved(gapStart + oldGapSize, size(), newGapSize - oldGapSize);
89     }
90   }
91   /**
92    * @see gnu.lists.GapVector#shiftGap(int)
93    */

94   protected void shiftGap(int newGapStart)
95   {
96     int oldGapStart = gapStart;
97     super.shiftGap(newGapStart);
98     if (oldGapStart != gapStart)
99     {
100       int regionStart = oldGapStart < newGapStart ? oldGapStart + gapEnd - gapStart : gapStart;
101       int regionEnd = oldGapStart < newGapStart ? gapEnd : oldGapStart;
102       int displacement = oldGapStart < newGapStart ? gapStart - gapEnd : gapEnd - gapStart;
103       lineOffsets.textRegionMoved(regionStart, regionEnd, displacement);
104     
105     }
106   }
107   /**
108    * @see java.lang.CharSequence#length()
109    */

110   public int length()
111   {
112     return size();
113   }
114   
115   /**
116    * @see java.lang.CharSequence#charAt(int)
117    */

118   public char charAt(int index)
119   {
120     char c = chars.charAt(index < gapStart ? index : index + gapEnd - gapStart);
121     return c;
122   }
123   /**
124    * @param where
125    * @param str
126    */

127   public void insert(int where, String JavaDoc str)
128   {
129     gapReserve(where, str.length());
130     str.getChars(0, str.length(), chars.data, where);
131     gapStart += str.length();
132     lineOffsets.textInserted(where, str);
133   }
134   
135   /**
136    * @param where
137    * @param count
138    */

139   public void delete(int where, int count)
140   {
141     shiftGap(where + count);
142     gapStart -= count;
143     lineOffsets.textDeleted(where, where + count);
144   }
145
146   public void getChars(int start, int end, char[] dest, int destStart)
147   {
148     int startOffset = pos2offset(start);
149     int endOffset = pos2offset(end);
150     // First we get the part of the string before the gap
151
int startOffset1 = Math.min(startOffset, gapStart);
152     int len1 = Math.min(endOffset, gapStart) - startOffset1;
153     System.arraycopy(chars.data, startOffset1, dest, destStart, len1);
154         
155     // Then the part after the gap
156
int startOffset2 = Math.max(startOffset, gapEnd);
157     int len2 = Math.max(endOffset, gapEnd) - startOffset2;
158     System.arraycopy(chars.data, startOffset2, dest, destStart + len1, len2);
159   }
160
161   public String JavaDoc toString()
162   {
163     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
164     sbuf.append("Text: {" + size() + ", " + gapStart + ", " + gapEnd);
165     sbuf.append(" [");
166     for (int i = 0; i < size(); i++)
167     {
168       if (i == gapStart)
169       {
170         sbuf.append("|");
171       }
172       char c = chars.charAt(i < gapStart ? i : i + gapEnd - gapStart);
173       if (c == '\r') c = 'R';
174       if (c == '\n') c = 'N';
175       sbuf.append(c);
176     }
177     sbuf.append("]}\n");
178     sbuf.append(lineOffsets.toString());
179     
180     return sbuf.toString();
181     
182
183   }
184
185   
186   /**
187    *
188    */

189   protected void show()
190   {
191     System.out.println(this);
192   }
193
194   protected static String JavaDoc printable(String JavaDoc s)
195   {
196     return ">>" + s.replaceAll("\r", "R").replaceAll("\n", "N") + "<<";
197   }
198
199    
200 }
201
Popular Tags