1 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 31 public abstract class JavaFileBuffer extends AbstractFileBuffer { 32 33 34 protected IPath fLocation; 35 36 protected IFileStore fFileStore; 37 38 protected int fReferenceCount; 39 40 protected boolean fCanBeSaved= false; 41 42 protected IStatus fStatus; 43 44 protected long fSynchronizationStamp= IResource.NULL_STAMP; 45 46 protected int fSynchronizationContextCount; 47 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 89 protected void connected() { 90 } 91 92 public void disconnect() throws CoreException { 93 --fReferenceCount; 94 if (fReferenceCount <= 0) 95 disconnected(); 96 } 97 98 104 protected void disconnected() { 105 } 106 107 111 protected boolean isDisconnected() { 112 return fReferenceCount <= 0; 113 } 114 115 118 public IPath getLocation() { 119 return fLocation; 120 } 121 122 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 x) { 136 fManager.fireStateChangeFailed(this); 137 throw x; 138 } 139 140 fCanBeSaved= false; 141 addFileBufferContentListeners(); 142 fManager.fireDirtyStateChanged(this, fCanBeSaved); 143 } 144 } 145 146 149 public ISchedulingRule computeCommitRule() { 150 return null; 151 } 152 153 156 public boolean isDirty() { 157 return fCanBeSaved; 158 } 159 160 163 public void setDirty(boolean isDirty) { 164 fCanBeSaved= isDirty; 165 } 166 167 170 public boolean isShared() { 171 return fReferenceCount > 1; 172 } 173 174 177 public ISchedulingRule computeValidateStateRule() { 178 return null; 179 } 180 181 184 public void validateState(IProgressMonitor monitor, Object computationContext) throws CoreException { 185 } 187 188 191 public boolean isStateValidated() { 192 return true; 193 } 194 195 198 public void resetStateValidation() { 199 } 201 202 207 protected void handleFileMoved(IPath newLocation) { 208 fManager.fireUnderlyingFileMoved(this, newLocation); 209 } 210 211 217 protected void handleCoreException(CoreException exception) { 218 ILog log= FileBuffersPlugin.getDefault().getLog(); 219 log.log(exception.getStatus()); 220 } 221 222 225 public boolean isSynchronized() { 226 return fSynchronizationStamp == getModificationStamp(); 227 } 228 229 232 public long getModificationStamp() { 233 return fFileStore != null ? fFileStore.fetchInfo().getLastModified() : IResource.NULL_STAMP; 234 } 235 236 239 public void requestSynchronizationContext() { 240 ++ fSynchronizationContextCount; 241 } 242 243 246 public void releaseSynchronizationContext() { 247 -- fSynchronizationContextCount; 248 } 249 250 253 public boolean isSynchronizationContextRequested() { 254 return fSynchronizationContextCount > 0; 255 } 256 257 260 public boolean isCommitable() { 261 IFileInfo info= fFileStore.fetchInfo(); 262 return info.exists() && !info.getAttribute(EFS.ATTRIBUTE_READ_ONLY); 263 } 264 265 268 public void validationStateChanged(boolean validationState, IStatus status) { 269 } 271 } 272 | Popular Tags |