1 33 34 package edu.rice.cs.util.text; 35 36 import edu.rice.cs.util.UnexpectedException; 37 import static edu.rice.cs.util.text.AbstractDocumentInterface.*; 38 39 import java.awt.print.Pageable ; 40 41 import javax.swing.text.DefaultStyledDocument ; 42 import javax.swing.text.AttributeSet ; 43 import javax.swing.text.Position ; 44 import javax.swing.text.BadLocationException ; 45 46 import java.util.Hashtable ; 47 48 53 public class SwingDocument extends DefaultStyledDocument implements EditDocumentInterface, AbstractDocumentInterface { 54 55 58 59 final protected Hashtable <String , AttributeSet > _styles; 60 61 62 protected DocumentEditCondition _condition; 63 64 66 protected static final Object _wrappedPosListLock = new Object (); 67 68 70 public SwingDocument() { 71 _styles = new Hashtable <String , AttributeSet >(); 72 _condition = new DocumentEditCondition(); 73 } 74 75 79 public void setDocStyle(String name, AttributeSet s) { 80 _styles.put(name, s); } 82 83 84 public AttributeSet getDocStyle(String name) { 85 return _styles.get(name); } 87 88 89 public void addColoring(int start, int end, String style) { } 90 91 94 public DocumentEditCondition getEditCondition() { return _condition; } 95 96 99 public void setEditCondition(DocumentEditCondition condition) { 100 acquireWriteLock(); 101 try { _condition = condition; } 102 finally { releaseWriteLock(); } 103 } 104 105 106 public void clear() { 107 acquireWriteLock(); 108 try { remove(0, getLength()); } 109 catch(BadLocationException e) { throw new UnexpectedException(e); } 110 finally { releaseWriteLock(); } 111 } 112 113 114 121 public void insertText(int offs, String str, String style) { 122 acquireWriteLock(); 123 try { if (_condition.canInsertText(offs)) forceInsertText(offs, str, style); } 124 finally { releaseWriteLock(); } 125 } 126 127 134 public void forceInsertText(int offs, String str, String style) { 135 AttributeSet s = null; 136 if (style != null) s = getDocStyle(style); 137 138 try { super.insertString(offs, str, s); } 139 catch (BadLocationException e) { throw new EditDocumentException(e); } 140 } 141 142 145 public void insertString(int offs, String str, AttributeSet set) throws BadLocationException { 146 acquireWriteLock(); try { if (_condition.canInsertText(offs)) super.insertString(offs, str, set); } 148 finally { releaseWriteLock(); } 149 } 150 151 156 public void removeText(int offs, int len) { 157 acquireWriteLock(); try { if (_condition.canRemoveText(offs)) forceRemoveText(offs, len); } 159 finally { releaseWriteLock(); } 160 } 161 162 167 public void forceRemoveText(int offs, int len) { 168 169 try { super.remove(offs, len); } 170 catch (BadLocationException e) { throw new EditDocumentException(e); } 171 } 172 173 174 public void remove(int offs, int len) throws BadLocationException { 175 acquireWriteLock(); try { if (_condition.canRemoveText(offs)) super.remove(offs, len); } 177 finally { releaseWriteLock(); } 178 } 179 180 183 188 public String getDocText(int offs, int len) { 189 try { return getText(offs, len); } catch (BadLocationException e) { throw new EditDocumentException(e); } 191 } 192 193 194 public String getText() { 195 acquireReadLock(); 196 try { return getText(0, getLength()); } 197 catch (BadLocationException e) { throw new UnexpectedException(e); } finally { releaseReadLock(); } 199 } 200 201 202 public void append(String str, AttributeSet set) { 203 acquireWriteLock(); 204 try { insertString(getLength(), str, set); } 205 catch (BadLocationException e) { throw new UnexpectedException(e); } finally { releaseWriteLock(); } 207 } 208 209 210 public void append(String str, String style) { append(str, style == null ? null : getDocStyle(style)); } 211 212 213 public String getDefaultStyle() { return null; } 214 215 public void print() { 216 throw new UnsupportedOperationException ("Printing not supported"); 217 } 218 219 public Pageable getPageable() { 220 throw new UnsupportedOperationException ("Printing not supported"); 221 } 222 223 224 225 226 public void acquireReadLock() { 227 readLock(); 229 } 230 231 232 public void releaseReadLock() { 233 readUnlock(); 234 } 236 237 238 public void acquireWriteLock() { 239 writeLock(); 241 } 242 243 244 public void releaseWriteLock() { 245 writeUnlock(); 246 } 248 249 250 public Position createUnwrappedPosition(int offs) throws BadLocationException { return super.createPosition(offs); } 251 252 } 254 255 | Popular Tags |