Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 423727e534fa2b7425d4f93b265505c589011d46 (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
/*****************************************************************************
 * Copyright (c) 2013, 2014 CEA LIST and others.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (camille.letavernier@cea.fr) - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 437217
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.ui.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentMap;

import org.eclipse.core.resources.IResource;
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.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.IService;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.ui.Activator;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.ui.editor.IReloadableEditor;
import org.eclipse.papyrus.infra.ui.editor.IReloadableEditor.DirtyPolicy;
import org.eclipse.papyrus.infra.ui.lifecycleevents.DoSaveEvent;
import org.eclipse.papyrus.infra.ui.lifecycleevents.ILifeCycleEventsProvider;
import org.eclipse.papyrus.infra.ui.lifecycleevents.ISaveEventListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
import org.eclipse.ui.progress.UIJob;

import com.google.common.collect.Maps;

/**
 * A Service to check workspace modifications on current resources
 *
 * @author Camille Letavernier
 *
 */
public class ResourceUpdateService implements IService, IPartListener {

	protected ServicesRegistry registry;

	protected ModelSet modelSet;

	static int[] handledTypes = new int[] { IResourceChangeEvent.POST_CHANGE, IResourceChangeEvent.PRE_DELETE, IResourceChangeEvent.PRE_CLOSE };

	protected boolean isSaving;

	protected ConcurrentMap<IMultiDiagramEditor, Job> pendingEditorCloseJobs = Maps.newConcurrentMap();

	private final ISaveEventListener preSaveListener = new ISaveEventListener() {

		@Override
		public void doSaveAs(DoSaveEvent event) {
			isSaving = true;
		}

		@Override
		public void doSave(DoSaveEvent event) {
			isSaving = true;
		}
	};

	private final ISaveEventListener postSaveListener = new ISaveEventListener() {

		@Override
		public void doSaveAs(DoSaveEvent event) {
			isSaving = false;
		}

		@Override
		public void doSave(DoSaveEvent event) {
			isSaving = false;
		}
	};

	@Override
	public void init(ServicesRegistry servicesRegistry) throws ServiceException {
		this.registry = servicesRegistry;
	}

	@Override
	public void startService() throws ServiceException {
		ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener, IResourceChangeEvent.POST_CHANGE);
		modelSet = registry.getService(ModelSet.class);
		registry.getService(ILifeCycleEventsProvider.class).addAboutToDoSaveListener(preSaveListener);
		registry.getService(ILifeCycleEventsProvider.class).addPostDoSaveListener(postSaveListener);
	}

	@Override
	public void disposeService() throws ServiceException {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceChangeListener);
		modelSet = null;
	}

	protected void closeEditor() {
		closeEditor(Collections.<Resource> emptyList(), false);
	}

	protected void closeEditor(final Collection<? extends Resource> triggeringResources, final boolean reopen) {
		try {
			if (!reopen) {
				registry.remove(SaveLayoutBeforeClose.class.getName());
			}

			final IMultiDiagramEditor editor = registry.getService(IMultiDiagramEditor.class);
			if (editor != null) {
				final IWorkbenchPartSite site = editor.getSite();
				UIJob closeEditorJob = new UIJob(site.getShell().getDisplay(), NLS.bind("Reload editor {0}", editor.getTitle())) {

					@Override
					public IStatus runInUIThread(IProgressMonitor monitor) {
						// Remove the pending job
						pendingEditorCloseJobs.remove(editor);

						IStatus result = Status.OK_STATUS;
						monitor = SubMonitor.convert(monitor, IProgressMonitor.UNKNOWN);

						try {
							IReloadableEditor.ReloadReason reason = reopen ? IReloadableEditor.ReloadReason.RESOURCES_CHANGED : IReloadableEditor.ReloadReason.RESOURCES_DELETED;

							DirtyPolicy dirtyPolicy = DirtyPolicy.getDefault();
							if (!reopen && !editor.isDirty()) {
								// Check whether we're deleting one of our own resources. If so, just close
								URI principalURI = modelSet.getURIWithoutExtension();
								for (Resource next : triggeringResources) {
									if (next.getURI().trimFileExtension().equals(principalURI)) {
										dirtyPolicy = DirtyPolicy.DO_NOT_SAVE;
										break;
									}
								}
							}

							try {
								IReloadableEditor.Adapter.getAdapter(editor).reloadEditor(triggeringResources, reason, dirtyPolicy);
							} catch (CoreException e) {
								result = e.getStatus();
							}
						} finally {
							monitor.done();
						}

						return result;
					}
				};

				// We are notified usually of at least three resources (*.di, *.notation, *.uml) that are unloaded, but
				// there's no need to close and re-open the same editor three times
				if (pendingEditorCloseJobs.putIfAbsent(editor, closeEditorJob) == null) {
					// Async execution to avoid lock conflicts on the Workspace (Probably owned by this thread, and not the UI thread)
					IWorkbenchSiteProgressService progressService = site.getService(IWorkbenchSiteProgressService.class);
					progressService.schedule(closeEditorJob);
				}
			}
		} catch (ServiceException ex) {
			// Nothing
		}
	}

	protected void handleResourcesRemoved(Collection<Resource> emfResources) {
		closeEditor(emfResources, false);
	}

	protected void handleResourceChanged(Collection<Resource> emfResources) {
		closeEditor(emfResources, true);
	}

	// Copied from org.eclipse.emf.ecore.presentation.EcoreEditor
	protected IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {

		@Override
		public void resourceChanged(IResourceChangeEvent event) {
			IResourceDelta delta = event.getDelta();
			try {
				class ResourceDeltaVisitor implements IResourceDeltaVisitor {

					protected Collection<Resource> changedResources = new ArrayList<Resource>();

					protected Collection<Resource> removedResources = new ArrayList<Resource>();

					@Override
					public boolean visit(final IResourceDelta delta) {
						if (delta.getResource().getType() == IResource.FILE) {
							if (delta.getKind() == IResourceDelta.REMOVED || delta.getKind() == IResourceDelta.CHANGED) {
								URI resourceURI = URI.createPlatformResourceURI(delta.getFullPath().toString(), true);
								Resource resource = modelSet.getResource(resourceURI, false);
								if (resource == null) {
									// try again, with a pluginURI, see bug 418428
									URI pluginURI = URI.createPlatformPluginURI(delta.getFullPath().toString(), true);
									resource = modelSet.getResource(pluginURI, false);
								}
								if (resource != null) {

									if (delta.getKind() == IResourceDelta.REMOVED) {
										removedResources.add(resource);
									} else {
										if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) {
											// Skip markers
											// DiagnosticDecorator.DiagnosticAdapter.update(resource, markerHelper.getMarkerDiagnostics(resource, (IFile)delta.getResource()));
										}
										if ((delta.getFlags() & IResourceDelta.CONTENT) != 0) {
											// if(!savedResources.remove(resource)) {
											// changedResources.add(resource);
											// }
											if (!isSaving) {
												changedResources.add(resource);
											}
										}
									}
								}
							}
							return false;
						}

						return true;
					}

					public Collection<Resource> getChangedResources() {
						return changedResources;
					}

					public Collection<Resource> getRemovedResources() {
						return removedResources;
					}
				}

				final ResourceDeltaVisitor visitor = new ResourceDeltaVisitor();

				delta.accept(visitor);

				if (!visitor.getRemovedResources().isEmpty()) {
					handleResourcesRemoved(visitor.getRemovedResources());
				}

				if (!visitor.getChangedResources().isEmpty()) {
					handleResourceChanged(visitor.getChangedResources());
				}
			} catch (CoreException exception) {
				Activator.log.error(exception);
			}
		}
	};

	@Override
	public void partActivated(IWorkbenchPart part) {
		// Nothing
	}

	@Override
	public void partBroughtToTop(IWorkbenchPart part) {
		// Nothing
	}

	@Override
	public void partClosed(IWorkbenchPart part) {
		// Nothing
	}

	@Override
	public void partDeactivated(IWorkbenchPart part) {
		// Nothing
	}

	@Override
	public void partOpened(IWorkbenchPart part) {
		// Nothing
	}

}

Back to the top