Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8068c0bba2b26c48166aa3e2fd41e5203bd3d61e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**********************************************************************
Copyright (c) 2000, 2003 IBM Corp. and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html

Contributors:
	IBM Corporation - Initial implementation
**********************************************************************/
package org.eclipse.core.internal.filebuffers;

import org.eclipse.core.filebuffers.IFileBuffer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;


public abstract class FileBuffer implements IFileBuffer {
	
		/**
		 * Runnable encapsulating an element state change. This runnable ensures 
		 * that a element change failed message is sent out to the element state
		 * listeners in case an exception occurred.
		 */
		private class SafeFileChange implements Runnable {
				
			/**
			 * Creates a new safe runnable for the given file.
			 */
			public SafeFileChange() {
			}
					
			/** 
			 * Execute the change.
			 * Subclass responsibility.
			 * 
			 * @exception an exception in case of error
			 */
			protected void execute() throws Exception {
			}
				
			/*
			 * @see java.lang.Runnable#run()
			 */
			public void run() {
					
				if (isDisposed()) {
					fManager.fireStateChangeFailed(FileBuffer.this);
					return;
				}
					
				try {
					execute();
				} catch (Exception x) {
					fManager.fireStateChangeFailed(FileBuffer.this);
				}
			}
		}
		
		/**
		 * Synchronizes the document with external resource changes.
		 */
		private class FileSynchronizer implements IResourceChangeListener, IResourceDeltaVisitor {
				
			/** A flag indicating whether this synchronizer is installed or not. */
			private boolean fIsInstalled= false;
				
			/**
			 * Creates a new file synchronizer. Is not yet installed on a file.
			 */
			public FileSynchronizer() {
			}
						
			/**
			 * Installs the synchronizer on the file.
			 */
			public void install() {
				fFile.getWorkspace().addResourceChangeListener(this);
				fIsInstalled= true;
			}
				
			/**
			 * Uninstalls the synchronizer from the file.
			 */
			public void uninstall() {
				fFile.getWorkspace().removeResourceChangeListener(this);
				fIsInstalled= false;
			}
				
			/*
			 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
			 */
			public void resourceChanged(IResourceChangeEvent e) {
				IResourceDelta delta= e.getDelta();
				try {
					if (delta != null && fIsInstalled)
						delta.accept(this);
				} catch (CoreException x) {
					handleCoreException(x); 
				}
			}
				
			/*
			 * @see IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
			 */
			public boolean visit(IResourceDelta delta) throws CoreException {
								
				if (delta != null && fFile.equals(delta.getResource())) {
						
					Runnable runnable= null;
						
					switch (delta.getKind()) {
						case IResourceDelta.CHANGED:
							if ((IResourceDelta.CONTENT & delta.getFlags()) != 0) {
								if (!isDisposed() && !fCanBeSaved && fFile.isSynchronized(IFile.DEPTH_ZERO)) {
									runnable= new SafeFileChange() {
										protected void execute() throws Exception {
											if (fModificationStamp != fFile.getModificationStamp())
												handleFileContentChanged();
										}
									};
								}
							}
							break;
						case IResourceDelta.REMOVED:
							if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) {
								final IPath path= delta.getMovedToPath();
								runnable= new SafeFileChange() {
									protected void execute() throws Exception {
										handleFileMoved(path);
									}
								};
							} else {
								if (!isDisposed() && !fCanBeSaved) {
									runnable= new SafeFileChange() {
										protected void execute() throws Exception {
											handleFileDeleted();
										}
									};
								}
							}
							break;
					}
						
					if (runnable != null)
						update(runnable);
				}
					
				return true; // because we are sitting on files anyway
			}
				
