Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d343506028d97e9374e934542c90e82b92cd09ba (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (c) 2002 IBM Corporation 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 - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.eclipse.ui.editors.text.ILocationProvider;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IElementStateListener;
import org.eclipse.wst.sse.core.FactoryRegistry;
import org.eclipse.wst.sse.core.IFactoryRegistry;
import org.eclipse.wst.sse.core.IModelManager;
import org.eclipse.wst.sse.core.IModelManagerPlugin;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.ui.extensions.breakpoint.IExtendedStorageEditorInput;
import org.eclipse.wst.sse.ui.nls.ResourceHandler;
import org.eclipse.wst.sse.ui.registry.AdapterFactoryProvider;
import org.eclipse.wst.sse.ui.registry.AdapterFactoryRegistry;


/**
 * A TextFileDocumentProvider that is IStructuredModel aware. This
 * implementation is marked as final since it will change significantly in C4,
 * possibly even be removed.
 */
public final class TextFileModelProvider extends TextFileDocumentProvider implements IModelProvider {

	/**
	 * Collection of info that goes with a model.
	 */
	protected class ModelInfo {
		public IAnnotationModel fAnnotationModel;
		public IEditorInput fElement;
		public boolean fShouldReleaseOnInfoDispose;
		public IStructuredModel fStructuredModel;

		public ModelInfo(IStructuredModel structuredModel, IEditorInput element, IAnnotationModel model, boolean selfCreated) {
			fElement = element;
			fStructuredModel = structuredModel;
			fAnnotationModel = model;
			fShouldReleaseOnInfoDispose = selfCreated;
		}
	}

	private static TextFileModelProvider fInstance = null;
	private static IModelManager fModelManager;

	public synchronized static TextFileModelProvider getInstance() {
		if (fInstance == null)
			fInstance = new TextFileModelProvider();
		return fInstance;
	}

	/**
	 * Utility method also used in subclasses
	 */
	protected static IModelManager getModelManager() {
		if (fModelManager == null) {
			// get the model manager from the plugin
			// note: we can use the static "ID" variable, since we pre-req
			// that plugin
			IModelManagerPlugin plugin = (IModelManagerPlugin) Platform.getPlugin(IModelManagerPlugin.ID);
			fModelManager = plugin.getModelManager();
		}
		return fModelManager;
	}

	protected IElementStateListener fInternalListener;
	/** IStructuredModel information of all connected elements */
	private Map fModelInfoMap = new HashMap();

	protected TextFileModelProvider() {
		super();
	}

	public void addProviderFactories(IStructuredModel structuredModel) {
		// (mostly) COPIED FROM FileModelProvider
		EditorPlugin plugin = ((EditorPlugin) Platform.getPlugin(EditorPlugin.ID));
		AdapterFactoryRegistry adapterRegistry = plugin.getAdapterFactoryRegistry();
		Iterator adapterFactoryList = adapterRegistry.getAdapterFactories();

		IFactoryRegistry factoryRegistry = structuredModel.getFactoryRegistry();
		if (factoryRegistry == null) {
			factoryRegistry = new FactoryRegistry();
			structuredModel.setFactoryRegistry(factoryRegistry);
		}

		while (adapterFactoryList.hasNext()) {
			try {
				AdapterFactoryProvider provider = (AdapterFactoryProvider) adapterFactoryList.next();
				if (provider.isFor(structuredModel.getModelHandler())) {
					provider.addAdapterFactories(structuredModel);
				}
			}
			catch (Exception e) {
				Logger.logException(e);
			}
		}
		// END COPY FileModelProvider
	}

	protected String computePath(IEditorInput input) {
		/**
		 * Typically CVS will return a path of "filename.ext" and the input's
		 * name will be "filename.ext version". The path must be used to load
		 * the model so that the suffix will be available to compute the
		 * contentType properly. The editor input name can then be set as the
		 * base location for display on the editor title bar.
		 */
		String path = null;
		boolean addHash = false;
		try {
			if (input instanceof IStorageEditorInput) {
				IStorage storage = ((IStorageEditorInput) input).getStorage();
				if (storage != null) {
					IPath storagePath = storage.getFullPath();
					String name = storage.getName();
					// if either the name or storage path are null or they are
					// identical, add a hash to it to guarantee uniqueness
					addHash = storagePath == null || storagePath.toString().equals(name);
					if (storagePath != null)
						path = storagePath.makeAbsolute().toString();
					if (path == null)
						path = name;
				}
			}
			else if (input.getAdapter(ILocationProvider.class) != null) {
				IPath locationPath = ((ILocationProvider) input.getAdapter(ILocationProvider.class)).getPath(input);
				if (locationPath != null) {
					path = locationPath.toString();
				}
			}

		}
		catch (CoreException e) {
			Logger.logException(e);
			return null;
		}
		finally {
			if (path == null)
				path = ""; //$NON-NLS-1$
		}
		if (addHash)
			path = input.hashCode() + path;
		return path;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#commitFileBuffer(org.eclipse.core.runtime.IProgressMonitor,
	 *      org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo,
	 *      boolean)
	 */
	protected void commitFileBuffer(IProgressMonitor monitor, FileInfo info, boolean overwrite) throws CoreException {
		IStructuredModel model = getModel(info.fElement);
		if (model != null) {
			String contents = model.getStructuredDocument().get();
			info.fTextFileBuffer.getDocument().set(contents);
		}
		super.commitFileBuffer(monitor, info, overwrite);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createAnnotationModel(org.eclipse.core.resources.IFile)
	 */
	protected IAnnotationModel createAnnotationModel(IFile file) {
		String id = file.getFullPath().toString();
		IAnnotationModel model = new StructuredResourceMarkerAnnotationModel(file, id);
		IEditorInput input = null;
		Iterator i = getConnectedElementsIterator();
		while (input == null && i.hasNext()) {
			FileInfo info = (FileInfo) i.next();
			if (getSystemFile(info).equals(file)) {
				input = (IEditorInput) info.fElement;
			}
		}
		ModelInfo modelInfo = getModelInfoFor(input);
		modelInfo.fAnnotationModel = model;
		return model;
	}

	public IDocument getDocument(Object element) {
		return getModel(element).getStructuredDocument();
	}

	/**
	 * Also create ModelInfo - extra resource synchronization classes should
	 * be stored within the ModelInfo
	 */
	protected FileInfo createFileInfo(Object element) throws CoreException {
		FileInfo info = super.createFileInfo(element);
		// create the corresponding ModelInfo if necessary
		if (getModelInfoFor((IEditorInput) element) == null) {
			createModelInfo((IEditorInput) element);
		}

		return info;
	}

	public void createModelInfo(IEditorInput input) {
		if (getModelInfoFor(input) == null) {
			IStructuredModel structuredModel = selfCreateModel(input);
			createModelInfo(input, structuredModel, true);
		}
	}

	/**
	 * To be used when model is provided to us, ensures that when setInput is
	 * used on this input, the given model will be used.
	 */
	public void createModelInfo(IEditorInput input, IStructuredModel structuredModel, boolean releaseModelOnDisconnect) {
		// we have to make sure factories are added, whether we created or
		// not.
		if (getModelInfoFor(input) != null || getModelInfoFor(structuredModel) != null)
			return;

		if (input instanceof IExtendedStorageEditorInput) {
			((IExtendedStorageEditorInput) input).addElementStateListener(fInternalListener);
		}

		addProviderFactories(structuredModel);

		ModelInfo modelInfo = new ModelInfo(structuredModel, input, null, releaseModelOnDisconnect);
		// to help with downstream usage, create a dummy/"null" annotation model
		if (!(input instanceof IFileEditorInput)) {
			modelInfo.fAnnotationModel = new AnnotationModel();
		}
		fModelInfoMap.put(input, modelInfo);
	}

	protected void disposeFileInfo(Object element, FileInfo info) {
		if (element instanceof IEditorInput) {
			IEditorInput input = (IEditorInput) element;
			ModelInfo modelInfo = getModelInfoFor(input);
			disposeModelInfo(modelInfo);
		}
		super.disposeFileInfo(element, info);
	}

	/**
	 * disconnect from this model info
	 * 
	 * @param info
	 */
	public void disposeModelInfo(ModelInfo info) {
		if (info.fElement instanceof IStorageEditorInput) {
			if (info.fElement instanceof IExtendedStorageEditorInput) {
				((IExtendedStorageEditorInput) info.fElement).removeElementStateListener(fInternalListener);
			}
			if (info.fShouldReleaseOnInfoDispose) {
				info.fStructuredModel.releaseFromEdit();
			}
		}
		fModelInfoMap.remove(info.fElement);
	}

	/**
	 * @see org.eclipse.ui.texteditor.AbstractDocumentProvider#doSaveDocument(org.eclipse.core.runtime.IProgressMonitor,
	 *      java.lang.Object, org.eclipse.jface.text.IDocument, boolean)
	 */
	protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
		new FileDocumentProvider().saveDocument(monitor, element, document, overwrite);
	}

	/**
	 * Overridden to use ModelInfo's annotation model
	 * 
	 * @see org.eclipse.ui.texteditor.IDocumentProvider#getAnnotationModel(java.lang.Object)
	 */
	public IAnnotationModel getAnnotationModel(Object element) {
		// override behavior an retrieve the annotation model from the model
		// info
		ModelInfo info = getModelInfoFor((IEditorInput) element);
		if (info != null)
			return info.fAnnotationModel;
		return null;
	}

	protected IEditorInput getInputFor(IDocument document) {
		IStructuredModel model = getModelManager().getExistingModelForRead(document);
		IEditorInput input = getInputFor(model);
		model.releaseFromRead();
		return input;
	}

	protected IEditorInput getInputFor(IStructuredModel structuredModel) {
		IEditorInput result = null;
		ModelInfo info = getModelInfoFor(structuredModel);
		if (info != null)
			result = info.fElement;
		return result;
	}

	public IStructuredModel getModel(IEditorInput element) {
		IStructuredModel result = null;
		ModelInfo info = getModelInfoFor(element);
		if (info != null) {
			result = info.fStructuredModel;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 */
	public IStructuredModel getModel(Object element) {
		if (element instanceof IEditorInput)
			return getModel((IEditorInput) element);
		return null;
	}

	protected ModelInfo getModelInfoFor(IEditorInput element) {
		ModelInfo result = (ModelInfo) fModelInfoMap.get(element);
		return result;
	}

	protected ModelInfo getModelInfoFor(IStructuredModel structuredModel) {
		ModelInfo result = null;
		if (structuredModel != null) {
			ModelInfo[] modelInfos = (ModelInfo[]) fModelInfoMap.values().toArray(new ModelInfo[0]);
			for (int i = 0; i < modelInfos.length; i++) {
				ModelInfo info = modelInfos[i];
				if (structuredModel.equals(info.fStructuredModel)) {
					result = info;
					break;
				}
			}
		}
		return result;
	}

	/**
	 * Method loadModel.
	 * 
	 * @param input
	 * @return IStructuredModel
	 */
	public IStructuredModel loadModel(IEditorInput input) {
		return loadModel(input, false);
	}

	/**
	 * Method loadModel.
	 * 
	 * @param input
	 * @param logExceptions
	 * @return IStructuredModel
	 */
	public IStructuredModel loadModel(IEditorInput input, boolean logExceptions) {
		InputStream contents = null;
		IStructuredModel model = null;
		File file = getSystemFile(input);
		if (file != null) {
			String path = file.getAbsolutePath();
			try {
				contents = new FileInputStream(file);
				// first parameter must be unique
				model = getModelManager().getModelForEdit(path, contents, null);
				model.setBaseLocation(path);
			}
			catch (IOException e) {
				if (logExceptions)
					Logger.logException(ResourceHandler.getString("32concat_EXC_", new Object[]{input}), e); //$NON-NLS-1$
			}
			try {
				contents.close();
			}
			catch (Throwable e1) {
				// do nothing, this is just to ensure the resource isn't held
				// open
			}
		}
		return model;
	}

	/**
	 * @param input
	 * @return
	 */
	private File getSystemFile(IEditorInput input) {
		File file = null;
		ILocationProvider provider = (ILocationProvider) input.getAdapter(ILocationProvider.class);
		if (provider != null) {
			IPath fullPath = provider.getPath(input);
			if (fullPath != null) {
				file = fullPath.toFile();
			}
		}
		return file;
	}

	/**
	 * @param input
	 * @return
	 */
	protected IStructuredModel selfCreateModel(IEditorInput input) {
		return loadModel(input);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.IDocumentProvider#resetDocument(java.lang.Object)
	 */
	public void resetDocument(Object element) throws CoreException {
		super.resetDocument(element);
		FileInfo info = getFileInfo(element);
		File file = getSystemFile(info);
		if (file != null && file.exists()) {
			IStructuredModel model = getModel(element);
			InputStream fis = null;
			try {
				fis = new FileInputStream(file);
			}
			catch (FileNotFoundException e) {
				// possibly nothing if the file was deleted, shouldn't happen
				// otherwise
			}
			if (fis != null) {
				String oldContents = model.getStructuredDocument().get();
				try {
					model.reload(fis);
					info.fTextFileBuffer.getDocument().set(model.getStructuredDocument().get());
				}
				catch (IOException e1) {
					Logger.logException("Exception caught reloading model from " + file.getName(), e1); //$NON-NLS-1$
					model.getStructuredDocument().set(oldContents);
				}
			}
		}
	}
}

Back to the top