1 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 44 public abstract class AbstractDocument implements Document, Serializable { 45 46 47 protected Content content = null; 48 49 protected Vector documentListeners = new Vector(); 50 51 protected Vector undoListeners = new Vector(); 52 53 protected Hashtable props = new Hashtable(2); 54 55 public abstract Element getParagraphElement(int pos); 56 57 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 getProperty(Object key) { return props.get(key); } 65 public void putProperty(Object key, Object value) { props.put(key, value); } 66 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 72 public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { 73 content.insertString(offset, str); 74 fireDocumentEvent(new DefaultDocumentEvent(offset, str.length(), DocumentEvent.EventType.INSERT)); 75 } 76 public String 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 98 public Element[] getRootElements() { 99 return null; 100 } 101 102 103 public void render(Runnable r) { 104 } 105 106 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 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 attrName) { return false; } 127 public boolean isEqual(AttributeSet attr) { return false; } 128 public AttributeSet copyAttributes() { return null; } 129 public Object getAttribute(Object attrName) { return null; } 130 public Enumeration getAttributeNames() { return null; } 131 public boolean containsAttribute(Object name, Object value) { return false; } 132 public boolean containsAttributes(AttributeSet attrs) { return false; } 133 public AttributeSet getResolveParent() { return null; } 134 public void addAttribute(Object name, Object value) {} 135 public void addAttributes(AttributeSet attr) {} 136 public void removeAttribute(Object 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 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 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 toString() { return ""; } 165 public int getStartOffset() { return 0; } 166 public int getEndOffset() { return 0; } 167 public String 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 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 getPresentationName() { return ""; } 204 public String getUndoPresentationName() { return ""; } 205 public String 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 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 231 public interface Content { 232 public Position createPosition(int offset) throws BadLocationException; 233 public int length(); 234 public UndoableEdit insertString(int where, String str) throws BadLocationException; 235 public UndoableEdit remove(int where, int nitems) throws BadLocationException; 236 public String 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 name, Object value); 242 public AttributeSet addAttributes(AttributeSet old, AttributeSet attr); 243 public AttributeSet removeAttribute(AttributeSet old, Object 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 |