Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c30cb360e1a5574e12dafb2d2139093f857b1b11 (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
 * Copyright (c) 2007, 2009 Borland Software Corporation
 * 
 * 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:
 *    Alexander Shatalin (Borland) - initial API and implementation
 */

«IMPORT 'http://www.eclipse.org/gmf/2009/GenModel'»
«EXTENSION xpt::editor::Utils»

«DEFINE DocumentProvider FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::copyright FOR editorGen-»
package «editorGen.editor.packageName»;

«EXPAND xpt::Common::generatedClassComment»
public class «documentProviderClassName» extends org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.AbstractDocumentProvider implements org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocumentProvider {
	
	«EXPAND createElementInfo-»
	
	«EXPAND createDocument-»
	
	«EXPAND setupDocument-»
	
	«EXPAND computeModificationStamp-»
	
	«EXPAND createEmptyDocument-»
	
	«EXPAND createEditingDomain-»
	
	«EXPAND setDocumentContent-»
	
	«EXPAND getModificationStamp-»
	
	«EXPAND isDeleted-»
	
	«EXPAND getResourceSetInfo-»
	
	«EXPAND disposeElementInfo-»
	«IF null = editorGen.application-»

		«EXPAND doValidateState-»
	«ENDIF-»
	
	«EXPAND isReadOnly-»
	
	«EXPAND isModifiable-»
	
	«EXPAND updateCache-»
	
	«EXPAND doUpdateStateCache-»
	
	«EXPAND isSynchronized-»
	«IF null = editorGen.application-»
		
		«EXPAND getResetRule-»
	
		«EXPAND getSaveRule-»
	
		«EXPAND getSynchronizeRule-»
	
		«EXPAND getValidateStateRule-»
	
		«EXPAND computeSchedulingRule-»
	«ENDIF-»
	
	«EXPAND doSynchronize-»
	
	«EXPAND doSaveDocument-»
	
	«EXPAND handleElementChanged-»

	«EXPAND handleElementMoved-»

	«EXPAND createInputWithEditingDomain-»

	«EXPAND getDiagramDocument-»
	
	«EXPAND getOperationRunner-»
	«IF null <> editorGen.application-»

		«EXPAND getFile-»
	«ENDIF-»
	
	«EXPAND xpt::editor::ResourceSetInfo::ResourceSetInfo-»
	
	«EXPAND xpt::editor::ResourceSetModificationListener::ResourceSetModificationListener-»

	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE createElementInfo FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected ElementInfo createElementInfo(Object element) throws org.eclipse.core.runtime.CoreException {
	«EXPAND checkEditorInputInstance-»
	org.eclipse.ui.IEditorInput editorInput = (org.eclipse.ui.IEditorInput) element;
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument document = (org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) createDocument(editorInput);

	ResourceSetInfo info = new ResourceSetInfo(document, editorInput);
	info.setModificationStamp(computeModificationStamp(info));
	info.fStatus = null;
	return info;
}
«ENDDEFINE»

«DEFINE checkEditorInputInstance FOR gmfgen::GenDiagram-»
if («IF null = editorGen.application»false == element instanceof «EXPAND fileEditorInputClassFQName» && «ENDIF»false == element instanceof «EXPAND uriEditorInputClassFQName») {
	«EXPAND throwIncorrectInputException-»
}
«ENDDEFINE»

«DEFINE throwIncorrectInputException FOR gmfgen::GenDiagram-»
throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 0, 
		org.eclipse.osgi.util.NLS.bind(
			«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentProviderIncorrectInputError(self)) FOR editorGen»,
			new Object[] {element, «IF null = editorGen.application»"«EXPAND fileEditorInputClassFQName»", «ENDIF»"«EXPAND uriEditorInputClassFQName»"}),«EXPAND xpt::Common::nonNLS(1)»«IF null = editorGen.application»«EXPAND xpt::Common::nonNLS(2)»«ENDIF» 
		null));
«ENDDEFINE»

