KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > swingviews > SwingContent


1 package gnu.kawa.swingviews;
2 import javax.swing.text.*;
3 import javax.swing.undo.*;
4 import gnu.lists.*;
5
6 /** A wrapper around a CharBuffer that implements Swing's Content.
7  * This allows us to use a CharBuffer for a Document's Content. */

8
9 public class SwingContent
10   implements javax.swing.text.AbstractDocument.Content
11 {
12   public final CharBuffer buffer;
13
14   public SwingContent (CharBuffer buffer)
15   {
16     this.buffer = buffer;
17   }
18
19   public SwingContent (int initialSize)
20   {
21     CharBuffer b = new CharBuffer(initialSize);
22     // Swing assumes that a Content object is initialized to contain
23
// a single '\n'. This of course is not clearly documented ...
24
b.gapEnd = initialSize-1;
25     b.getArray()[b.gapEnd] = '\n';
26     this.buffer = b;
27   }
28
29   public SwingContent ()
30   {
31     this(100);
32   }
33
34   public int length () { return buffer.length(); }
35
36   public void getChars(int where, int len, Segment txt)
37     throws BadLocationException
38   {
39     CharBuffer b = buffer;
40     int start = b.getSegment(where, len);
41     if (start < 0)
42       throw new BadLocationException("invalid offset", where);
43     txt.offset = start;
44     txt.array = b.getArray();
45     txt.count = len;
46   }
47
48   public String JavaDoc getString(int where, int len)
49     throws BadLocationException
50   {
51     CharBuffer b = buffer;
52     int start = b.getSegment(where, len);
53     if (start < 0)
54       throw new BadLocationException("invalid offset", where);
55     return new String JavaDoc(b.getArray(), start, len);
56   }
57
58   public UndoableEdit remove(int where, int nitems)
59     throws BadLocationException
60   {
61     CharBuffer b = buffer;
62     if (nitems < 0 || where < 0 || where + nitems > b.length())
63       throw new BadLocationException("invalid remove", where);
64
65     b.delete(where, nitems);
66
67     GapUndoableEdit undo = new GapUndoableEdit(where);
68     undo.content = this;
69     undo.data = new String JavaDoc(b.getArray(), b.gapEnd - nitems, nitems);
70     undo.nitems = nitems;
71     undo.isInsertion = false;
72     return undo;
73   }
74
75   public UndoableEdit
76   insertString(int where, String JavaDoc str, boolean beforeMarkers)
77     throws BadLocationException
78   {
79     CharBuffer b = buffer;
80     if (where < 0 || where > b.length())
81       throw new BadLocationException("bad insert", where);
82     b.insert(where, str, beforeMarkers);
83
84     GapUndoableEdit undo = new GapUndoableEdit(where);
85     undo.content = this;
86     undo.data = str;
87     undo.nitems = str.length();
88     undo.isInsertion = true;
89     return undo;
90   }
91
92   public UndoableEdit insertString(int where, String JavaDoc str)
93     throws BadLocationException
94   {
95     return insertString(where, str, false);
96   }
97
98   public javax.swing.text.Position JavaDoc createPosition(int offset)
99     throws BadLocationException
100   {
101     CharBuffer b = buffer;
102     if (offset < 0 || offset > b.length())
103       throw new BadLocationException("bad offset to createPosition", offset);
104     return new GapPosition(b, offset);
105   }
106
107 }
108
109 class GapPosition extends SeqPosition
110     implements javax.swing.text.Position JavaDoc
111 {
112   public GapPosition(CharBuffer content, int offset)
113   {
114     super(content, offset, false);
115   }
116
117   public int getOffset() { return nextIndex(); }
118 }
119
120 class GapUndoableEdit extends AbstractUndoableEdit
121 {
122   // False if this is a remove (delete); true if an insertion.
123
boolean isInsertion;
124
125   SwingContent content;
126
127   String JavaDoc data;
128
129   int startOffset;
130   int nitems;
131
132   GapUndoableEdit(int offset)
133   {
134     startOffset = offset;
135   }
136
137   private void doit(boolean isInsertion)
138     throws BadLocationException
139   {
140     //int startOffset = content.positions[content.indexes[startIndex]];
141
if (isInsertion)
142       {
143         // FIXME returns useless Undo
144
content.insertString(startOffset, data);
145       }
146     else
147       {
148         // FIXME returns useless Undo
149
content.remove(startOffset, nitems);
150       }
151   }
152
153   public void undo () throws CannotUndoException
154   {
155     super.undo();
156     try
157       {
158         doit (! isInsertion);
159       }
160     catch (BadLocationException ex)
161       {
162         throw new CannotUndoException();
163       }
164   }
165
166   public void redo () throws CannotUndoException
167   {
168     super.redo();
169     try
170       {
171         doit (isInsertion);
172       }
173     catch (BadLocationException ex)
174       {
175         throw new CannotRedoException();
176       }
177   }
178 }
179
Popular Tags