KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > text > SwingDocument


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
40
41 import javax.swing.text.DefaultStyledDocument JavaDoc;
42 import javax.swing.text.AttributeSet JavaDoc;
43 import javax.swing.text.Position JavaDoc;
44 import javax.swing.text.BadLocationException JavaDoc;
45
46 import java.util.Hashtable JavaDoc;
47
48 /** A swing implementation of the toolkit-independent EditDocumentInterface. This class is instantiated only
49  * by test code. This document must use the readers/writers locking protocol established in its superclasses.
50  * TODO: create a separate DummySwingDocument class for testing and make SwingDocument abstract.
51  * @version $Id: SwingDocument.java 4038 2006-11-22 16:20:42Z rcartwright $
52  */

53 public class SwingDocument extends DefaultStyledDocument JavaDoc implements EditDocumentInterface, AbstractDocumentInterface {
54   
55 // /** The lock state. See ReadersWritersLocking interface for documentation. */
56
// private volatile int _lockState = UNLOCKED;
57

58   /** Maps names to attribute sets */
59   final protected Hashtable JavaDoc<String JavaDoc, AttributeSet JavaDoc> _styles;
60
61   /** Determines which edits are legal on this document. */
62   protected DocumentEditCondition _condition;
63   
64   /** Lock used to protect _wrappedPosListLock in DefinitionsDocument. Placed here to ensure that it initialized before
65     * use! */

66   protected static final Object JavaDoc _wrappedPosListLock = new Object JavaDoc();
67
68   /** Creates a new document adapter for a Swing StyledDocument. TODO: convert _styles and _condition to lazily
69     * initialized volatiles as soon as support for Java 1.4 is dropped and the double-check idiom is safe. */

70   public SwingDocument() {
71     _styles = new Hashtable JavaDoc<String JavaDoc, AttributeSet JavaDoc>();
72     _condition = new DocumentEditCondition();
73   }
74
75   /** Adds the given AttributeSet as a style with the given name. It can then be used in insertString.
76    * @param name Name of the style, to be passed to insertString
77    * @param s AttributeSet to use for the style
78    */

79   public void setDocStyle(String JavaDoc name, AttributeSet JavaDoc s) {
80     _styles.put(name, s); // no locking necessary: _styles is final and Hashtable is thread-safe
81
}
82
83   /** Returns the style with the given name, or null if no such named style exists. */
84   public AttributeSet JavaDoc getDocStyle(String JavaDoc name) {
85     return _styles.get(name); // no locking necessary: _styles is final and Hashtable is thread-safe
86
}
87     
88   /** Adds the given coloring style to the styles list. Not supported in SwingDocument. */
89   public void addColoring(int start, int end, String JavaDoc style) { }
90
91   /** Gets the object which can determine whether an insert or remove edit should be applied, based on the inputs.
92    * @return an Object to determine legality of inputs
93    */

94   public DocumentEditCondition getEditCondition() { return _condition; }
95
96   /** Provides an object which can determine whether an insert or remove edit should be applied, based on the inputs.
97    * @param condition Object to determine legality of inputs
98    */

99   public void setEditCondition(DocumentEditCondition condition) {
100     acquireWriteLock();
101     try { _condition = condition; }
102     finally { releaseWriteLock(); }
103   }
104   
105   /* Clears the document. */
106   public void clear() {
107     acquireWriteLock();
108     try { remove(0, getLength()); }
109     catch(BadLocationException JavaDoc e) { throw new UnexpectedException(e); }
110     finally { releaseWriteLock(); }
111   }
112   
113
114   /** Inserts a string into the document at the given offset and the given named style, if the edit condition
115    * allows it.
116    * @param offs Offset into the document
117    * @param str String to be inserted
118    * @param style Name of the style to use. Must have been added using addStyle.
119    * @throws EditDocumentException if the offset is illegal
120    */

121   public void insertText(int offs, String JavaDoc str, String JavaDoc style) {
122     acquireWriteLock();
123     try { if (_condition.canInsertText(offs)) forceInsertText(offs, str, style); }
124     finally { releaseWriteLock(); }
125   }
126
127   /** Inserts a string into the document at the given offset and the given named style, regardless of the edit
128    * condition.
129    * @param offs Offset into the document
130    * @param str String to be inserted
131    * @param style Name of the style to use. Must have been added using addStyle.
132    * @throws EditDocumentException if the offset is illegal
133    */

134   public void forceInsertText(int offs, String JavaDoc str, String JavaDoc style) {
135     AttributeSet JavaDoc s = null;
136     if (style != null) s = getDocStyle(style);
137     /* Using a writeLock is unnecessary because insertString is already thread-safe */
138     try { super.insertString(offs, str, s); }
139     catch (BadLocationException JavaDoc e) { throw new EditDocumentException(e); }
140   }
141
142   /** Overrides superclass's insertString to impose the edit condition. The AttributeSet is ignored in the condition,
143    * which sees a null style name.
144    */

145   public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc set) throws BadLocationException JavaDoc {
146     acquireWriteLock(); // locking is used to make the test and modification atomic
147
try { if (_condition.canInsertText(offs)) super.insertString(offs, str, set); }
148     finally { releaseWriteLock(); }
149   }
150
151   /** Removes a portion of the document, if the edit condition allows it.
152    * @param offs Offset to start deleting from
153    * @param len Number of characters to remove
154    * @throws EditDocumentException if the offset or length are illegal
155    */