«DEFINE fileEditorInputClassFQName FOR gmfgen::GenDiagram»org.eclipse.ui.part.FileEditorInput«ENDDEFINE»

«DEFINE uriEditorInputClassFQName FOR gmfgen::GenDiagram»org.eclipse.emf.common.ui.URIEditorInput«ENDDEFINE»

«DEFINE createDocument FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument createDocument(Object element) throws org.eclipse.core.runtime.CoreException {
	«EXPAND checkEditorInputInstance-»
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument document = createEmptyDocument();
	setDocumentContent(document, (org.eclipse.ui.IEditorInput) element);
	setupDocument(element, document);
	return document;
}
«ENDDEFINE»

«DEFINE setupDocument FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment(
		'Sets up the given document as it would be provided for the given element. The\n' + 
		'content of the document is not changed. This default implementation is empty.\n' + 
		'Subclasses may reimplement.\n' + 
		'\n' + 
		'@param element the blue-print element\n' + 
		'@param document the document to set up'
	)»
protected void setupDocument(Object element, org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument document) {
	// for subclasses
}
«ENDDEFINE»

«DEFINE computeModificationStamp FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private long computeModificationStamp(ResourceSetInfo info) {
	int result = 0;
	for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
		org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
	«IF null = editorGen.application-»
		org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
		if (file != null) {
			if (file.getLocation() != null) {
				result += file.getLocation().toFile().lastModified();
			} else {
				result += file.getModificationStamp();
			}
		}
	«ELSE-»
		java.io.File file = getFile(nextResource);
		if (file != null && file.exists()) {
			result += file.lastModified();
		}
	«ENDIF-»
	}
	return result;
}
«ENDDEFINE»

«DEFINE createEmptyDocument FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument createEmptyDocument() {
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramDocument document = new org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.DiagramDocument();
	document.setEditingDomain(createEditingDomain());
	return document;
}
«ENDDEFINE»

«DEFINE createEditingDomain FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.emf.transaction.TransactionalEditingDomain createEditingDomain() {
	org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain = org.eclipse.gmf.runtime.diagram.core.DiagramEditingDomainFactory.getInstance().createEditingDomain();
	editingDomain.setID("«editingDomainID»");«EXPAND xpt::Common::nonNLS»
	final org.eclipse.emf.transaction.NotificationFilter diagramResourceModifiedFilter = org.eclipse.emf.transaction.NotificationFilter.createNotifierFilter(editingDomain.getResourceSet()).and(org.eclipse.emf.transaction.NotificationFilter.createEventTypeFilter(org.eclipse.emf.common.notify.Notification.ADD)).and(org.eclipse.emf.transaction.NotificationFilter.createFeatureFilter(org.eclipse.emf.ecore.resource.ResourceSet.class, org.eclipse.emf.ecore.resource.ResourceSet.RESOURCE_SET__RESOURCES));
	editingDomain.getResourceSet().eAdapters().add(new org.eclipse.emf.common.notify.Adapter() {

		private org.eclipse.emf.common.notify.Notifier myTarger;

		public org.eclipse.emf.common.notify.Notifier getTarget() {
			return myTarger;
		}

		public boolean isAdapterForType(Object type) {
			return false;
		}

		public void notifyChanged(org.eclipse.emf.common.notify.Notification notification) {
			if (diagramResourceModifiedFilter.matches(notification)) {
				Object value = notification.getNewValue();
				if (value instanceof org.eclipse.emf.ecore.resource.Resource) {
					((org.eclipse.emf.ecore.resource.Resource) value).setTrackingModification(true);
				}
			}
		}

		public void setTarget(org.eclipse.emf.common.notify.Notifier newTarget) {
			myTarger = newTarget;
		}
			
	});	
	
	return editingDomain;
}
«ENDDEFINE»

