KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > text > AbstractDocument


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab, Robin Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9                                   bobintetley@users.sourceforge.net
10
11    $Log: AbstractDocument.java,v $
12    Revision 1.5 2004/05/04 09:31:43 bobintetley
13    PlainDocument/View support and implementation. Build script supports java/javax
14    packages - fix to build script to use nested args in bootclasspath (single path broke on my Ant 1.6.1/Linux)
15
16    Revision 1.4 2004/04/28 11:02:05 bobintetley
17    PlainDocument implementation
18
19    Revision 1.3 2004/04/27 13:31:21 bobintetley
20    AbstractDocument support and build script notes on using GCJ 3.3
21
22    Revision 1.2 2004/04/16 22:45:50 dannaab
23    Add copyright msg
24
25 */

26
27 package swingwtx.swing.text;
28
29 import swingwtx.swing.event.DocumentEvent;
30 import swingwtx.swing.event.DocumentListener;
31 import swingwtx.swing.event.UndoableEditListener;
32 import swingwtx.swing.tree.TreeNode;
33 import swingwtx.swing.undo.*;
34
35 import java.io.*;
36 import java.util.*;
37
38 /**
39  * Main implementation of document support.
40  *
41  * @author Robin Rawson-Tetley
42  * @author Naab
43  */

