KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.filesystem.EFS;
14 import org.eclipse.core.filesystem.IFileInfo;
15 import org.eclipse.core.filesystem.IFileStore;
16 import org.eclipse.core.filesystem.URIUtil;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.jobs.ISchedulingRule;
23
24 import org.eclipse.jface.text.IDocumentExtension4;
25
26 /**
27  * @since 3.3 (previously available as JavaFileBuffer since 3.0)
28  */

29 public abstract class FileStoreFileBuffer extends AbstractFileBuffer {
30
31     /** The location */
32     protected IPath fLocation;
33     /** How often the element has been connected */
34     protected int fReferenceCount;
35     /** Can the element be saved */
36     protected boolean fCanBeSaved= false;
37     /** The status of this element */
38     protected IStatus fStatus;
39     /** The time stamp at which this buffer synchronized with the underlying file. */
40     protected long fSynchronizationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP;
41     /** How often the synchronization context has been requested */
42     protected int fSynchronizationContextCount;
43     /** The text file buffer manager */
44     protected TextFileBufferManager fManager;
45
46
47     public FileStoreFileBuffer(TextFileBufferManager manager) {
48         super();
49         fManager= manager;
50     }
51
52     abstract protected void addFileBufferContentListeners();
53
54     abstract protected void removeFileBufferContentListeners();
55
56     abstract protected void initializeFileBufferContent(IProgressMonitor monitor) throws CoreException;
57
58     abstract protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException;
59
60     public void create(IFileStore fileStore, IProgressMonitor monitor) throws CoreException {
61         IFileInfo info= fileStore.fetchInfo();
62         fFileStore= fileStore;
63         if (fLocation == null)
64             fLocation= URIUtil.toPath(fileStore.toURI());
65         
66         initializeFileBufferContent(monitor);
67         if (info.exists())
68             fSynchronizationStamp= info.getLastModified();
69         
70         addFileBufferContentListeners();
71     }
72     
73     public void create(IPath location, IProgressMonitor monitor) throws CoreException {
74         fLocation= location;
75         create(EFS.getStore(URIUtil.toURI(getLocation())), monitor);
76     }
77
78     public void connect() {
79         ++ fReferenceCount;
80         if (fReferenceCount == 1)
81             connected();
82     }
83
84     /**
85      * Called when this file buffer has been connected. This is the case when
86      * there is exactly one connection.
87      * <p>
88      * Clients may extend this method.
89      */

90     protected void connected() {
91     }
92
93     public void disconnect() throws CoreException {
94         --fReferenceCount;
95         if (fReferenceCount <= 0)
96             disconnected();
97     }
98
99     /**
100      * Called when this file buffer has been disconnected. This is the case when
101      * the number of connections drops below <code>1</code>.
102      * <p>
103      * Clients may extend this method.
104      */

105     protected void disconnected() {
106     }
107
108     /*
109      * @see org.eclipse.core.internal.filebuffers.AbstractFileBuffer#isDisconnected()
110      * @since 3.1
111      */

112     protected boolean isDisconnected() {
113         return fReferenceCount <= 0;
114     }
115
116     /*
117      * @see org.eclipse.core.filebuffers.IFileBuffer#getLocation()
118      */

119     public IPath getLocation() {
120         return fLocation;
121     }
122     
123     /*
124      * @see org.eclipse.core.filebuffers.IFileBuffer#commit(org.eclipse.core.runtime.IProgressMonitor, boolean)
125      */

126     public void commit(IProgressMonitor monitor, boolean overwrite) throws CoreException {
127         if (!isDisconnected() && fCanBeSaved) {
128
129             fManager.fireStateChanging(this);
130
131             try {
132                 commitFileBufferContent(monitor, overwrite);
133             } catch (CoreException x) {
134                 fManager.fireStateChangeFailed(this);
135                 throw x;
136             } catch (RuntimeException JavaDoc x) {
137                 fManager.fireStateChangeFailed(this);
138                 throw x;
139             }
140
141             fCanBeSaved= false;
142             addFileBufferContentListeners();
143             fManager.fireDirtyStateChanged(this, fCanBeSaved);
144         }
145     }
146
147     /*
148      * @see org.eclipse.core.filebuffers.IFileBuffer#computeCommitRule()
149      */

150     public ISchedulingRule computeCommitRule() {
151         return null;
152     }
153
154     /*
155      * @see org.eclipse.core.filebuffers.IFileBuffer#isDirty()
156      */

157     public boolean isDirty() {
158         return fCanBeSaved;
159     }
160
161     /*
162      * @see org.eclipse.core.filebuffers.IFileBuffer#setDirty(boolean)
163      */

164     public void setDirty(boolean isDirty) {
165         fCanBeSaved= isDirty;
166     }
167
168     /*
169      * @see org.eclipse.core.filebuffers.IFileBuffer#isShared()
170      */

171     public boolean isShared() {
172         return fReferenceCount > 1;
173     }
174
175     /*
176      * @see org.eclipse.core.filebuffers.IFileBuffer#computeValidateStateRule()
177      */

178     public ISchedulingRule computeValidateStateRule() {
179         return null;
180     }
181
182     /*
183      * @see org.eclipse.core.filebuffers.IFileBuffer#validateState(org.eclipse.core.runtime.IProgressMonitor, java.lang.Object)
184      */

185     public void validateState(IProgressMonitor monitor, Object JavaDoc computationContext) throws CoreException {
186         // nop
187
}
188
189     /*
190      * @see org.eclipse.core.filebuffers.IFileBuffer#isStateValidated()
191      */

192     public boolean isStateValidated() {
193         return true;
194     }
195
196     /*
197      * @see org.eclipse.core.filebuffers.IFileBuffer#resetStateValidation()
198      */

199     public void resetStateValidation() {
200         // nop
201
}
202
203     /*
204      * @see org.eclipse.core.filebuffers.IFileBuffer#isSynchronized()
205      */

206     public boolean isSynchronized() {
207         return fSynchronizationStamp == getModificationStamp();
208     }
209
210     /*
211      * @see org.eclipse.core.filebuffers.IFileBuffer#requestSynchronizationContext()
212      */

213     public void requestSynchronizationContext() {
214         ++ fSynchronizationContextCount;
215     }
216
217     /*
218      * @see org.eclipse.core.filebuffers.IFileBuffer#releaseSynchronizationContext()
219      */

220     public void releaseSynchronizationContext() {
221         -- fSynchronizationContextCount;
222     }
223
224     /*
225      * @see org.eclipse.core.filebuffers.IFileBuffer#isSynchronizationContextRequested()
226      */

227     public boolean isSynchronizationContextRequested() {
228         return fSynchronizationContextCount > 0;
229     }
230
231     /*
232      * @see org.eclipse.core.filebuffers.IFileBuffer#isCommitable()
233      */

234     public boolean isCommitable() {
235         IFileInfo info= fFileStore.fetchInfo();
236         return info.exists() && !info.getAttribute(EFS.ATTRIBUTE_READ_ONLY);
237     }
238
239     /*
240      * @see org.eclipse.core.filebuffers.IStateValidationSupport#validationStateChanged(boolean, org.eclipse.core.runtime.IStatus)
241      */

242     public void validationStateChanged(boolean validationState, IStatus status) {
243         //nop
244
}
245 }
246
Popular Tags