«DEFINE setDocumentContent FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void setDocumentContent(org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument document, org.eclipse.ui.IEditorInput element) throws org.eclipse.core.runtime.CoreException {
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument diagramDocument = (org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) document;
	org.eclipse.emf.transaction.TransactionalEditingDomain domain = diagramDocument.getEditingDomain();
	«IF null = editorGen.application»if (element instanceof «EXPAND fileEditorInputClassFQName») {
		org.eclipse.core.resources.IStorage storage = ((«EXPAND fileEditorInputClassFQName») element).getStorage();
		org.eclipse.gmf.runtime.notation.Diagram diagram = org.eclipse.gmf.runtime.diagram.ui.resources.editor.internal.util.DiagramIOUtil.load(domain, storage, true, getProgressMonitor());
		document.setContent(diagram);
	} else «ENDIF»if(element instanceof «EXPAND uriEditorInputClassFQName») {
		org.eclipse.emf.common.util.URI uri = ((«EXPAND uriEditorInputClassFQName») element).getURI();
		org.eclipse.emf.ecore.resource.Resource resource = null;
		try {
			resource = domain.getResourceSet().getResource(uri.trimFragment(), false);
			if (resource == null) {
				resource = domain.getResourceSet().createResource(uri.trimFragment());
			}
			if (!resource.isLoaded()) {
				try {
					java.util.Map options = new java.util.HashMap(org.eclipse.gmf.runtime.emf.core.resources.GMFResourceFactory.getDefaultLoadOptions());
					// @see 171060 
					// options.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
					resource.load(options);
				} catch (java.io.IOException e) {
					resource.unload();
					throw e;
				}
			}
			if (uri.fragment() != null) {
				org.eclipse.emf.ecore.EObject rootElement = resource.getEObject(uri.fragment());
				if (rootElement instanceof org.eclipse.gmf.runtime.notation.Diagram) {
					document.setContent((org.eclipse.gmf.runtime.notation.Diagram) rootElement);
					return;
				}
			} else {
				for (java.util.Iterator it = resource.getContents().iterator(); it.hasNext();) {
					Object rootElement = it.next();
					if (rootElement instanceof org.eclipse.gmf.runtime.notation.Diagram) {
						document.setContent((org.eclipse.gmf.runtime.notation.Diagram) rootElement);
						return;
					}
				}
			}
			throw new RuntimeException(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentProviderNoDiagramInResourceError(self)) FOR editorGen»);
		} catch (Exception e) {
			org.eclipse.core.runtime.CoreException thrownExcp = null;
			if (e instanceof org.eclipse.core.runtime.CoreException) {
				thrownExcp = (org.eclipse.core.runtime.CoreException) e;
			} else {
				String msg = e.getLocalizedMessage();
				thrownExcp = new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 0, 
				msg != null ? msg : «EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentProviderDiagramLoadingError(self)) FOR editorGen», e));
			}
			throw thrownExcp;
		}
	} else {
	«EXPAND throwIncorrectInputException-»
	}	
}
«ENDDEFINE»

«DEFINE getModificationStamp FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public long getModificationStamp(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		return computeModificationStamp(info);
	}
	return super.getModificationStamp(element);
}
«ENDDEFINE»

«DEFINE isDeleted FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean isDeleted(Object element) {
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument document = getDiagramDocument(element);
	if (document != null) {
		org.eclipse.emf.ecore.resource.Resource diagramResource = document.getDiagram().eResource();
		if (diagramResource != null) {
	«IF null = editorGen.application-»
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(diagramResource);
			return file == null || file.getLocation() == null || !file.getLocation().toFile().exists();
	«ELSE-»
			java.io.File file = getFile(diagramResource);
			return file != null && !file.exists();
	«ENDIF-»
		}
	}
	return super.isDeleted(element);
}
«ENDDEFINE»

«DEFINE getResourceSetInfo FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public ResourceSetInfo getResourceSetInfo(Object editorInput) {
	return (ResourceSetInfo) super.getElementInfo(editorInput);
}
«ENDDEFINE»

«DEFINE disposeElementInfo FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void disposeElementInfo(Object element, ElementInfo info) {
	if (info instanceof ResourceSetInfo) {
		ResourceSetInfo resourceSetInfo = (ResourceSetInfo) info;
		resourceSetInfo.dispose();
	}
	super.disposeElementInfo(element, info);
}
«ENDDEFINE»

