1 /* AbstractDocument.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.swing
.text
;
40 import javax
.swing
.event
.*;
41 import javax
.swing
.undo
.*;
43 import javax
.swing
.tree
.*;
45 public abstract class AbstractDocument
implements Document
47 Vector doc_list
= new Vector();
48 Vector undo_list
= new Vector();
50 // these still need to be implemented by a derived class:
51 public abstract Element
getParagraphElement(int pos
);
52 public abstract Element
getDefaultRootElement();
54 // some inner classes sun says I should have:
55 abstract class AbstractElement
implements Element
, TreeNode
59 Vector elts
= new Vector();
62 Vector kids
= new Vector();
65 public AbstractElement(Element p
, AttributeSet s
)
66 { parent
= p
; attr
= s
; }
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
; }
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
; }
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();
88 interface AttributeContext
93 class BranchElement
extends AbstractElement
95 public BranchElement(Element e
, AttributeSet a
, int s
, int end
)
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; }
107 Position
createPosition(int offset
) throws BadLocationException
;
109 UndoableEdit
insertString(int where
, String str
) throws BadLocationException
;
110 UndoableEdit
remove(int where
, int nitems
) throws BadLocationException
;
111 String
getString(int where
, int len
) throws BadLocationException
;
112 void getChars(int where
, int len
, Segment txt
) throws BadLocationException
;
115 class DefaultDocumentEvent
implements DocumentEvent
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; }
125 static class ElementEdit
129 class LeafElement
extends AbstractElement
131 LeafElement(Element e
, AttributeSet a
, int s
, int end
)
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; }
144 AbstractDocument(Content doc
)
149 /********************************************************
156 public void addDocumentListener(DocumentListener listener
)
158 doc_list
.addElement(listener
);
161 public void addUndoableEditListener(UndoableEditListener listener
)
163 undo_list
.addElement(listener
);
166 protected Element
createBranchElement(Element parent
, AttributeSet a
)
168 return new BranchElement(parent
, a
, 0, 0);
171 protected Element
createLeafElement(Element parent
, AttributeSet a
, int p0
, int p1
)
173 return new LeafElement(parent
, a
, p0
, p1
-p0
);
176 public Position
createPosition(int offs
)
179 return new Position()
181 public int getOffset()
188 protected void fireChangedUpdate(DocumentEvent e
)
192 protected void fireInsertUpdate(DocumentEvent e
)
196 protected void fireRemoveUpdate(DocumentEvent e
)
200 protected void fireUndoableEditUpdate(UndoableEditEvent e
)
203 int getAsynchronousLoadPriority()
208 protected AttributeContext
getAttributeContext()
213 Element
getBidiRootElement()
218 protected Content
getContent()
223 protected Thread
getCurrentWriter()
229 public Dictionary
getDocumentProperties()
234 public Position
getEndPosition()
239 public int getLength()
241 return content
.length();
244 public EventListener
[] getListeners(Class listenerType
)
249 public Object
getProperty(Object key
)
254 public Element
[] getRootElements()
259 public Position
getStartPosition()
264 public String
getText(int offset
, int length
)
267 return content
.getString(offset
, length
);
268 } catch (Exception e
) {
269 System
.out
.println("Hmmm, fail to getText: " + offset
+ " -> " + length
);
274 public void getText(int offset
, int length
, Segment txt
)
276 String a
= getText(offset
, length
);
282 txt
.array
= new char[0];
289 char chars
[] = new char[ a
.length() ];
291 a
.getChars(0, a
.length(), chars
, 0);
296 public void insertString(int offs
, String str
, AttributeSet a
)
299 content
.insertString(offs
, str
);
300 } catch (Exception e
) {
301 System
.err
.println("FAILED TO INSERT-STRING: " + e
+ ", at:"+offs
);
305 protected void insertUpdate(DefaultDocumentEvent chng
, AttributeSet attr
)
309 protected void postRemoveUpdate(DefaultDocumentEvent chng
)
313 public void putProperty(Object key
, Object value
)
317 public void readLock()
321 public void readUnlock()
325 public void remove(int offs
, int len
)
329 public void removeDocumentListener(DocumentListener listener
)
333 public void removeUndoableEditListener(UndoableEditListener listener
)
337 protected void removeUpdate(DefaultDocumentEvent chng
)
341 public void render(Runnable r
)
345 void setAsynchronousLoadPriority(int p
)
349 void setDocumentProperties(Dictionary x
)
353 protected void writeLock()
357 protected void writeUnlock()