summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor.ui/src/rba/tool/editor/ui/editor/model/manager/ResourceManagerUI.xtend
blob: 105d9de5313db1036caf43774e6ed11700a6bc2e (plain)
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
package rba.tool.editor.ui.editor.model.manager

import com.google.inject.Inject
import java.util.ArrayList
import java.util.Collection
import java.util.Collections
import java.util.List
import org.eclipse.core.resources.IProject
import org.eclipse.core.resources.IncrementalProjectBuilder
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.core.runtime.OperationCanceledException
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.EAttribute
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider
import org.eclipse.jface.dialogs.MessageDialog
import org.eclipse.swt.widgets.Display
import org.eclipse.ui.IEditorPart
import org.eclipse.xtext.resource.ILocationInFileProvider
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.serializer.ISerializer
import org.eclipse.xtext.service.OperationCanceledError
import org.eclipse.xtext.ui.editor.LanguageSpecificURIEditorOpener
import org.eclipse.xtext.ui.editor.XtextEditor
import org.eclipse.xtext.ui.editor.model.IXtextDocument
import org.eclipse.xtext.ui.editor.utils.EditorUtils
import org.eclipse.xtext.util.ITextRegion
import org.eclipse.xtext.util.concurrent.IUnitOfWork
import rba.core.provider.RBACoreItemProviderAdapterFactory
import rba.sound.provider.RBASoundItemProviderAdapterFactory
import rba.tool.editor.resource.IRBAModelResourceLoader
import rba.tool.editor.ui.activator.ExtensionEditorActivator
import rba.tool.editor.ui.editor.model.edit.processor.RBAModelProcessorUtil
import rba.tool.editor.ui.resource.RBAModelResourceSetProvider
import rba.view.provider.RBAViewItemProviderAdapterFactory

class ResourceManagerUI {

	public static ResourceManagerUI INSTANCE = new ResourceManagerUI();

	@Inject private RBAModelResourceSetProvider resourceSetProvider;

	@Inject private LanguageSpecificURIEditorOpener editorOpener ;

	@Inject private ILocationInFileProvider locationProvider;

	@Inject private ISerializer serializer;

	@Inject IRBAModelResourceLoader resourceLoader;

	private List<ComposeableAdapterFactory> adapterFactorys = new ArrayList<ComposeableAdapterFactory>;

	private IProject currentProject;

	private new() {
		ExtensionEditorActivator.getInstance().injectMembers(this);
		adapterFactorys.add(new RBACoreItemProviderAdapterFactory());
		adapterFactorys.add(new RBAViewItemProviderAdapterFactory());
		adapterFactorys.add(new RBASoundItemProviderAdapterFactory());
	}

	def public IProject getCurrentProject() {
		return currentProject;
	}

	def public void setCurrentProject(IProject project) {
		currentProject = project;
	}

	def public boolean isCurrentProject(IProject project) {
		if (project === null || !project.isOpen()) {
			return false;
		}

		if (project.equals(currentProject)) {
			return true;
		}

		return false;
	}

	def public ResourceSet getCurrentResourceSet() {
		return resourceSetProvider.getResourceSet(currentProject);
	}

	def public void setCurrentResourceSet(ResourceSet resourceSet) {
		resourceSetProvider.setResourceSet(currentProject, resourceSet);
	}
	