«DEFINE doValidateState FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void doValidateState(Object element, Object computationContext) throws org.eclipse.core.runtime.CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		java.util.Collection/*<org.eclipse.core.resources.IFile>*/ files2Validate = new java.util.ArrayList/*<org.eclipse.core.resources.IFile>*/();
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
				files2Validate.add(file);
			}
		}
		org.eclipse.core.resources.ResourcesPlugin.getWorkspace().validateEdit((org.eclipse.core.resources.IFile[]) files2Validate.toArray(new org.eclipse.core.resources.IFile[files2Validate.size()]), computationContext);
	}

	super.doValidateState(element, computationContext);
}
«ENDDEFINE»

«DEFINE isReadOnly FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean isReadOnly(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		«EXPAND callUpdateCache-»
		return info.isReadOnly();
	}
	return super.isReadOnly(element);
}
«ENDDEFINE»

«DEFINE isModifiable FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean isModifiable(Object element) {
	if (!isStateValidated(element)) {
		if («IF null = editorGen.application»element instanceof «EXPAND fileEditorInputClassFQName» || «ENDIF»element instanceof «EXPAND uriEditorInputClassFQName») {
			return true;
		}
	}
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		«EXPAND callUpdateCache-»
		return info.isModifiable();
	}
	return super.isModifiable(element);
}
«ENDDEFINE»

«DEFINE callUpdateCache FOR gmfgen::GenDiagram-»
if (info.isUpdateCache()) {
	try {
		updateCache(element);
	} catch (org.eclipse.core.runtime.CoreException ex) {
		«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentProviderIsModifiable(self)) FOR editorGen», ex);
		// Error message to log was initially taken from org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.internal.l10n.EditorMessages.StorageDocumentProvider_isModifiable
	}
}
«ENDDEFINE»

«DEFINE updateCache FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void updateCache(Object element) throws org.eclipse.core.runtime.CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
	«IF null = editorGen.application-»
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null && file.isReadOnly()) {
	«ELSE-»
			java.io.File file = getFile(nextResource);
			if (file != null && file.exists() && !file.canWrite()) {
	«ENDIF-»
				info.setReadOnly(true);
				info.setModifiable(false);
				return;
			}
		}
		info.setReadOnly(false);
		info.setModifiable(true);
		return;
	}
}
«ENDDEFINE»

«DEFINE doUpdateStateCache FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void doUpdateStateCache(Object element) throws org.eclipse.core.runtime.CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		info.setUpdateCache(true);
	}
	super.doUpdateStateCache(element);
}
«ENDDEFINE»

«DEFINE isSynchronized FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public boolean isSynchronized(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		return info.isSynchronized();
	}
	return super.isSynchronized(element);
}
«ENDDEFINE»

«DEFINE getResetRule FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.core.runtime.jobs.ISchedulingRule getResetRule(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		java.util.Collection/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/ rules = new java.util.ArrayList/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/();
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null) {
				rules.add(org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(file));
			}
		}
		return new org.eclipse.core.runtime.jobs.MultiRule((org.eclipse.core.runtime.jobs.ISchedulingRule[]) rules.toArray(new org.eclipse.core.runtime.jobs.ISchedulingRule[rules.size()]));
	}
	return null;
}
«ENDDEFINE»

«DEFINE getSaveRule FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.core.runtime.jobs.ISchedulingRule getSaveRule(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		java.util.Collection/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/ rules = new java.util.ArrayList/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/();
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null) {
				rules.add(computeSchedulingRule(file));
			}
		}
		return new org.eclipse.core.runtime.jobs.MultiRule((org.eclipse.core.runtime.jobs.ISchedulingRule[]) rules.toArray(new org.eclipse.core.runtime.jobs.ISchedulingRule[rules.size()]));
	}
	return null;
}
«ENDDEFINE»

