KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filebuffers > SynchronizableDocument


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.filebuffers;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.BadPositionCategoryException;
15 import org.eclipse.jface.text.Document;
16 import org.eclipse.jface.text.ISynchronizable;
17 import org.eclipse.jface.text.Position;
18
19
20 /**
21  * Document that can be synchronized with a lock object.
22  * <p>
23  * Initially no locking takes place.
24  * </p>
25  *
26  * @since 3.2
27  */

28 public class SynchronizableDocument extends Document implements ISynchronizable {
29     
30     private Object JavaDoc fLockObject;
31     
32     /*
33      * @see org.eclipse.jface.text.ISynchronizable#setLockObject(java.lang.Object)
34      */

35     public synchronized void setLockObject(Object JavaDoc lockObject) {
36         fLockObject= lockObject;
37     }
38
39     /*
40      * @see org.eclipse.jface.text.ISynchronizable#getLockObject()
41      */

42     public synchronized Object JavaDoc getLockObject() {
43         return fLockObject;
44     }
45     
46     /*
47      * @see IDocumentExtension#startSequentialRewrite(boolean)
48      */

49     public void startSequentialRewrite(boolean normalized) {
50         Object JavaDoc lockObject= getLockObject();
51         if (lockObject == null) {
52             super.startSequentialRewrite(normalized);
53             return;
54         }
55         synchronized (lockObject) {
56             super.startSequentialRewrite(normalized);
57         }
58     }
59
60     /*
61      * @see IDocumentExtension#stopSequentialRewrite()
62      */

63     public void stopSequentialRewrite() {
64         Object JavaDoc lockObject= getLockObject();
65         if (lockObject == null) {
66             super.stopSequentialRewrite();
67             return;
68         }
69         synchronized (lockObject) {
70             super.stopSequentialRewrite();
71         }
72     }
73     
74     /*
75      * @see IDocument#get()
76      */

77     public String JavaDoc get() {
78         Object JavaDoc lockObject= getLockObject();
79         if (lockObject == null) {
80             return super.get();
81         }
82         synchronized (lockObject) {
83             return super.get();
84         }
85     }
86     
87     /*
88      * @see IDocument#get(int, int)
89      */

90     public String JavaDoc get(int offset, int length) throws BadLocationException {
91         Object JavaDoc lockObject= getLockObject();
92         if (lockObject == null) {
93             return super.get(offset, length);
94         }
95         synchronized (lockObject) {
96             return super.get(offset, length);
97         }
98     }
99     
100     /*
101      * @see IDocument#getChar(int)
102      */

103     public char getChar(int offset) throws BadLocationException {
104         Object JavaDoc lockObject= getLockObject();
105         if (lockObject == null) {
106             return super.getChar(offset);
107         }
108         synchronized (lockObject) {
109             return super.getChar(offset);
110         }
111     }
112     
113     /*
114      * @see org.eclipse.jface.text.IDocumentExtension4#getModificationStamp()
115      * @since 3.1
116      */

117     public long getModificationStamp() {
118         Object JavaDoc lockObject= getLockObject();
119         if (lockObject == null) {
120             return super.getModificationStamp();
121         }
122         synchronized (lockObject) {
123             return super.getModificationStamp();
124         }
125     }
126     
127     /*
128      * @see IDocument#replace(int, int, String)
129      */

130     public void replace(int offset, int length, String JavaDoc text) throws BadLocationException {
131         Object JavaDoc lockObject= getLockObject();
132         if (lockObject == null) {
133             super.replace(offset, length, text);
134             return;
135         }
136         synchronized (lockObject) {
137             super.replace(offset, length, text);
138         }
139     }
140     
141     /*
142      * @see IDocumentExtension4#replace(int, int, String, long)
143      */

144     public void replace(int offset, int length, String JavaDoc text, long modificationStamp) throws BadLocationException {
145         Object JavaDoc lockObject= getLockObject();
146         if (lockObject == null) {
147             super.replace(offset, length, text, modificationStamp);
148             return;
149         }
150         synchronized (lockObject) {
151             super.replace(offset, length, text, modificationStamp);
152         }
153     }
154     
155     /*
156      * @see IDocument#set(String)
157      */

158     public void set(String JavaDoc text) {
159         Object JavaDoc lockObject= getLockObject();
160         if (lockObject == null) {
161             super.set(text);
162             return;
163         }
164         synchronized (lockObject) {
165             super.set(text);
166         }
167     }
168     
169     /*
170      * @see IDocumentExtension4#set(String, long)
171      */

172     public void set(String JavaDoc text, long modificationStamp) {
173         Object JavaDoc lockObject= getLockObject();
174         if (lockObject == null) {
175             super.set(text, modificationStamp);
176             return;
177         }
178         synchronized (lockObject) {
179             super.set(text, modificationStamp);
180         }
181     }
182     
183     /*
184      * @see org.eclipse.jface.text.AbstractDocument#addPosition(java.lang.String, org.eclipse.jface.text.Position)
185      */

186     public void addPosition(String JavaDoc category, Position position) throws BadLocationException, BadPositionCategoryException {
187         Object JavaDoc lockObject= getLockObject();
188         if (lockObject == null) {
189             super.addPosition(category, position);
190             return;
191         }
192         synchronized (lockObject) {
193             super.addPosition(category, position);
194         }
195     }
196     
197     /*
198      * @see org.eclipse.jface.text.AbstractDocument#removePosition(java.lang.String, org.eclipse.jface.text.Position)
199      */

200     public void removePosition(String JavaDoc category, Position position) throws BadPositionCategoryException {
201         Object JavaDoc lockObject= getLockObject();
202         if (lockObject == null) {
203             super.removePosition(category, position);
204             return;
205         }
206         synchronized (lockObject) {
207             super.removePosition(category, position);
208         }
209     }
210     
211     /*
212      * @see org.eclipse.jface.text.AbstractDocument#getPositions(java.lang.String)
213      */

214     public Position[] getPositions(String JavaDoc category) throws BadPositionCategoryException {
215         Object JavaDoc lockObject= getLockObject();
216         if (lockObject == null) {
217             return super.getPositions(category);
218         }
219         synchronized (lockObject) {
220             return super.getPositions(category);
221         }
222     }
223 }
224
Popular Tags