	def public IXtextDocument getXtextDocument(IEditorPart openEditor) {
		val XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor);
		if (xtextEditor !== null) {
			return xtextEditor.getDocument();
		}
		return null;
	}

	def public void add(EObject owner, EStructuralFeature feature, Object object, int index) {
		val Object outerOwner = owner ?: getParent(object);
		if (outerOwner === null) {
			return;
		}
		if (!(outerOwner instanceof EObject)) {
			return;
		}

		if (feature === null || object === null) {
			return;
		}

		if (!isValid(outerOwner as EObject, feature)) {
			return;
		}

		val editorPart = open(outerOwner as EObject, false);
		var IXtextDocument document = getXtextDocument(editorPart);

		if (document !== null) {
			document.modify(RBAModelProcessorUtil.getAddProcessor(outerOwner as EObject, feature, object, if (index < 0) RBAModelProcessorUtil.NO_INDEX else index));
		}
	}

	def public void remove(EObject owner, EStructuralFeature feature, Object object) {
		val Object outerOwner = owner ?: getParent(object);
		if (outerOwner === null) {
			return;
		}
		if (!(outerOwner instanceof EObject)) {
			return;
		}

		remove(outerOwner as EObject, feature, Collections.singleton(object));
	}

	def public void remove(EObject owner, EStructuralFeature feature, Collection<Object> collection) {
		if (owner !== null) {
			if (feature === null || collection === null || collection.isEmpty()) {
				return;
			}

			if (!isValid(owner, feature)) {
				return;
			}

			val editorPart = open(owner as EObject, false);
			var IXtextDocument document = getXtextDocument(editorPart);

			if (document !== null) {
				document.modify(RBAModelProcessorUtil.getRemoveProcessor(owner as EObject, feature, collection));
			}
		} else {
			remove(feature, collection);
		}
	}

	def public void remove(EStructuralFeature feature, Collection<Object> collection) {
		if (feature === null || collection === null || collection.isEmpty()) {
			return;
		}

		val List<Object> objects = new ArrayList<Object>(collection);
		while (!objects.isEmpty()) {
			// We will iterate over the whole collection, removing some as we go.
			val remainingObjects = objects.listIterator();

			// Take the first object, and remove it.
			val object = remainingObjects.next();
			remainingObjects.remove();

			// Determine the object's parent.
			val parent = getParent(object);
			if (parent instanceof EObject) {
				// Now we want to find all the other objects with this same parent.
				// So we can collection siblings together and give the parent control over their removal.
				val List<Object> siblings = new ArrayList<Object>();
				siblings.add(object);

				while (remainingObjects.hasNext()) {
					// Get the next object and check if it has the same parent.
					val otherObject = remainingObjects.next();
					val otherParent = getParent(otherObject);
					if (otherParent == parent) {
						// Remove the object and add it as a sibling.
						remainingObjects.remove();
						siblings.add(otherObject);
					}
				}
				remove(parent as EObject, feature, siblings);
			}
		}
	}

	def public void set(EObject owner, EStructuralFeature feature, Object value) {
		val Object outerOwner = owner ?: getParent(value);
		if (outerOwner === null) {
			return;
		}
		if (!(outerOwner instanceof EObject)) {
			return;
		}

		if (feature === null) {
			return;
		}

		if (!isValid(owner, feature)) {
			return;
		}

		val innerValue = value ?: RBAModelProcessorUtil.UNSET_VALUE;
		val editorPart = open(outerOwner as EObject, false);
		var IXtextDocument document = getXtextDocument(editorPart);

		if (document !== null) {
			document.modify(RBAModelProcessorUtil.getSetProcessor(outerOwner as EObject, feature, innerValue));
			selectAndReveal(editorPart, EcoreUtil.getURI(outerOwner as EObject), feature, true);
		}
	}

	def public void unset(EObject owner, EStructuralFeature feature) {
		if (owner === null || feature === null) {
			return;
		}

		if (!isValid(owner, feature)) {
			return;
		}

		val editorPart = open(owner as EObject, false);
		var IXtextDocument document = getXtextDocument(editorPart);

		if (document !== null) {
			document.modify(RBAModelProcessorUtil.getSetProcessor(owner, feature, RBAModelProcessorUtil.UNSET_VALUE));
		}
	}

	def public void setName(EObject owner, EStructuralFeature feature, Object value) {
		val Object outerOwner = owner ?: getParent(value);
		if (outerOwner === null) {
			return;
		}
		if (!(outerOwner instanceof EObject)) {
			return;
		}

		val XtextEditor editor = open(outerOwner as EObject, true);
		if (editor === null) {
			return;
		}

		if (feature === null) {
			return;
		}

		if (!isValid(owner, feature)) {
			return;
		}

		if (!(value instanceof String)) {
			return;
		}
		val innerValue = value as String;

		RBAModelProcessorUtil.doSetNameProcess(innerValue)
	}

	def public void waitForBuildProgress(Object family, IProgressMonitor monitor) {
		var boolean wasInterrupted = false;
		do {
			try {
				Job.getJobManager().join(family, monitor);
				wasInterrupted = false;
			} catch (OperationCanceledException e) {
				throw e;
			} catch (InterruptedException e) {
				wasInterrupted = true;
			}
		} while (wasInterrupted);
	}

	def public XtextEditor open(EObject eObject, boolean select) {
		val resource = eObject.eResource();
		if (resource === null) {
			return null;
		}

		val editorPart = editorOpener.open(EcoreUtil.getURI(eObject), select);
		if (!(editorPart instanceof XtextEditor)) {
			return null;
		}

		return (editorPart as XtextEditor);
	}

	def public XtextEditor open(URI uri, boolean select) {
		if (uri === null || uri.isEmpty()) {
			return null
		}

		val editorPart = editorOpener.open(uri, select);
		if (!(editorPart instanceof XtextEditor)) {
			return null;
		}

		return (editorPart as XtextEditor);
	}

	def private Object getParent(Object object) {
		for (adapterFactory : adapterFactorys) {
			val IEditingDomainItemProvider editingDomainItemProvider = adapterFactory.adapt(object, IEditingDomainItemProvider) as IEditingDomainItemProvider;
			if (editingDomainItemProvider !== null) {
				return editingDomainItemProvider.getParent(object);
			}
		}
		return null;
	}

	def private static isValid(EObject owner, EStructuralFeature feature) {
		if (feature instanceof EReference && owner.eClass().getEAllReferences().contains(feature)) {
			return true;
		}
		if (feature instanceof EAttribute && owner.eClass().getEAllAttributes().contains(feature)) {
			return true;
		}
		return false;
	}

	def public void switchToProject(IProject project, IProgressMonitor monitor) throws InterruptedException {
		if (project === null) {
			return;
		}

		try {
			val SubMonitor progress = SubMonitor.convert(monitor, 10);
			doSwitch(project, progress.newChild(2));
			if (progress.isCanceled()) {
				throw new InterruptedException();
			}
			doBuild(project, progress.newChild(6));
			if (progress.isCanceled()) {
				throw new InterruptedException();
			}
			waitForBuildProgress(ResourcesPlugin.FAMILY_MANUAL_BUILD, progress.newChild(2));
		} catch (Exception e) {
			throw new InterruptedException(e.getMessage());
		}
	}

	def public boolean doConfirm(IProject project, boolean showConfirmDialog) throws InterruptedException {
		if (project === null || !project.isOpen()) {
			throw new InterruptedException();
		}
		if (isCurrentProject(project)) {
			return true;
		}

		val boolean confirm = if (showConfirmDialog)
				MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Switch Project", "Do you want to switch to [" + project.getName() + "] project?")
			else
				true;

		return confirm;
	}

	def private void doSwitch(IProject project, IProgressMonitor monitor) {
		try {
			setCurrentProject(project);
		} catch (Exception e) {
			throw new InterruptedException(e.getMessage());
		}
	}

	def private void doBuild(IProject project, IProgressMonitor monitor) {
		try {
			project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
		} catch (Exception e) {
			throw new InterruptedException(e.getMessage());
		}
	}

	def private void selectAndReveal(IEditorPart openEditor, URI uri, EStructuralFeature feature, boolean select) {
		if (openEditor instanceof XtextEditor && select) {
			val xtextEditor = openEditor as XtextEditor;
			var boolean success = false;
			var int tries = 0;
			while (!success || tries >= 5) {
				try {
					xtextEditor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
						override process(XtextResource resource) throws Exception {
							if (resource !== null) {
								val EObject object = RBAModelProcessorUtil.findEObjectByURI(uri, resource);
								if (object !== null) {
									val ITextRegion location = if (feature !== null)
											locationProvider.getSignificantTextRegion(object, feature, 0)
										else
											locationProvider.getSignificantTextRegion(object);
									xtextEditor.selectAndReveal(location.getOffset(), location.getLength());
								}
							}
						}
					});
					xtextEditor.doSave(null)
					success = true;
				} catch (OperationCanceledException e) {
				} catch (OperationCanceledError e) {
				} finally {
					tries++;
				}
			}
		}
	}

	def public String serialize(EObject object) {
		return serializer.serialize(object)
	}

	def public void loadAndResolveResource(ResourceSet resourceSet, URI[] URIs, IProject project) {
		resourceLoader.loadAndResolveResource(resourceSet, URIs, project);
	}
}