«DEFINE getSynchronizeRule FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.core.runtime.jobs.ISchedulingRule getSynchronizeRule(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		java.util.Collection/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/ rules = new java.util.ArrayList/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/();
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null) {
				rules.add(org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRuleFactory().refreshRule(file));
			}
		}
		return new org.eclipse.core.runtime.jobs.MultiRule((org.eclipse.core.runtime.jobs.ISchedulingRule[]) rules.toArray(new org.eclipse.core.runtime.jobs.ISchedulingRule[rules.size()]));
	}
	return null;
}
«ENDDEFINE»

«DEFINE getValidateStateRule FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.core.runtime.jobs.ISchedulingRule getValidateStateRule(Object element) {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		java.util.Collection/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/ files = new java.util.ArrayList/*<org.eclipse.core.runtime.jobs.ISchedulingRule>*/();
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(nextResource);
			if (file != null) {
				files.add(file);
			}
		}
		return org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRuleFactory().validateEditRule((org.eclipse.core.resources.IFile[]) files.toArray(new org.eclipse.core.resources.IFile[files.size()]));
	}
	return null;
}
«ENDDEFINE»

«DEFINE computeSchedulingRule FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private org.eclipse.core.runtime.jobs.ISchedulingRule computeSchedulingRule(org.eclipse.core.resources.IResource toCreateOrModify) {
	if (toCreateOrModify.exists())
		return org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(toCreateOrModify);

	org.eclipse.core.resources.IResource parent = toCreateOrModify;
	do {
		/*
		 * XXX This is a workaround for
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=67601
		 * IResourceRuleFactory.createRule should iterate the hierarchy
		 * itself.
		 */
		toCreateOrModify = parent;
		parent = toCreateOrModify.getParent();
	} while (parent != null && !parent.exists());

	return org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRuleFactory().createRule(toCreateOrModify);
}
«ENDDEFINE»

«DEFINE doSynchronize FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void doSynchronize(Object element, org.eclipse.core.runtime.IProgressMonitor monitor) throws org.eclipse.core.runtime.CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
			org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
			handleElementChanged(info, nextResource, monitor);	
		}
		return;
	}
	super.doSynchronize(element, monitor);
}
«ENDDEFINE»

«DEFINE handleElementChanged FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void handleElementChanged(ResourceSetInfo info, org.eclipse.emf.ecore.resource.Resource changedResource, org.eclipse.core.runtime.IProgressMonitor monitor) {
	«IF null = editorGen.application-»
	org.eclipse.core.resources.IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(changedResource);
	if (file != null) {
		try {
			file.refreshLocal(org.eclipse.core.resources.IResource.DEPTH_INFINITE, monitor);
		} catch (org.eclipse.core.runtime.CoreException ex) {
			«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentProviderHandleElementContentChanged(self)) FOR editorGen», ex);
			// Error message to log was initially taken from org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.internal.l10n.EditorMessages.FileDocumentProvider_handleElementContentChanged
		}
	}
	«ENDIF-»
	changedResource.unload();
	
	fireElementContentAboutToBeReplaced(info.getEditorInput());
	removeUnchangedElementListeners(info.getEditorInput(), info);
	info.fStatus = null;
	try {
		setDocumentContent(info.fDocument, info.getEditorInput());
	} catch (org.eclipse.core.runtime.CoreException e) {
		info.fStatus = e.getStatus();
	}
«REM»TODO: Remove this if and call setModificationStamp only from doSaveDocument method«ENDREM»«-»
	if (!info.fCanBeSaved) {
		info.setModificationStamp(computeModificationStamp(info));
	}
	addUnchangedElementListeners(info.getEditorInput(), info);
	fireElementContentReplaced(info.getEditorInput());
}
«ENDDEFINE»

