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 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 29 public abstract class FileStoreFileBuffer extends AbstractFileBuffer { 30 31 32 protected IPath fLocation; 33 34 protected int fReferenceCount; 35 36 protected boolean fCanBeSaved= false; 37 38 protected IStatus fStatus; 39 40 protected long fSynchronizationStamp= IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP; 41 42 protected int fSynchronizationContextCount; 43 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 90 protected void connected() { 91 } 92 93 public void disconnect() throws CoreException { 94 --fReferenceCount; 95 if (fReferenceCount <= 0) 96 disconnected(); 97 } 98 99 105 protected void disconnected() { 106 } 107 108 112 protected boolean isDisconnected() { 113 return fReferenceCount <= 0; 114 } 115 116 119 public IPath getLocation() { 120 return fLocation; 121 } 122 123 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 x) { 137 fManager.fireStateChangeFailed(this); 138 throw x; 139 } 140 141 fCanBeSaved= false; 142 addFileBufferContentListeners(); 143 fManager.fireDirtyStateChanged(this, fCanBeSaved); 144 } 145 } 146 147 150 public ISchedulingRule computeCommitRule() { 151 return null; 152 } 153 154 157 public boolean isDirty() { 158 return fCanBeSaved; 159 } 160 161 164 public void setDirty(boolean isDirty) { 165 fCanBeSaved= isDirty; 166 } 167 168 171 public boolean isShared() { 172 return fReferenceCount > 1; 173 } 174 175 178 public ISchedulingRule computeValidateStateRule() { 179 return null; 180 } 181 182 185 public void validateState(IProgressMonitor monitor, Object computationContext) throws CoreException { 186 } 188 189 192 public boolean isStateValidated() { 193 return true; 194 } 195 196 199 public void resetStateValidation() { 200 } 202 203 206 public boolean isSynchronized() { 207 return fSynchronizationStamp == getModificationStamp(); 208 } 209 210 213 public void requestSynchronizationContext() { 214 ++ fSynchronizationContextCount; 215 } 216 217 220 public void releaseSynchronizationContext() { 221 -- fSynchronizationContextCount; 222 } 223 224 227 public boolean isSynchronizationContextRequested() { 228 return fSynchronizationContextCount > 0; 229 } 230 231 234 public boolean isCommitable() { 235 IFileInfo info= fFileStore.fetchInfo(); 236 return info.exists() && !info.getAttribute(EFS.ATTRIBUTE_READ_ONLY); 237 } 238 239 242 public void validationStateChanged(boolean validationState, IStatus status) { 243 } 245 } 246 | Popular Tags |