44 public abstract class AbstractDocument implements Document, Serializable {
45     
46     /** The document content */
47     protected Content content = null;
48     /** Document listeners */
49     protected Vector documentListeners = new Vector();
50     /** Undo listeners */
51     protected Vector undoListeners = new Vector();
52     /** Properties */
53     protected Hashtable props = new Hashtable(2);
54     
55     public abstract Element getParagraphElement(int pos);
56     
57     /** @return the content length */
58     public int getLength() { return content.length(); }
59
60     public void addDocumentListener(DocumentListener listener) { documentListeners.add(listener); }
61     public void removeDocumentListener(DocumentListener listener) { documentListeners.remove(listener); }
62     public void addUndoableEditListener(UndoableEditListener listener) { undoListeners.add(listener); }
63     public void removeUndoableEditListener(UndoableEditListener listener) { undoListeners.remove(listener); }
64     public Object JavaDoc getProperty(Object JavaDoc key) { return props.get(key); }
65     public void putProperty(Object JavaDoc key, Object JavaDoc value) { props.put(key, value); }
66     /** FIXME: Need to work with UndoManager ? */
67     public void remove(int offs, int len) throws BadLocationException {
68         content.remove(offs, len);
69         fireDocumentEvent(new DefaultDocumentEvent(offs, len, DocumentEvent.EventType.REMOVE));
70     }
71     /** FIXME: Need to work with UndoManager ? */
72     public void insertString(int offset, String JavaDoc str, AttributeSet a) throws BadLocationException {
73         content.insertString(offset, str);
74         fireDocumentEvent(new DefaultDocumentEvent(offset, str.length(), DocumentEvent.EventType.INSERT));
75     }
76     public String JavaDoc getText(int offset, int length) throws BadLocationException { return content.getString(offset, length); }
77     public void getText(int offset, int length, Segment txt) throws BadLocationException { content.getChars(offset, length, txt); }
78
79     public Position getStartPosition() {
80     try {
81         return content.createPosition(0);
82     }
83     catch (BadLocationException e) {
84     }
85     return null;
86     }
87     public Position getEndPosition() {
88     try {
89         return content.createPosition(content.length() - 1);
90     }
91     catch (BadLocationException e) {
92     }
93     return null;
94     }
95     public Position createPosition(int offs) throws BadLocationException { return content.createPosition(offs); }
96
97     /** FIXME: NOT IMPLEMENTED */
98     public Element[] getRootElements() {
99         return null;
100     }
101
102     /** FIXME: What the hell is this routine for? */
103     public void render(Runnable JavaDoc r) {
104     }
105     
106     /** Fires document change events to listeners */
107     protected void fireDocumentEvent(DocumentEvent e) {
108         for (int i = 0; i < documentListeners.size(); i++) {
109                 if (e.getType().equals(DocumentEvent.EventType.INSERT))
110                     ((DocumentListener) documentListeners.get(i)).insertUpdate(e);
111                 if (e.getType().equals(DocumentEvent.EventType.CHANGE))
112                     ((DocumentListener) documentListeners.get(i)).changedUpdate(e);
113                 if (e.getType().equals(DocumentEvent.EventType.REMOVE))
114                     ((DocumentListener) documentListeners.get(i)).removeUpdate(e);
115         }
116     }
117
118     
119     /**
120      * Sub-classes
121      */

122     public abstract class AbstractElement implements Element, MutableAttributeSet, Serializable, TreeNode {
123         public AbstractElement(Element parent, AttributeSet a) { }
124         public void dump(PrintStream psOut, int indentAmount) {}
125         public int getAttributeCount() { return 0; }
126         public boolean isDefined(Object JavaDoc attrName) { return false; }
127         public boolean isEqual(AttributeSet attr) { return false; }
128         public AttributeSet copyAttributes() { return null; }
129         public Object JavaDoc getAttribute(Object JavaDoc attrName) { return null; }
130         public Enumeration getAttributeNames() { return null; }
131         public boolean containsAttribute(Object JavaDoc name, Object JavaDoc value) { return false; }
132         public boolean containsAttributes(AttributeSet attrs) { return false; }
133         public AttributeSet getResolveParent() { return null; }
134         public void addAttribute(Object JavaDoc name, Object JavaDoc value) {}
135         public void addAttributes(AttributeSet attr) {}
136         public void removeAttribute(Object JavaDoc name) {}
137         public void removeAttributes(Enumeration names) {}
138         public void removeAttributes(AttributeSet attrs) {}
139         public void setResolveParent(AttributeSet parent) {}
140         public Document getDocument() { return null; }
141         public Element getParentElement() { return null; }
142         public AttributeSet getAttributes() { return null; }
143         public String JavaDoc getName() { return ""; }
144         public TreeNode getChildAt(int childIndex) { return null; }
145         public int getChildCount() { return 0; }
146         public TreeNode getParent() { return null; }
147         public int getIndex(TreeNode node) { return 0; }
148
149         /**
150          * Abstract methods
151          */

152         public abstract int getStartOffset();
153         public abstract int getEndOffset();
154         public abstract Element getElement(int index);
155         public abstract int getElementCount();
156         public abstract int getElementIndex(int offset);
157         public abstract boolean isLeaf();
158         public abstract boolean getAllowsChildren();
159         public abstract Enumeration children();
160     }
161
162     public class LeafElement extends AbstractElement {
163         public LeafElement(Element parent, AttributeSet a, int offs0, int offs1) { super(parent, a); }
164         public String JavaDoc toString() { return ""; }
165         public int getStartOffset() { return 0; }
166         public int getEndOffset() { return 0; }
167         public String JavaDoc getName() { return ""; }
168         public int getElementIndex(int pos) { return 0; }
169         public Element getElement(int index) { return null; }
170         public int getElementCount() { return 0; }
171         public boolean isLeaf() { return false; }
172         public boolean getAllowsChildren() { return false; }
173         public Enumeration children() { return null; }
174     }
175
176     public static class ElementEdit extends AbstractUndoableEdit implements DocumentEvent.ElementChange {
177         public ElementEdit(Element e, int index, Element[] removed, Element[] added) { super(); }
178         public Element getElement() { return null; }
179         public int getIndex() { return 0; }
180         public Element[] getChildrenRemoved() { return null; }
181         public Element[] getChildrenAdded() { return null; }
182         public void redo() throws CannotRedoException {}
183         public void undo() throws CannotUndoException {}
184     }
185
186     public class DefaultDocumentEvent extends CompoundEdit implements DocumentEvent {
187         
188         protected DocumentEvent.EventType type = null;
189         protected int offset = 0;
190         protected int len = 0;
191         
192         public DefaultDocumentEvent(int offs, int len, DocumentEvent.EventType type) {
193             super();
194             this.type = type;
195             this.offset = offs;
196             this.len = len;
197         }
198         public String JavaDoc toString() { return edits.toString(); }
199         public boolean addEdit(UndoableEdit anEdit) { return false; }
200         public void redo() throws CannotRedoException {}
201         public void undo() throws CannotUndoException {}
202         public boolean isSignificant() { return true; }
203         public String JavaDoc getPresentationName() { return ""; }
204         public String JavaDoc getUndoPresentationName() { return ""; }
205         public String JavaDoc getRedoPresentationName() { return ""; }
206         public DocumentEvent.EventType getType() { return type; }
207         public int getOffset() { return offset; }
208         public int getLength() { return len; }
209         public Document getDocument() { return AbstractDocument.this; }
210         public DocumentEvent.ElementChange getChange(Element elem) { return null; }
211     }
212
213     public class BranchElement extends AbstractElement {
214         public BranchElement(Element parent, AttributeSet attributeSet) { super(parent, attributeSet); }
215         public Element positionToElement(int pos) { return null; }
216         public void replace(int offset, int length, Element[] elems) { }
217         public String JavaDoc getName() { return ""; }
218         public int getStartOffset() { return 0; }
219         public int getEndOffset() { return 0; }
220         public Element getElement(int index) { return null; }
221         public int getElementCount() { return 0; }
222         public int getElementIndex(int offset) { return 0; }
223         public boolean isLeaf() { return false; }
224         public boolean getAllowsChildren() { return true; }
225         public Enumeration children() { return null; }
226     }
227
228     /**
229      * Interfaces:
230      */

231     public interface Content {
232         public Position createPosition(int offset) throws BadLocationException;
233         public int length();
234         public UndoableEdit insertString(int where, String JavaDoc str) throws BadLocationException;
235         public UndoableEdit remove(int where, int nitems) throws BadLocationException;
236         public String JavaDoc getString(int where, int len) throws BadLocationException;
237         public void getChars(int where, int len, Segment txt) throws BadLocationException;
238     }
239
240     public interface AttributeContext {
241         public AttributeSet addAttribute(AttributeSet old, Object JavaDoc name, Object JavaDoc value);
242         public AttributeSet addAttributes(AttributeSet old, AttributeSet attr);
243         public AttributeSet removeAttribute(AttributeSet old, Object JavaDoc name);
244         public AttributeSet removeAttributes(AttributeSet old, Enumeration names);
245         public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs);
246         public AttributeSet getEmptySet();
247         public void reclaim(AttributeSet a);
248     }
249     
250 }
251
Popular Tags