KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.ILog;
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.core.resources.IResource;
25
26 import org.eclipse.core.filebuffers.FileBuffers;
27
28 /**
29  * @since 3.0
30  */

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

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

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

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

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

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

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

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

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

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

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

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

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

198     public void resetStateValidation() {
199         // nop
200
}
201
202     /**
203      * Sends out the notification that the file serving as document input has been moved.
204      *
205      * @param newLocation the path of the new location of the file
206      */

207     protected void handleFileMoved(IPath newLocation) {
208         fManager.fireUnderlyingFileMoved(this, newLocation);
209     }
210
211     /**
212      * Defines the standard procedure to handle <code>CoreExceptions</code>. Exceptions
213      * are written to the plug-in log.
214      *
215      * @param exception the exception to be logged
216      */

217     protected void handleCoreException(CoreException exception) {
218         ILog log= FileBuffersPlugin.getDefault().getLog();
219         log.log(exception.getStatus());
220     }
221
222     /*
223      * @see org.eclipse.core.filebuffers.IFileBuffer#isSynchronized()
224      */

225     public boolean isSynchronized() {
226         return fSynchronizationStamp == getModificationStamp();
227     }
228
229     /*
230      * @see org.eclipse.core.filebuffers.IFileBuffer#getModificationStamp()
231      */

232     public long getModificationStamp() {
233         return fFileStore != null ? fFileStore.fetchInfo().getLastModified() : IResource.NULL_STAMP;
234     }
235
236     /*
237      * @see org.eclipse.core.filebuffers.IFileBuffer#requestSynchronizationContext()
238      */

239     public void requestSynchronizationContext() {
240         ++ fSynchronizationContextCount;
241     }
242
243     /*
244      * @see org.eclipse.core.filebuffers.IFileBuffer#releaseSynchronizationContext()
245      */

246     public void releaseSynchronizationContext() {
247         -- fSynchronizationContextCount;
248     }
249
250     /*
251      * @see org.eclipse.core.filebuffers.IFileBuffer#isSynchronizationContextRequested()
252      */

253     public boolean isSynchronizationContextRequested() {
254         return fSynchronizationContextCount > 0;
255     }
256
257     /*
258      * @see org.eclipse.core.filebuffers.IFileBuffer#isCommitable()
259      */

260     public boolean isCommitable() {
261         IFileInfo info= fFileStore.fetchInfo();
262         return info.exists() && !info.getAttribute(EFS.ATTRIBUTE_READ_ONLY);
263     }
264
265     /*
266      * @see org.eclipse.core.filebuffers.IStateValidationSupport#validationStateChanged(boolean, org.eclipse.core.runtime.IStatus)
267      */

268     public void validationStateChanged(boolean validationState, IStatus status) {
269         //nop
270
}
271 }
272
Popular Tags