156   public void removeText(int offs, int len) {
157     acquireWriteLock(); // locking is used to make the test and modification atomic
158
try { if (_condition.canRemoveText(offs)) forceRemoveText(offs, len); }
159     finally { releaseWriteLock(); }
160   }
161
162   /** Removes a portion of the document, regardless of the edit condition.
163    * @param offs Offset to start deleting from
164    * @param len Number of characters to remove
165    * @throws EditDocumentException if the offset or length are illegal
166    */

167   public void forceRemoveText(int offs, int len) {
168     /* Using a writeLock is unnecessary because remove is already thread-safe */
169     try { super.remove(offs, len); }
170     catch (BadLocationException JavaDoc e) { throw new EditDocumentException(e); }
171   }
172
173   /** Overrides superclass's remove to impose the edit condition. */
174   public void remove(int offs, int len) throws BadLocationException JavaDoc {
175     acquireWriteLock(); // locking is used to make the test and modification atomic
176
try { if (_condition.canRemoveText(offs)) super.remove(offs, len); }
177     finally { releaseWriteLock(); }
178   }
179
180 // /** Returns the length of the document. */
181
// public int getDocLength() { return getLength(); } // locking is unnecessary because getLength is already thread-safe
182

183   /** Returns a portion of the document.
184    * @param offs First offset of the desired text
185    * @param len Number of characters to return
186    * @throws EditDocumentException if the offset or length are illegal
187    */

188   public String JavaDoc getDocText(int offs, int len) {
189     try { return getText(offs, len); } // locking is unnecessary because getText is already thread-safe
190
catch (BadLocationException JavaDoc e) { throw new EditDocumentException(e); }
191   }
192   
193   /** Returns entire text of this document. */
194   public String JavaDoc getText() {
195     acquireReadLock();
196     try { return getText(0, getLength()); }
197     catch (BadLocationException JavaDoc e) { throw new UnexpectedException(e); } // impossible
198
finally { releaseReadLock(); }
199   }
200   
201   /** Appends given string with specified attributes to end of this document. */
202   public void append(String JavaDoc str, AttributeSet JavaDoc set) {
203     acquireWriteLock();
204     try { insertString(getLength(), str, set); }
205     catch (BadLocationException JavaDoc e) { throw new UnexpectedException(e); } // impossible
206
finally { releaseWriteLock(); }
207   }
208   
209   /** Appends given string with specified named style to end of this document. */
210   public void append(String JavaDoc str, String JavaDoc style) { append(str, style == null ? null : getDocStyle(style)); }
211   
212   /** A SwingDocument instance does not have a default style */
213   public String JavaDoc getDefaultStyle() { return null; }
214   
215   public void print() {
216     throw new UnsupportedOperationException JavaDoc("Printing not supported");
217   }
218   
219   public Pageable JavaDoc getPageable() {
220     throw new UnsupportedOperationException JavaDoc("Printing not supported");
221   }
222   
223   /* Locking operations */
224   
225   /* Swing-style readLock(). Must be renamed because inherited writeLock is final. */
226   public /* synchronized */ void acquireReadLock() {
227 // _lockState++;
228
readLock();
229   }
230    
231   /* Swing-style readUnlock(). Must be renamed because inherited writeLock is final. */
232   public /* synchronized */ void releaseReadLock() {
233     readUnlock();
234 // _lockState--;
235
}
236
237   /** Swing-style writeLock(). Must be renamed because inherited writeLock is final. */
238   public /* synchronized */ void acquireWriteLock() {
239 // _lockState = MODIFYLOCKED;
240
writeLock();
241   }
242   
243    /** Swing-style writeUnlock(). Must be renamed because inherited writeUnlock is final.*/
244   public /* synchronized*/ void releaseWriteLock() {
245     writeUnlock();
246 // _lockState = UNLOCKED;
247
}
248   
249   /** Performs the default behavior for createPosition in DefaultStyledDocument. */
250   public Position JavaDoc createUnwrappedPosition(int offs) throws BadLocationException JavaDoc { return super.createPosition(offs); }
251   
252 // public int getLockState() { return _lockState; }
253
}
254
255
Popular Tags