«DEFINE doSaveDocument FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void doSaveDocument(org.eclipse.core.runtime.IProgressMonitor monitor, Object element, org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument document, boolean overwrite) throws org.eclipse.core.runtime.CoreException {
	ResourceSetInfo info = getResourceSetInfo(element);
	if (info != null) {
		if (!overwrite && !info.isSynchronized()) {
			throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 
				«IF null = editorGen.application»org.eclipse.core.resources.IResourceStatus.OUT_OF_SYNC_LOCAL«ELSE»org.eclipse.core.runtime.IStatus.ERROR«ENDIF», 
				«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentUnsynchronizedFileSaveError(self)) FOR editorGen», 
				null));
		}
	«IF null = editorGen.application-»
		info.stopResourceListening();
	«ENDIF-»
		fireElementStateChanging(element);
		try {
			monitor.beginTask(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentSaveDiagramTask(self)) FOR editorGen», info.getResourceSet().getResources().size() + 1); //"Saving diagram"
			for (java.util.Iterator/*<org.eclipse.emf.ecore.resource.Resource>*/ it = info.getLoadedResourcesIterator(); it.hasNext();) {
				org.eclipse.emf.ecore.resource.Resource nextResource = (org.eclipse.emf.ecore.resource.Resource) it.next();
				monitor.setTaskName(org.eclipse.osgi.util.NLS.bind(
						«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentSaveNextResourceTask(self)) FOR editorGen», 
						nextResource.getURI()));
				if (nextResource.isLoaded() && !info.getEditingDomain().isReadOnly(nextResource)) {
					try {
						nextResource.save(«EXPAND xpt::Common::getSaveOptions»);
					} catch (java.io.IOException e) {
						fireElementStateChangeFailed(element);
						throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, org.eclipse.gmf.runtime.diagram.ui.resources.editor.internal.EditorStatusCodes.RESOURCE_FAILURE, e.getLocalizedMessage(), null));
					}
				}
				monitor.worked(1);
			}
			monitor.done();
			info.setModificationStamp(computeModificationStamp(info));
		} catch (RuntimeException x) {
			fireElementStateChangeFailed(element);
			throw x;
		} «IF null = editorGen.application» finally {
			info.startResourceListening();
		} «ENDIF»
	} else {
		org.eclipse.emf.common.util.URI newResoruceURI;
		java.util.List affectedFiles = null;
		«IF null = editorGen.application»if (element instanceof «EXPAND fileEditorInputClassFQName») {
			org.eclipse.core.resources.IFile newFile = ((«EXPAND fileEditorInputClassFQName») element).getFile();
			affectedFiles = java.util.Collections.singletonList(newFile);
			newResoruceURI = org.eclipse.emf.common.util.URI.createPlatformResourceURI(newFile.getFullPath().toString(), true);
		} else «ENDIF»if(element instanceof «EXPAND uriEditorInputClassFQName») {
			newResoruceURI = ((«EXPAND uriEditorInputClassFQName») element).getURI();
		} else {
			fireElementStateChangeFailed(element);
			«EXPAND throwIncorrectInputException-»
		}
		if (false == document instanceof org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) {
			fireElementStateChangeFailed(element);
			throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 0,
			"Incorrect document used: " + document + " instead of org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument", null));«EXPAND xpt::Common::nonNLS(1)»«EXPAND xpt::Common::nonNLS(2)»
		}
		org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument diagramDocument = (org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) document;
		final org.eclipse.emf.ecore.resource.Resource newResource = diagramDocument.getEditingDomain().getResourceSet().createResource(newResoruceURI);
		final org.eclipse.gmf.runtime.notation.Diagram diagramCopy = (org.eclipse.gmf.runtime.notation.Diagram) org.eclipse.emf.ecore.util.EcoreUtil.copy(diagramDocument.getDiagram());
		try {
			new org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand(diagramDocument.getEditingDomain(), org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForDocumentSaveAs(self)) FOR editorGen», diagramCopy.getName()), affectedFiles) {
				protected org.eclipse.gmf.runtime.common.core.command.CommandResult doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) throws org.eclipse.core.commands.ExecutionException {
					newResource.getContents().add(diagramCopy);					
					return org.eclipse.gmf.runtime.common.core.command.CommandResult.newOKCommandResult();
				}
			}.execute(monitor, null);
			newResource.save(«EXPAND xpt::Common::getSaveOptions»);
		} catch (org.eclipse.core.commands.ExecutionException e) {
			fireElementStateChangeFailed(element);
			throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 0, e.getLocalizedMessage(), null));
		} catch (java.io.IOException e) {
			fireElementStateChangeFailed(element);
			throw new org.eclipse.core.runtime.CoreException(new org.eclipse.core.runtime.Status(org.eclipse.core.runtime.IStatus.ERROR, «editorGen.plugin.getActivatorQualifiedClassName()».ID, 0, e.getLocalizedMessage(), null));
		}
		newResource.unload();
	}
}
«ENDDEFINE»

