source: trunk/gcc/libjava/javax/swing/text/AbstractDocument.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.7 KB
Line 
1/* AbstractDocument.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package javax.swing.text;
39
40import javax.swing.event.*;
41import javax.swing.undo.*;
42import java.util.*;
43import javax.swing.tree.*;
44
45public abstract class AbstractDocument implements Document
46{
47 Vector doc_list = new Vector();
48 Vector undo_list = new Vector();
49
50 // these still need to be implemented by a derived class:
51 public abstract Element getParagraphElement(int pos);
52 public abstract Element getDefaultRootElement();
53
54 // some inner classes sun says I should have:
55 abstract class AbstractElement implements Element, TreeNode
56 {
57 int count, offset;
58 AttributeSet attr;
59 Vector elts = new Vector();
60 String name;
61 Element parent;
62 Vector kids = new Vector();
63 TreeNode tree_parent;
64
65 public AbstractElement(Element p, AttributeSet s)
66 { parent = p; attr = s; }
67
68 public Enumeration children() { return kids.elements(); }
69 public boolean getAllowsChildren() { return true; }
70 public TreeNode getChildAt(int index) { return (TreeNode) kids.elementAt(index); }
71 public int getChildCount() { return kids.size(); }
72 public int getIndex(TreeNode node) { return kids.indexOf(node); }
73 public TreeNode getParent() { return tree_parent; }
74
75 public AttributeSet getAttributes() { return attr; }
76 public Document getDocument() { return AbstractDocument.this; }
77 public Element getElement(int index) { return (Element)elts.elementAt(index); }
78 public String getName() { return name; }
79 public Element getParentElement() { return parent; }
80
81 public abstract boolean isLeaf();
82 public abstract int getEndOffset();
83 public abstract int getElementCount();
84 public abstract int getElementIndex(int offset);
85 public abstract int getStartOffset();
86 }
87
88 interface AttributeContext
89 {
90 }
91
92
93 class BranchElement extends AbstractElement
94 {
95 public BranchElement(Element e, AttributeSet a, int s, int end)
96 { super(e, a); }
97
98 public boolean isLeaf() { return false; }
99 public int getEndOffset() { return 0; }
100 public int getElementCount() { return 0; }
101 public int getElementIndex(int offset) { return 0; }
102 public int getStartOffset() { return 0; }
103 }
104
105 public interface Content
106 {
107 public Position createPosition(int offset) throws BadLocationException;
108 public int length();
109 public UndoableEdit insertString(int where, String str) throws BadLocationException;
110 public UndoableEdit remove(int where, int nitems) throws BadLocationException;
111 public String getString(int where, int len) throws BadLocationException;
112 public void getChars(int where, int len, Segment txt) throws BadLocationException;
113 }
114
115 class DefaultDocumentEvent implements DocumentEvent
116 {
117 int len, off;
118 public Document getDocument() { return AbstractDocument.this; }
119 public int getLength() { return len; }
120 public int getOffset() { return off; }
121 public DocumentEvent.EventType getType() { return null; }
122 public DocumentEvent.ElementChange getChange(Element elem) { return null; }
123 }
124
125 static class ElementEdit
126 {
127 }
128
129 class LeafElement extends AbstractElement
130 {
131 LeafElement(Element e, AttributeSet a, int s, int end)
132 { super(e, a); }
133
134 public boolean isLeaf() { return true; }
135 public int getEndOffset() { return 0; }
136 public int getElementCount() { return 0; }
137 public int getElementIndex(int offset) { return 0; }
138 public int getStartOffset() { return 0; }
139 }
140
141
142 Content content;
143
144 AbstractDocument(Content doc)
145 {
146 content = doc;
147 }
148
149 /********************************************************
150 *
151 * the meat:
152 *
153 ***********/
154
155
156 public void addDocumentListener(DocumentListener listener)
157 {
158 doc_list.addElement(listener);
159 }
160
161 public void addUndoableEditListener(UndoableEditListener listener)
162 {
163 undo_list.addElement(listener);
164 }
165
166 protected Element createBranchElement(Element parent, AttributeSet a)
167 {
168 return new BranchElement(parent, a, 0, 0);
169 }
170
171 protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1)
172 {
173 return new LeafElement(parent, a, p0, p1-p0);
174 }
175
176 public Position createPosition(int offs)
177 {
178 final int a = offs;
179 return new Position()
180 {
181 public int getOffset()
182 {
183 return a;
184 }
185 };
186 }
187
188 protected void fireChangedUpdate(DocumentEvent e)
189 {
190 }
191
192 protected void fireInsertUpdate(DocumentEvent e)
193 {
194 }
195
196 protected void fireRemoveUpdate(DocumentEvent e)
197 {
198 }
199
200 protected void fireUndoableEditUpdate(UndoableEditEvent e)
201 {
202 }
203 int getAsynchronousLoadPriority()
204 {
205 return 0;
206 }
207
208 protected AttributeContext getAttributeContext()
209 {
210 return null;
211 }
212
213 Element getBidiRootElement()
214 {
215 return null;
216 }
217
218 protected Content getContent()
219 {
220 return content;
221 }
222
223 protected Thread getCurrentWriter()
224 {
225 return null;
226 }
227
228
229 Dictionary getDocumentProperties()
230 {
231 return null;
232 }
233
234 public Position getEndPosition()
235 {
236 return null;
237 }
238
239 public int getLength()
240 {
241 return content.length();
242 }
243
244 EventListener[] getListeners(Class listenerType)
245 {
246 return null;
247 }
248
249 public Object getProperty(Object key)
250 {
251 return null;
252 }
253
254 public Element[] getRootElements()
255 {
256 return null;
257 }
258
259 public Position getStartPosition()
260 {
261 return null;
262 }
263
264 public String getText(int offset, int length)
265 {
266 try {
267 return content.getString(offset, length);
268 } catch (Exception e) {
269 System.out.println("Hmmm, fail to getText: " + offset + " -> " + length);
270 return null;
271 }
272 }
273
274 public void getText(int offset, int length, Segment txt)
275 {
276 String a = getText(offset, length);
277
278 if (a == null)
279 {
280 txt.offset = 0;
281 txt.count = 0;
282 txt.array = new char[0];
283 return;
284 }
285
286 txt.offset = offset;
287 txt.count = length;
288
289 char chars[] = new char[ a.length() ];
290
291 a.getChars(0, a.length(), chars, 0);
292
293 txt.array = chars;
294 }
295
296 public void insertString(int offs, String str, AttributeSet a)
297 {
298 try {
299 content.insertString(offs, str);
300 } catch (Exception e) {
301 System.err.println("FAILED TO INSERT-STRING: " + e + ", at:"+offs);
302 }
303 }
304
305 protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr)
306 {
307 }
308
309 protected void postRemoveUpdate(DefaultDocumentEvent chng)
310 {
311 }
312
313 public void putProperty(Object key, Object value)
314 {
315 }
316
317 void readLock()
318 {
319 }
320
321 void readUnlock()
322 {
323 }
324
325 public void remove(int offs, int len)
326 {
327 }
328
329 public void removeDocumentListener(DocumentListener listener)
330 {
331 }
332
333 public void removeUndoableEditListener(UndoableEditListener listener)
334 {
335 }
336
337 protected void removeUpdate(DefaultDocumentEvent chng)
338 {
339 }
340
341 public void render(Runnable r)
342 {
343 }
344
345 void setAsynchronousLoadPriority(int p)
346 {
347 }
348
349 void setDocumentProperties(Dictionary x)
350 {
351 }
352
353 protected void writeLock()
354 {
355 }
356
357 protected void writeUnlock()
358 {
359 }
360}
Note: See TracBrowser for help on using the repository browser.