			/**
			 * Posts the update code "behind" the running operation.
			 *
			 * @param runnable the update code
			 */
			protected void update(Runnable runnable) {
				if (runnable instanceof SafeFileChange)
					fManager.fireStateChanging(FileBuffer.this);
				runnable.run();			
			}
		}
		
		
		
	/** The element for which the info is stored */
	protected IFile fFile;
	/** How often the element has been connected */
	protected int fReferenceCount;
	/** Can the element be saved */
	protected boolean fCanBeSaved= false;
	/** Has element state been validated */
	protected boolean fIsStateValidated= false;
	/** The status of this element */
	protected IStatus fStatus;
	/** The file synchronizer. */
	protected FileSynchronizer fFileSynchronizer;
	/** The time stamp at which this provider changed the file. */
	protected long fModificationStamp= IFile.NULL_STAMP;

	/** The text file buffer manager */
	protected TextFileBufferManager fManager;
	
	
	
	
	public FileBuffer(TextFileBufferManager manager) {
		super();
		fManager= manager;
	}

	abstract protected void handleFileContentChanged();
	
	abstract protected void addFileBufferContentListeners();
	
	abstract protected void removeFileBufferContentListeners();
	
	abstract protected void initializeFileBufferContent(IProgressMonitor monitor) throws CoreException;
	
	abstract protected void commitFileBufferContent(IProgressMonitor monitor, boolean overwrite) throws CoreException;
	
	
	public void create(IFile file, IProgressMonitor monitor) throws CoreException {
		fFile= file;
		fFileSynchronizer= new FileSynchronizer();
		refreshFile(monitor);
		
		initializeFileBufferContent(monitor);
		addFileBufferContentListeners();
	}
	
	public void connect() {
		++ fReferenceCount;
		if (fReferenceCount == 1)
			fFileSynchronizer.install();
	}
	
	public void disconnect() throws CoreException {
		-- fReferenceCount;
		if (fReferenceCount == 0) {
			if (fFileSynchronizer != null)
				fFileSynchronizer.uninstall();
			fFileSynchronizer= null;
		}
	}
	
	protected boolean isDisposed() {
		return fFileSynchronizer == null;
	}

	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#getUnderlyingFile()
	 */
	public IFile getUnderlyingFile() {
		return fFile;
	}

	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#commit(org.eclipse.core.runtime.IProgressMonitor, boolean)
	 */
	public void commit(IProgressMonitor monitor, boolean overwrite) throws CoreException {
		if (!isDisposed() && fCanBeSaved) {
			commitFileBufferContent(monitor, overwrite);
			fCanBeSaved= false;
			addFileBufferContentListeners();
			fManager.fireDirtyStateChanged(this, fCanBeSaved);
		}
	}

	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#isDirty()
	 */
	public boolean isDirty() {
		return fCanBeSaved;
	}
	
	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#isShared()
	 */
	public boolean isShared() {
		return fReferenceCount > 1;
	}

	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#validateState(org.eclipse.core.runtime.IProgressMonitor, java.lang.Object)
	 */
	public void validateState(IProgressMonitor monitor, Object computationContext) throws CoreException {
		if (!isDisposed() && !fIsStateValidated)  {
			
			if (fFile.isReadOnly()) {
				IWorkspace workspace= fFile.getWorkspace();
				fStatus= workspace.validateEdit(new IFile[] { fFile }, computationContext);
			}
			fIsStateValidated= true;
			fManager.fireStateValidationChanged(this, fIsStateValidated);
		}
	}

	/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#isStateValidated()
	 */
	public boolean isStateValidated() {
		return fIsStateValidated;
	}

	/**
	 * Sends out the notification that the file serving as document input has been moved.
	 * 
	 * @param newLocation the path of the new location of the file
	 */
	protected void handleFileMoved(IPath newLocation) {
		IWorkspace workspace=fFile.getWorkspace();
		IFile newFile= workspace.getRoot().getFile(newLocation);
		fManager.fireUnderlyingFileMoved(this, newFile);
	}
	
	/**
	 * Sends out the notification that the file serving as document input has been deleted.
	 */
	protected void handleFileDeleted() {
		fManager.fireUnderlyingFileDeleted(this);
	}
	
	/**
	 * Refreshes the given  file.
	 */
	protected void refreshFile(IProgressMonitor monitor) {
		try {
			fFile.refreshLocal(IFile.DEPTH_INFINITE, monitor);
		} catch (OperationCanceledException x) {
		} catch (CoreException x) {
			handleCoreException(x);
		}
	}

	/**
	 * Defines the standard procedure to handle <code>CoreExceptions</code>. Exceptions
	 * are written to the plug-in log.
	 *
	 * @param exception the exception to be logged
	 * @param message the message to be logged
	 */
	protected void handleCoreException(CoreException exception) {
		ILog log= Platform.getPlugin(FileBuffersPlugin.PLUGIN_ID).getLog();
		log.log(exception.getStatus());
	}
}

Back to the top