«DEFINE handleElementMoved FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected void handleElementMoved(org.eclipse.ui.IEditorInput input, org.eclipse.emf.common.util.URI uri) {
	«IF null = editorGen.application»if (input instanceof «EXPAND fileEditorInputClassFQName») {
		org.eclipse.core.resources.IFile newFile = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getFile(new org.eclipse.core.runtime.Path(org.eclipse.emf.common.util.URI.decode(uri.path())).removeFirstSegments(1));
		fireElementMoved(input, newFile == null ? null : new org.eclipse.ui.part.FileEditorInput(newFile));
		return;
	}«ENDIF»
	// TODO: append suffix to the URI! (use diagram as a parameter)
	fireElementMoved(input, new org.eclipse.emf.common.ui.URIEditorInput(uri));
}
«ENDDEFINE»

«DEFINE createInputWithEditingDomain FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public org.eclipse.ui.IEditorInput createInputWithEditingDomain(org.eclipse.ui.IEditorInput editorInput, org.eclipse.emf.transaction.TransactionalEditingDomain domain) {
	return editorInput;
}
«ENDDEFINE»

«DEFINE getDiagramDocument FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
public org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument getDiagramDocument(Object element) {
	org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument doc = getDocument(element);
	if (doc instanceof org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) {
		return (org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument) doc;
	}
	return null;
}
«ENDDEFINE»

«DEFINE getOperationRunner FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
protected org.eclipse.jface.operation.IRunnableContext getOperationRunner(org.eclipse.core.runtime.IProgressMonitor monitor) {
	return null;
}
«ENDDEFINE»

«DEFINE getFile FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Common::generatedMemberComment»
private static java.io.File getFile(org.eclipse.emf.ecore.resource.Resource resource) {
	org.eclipse.emf.common.util.URI resourceUri = resource.getURI();
	if (resourceUri != null && resourceUri.isFile()) {
		java.io.File file = new java.io.File(resourceUri.toFileString());
		if (!file.isDirectory()) {
			return file;
		}
	}
	return null;
}
«ENDDEFINE»

«DEFINE i18nAccessors FOR gmfgen::GenDiagram-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentProviderIsModifiable(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentProviderHandleElementContentChanged(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentProviderIncorrectInputError(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentProviderNoDiagramInResourceError(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentProviderDiagramLoadingError(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentUnsynchronizedFileSaveError(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentSaveDiagramTask(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentSaveNextResourceTask(self))-»
	«EXPAND xpt::Externalizer::accessorField(i18nKeyForDocumentSaveAs(self))-»
«ENDDEFINE»

«DEFINE i18nValues FOR gmfgen::GenDiagram-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentProviderIsModifiable(self), 'Updating cache failed')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentProviderHandleElementContentChanged(self), 'Failed to refresh hierarchy for changed resource')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentProviderIncorrectInputError(self), (if 'Incorrect element used: {0} instead of ' + editorGen.application.repr() = null then '{1} or {2}' else '{1}' endif))-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentProviderNoDiagramInResourceError(self), 'Diagram is not present in resource')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentProviderDiagramLoadingError(self), 'Error loading diagram')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentUnsynchronizedFileSaveError(self), 'The file has been changed on the file system')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentSaveDiagramTask(self), 'Saving diagram')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentSaveNextResourceTask(self), 'Saving {0}')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForDocumentSaveAs(self), 'Saving {0} diagram as')-»
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenDiagram-»
«ENDDEFINE»

Back to the top